Class ConnectedStatementsWriter
java.lang.Object
org.fastnate.generator.statements.AbstractStatementsWriter
org.fastnate.generator.statements.ConnectedStatementsWriter
- All Implemented Interfaces:
Closeable,AutoCloseable,StatementsWriter
An implementation of a
StatementsWriter which writes the SQL statements directly to a database connection.- Author:
- Tobias Liefke
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate static final classNested classes/interfaces inherited from class org.fastnate.generator.statements.AbstractStatementsWriter
AbstractStatementsWriter.AbstractTableStatement, AbstractStatementsWriter.InsertStatement, AbstractStatementsWriter.PlainStatement, AbstractStatementsWriter.UpdateStatement -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<GeneratorTable, List<ConnectedStatementsWriter.PreparedInsertStatement>> All available prepared statements per table.private intThe count of statements executed in the current batch.private final booleanIndicates that the database connection supports batch statements.private final booleanIndicates that we should close the connection at the end.private final ConnectionThe database connection that was used when creating this generator.private final GeneratorContextThe generator context that is attached to this writer.private final ContextModelListenerThe listener for model changes in the context when new classes are discovered.static final StringName of the setting that contains the connection driver class, if the writer create the a connection himself.static final StringName of the setting that contains the connection password, if the writer create the a connection himself.static final StringName of the setting that contains the connection URL, if the writer create the a connection himself.static final StringName of the setting that contains the connection user, if the writer create the a connection himself.private longThe last time that we have written a log message about the count of statements.private static final org.slf4j.Loggerstatic final StringName of the setting which turns logging of statements on or off.private final booleanIndicates that we log each statement for debugging purposes.static final StringName of the setting which controls the maximum size of generated batches.private final intThe maximum count of statements per batch job.private static final longThe count of milliseconds to wait, until a log message with the current count of statements is written.private static final intThe minimum major version of the JDBC API that the driver needs to support.private static final intThe minimum minor version of the JDBC API that the driver needs to support.private final StatementUsed to execute all plain SQL statements.private final List<ConnectedStatementsWriter.PreparedInsertStatement> All prepared statements for the tables.private longThe count of statements that we have executed up to now. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateConnectedStatementsWriter(Connection connection, boolean closeConnection, GeneratorContext context) ConnectedStatementsWriter(Connection connection, GeneratorContext context) Creates a new StatementsWriter that writes to a database connection.Creates a new StatementsWriter that writes to a database connection. -
Method Summary
Modifier and TypeMethodDescriptionprivate static ConnectionbuildConnection(GeneratorContext context) private voidcheckUpdate(int updatedRows, String sql) voidclose()private voidcreateInsertStatement(GeneratorDialect dialect, GeneratorTable table) Creates a container for a dedicated "insert into table" statement.voidflush()Flushes any open statements.The database connection that was used when creating this generator.longThe count of statements that we have executed up to now.private voidvoidwritePlainStatement(GeneratorDialect dialect, String sql) Writes a plain SQL statement.voidWrites the given statement to a file or database.private voidMethods inherited from class org.fastnate.generator.statements.AbstractStatementsWriter
createPlainStatement, createUpdateStatement, writeComment, writeSectionSeparatorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.fastnate.generator.statements.StatementsWriter
truncateTables
-
Field Details
-
log
private static final org.slf4j.Logger log -
DATABASE_DRIVER_KEY
Name of the setting that contains the connection driver class, if the writer create the a connection himself.- See Also:
-
DATABASE_URL_KEY
Name of the setting that contains the connection URL, if the writer create the a connection himself.- See Also:
-
DATABASE_USER_KEY
Name of the setting that contains the connection user, if the writer create the a connection himself.- See Also:
-
DATABASE_PASSWORD_KEY
Name of the setting that contains the connection password, if the writer create the a connection himself.- See Also:
-
MAX_BATCH_SIZE_KEY
Name of the setting which controls the maximum size of generated batches. If set to something below 2, no batches are used.- See Also:
-
LOG_STATEMENTS_KEY
Name of the setting which turns logging of statements on or off.- See Also:
-
MILLISECONDS_BETWEEN_LOG_MESSAGES
private static final long MILLISECONDS_BETWEEN_LOG_MESSAGESThe count of milliseconds to wait, until a log message with the current count of statements is written.- See Also:
-
MINIMUM_JDBC_DRIVER_MAJOR_VERSION
private static final int MINIMUM_JDBC_DRIVER_MAJOR_VERSIONThe minimum major version of the JDBC API that the driver needs to support.- See Also:
-
MINIMUM_JDBC_DRIVER_MINOR_VERSION
private static final int MINIMUM_JDBC_DRIVER_MINOR_VERSIONThe minimum minor version of the JDBC API that the driver needs to support.- See Also:
-
context
The generator context that is attached to this writer. -
contextListener
The listener for model changes in the context when new classes are discovered. -
connection
The database connection that was used when creating this generator. -
closeConnection
private final boolean closeConnectionIndicates that we should close the connection at the end. -
batchSupported
private final boolean batchSupportedIndicates that the database connection supports batch statements. -
logStatements
private final boolean logStatementsIndicates that we log each statement for debugging purposes. -
maxBatchSize
private final int maxBatchSizeThe maximum count of statements per batch job. -
plainStatement
Used to execute all plain SQL statements. -
preparedStatements
All prepared statements for the tables. -
availablePreparedStatements
private final Map<GeneratorTable,List<ConnectedStatementsWriter.PreparedInsertStatement>> availablePreparedStatementsAll available prepared statements per table. -
batchCount
private int batchCountThe count of statements executed in the current batch. -
lastLogTime
private long lastLogTimeThe last time that we have written a log message about the count of statements. -
statementsCount
private long statementsCountThe count of statements that we have executed up to now.
-
-
Constructor Details
-
ConnectedStatementsWriter
private ConnectedStatementsWriter(Connection connection, boolean closeConnection, GeneratorContext context) throws SQLException - Throws:
SQLException
-
ConnectedStatementsWriter
public ConnectedStatementsWriter(Connection connection, GeneratorContext context) throws SQLException Creates a new StatementsWriter that writes to a database connection.- Parameters:
connection- the database connectioncontext- contains the indices for initialization- Throws:
SQLException- if the connection is invalid
-
ConnectedStatementsWriter
Creates a new StatementsWriter that writes to a database connection. The database connection is created from the properties in the context.- Parameters:
context- contains the properties and indices for initialization- Throws:
SQLException- if the connection is invalid
-
-
Method Details
-
buildConnection
- Throws:
SQLException
-
checkUpdate
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classAbstractStatementsWriter- Throws:
IOException
-
closeBatch
- Throws:
IOException
-
createInsertStatement
Description copied from interface:StatementsWriterCreates a container for a dedicated "insert into table" statement.- Specified by:
createInsertStatementin interfaceStatementsWriter- Overrides:
createInsertStatementin classAbstractStatementsWriter- Parameters:
dialect- the current database dialecttable- the name of the table- Returns:
- the created statement
-
flush
Description copied from interface:StatementsWriterFlushes any open statements.- Specified by:
flushin interfaceStatementsWriter- Overrides:
flushin classAbstractStatementsWriter- Throws:
IOException- if the target throws an exception
-
writePlainStatement
Description copied from interface:StatementsWriterWrites a plain SQL statement. Shortcut forwriteStatement(createPlainStatement(sql), dialect).- Parameters:
dialect- the current database dialectsql- the content of the SQL statement- Throws:
IOException- if the file or database throws an exception
-
writePlainStatement
- Throws:
IOException
-
writeStatement
Description copied from interface:StatementsWriterWrites the given statement to a file or database.- Parameters:
stmt- contains the values to write- Throws:
IOException- if the file or database throws an exception
-
writeTableStatement
- Throws:
IOException
-
getConnection
The database connection that was used when creating this generator. -
getStatementsCount
public long getStatementsCount()The count of statements that we have executed up to now.
-