Class GenericUpsert

java.lang.Object
com.northconcepts.datapipeline.jdbc.upsert.GenericUpsert
All Implemented Interfaces:
IUpsert, Cloneable

public class GenericUpsert extends Object implements IUpsert

An upsert strategy that attempts to either:

  1. "insert" and then "update" if SQLIntegrityConstraintViolationException is thrown or
  2. update and then insert if no records were affected.

Sample Generated SQL:
Insert SQL: INSERT INTO tableName (col1, col2, col3) VALUES (?, ?, ?)
Update SQL: UPDATE tableName SET col1=?, col2=?, col3=? WHERE ID=?

  • Constructor Details

    • GenericUpsert

      public GenericUpsert()
  • Method Details

    • isInsertFirst

      public boolean isInsertFirst()
      Indicates if an insert should be attempted before and update (the default) or false if the update should be attempted first.
    • setInsertFirst

      public GenericUpsert setInsertFirst(boolean insertFirst)
      Indicates if this upsert strategy should attempt an insert before and update (the default). Passing in false will cause an update to be attempted first followed by an insert if 0 records were affected by the update.
    • getInsertSql

      public String getInsertSql()
      Returns the generated INSERT SQL or null before prepare(JdbcUpsertWriter, Record) is called.
    • getInsertStatement

      public PreparedStatement getInsertStatement()
      Returns the prepared INSERT statement or null before prepare(JdbcUpsertWriter, Record) is called.
    • getUpdateSql

      public String getUpdateSql()
      Returns the generated UPDATE SQL or null before prepare(JdbcUpsertWriter, Record) is called.
    • getUpdateStatement

      public PreparedStatement getUpdateStatement()
      Returns the prepared UPDATE statement or null before prepare(JdbcUpsertWriter, Record) is called.
    • isDebug

      public boolean isDebug()
      Indicates if the generated SQL should be logged (default false).
      Specified by:
      isDebug in interface IUpsert
    • setDebug

      public GenericUpsert setDebug(boolean debug)
      Set to true to log generated SQL (default false).
      Specified by:
      setDebug in interface IUpsert
    • prepare

      public void prepare(JdbcUpsertWriter writer, Record record) throws Throwable
      Description copied from interface: IUpsert
      Called once, with the first record written, to generate the SQL and create the statement(s).
      Specified by:
      prepare in interface IUpsert
      Throws:
      Throwable
    • setValues

      public void setValues(JdbcUpsertWriter writer, Record record) throws Throwable
      Description copied from interface: IUpsert
      Applies the record's values to the pending statement(s); called for every record written.
      Specified by:
      setValues in interface IUpsert
      Throws:
      Throwable
    • executeUpdate

      public void executeUpdate() throws Throwable
      Description copied from interface: IUpsert
      Executes the upsert with the values applied; called after each record when the writer is not batching.
      Specified by:
      executeUpdate in interface IUpsert
      Throws:
      Throwable
    • isBatchSupported

      public boolean isBatchSupported()
      Description copied from interface: IUpsert
      Returns true if IUpsert.addBatch() and IUpsert.executeBatch() are supported; otherwise JdbcUpsertWriter.setBatchSize(int) rejects batch sizes above 1.
      Specified by:
      isBatchSupported in interface IUpsert
    • addBatch

      public void addBatch() throws Throwable
      Description copied from interface: IUpsert
      Adds the applied values to the batch; called instead of IUpsert.executeUpdate() when the writer is batching.
      Specified by:
      addBatch in interface IUpsert
      Throws:
      Throwable
    • executeBatch

      public void executeBatch() throws Throwable
      Description copied from interface: IUpsert
      Executes the batch; called each time the writer's batch size is reached and on close if records remain.
      Specified by:
      executeBatch in interface IUpsert
      Throws:
      Throwable
    • clone

      public GenericUpsert clone()
      Description copied from interface: IUpsert
      Returns a new, unprepared copy with the same settings; JdbcMultiUpsertWriter uses one for each connection.
      Specified by:
      clone in interface IUpsert
      Overrides:
      clone in class Object
    • close

      public void close() throws Throwable
      Description copied from interface: IUpsert
      Releases the statement(s); called when the writer is closed.
      Specified by:
      close in interface IUpsert
      Throws:
      Throwable
    • addExceptionProperties

      public void addExceptionProperties(DataException exception)
      Description copied from interface: IUpsert
      Adds this strategy's details, such as the generated SQL, to the exception.
      Specified by:
      addExceptionProperties in interface IUpsert