public class MockFileDatabase extends Object implements MockDataProvider
MockDataProvider
.
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:
>
, then
DSLContext.fetchFromTXT(String)
is usedThis implementation is still very experimental and not officially supported!
Constructor and Description |
---|
MockFileDatabase(File file) |
MockFileDatabase(File file,
String encoding) |
MockFileDatabase(InputStream stream) |
MockFileDatabase(InputStream stream,
String encoding) |
MockFileDatabase(Reader reader) |
MockFileDatabase(String string) |
Modifier and Type | Method and Description |
---|---|
MockResult[] |
execute(MockExecuteContext ctx)
Execution callback for a JDBC query execution.
|
public MockFileDatabase(File file) throws IOException
IOException
public MockFileDatabase(File file, String encoding) throws IOException
IOException
public MockFileDatabase(InputStream stream) throws IOException
IOException
public MockFileDatabase(InputStream stream, String encoding) throws IOException
IOException
public MockFileDatabase(Reader reader) throws IOException
IOException
public MockFileDatabase(String string) throws IOException
IOException
public MockResult[] execute(MockExecuteContext ctx) throws SQLException
MockDataProvider
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-empty
MockExecuteContext.bindings()
is a strong indicator for a
PreparedStatement
.MockStatement
does not distinguish between "batch" and
"single" statements. However...
MockExecuteContext.batchSQL()
with more than one SQL
string is a strong indicator for a "multi-batch statement", as understood
by jOOQ's DSLContext.batch(Query...)
.MockExecuteContext.batchBindings()
with more than one
bind variable array is a strong indicator for a "single-batch statement",
as understood by jOOQ's DSLContext.batch(Query)
.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
)
Statement.getMoreResults()
.ResultQuery.fetchMany()
Statement.RETURN_GENERATED_KEYS
) are
requested from this execution, you can also add MockResult.data
to your result, in addition to the affected MockResult.rows
. The
relevant flag is passed from MockStatement
to any of these
properties:
execute
in interface MockDataProvider
ctx
- The execution context.SQLException
- A SQLException
that is passed through
to jOOQ.Copyright © 2014. All Rights Reserved.