Class MySqlUpsert

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

public class MySqlUpsert extends Object implements IUpsert

A batch-able upsert strategy where when a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row occurs.

Sample Generated SQL:
INSERT INTO tableName (col1, col2, col3) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE col1=?, col2=?, col3=?

  • Constructor Details

    • MySqlUpsert

      public MySqlUpsert()
  • Method Details

    • isDebug

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

      public MySqlUpsert setDebug(boolean debug)
      Description copied from interface: IUpsert
      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 MySqlUpsert 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