Class MergeUpsert

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

public class MergeUpsert extends Object implements IUpsert

A batch-able upsert strategy that relies on the SQL Merge statement.

Sample Generated SQL:
MERGE INTO tableName target USING (VALUES (?)) source(ID) ON target.ID=source.ID
WHEN NOT MATCHED THEN INSERT (col1, col2, col3) VALUES (?, ?, ?)
WHEN MATCHED THEN UPDATE SET SET col1=?, col2=?, col3=?;

  • Constructor Details

    • MergeUpsert

      public MergeUpsert()
  • Method Details

    • getMergeSql

      public String getMergeSql()
      Returns the generated MERGE SQL or null before prepare(JdbcUpsertWriter, Record) is called.
    • getMergeStatement

      public PreparedStatement getMergeStatement()
      Returns the prepared MERGE statement or null before prepare(JdbcUpsertWriter, Record) is called.
    • getMergeCondition

      public String getMergeCondition()
    • setMergeCondition

      public MergeUpsert setMergeCondition(String mergeCondition)
    • isTerminateWithSemicolon

      public boolean isTerminateWithSemicolon()
      Indicates if the MERGE statement ends with a semicolon (default is false); prepare(JdbcUpsertWriter, Record) turns this on when the database product name is Microsoft SQL Server or ASE.
    • setTerminateWithSemicolon

      public MergeUpsert setTerminateWithSemicolon(boolean terminateWithSemicolon)
      Indicates if the MERGE statement ends with a semicolon (default is false); prepare(JdbcUpsertWriter, Record) turns this on when the database product name is Microsoft SQL Server or ASE.
    • 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 MergeUpsert 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 MergeUpsert 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