Qore jni Module  1.0.1
org.qore.lang.bulksqlutil.BulkInsertOperation Class Reference

Java wrapper for the Qore class for bulk DML insert operations. More...

Inheritance diagram for org.qore.lang.bulksqlutil.BulkInsertOperation:
org.qore.lang.bulksqlutil.AbstractBulkOperation org.qore.jni.QoreObjectWrapper

Public Member Methods

 BulkInsertOperation (AbstractTable target, Map< String, Object > opts) throws Throwable
 creates the object from the supplied arguments More...
 
 BulkInsertOperation (AbstractTable target) throws Throwable
 creates the object from the supplied arguments More...
 
void setRowCode (BulkRowCallback rowc) throws Throwable
 sets a row callback that will be called when data has been sent to the database and all output data is available; must accept a hash argument that represents the data written to the database including any output arguments. This code will be reset once the transaction is commited. More...
 
void setRowCode () throws Throwable
 clears the row callback called when data has been sent to the database and all output data is available More...
 
- Public Member Methods inherited from org.qore.lang.bulksqlutil.AbstractBulkOperation
 AbstractBulkOperation (QoreObject obj) throws Throwable
 creates a new AbstractBulkOperation object wrapping the Qore object
 
void queueData (Map< String, Object > data) throws Throwable
 queues row data in the block buffer; the block buffer is flushed to the DB if the buffer size reaches the limit defined by the block_size option; does not commit the transaction More...
 
void queueData (Map< String, Object >[] l) throws Throwable
 queues row data in the block buffer; the block buffer is flushed to the DB if the buffer size reaches the limit defined by the block_size option; does not commit the transaction More...
 
void flush () throws Throwable
 flushes any remaining batched data to the database; this method should always be called before committing the transaction or destroying the object More...
 
void discard () throws Throwable
 discards any buffered batched data; this method should be called before destroying the object if an error occurs More...
 
void commit () throws Throwable
 flushes any queued data and commits the transaction
 
void rollback () throws Throwable
 discards any queued data and rolls back the transaction
 
String getTableName () throws Throwable
 returns the table name
 
AbstractTable getTable () throws Throwable
 returns the underlying SqlUtil::AbstractTable object
 
AbstractDatasource getDatasource () throws Throwable
 returns the AbstractDatasource object associated with this object
 
int getRowCount () throws Throwable
 returns the affected row count
 
- Public Member Methods inherited from org.qore.jni.QoreObjectWrapper
 QoreObjectWrapper (QoreObject obj)
 creates the wrapper object with the Qore object
 
void release ()
 releases the Qore object; do not call any further methods on the object after this call
 
QoreObject getQoreObject ()
 returns the Qore object
 
String className ()
 returns the class name for the Qore object
 
boolean instanceOf (String class_name)
 returns true if the object is an instance of the given class
 

Additional Inherited Members

- Private Attributes inherited from org.qore.jni.QoreObjectWrapper
QoreObject obj
 the wrapper Qore object
 

Detailed Description

Java wrapper for the Qore class for bulk DML insert operations.

This class assists with bulk inserts into a target table.

Submitting Data
To use this class, queue data in the form of a hash (a single row or a set of rows) or a list of rows by calling the queueData() method.

The queueData() method queues data to be written to the database; the queue is flush()ed automatically when block_size rows have been queued.
Retrieving Data From Inserts
It is possible to use sql_iop_funcs in the hashes submitted with queueData(); in this case the BulkInsertOperation class assumes that every row has the same operations as in the first row. Output data can then be processed by using the rowcode option in the constructor() or by calling setRowCode().

In case sql_iop_funcs are used and a rowcode option is set, then the SQL DML query for inserts is creating using the "returning" insert option, therefore the DBI driver in this case must support this option as well.
Flushing and Discarding Data
Each call to flush() (whether implicit or explicit) will cause a single call to be made to the dataserver; all queued rows are sent in a single bulk DML call, which allows for efficient processing of large amounts of data.

A call to flush() must be made before committing the transaction to ensure that any remaining rows in the internal queue have been written to the database. Because the destructor() will throw an exception if any data is left in the internal queue when the object is destroyed, a call to discard() must be made prior to the destruction of the object in case of errors.
// single commit and rollback
try {
// data is queued and flushed automatically when the buffer is full
for (Map<String, Object> i : data1) {
op1.queueData(i);
}
for (Map<String, Object> i : data2) {
op2.queueData(i);
}
// each operation needs to be flushed or discarded individually
op1.flush();
op2.flush();
ds.commit();
} catch (Throwable e) {
op1.discard();
op2.discard();
ds.rollback();
throw e;
}
Note
Each bulk DML object must be manually flush()ed before committing or manually discard()ed before rolling back to ensure that all data is managed properly in the same transaction and to ensure that no exception is thrown in Qore destructor. See the example above for more information.

Constructor & Destructor Documentation

◆ BulkInsertOperation() [1/2]

org.qore.lang.bulksqlutil.BulkInsertOperation.BulkInsertOperation ( AbstractTable  target,
Map< String, Object >  opts 
) throws Throwable
inline

creates the object from the supplied arguments

Parameters
targetthe target table object
optsan optional hash of options for the object as follows:
  • "info_log": an optional info logging callback of type LogCallback
  • "block_size": the number of rows executed at once (default: 1000)
  • "rowcode": a per-row callback that must be an instance of BulkRowCallback; the BulkRowCallback.call() method will be called for every row after a bulk insert; the hash argument representing the row inserted will also contain any output values if applicable (for example if sql_iop_funcs are used in the row hashes submitted to queueData())
See also
setRowCode()

◆ BulkInsertOperation() [2/2]

org.qore.lang.bulksqlutil.BulkInsertOperation.BulkInsertOperation ( AbstractTable  target) throws Throwable
inline

creates the object from the supplied arguments

Parameters
targetthe target table object
See also
setRowCode()

Member Function Documentation

◆ setRowCode() [1/2]

void org.qore.lang.bulksqlutil.BulkInsertOperation.setRowCode ( BulkRowCallback  rowc) throws Throwable
inline

sets a row callback that will be called when data has been sent to the database and all output data is available; must accept a hash argument that represents the data written to the database including any output arguments. This code will be reset once the transaction is commited.

Example:
// single commit and rollback
try {
inserter.setRowCode(new MyRowCallback());
// data is queued and flushed automatically when the buffer is full
for (Map<String, Object> i : data) {
inserter.queueData(i);
}
// each operation needs to be flushed or discarded individually
inserter.flush();
ds.commit();
} catch (Throwable e) {
inserter.discard();
ds.rollback();
throw e;
}
Parameters
rowca row callback that will be called when data has been sent to the database and all output data is available; must accept a hash argument that represents the data written to the database including any output arguments
Note
  • the row callback can also be set by using the "rowcode" option in the constructor()
  • if this method is not called before the first row is queued then output values will not be retrieved; the initial query is built when the template row is queued and output values are only retrieved if a rowcode callback is set beforehand

◆ setRowCode() [2/2]

void org.qore.lang.bulksqlutil.BulkInsertOperation.setRowCode ( ) throws Throwable
inline

clears the row callback called when data has been sent to the database and all output data is available

Example:
// single commit and rollback
try {
// clear any row callback
inserter.setRowCode();
// data is queued and flushed automatically when the buffer is full
for (Map<String, Object> i : data) {
inserter.queueData(i);
}
// each operation needs to be flushed or discarded individually
inserter.flush();
ds.commit();
} catch (Throwable e) {
inserter.discard();
ds.rollback();
throw e;
}

The documentation for this class was generated from the following file: