Class GenericUpsert
java.lang.Object
com.northconcepts.datapipeline.jdbc.upsert.GenericUpsert
An upsert strategy that attempts to either:
- "insert" and then "update" if SQLIntegrityConstraintViolationException is thrown or
- 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBatch()Adds the applied values to the batch; called instead ofIUpsert.executeUpdate()when the writer is batching.voidaddExceptionProperties(DataException exception) Adds this strategy's details, such as the generated SQL, to the exception.clone()Returns a new, unprepared copy with the same settings;JdbcMultiUpsertWriteruses one for each connection.voidclose()Releases the statement(s); called when the writer is closed.voidExecutes the batch; called each time the writer's batch size is reached and on close if records remain.voidExecutes the upsert with the values applied; called after each record when the writer is not batching.Returns the generated INSERT SQL ornullbeforeprepare(JdbcUpsertWriter, Record)is called.Returns the prepared INSERT statement ornullbeforeprepare(JdbcUpsertWriter, Record)is called.Returns the generated UPDATE SQL ornullbeforeprepare(JdbcUpsertWriter, Record)is called.Returns the prepared UPDATE statement ornullbeforeprepare(JdbcUpsertWriter, Record)is called.booleanReturns true ifIUpsert.addBatch()andIUpsert.executeBatch()are supported; otherwiseJdbcUpsertWriter.setBatchSize(int)rejects batch sizes above 1.booleanisDebug()Indicates if the generated SQL should be logged (default false).booleanIndicates if an insert should be attempted before and update (the default) or false if the update should be attempted first.voidprepare(JdbcUpsertWriter writer, Record record) Called once, with the first record written, to generate the SQL and create the statement(s).setDebug(boolean debug) Set totrueto log generated SQL (default false).setInsertFirst(boolean insertFirst) Indicates if this upsert strategy should attempt an insert before and update (the default).voidsetValues(JdbcUpsertWriter writer, Record record) Applies the record's values to the pending statement(s); called for every record written.
-
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
Indicates if this upsert strategy should attempt an insert before and update (the default). Passing infalsewill cause an update to be attempted first followed by an insert if 0 records were affected by the update. -
getInsertSql
Returns the generated INSERT SQL ornullbeforeprepare(JdbcUpsertWriter, Record)is called. -
getInsertStatement
Returns the prepared INSERT statement ornullbeforeprepare(JdbcUpsertWriter, Record)is called. -
getUpdateSql
Returns the generated UPDATE SQL ornullbeforeprepare(JdbcUpsertWriter, Record)is called. -
getUpdateStatement
Returns the prepared UPDATE statement ornullbeforeprepare(JdbcUpsertWriter, Record)is called. -
isDebug
public boolean isDebug()Indicates if the generated SQL should be logged (default false). -
setDebug
Set totrueto log generated SQL (default false). -
prepare
Description copied from interface:IUpsertCalled once, with the first record written, to generate the SQL and create the statement(s). -
setValues
Description copied from interface:IUpsertApplies the record's values to the pending statement(s); called for every record written. -
executeUpdate
Description copied from interface:IUpsertExecutes the upsert with the values applied; called after each record when the writer is not batching.- Specified by:
executeUpdatein interfaceIUpsert- Throws:
Throwable
-
isBatchSupported
public boolean isBatchSupported()Description copied from interface:IUpsertReturns true ifIUpsert.addBatch()andIUpsert.executeBatch()are supported; otherwiseJdbcUpsertWriter.setBatchSize(int)rejects batch sizes above 1.- Specified by:
isBatchSupportedin interfaceIUpsert
-
addBatch
Description copied from interface:IUpsertAdds the applied values to the batch; called instead ofIUpsert.executeUpdate()when the writer is batching. -
executeBatch
Description copied from interface:IUpsertExecutes the batch; called each time the writer's batch size is reached and on close if records remain.- Specified by:
executeBatchin interfaceIUpsert- Throws:
Throwable
-
clone
Description copied from interface:IUpsertReturns a new, unprepared copy with the same settings;JdbcMultiUpsertWriteruses one for each connection. -
close
Description copied from interface:IUpsertReleases the statement(s); called when the writer is closed. -
addExceptionProperties
Description copied from interface:IUpsertAdds this strategy's details, such as the generated SQL, to the exception.- Specified by:
addExceptionPropertiesin interfaceIUpsert
-