Class PostgreSqlInsertWriter

Direct Known Subclasses:
PostgreSqlUpsertWriter

public class PostgreSqlInsertWriter extends TextWriter
Generates PostgreSQL insert statement from records.
  • Field Details

    • pretty

      protected boolean pretty
    • batchSize

      protected int batchSize
    • batchedInserts

      protected int batchedInserts
    • tableName

      protected final String tableName
  • Constructor Details

    • PostgreSqlInsertWriter

      public PostgreSqlInsertWriter(String tableName, File file, boolean append)
    • PostgreSqlInsertWriter

      public PostgreSqlInsertWriter(String tableName, File file)
    • PostgreSqlInsertWriter

      public PostgreSqlInsertWriter(String tableName, Writer writer)
  • Method Details

    • writeRecord

      protected void writeRecord(Record record) throws Throwable
      Description copied from class: AbstractWriter
      Writes one row; called first for the header row (see AbstractWriter.isHeaderRow()) when field names are written, then for each record.
      Specified by:
      writeRecord in class AbstractWriter
      Throws:
      Throwable
    • close

      public void close()
      Description copied from class: DataEndpoint
      Indicates that this endpoint has finished reading or writing.
      Overrides:
      close in class TextWriter
    • getTableName

      public String getTableName()
    • isPretty

      public boolean isPretty()
      Indicates if line breaks and indentations should be added to the output insert statements (default false).
    • setPretty

      public PostgreSqlInsertWriter setPretty(boolean pretty)
      Indicates if line breaks and indentations should be added to output insert statement (default false).

      Example output (pretty = true):
      INSERT INTO "tableName" ("col1", "col2", "col3")
      VALUES (val1, val2, val3),
             (val1, val2, val3),
             (val1, val2, val3),...

      Example output (pretty = false):
      INSERT INTO "tableName" ("col1", "col2", "col3") VALUES (val1, val2, val3), (val1, val2, val3), (val1, val2, val3),...
    • getBatchSize

      public int getBatchSize()
      Returns the number of records to chunk together in batch inserts; inserts are not batched if it is 1 or less (default -1).
    • setBatchSize

      public PostgreSqlInsertWriter setBatchSize(int batchSize)
      Sets the number of records to chunk together in batch inserts. Inserts are not batched if this value is 1 or less. PostgreSqlInsertWriter defaults to non batching inserts.

      Example output (batchSize = 2):
      INSERT INTO "tableName" ("col1", "col2", "col3")
      VALUES (val1, val2, val3),
             (val1, val2, val3);

      Example output (batchSize = 0):
      INSERT INTO "tableName" ("col1", "col2", "col3") VALUES (val1, val2, val3);
      INSERT INTO "tableName" ("col1", "col2", "col3") VALUES (val1, val2, val3);
    • isBatchMode

      protected boolean isBatchMode()
      Indicates if inserts are batched, i.e. the batch size is greater than 1.
    • setFieldNamesInFirstRow

      public PostgreSqlInsertWriter setFieldNamesInFirstRow(boolean fieldNamesInFirstRow)
      Description copied from class: AbstractWriter
      Indicates if a header row of field names is written before the first record (default is true); must be called before AbstractWriter.open().
      Overrides:
      setFieldNamesInFirstRow in class AbstractWriter
    • addExceptionProperties

      public DataException addExceptionProperties(DataException exception)
      Description copied from class: Endpoint
      Adds this endpoint's current state to a DataException. Since this method is called whenever an exception is thrown, subclasses should override it to add their specific information.
      Overrides:
      addExceptionProperties in class TextWriter
    • getSqlValueLiteral

      protected String getSqlValueLiteral(Field field)
      Returns the SQL literal written for a field's value: null, a quoted string, a number, TRUE/FALSE, a JDBC escape such as {d '...'} for dates and times, '...'::jsonb for records, or a hex bytea string for blobs.