Class FileStatementsWriter

java.lang.Object
org.fastnate.generator.statements.AbstractStatementsWriter
org.fastnate.generator.statements.FileStatementsWriter
All Implemented Interfaces:
Closeable, AutoCloseable, StatementsWriter
Direct Known Subclasses:
PostgreSqlBulkWriter

public class FileStatementsWriter extends AbstractStatementsWriter
Implementation of StatementsWriter which writes the statements into a Writer resp. File.
Author:
Tobias Liefke
  • Field Details

  • Constructor Details

    • FileStatementsWriter

      public FileStatementsWriter(File file) throws FileNotFoundException
      Creates a new instance for a specifc file and UTF-8 encoding.
      Parameters:
      file - the target file
      Throws:
      FileNotFoundException - if the file could not be opened for writing
    • FileStatementsWriter

      public FileStatementsWriter(File file, Charset encoding) throws FileNotFoundException
      Creates a new instance for a specifc file and encoding.
      Parameters:
      file - the target file
      encoding - the charset of the target file
      Throws:
      FileNotFoundException - if the file could not be opened for writing
    • FileStatementsWriter

      public FileStatementsWriter(GeneratorContext context) throws FileNotFoundException
      Creates a new instance of FileStatementsWriter. The target file and its encoding are taken from the settings OUTPUT_FILE_KEY and OUTPUT_ENCODING_KEY.
      Parameters:
      context - the context of the generation (for lookup of the properties)
      Throws:
      FileNotFoundException - if the file path is not available for writing
    • FileStatementsWriter

      public FileStatementsWriter(Writer writer)
      Creates a new FileStatementsWriter instance.
      Parameters:
      writer - Used to write the SQL statements.
  • Method Details

    • ensureDirectoryExists

      protected static File ensureDirectoryExists(File outputFile)
      Ensures, that the parent directory of the given output file exists.
      Parameters:
      outputFile - the output file
      Returns:
      the output file (for chaining)
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class AbstractStatementsWriter
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Description copied from interface: StatementsWriter
      Flushes any open statements.
      Specified by:
      flush in interface StatementsWriter
      Overrides:
      flush in class AbstractStatementsWriter
      Throws:
      IOException - if the target throws an exception
    • write

      public void write(String statements) throws IOException
      Writes a bunch of SQL statements to the file.
      Parameters:
      statements - the SQL statements (or anything else what should be added to the file)
      Throws:
      IOException - if the writer throws one
    • writeComment

      public void writeComment(String comment) throws IOException
      Description copied from interface: StatementsWriter
      Writes a SQL comment to the target.
      Specified by:
      writeComment in interface StatementsWriter
      Overrides:
      writeComment in class AbstractStatementsWriter
      Parameters:
      comment - the comment to write
      Throws:
      IOException - if the target throws an exception
    • writePlainStatement

      public void writePlainStatement(GeneratorDialect dialect, String sql) throws IOException
      Description copied from interface: StatementsWriter
      Writes a plain SQL statement. Shortcut for writeStatement(createPlainStatement(sql), dialect).
      Parameters:
      dialect - the current database dialect
      sql - the content of the SQL statement
      Throws:
      IOException - if the file or database throws an exception
    • writeSectionSeparator

      public void writeSectionSeparator() throws IOException
      Description copied from interface: StatementsWriter
      Writes a new line to the target file to separate different sections in the SQL file.
      Specified by:
      writeSectionSeparator in interface StatementsWriter
      Overrides:
      writeSectionSeparator in class AbstractStatementsWriter
      Throws:
      IOException - if the target throws such an exception
    • writeStatement

      public void writeStatement(EntityStatement statement) throws IOException
      Description copied from interface: StatementsWriter
      Writes the given statement to a file or database.
      Parameters:
      statement - contains the values to write
      Throws:
      IOException - if the file or database throws an exception
    • getWriter

      public Writer getWriter()
      Used to write the SQL statements.
    • getStatementSeparator

      public String getStatementSeparator()
      The separator of the single statements, defaults to ";\n".
    • setStatementSeparator

      public void setStatementSeparator(String statementSeparator)
      The separator of the single statements, defaults to ";\n".
    • getStatementsCount

      public int getStatementsCount()
      The count of written statements.