Class JdbcUpsertWriter


public class JdbcUpsertWriter extends DataWriter
Writes records to a database table using the upsert idiom (attempt to insert, but update if duplicate key exists). The default strategy used is GenericUpsert, however several other are provided: MergeUpsert, VariableFieldsUpsert, MySqlUpsert.
  • Field Details

    • currentRecord

      protected Record currentRecord
  • Constructor Details

    • JdbcUpsertWriter

      public JdbcUpsertWriter(Connection connection, String tableName, IUpsert upsert, String... keyFieldNames)
      Creates a new instance with the autoCloseConnection property set to false; at least one key field is required.
    • JdbcUpsertWriter

      public JdbcUpsertWriter(Connection connection, String tableName, String... keyFieldNames)
      Creates a new instance using GenericUpsert with the autoCloseConnection property set to false.
    • JdbcUpsertWriter

      public JdbcUpsertWriter(JdbcConnectionFactory factory, String tableName, IUpsert upsert, String... keyFieldNames)
      Creates a new instance with the autoCloseConnection property set to true, using a connection obtained from the factory immediately; at least one key field is required.
    • JdbcUpsertWriter

      public JdbcUpsertWriter(JdbcConnectionFactory factory, String tableName, String... keyFieldNames)
      Creates a new instance using GenericUpsert with the autoCloseConnection property set to true.
  • Method Details

    • getConnection

      public Connection getConnection()
    • getTableName

      public String getTableName()
    • getAutoCloseConnection

      public boolean getAutoCloseConnection()
      Indicates if the database connection in this endpoint should be closed when the endpoint is closed. The default is false if this instance was constructed with a Connection or true if constructed with a JdbcConnectionFactory.
    • setAutoCloseConnection

      public JdbcUpsertWriter setAutoCloseConnection(boolean closeConnection)
      Indicates if the database connection in this endpoint should be closed when the endpoint is closed. The default is false if this instance was constructed with a Connection or true if constructed with a JdbcConnectionFactory.
    • getBatchSize

      public int getBatchSize()
      Returns the number of records to chunk together in batch updates; 1 or less (the default is -1) disables batching.
    • setBatchSize

      public JdbcUpsertWriter setBatchSize(int batchSize)
      Sets the number of records to chunk together in batch updates. Updates are not batched if this value is 1 or less. JdbcWriter defaults to non batching updates.
    • isBatchMode

      public boolean isBatchMode()
      Indicates if updates are batched, i.e. the batch size is greater than 1.
    • isCommitBatch

      public boolean isCommitBatch()
      Returns true if this reader calls commit on the connection after every batch is sent.
    • setCommitBatch

      public JdbcUpsertWriter setCommitBatch(boolean commitBatch)
      Indicates if this reader should call commit on the connection after every batch is sent, otherwise commit is called after the final batch update.
    • getKeyFields

      public FieldList getKeyFields()
      Returns the fields that identify the existing row to update.
    • getNonUpdateFields

      public FieldList getNonUpdateFields()
      Returns the list of fields that must not be updated if the recored already exists (default null). This method may return null or a list of empty fields.
    • setNonUpdateFields

      public JdbcUpsertWriter setNonUpdateFields(FieldList nonUpdateFields)
      Assigns the list of fields that must not be updated if the recored already exists (default null).
    • setNonUpdateFields

      public JdbcUpsertWriter setNonUpdateFields(String... nonUpdateFields)
      Assigns the list of fields that must not be updated if the record already exists (default null).
    • getUpsert

      public IUpsert getUpsert()
    • setJdbcType

      public JdbcUpsertWriter setJdbcType(String fieldName, int jdbcType)

      The given Java object will be converted to the given targetSqlType before being sent to the database.

      Note that this method may be used to pass database-specific abstract data types.

      Parameters:
      fieldName -
      jdbcType - the object containing the JDBC Type parameter value
    • getJdbcType

      public Integer getJdbcType(String fieldName)
      The Types constant (or a database specific value) assigned to the specified field using setJdbcType(String, int) or null if none was set.
      Returns:
      one of the Types constants or a database specific value.
    • setJdbcType

      public JdbcUpsertWriter setJdbcType(Class<?> type, int jdbcType)
      Forces field values of the specified Java class type to be sent to the database using PreparedStatement.setObject(parameterIndex, fieldValue, jdbcType) (instead of the appropriate PreparedStatement.setXXX() method for its field's type). This method may be used to pass database-specific abstract data types.
      Parameters:
      type - the Java class type to override.
      jdbcType - one of the Types constants or a database specific value.
    • getJdbcType

      public Integer getJdbcType(Class<?> type)
      The Types constant (or a database specific value) assigned to the specified Java class type using setJdbcType(Class, int) or null if none was set.
      Returns:
      one of the Types constants or a database specific value.
    • isDebug

      public boolean isDebug()
      Indicates if the generated SQL should be logged (default false).
    • setDebug

      public JdbcUpsertWriter setDebug(boolean debug)
      Indicates if the generated SQL should be logged (default false).
    • 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 DataWriter
    • setParameterValue

      public void setParameterValue(Field field, int fieldIndex, PreparedStatement statement) throws SQLException
      Sets statement parameter fieldIndex + 1 from the field, honoring any JDBC type assigned with setJdbcType.
      Throws:
      SQLException
    • setParameterValue

      public void setParameterValue(Field field, int fieldIndex, PreparedStatement statement, int parameterIndex) throws SQLException
      Sets the 1-based statement parameter from the field, honoring any JDBC type assigned with setJdbcType; fieldIndex is only used in error details.
      Throws:
      SQLException
    • write

      public void write(Record record) throws DataException
      Description copied from class: DataWriter
      Writes the specified record to this DataWriter and increases the record-count by 1. Any exception raised while writing will be converted to a DataException using DataObject.exception(Throwable).

      Subclasses generally do not need to override this method, instead they should implement DataWriter.writeImpl(Record).

      Overrides:
      write in class DataWriter
      Throws:
      DataException
    • writeImpl

      protected void writeImpl(Record record) throws Throwable
      Description copied from class: DataWriter
      Overridden by subclasses to write the specified record to this DataWriter.

      Contract for subclasses (see also docs/authoring/DataWriter.md):

      Specified by:
      writeImpl in class DataWriter
      Throws:
      Throwable
    • open

      public void open()
      Description copied from class: DataEndpoint
      Makes this endpoint ready for reading or writing.
      Overrides:
      open in class DataEndpoint
    • close

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