Class MockFileDatabase
- java.lang.Object
-
- org.jooq.tools.jdbc.MockFileDatabase
-
- All Implemented Interfaces:
MockDataProvider
public class MockFileDatabase extends java.lang.Object implements MockDataProvider
A file-basedMockDataProvider
.This data provider reads a database model from a text file, as documented in the below sample file:
# Comments start off with a hash # Statement strings have no prefix and should be ended with a semi-colon select 'A' from dual; # Statements may be followed by results, using > > A > - > A # Statements should be followed by "@ rows: [N]" indicating the update count @ rows: 1 # New statements can be listed int his file select 'A', 'B' from dual; > A B > - - > A B @ rows: 1 # Beware of the exact syntax (e.g. using quotes) select "TABLE1"."ID1", "TABLE1"."NAME1" from "TABLE1"; > ID1 NAME1 > --- ----- > 1 X > 2 Y @ rows: 2 # Statements can return several results > F1 F2 F3 is a bit more complex > --- -- ---------------------------- > 1 2 and a string containing data > 1.1 x another string @ rows: 2 > A B "C D" > - - ----- > x y z @ rows: 1
Results can be loaded using several techniques:
- When results are prefixed with
>
, thenDSLContext.fetchFromTXT(String)
is used - In the future, other types of result sources will be supported, such as CSV, XML, JSON
- Author:
- Lukas Eder, Samy Deghou
-
-
Constructor Summary
Constructors Constructor Description MockFileDatabase(java.io.File file)
MockFileDatabase(java.io.File file, java.lang.String encoding)
MockFileDatabase(java.io.InputStream stream)
MockFileDatabase(java.io.InputStream stream, java.lang.String encoding)
MockFileDatabase(java.io.Reader reader)
MockFileDatabase(java.lang.String string)
MockFileDatabase(MockFileDatabaseConfiguration configuration)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description MockResult[]
execute(MockExecuteContext ctx)
Execution callback for a JDBC query execution.MockFileDatabase
nullLiteral(java.lang.String literal)
Deprecated.- UseMockFileDatabaseConfiguration.nullLiteral(String)
instead.java.util.Map<java.lang.String,java.util.List<MockResult>>
queries()
Deprecated.- Experimental: Do not use.
-
-
-
Constructor Detail
-
MockFileDatabase
public MockFileDatabase(java.io.File file) throws java.io.IOException
- Throws:
java.io.IOException
-
MockFileDatabase
public MockFileDatabase(java.io.File file, java.lang.String encoding) throws java.io.IOException
- Throws:
java.io.IOException
-
MockFileDatabase
public MockFileDatabase(java.io.InputStream stream) throws java.io.IOException
- Throws:
java.io.IOException
-
MockFileDatabase
public MockFileDatabase(java.io.InputStream stream, java.lang.String encoding) throws java.io.IOException
- Throws:
java.io.IOException
-
MockFileDatabase
public MockFileDatabase(java.io.Reader reader) throws java.io.IOException
- Throws:
java.io.IOException
-
MockFileDatabase
public MockFileDatabase(java.lang.String string) throws java.io.IOException
- Throws:
java.io.IOException
-
MockFileDatabase
public MockFileDatabase(MockFileDatabaseConfiguration configuration) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
nullLiteral
@Deprecated public MockFileDatabase nullLiteral(java.lang.String literal)
Deprecated.- UseMockFileDatabaseConfiguration.nullLiteral(String)
instead.Specify thenull
literal, i.e. the string that should be parsed as anull
reference, rather than as the string itself.- See Also:
DSLContext.fetchFromTXT(String, String)
-
queries
@Deprecated public java.util.Map<java.lang.String,java.util.List<MockResult>> queries()
Deprecated.- Experimental: Do not use. Subject to change.
-
execute
public MockResult[] execute(MockExecuteContext ctx) throws java.sql.SQLException
Description copied from interface:MockDataProvider
Execution callback for a JDBC query execution.This callback will be called by
MockStatement
upon the various statement execution methods. These include:Statement.execute(String)
Statement.execute(String, int)
Statement.execute(String, int[])
Statement.execute(String, String[])
Statement.executeBatch()
Statement.executeQuery(String)
Statement.executeUpdate(String)
Statement.executeUpdate(String, int)
Statement.executeUpdate(String, int[])
Statement.executeUpdate(String, String[])
PreparedStatement.execute()
PreparedStatement.executeQuery()
PreparedStatement.executeUpdate()
The various execution modes are unified into this simple method. Implementations should adhere to this contract:
MockStatement
does not distinguish between "static" and "prepared" statements. However, a non-emptyMockExecuteContext.bindings()
is a strong indicator for aPreparedStatement
.MockStatement
does not distinguish between "batch" and "single" statements. However...- A
MockExecuteContext.batchSQL()
with more than one SQL string is a strong indicator for a "multi-batch statement", as understood by jOOQ'sDSLContext.batch(Query...)
. - A
MockExecuteContext.batchBindings()
with more than one bind variable array is a strong indicator for a "single-batch statement", as understood by jOOQ'sDSLContext.batch(Query)
.
- A
- It is recommended to return as many
MockResult
objects as batch executions. In other words, you should guarantee that:int multiSize = context.getBatchSQL().length; int singleSize = context.getBatchBindings().length; assertEquals(result.length, Math.max(multiSize, singleSize))
This holds true also for non-batch executions (where both sizes are equal to
1
) - You may also return more than one result for non-batch executions.
This is useful for procedure calls with several result sets.
- In JDBC, such additional result sets can be obtained with
Statement.getMoreResults()
. - In jOOQ, such additional result sets can be obtained with
ResultQuery.fetchMany()
- In JDBC, such additional result sets can be obtained with
- If generated keys (
Statement.RETURN_GENERATED_KEYS
) are requested from this execution, you can also addMockResult.data
to your first result, in addition to the affectedMockResult.rows
. The relevant flag is passed fromMockStatement
to any of these properties: - OUT parameters from stored procedures are generated from the first
MockResult
's firstRecord
. If OUT parameters are requested, implementors must ensure the presence of such aRecord
.
- Specified by:
execute
in interfaceMockDataProvider
- Parameters:
ctx
- The execution context.- Returns:
- The execution results. If a
null
or an emptyMockResult[]
is returned, this has the same effect as returningnew MockResult[] { new MockResult(-1, null) }
- Throws:
java.sql.SQLException
- ASQLException
that is passed through to jOOQ.
-
-