Class PostgreSqlBulkWriter
java.lang.Object
org.fastnate.generator.statements.AbstractStatementsWriter
org.fastnate.generator.statements.FileStatementsWriter
org.fastnate.generator.statements.PostgreSqlBulkWriter
- All Implemented Interfaces:
Closeable,AutoCloseable,StatementsWriter
Implementation of a
StatementsWriter that writes bulk text files for each table, and references them in "COPY
INTO".- Author:
- Tobias Liefke
-
Nested Class Summary
Nested ClassesNested 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, Writer> The open bulk files for each table.private final GeneratorContextThe current generation context.private final ContextModelListenerThe current generation context.private final FileThe directory for the bulk files.private final CharsetThe encoding of the bulk files.private final Map<GeneratorTable, Integer> Remembers for each table which files we've already generated.All files generated by this writer.private static final org.slf4j.Loggerprivate intThe count of written statements.Fields inherited from class org.fastnate.generator.statements.FileStatementsWriter
OUTPUT_ENCODING_KEY, OUTPUT_FILE_KEY -
Constructor Summary
ConstructorsConstructorDescriptionPostgreSqlBulkWriter(GeneratorContext context) Creates a new instance for a SQL file with UTF-8 encoding.PostgreSqlBulkWriter(GeneratorContext context, File sqlFile) Creates a new instance for a SQL file with UTF-8 encoding.PostgreSqlBulkWriter(GeneratorContext context, File directory, Writer writer, Charset encoding) Creates a new instance ofPostgreSqlBulkWriter.PostgreSqlBulkWriter(GeneratorContext context, File sqlFile, Charset encoding) Creates a new instance for a SQL file. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidcloseBulkWriter(GeneratorTable table) Closes the current writer of the given table (for example when the table structure has changed).private voidprivate WriterfindBulkWriter(GeneratorTable generatorTable, GeneratorDialect dialect) voidflush()Flushes any open statements.The directory for the bulk files.The encoding of the bulk files.All files generated by this writer.intThe count of written statements.private voidvoidwritePlainStatement(GeneratorDialect dialect, String sql) Writes a plain SQL statement.voidWrites the given statement to a file or database.Methods inherited from class org.fastnate.generator.statements.FileStatementsWriter
ensureDirectoryExists, getStatementSeparator, getWriter, setStatementSeparator, write, writeComment, writeSectionSeparatorMethods inherited from class org.fastnate.generator.statements.AbstractStatementsWriter
createInsertStatement, createPlainStatement, createUpdateStatementMethods 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 -
context
The current generation context. -
contextListener
The current generation context. -
directory
The directory for the bulk files. -
encoding
The encoding of the bulk files. -
fileNumbers
Remembers for each table which files we've already generated. -
bulkWriters
The open bulk files for each table. -
generatedFiles
All files generated by this writer. -
statementsCount
private int statementsCountThe count of written statements.
-
-
Constructor Details
-
PostgreSqlBulkWriter
Creates a new instance for a SQL file with UTF-8 encoding. All bulk files will end up in the same directory as the given file.- Parameters:
context- the current generation context- Throws:
FileNotFoundException- if the directory is not available
-
PostgreSqlBulkWriter
Creates a new instance for a SQL file with UTF-8 encoding. All bulk files will end up in the same directory as the given file.- Parameters:
context- the current generation contextsqlFile- the file that is feeded with all plain statements- Throws:
FileNotFoundException- if the directory is not available
-
PostgreSqlBulkWriter
public PostgreSqlBulkWriter(GeneratorContext context, File sqlFile, Charset encoding) throws FileNotFoundException Creates a new instance for a SQL file. All bulk files will end up in the same directory as the given file.- Parameters:
context- the current generation contextsqlFile- the file that is feeded with all plain statementsencoding- the encoding of all written files- Throws:
FileNotFoundException- if the directory is not available
-
PostgreSqlBulkWriter
public PostgreSqlBulkWriter(GeneratorContext context, File directory, Writer writer, Charset encoding) Creates a new instance ofPostgreSqlBulkWriter.- Parameters:
context- the current generation contextdirectory- the directory for the bulk fileswriter- the SQL file which contains the plain and the "COPY" statementsencoding- The encoding of the bulk files.
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFileStatementsWriter- Throws:
IOException
-
closeBulkWriter
Closes the current writer of the given table (for example when the table structure has changed).- Parameters:
table- the table of the writer- Throws:
IOException- if there was a problem when closing the writer
-
closeBulkWriters
- Throws:
IOException
-
findBulkWriter
private Writer findBulkWriter(GeneratorTable generatorTable, GeneratorDialect dialect) throws IOException - Throws:
IOException
-
flush
Description copied from interface:StatementsWriterFlushes any open statements.- Specified by:
flushin interfaceStatementsWriter- Overrides:
flushin classFileStatementsWriter- Throws:
IOException- if the target throws an exception
-
writePlainStatement
Description copied from interface:StatementsWriterWrites a plain SQL statement. Shortcut forwriteStatement(createPlainStatement(sql), dialect).- Specified by:
writePlainStatementin interfaceStatementsWriter- Overrides:
writePlainStatementin classFileStatementsWriter- 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.- Specified by:
writeStatementin interfaceStatementsWriter- Overrides:
writeStatementin classFileStatementsWriter- Parameters:
stmt- contains the values to write- Throws:
IOException- if the file or database throws an exception
-
getDirectory
The directory for the bulk files. -
getEncoding
The encoding of the bulk files. -
getGeneratedFiles
All files generated by this writer. -
getStatementsCount
public int getStatementsCount()The count of written statements.- Overrides:
getStatementsCountin classFileStatementsWriter
-