Qore TableMapper Module Reference 1.3
|
Classes provided by this module:
InboundTableMapper objects can be used to insert sequence values in the target table, and the values inserted are returned in the output value of InboundTableMapper::insertRow() and also present in the argument to any rowcode call when using the bulk insert API on supported databases (databases supporting the returning
clause in insert statements).
The mapper field options are:
sequence:
inserts the value of the given sequence into the target column and increments the sequencesequence_currval:
inserts the current value the given sequence into the target column; does not increment the sequenceThe following is an example map for an InboundTableMapper hash with comments:
If this map is applied in the following way:
This will insert all the mapped input data into data into the ORDER_TABLE
table and then print out the number of rows inserted.
The following is an example for TableMapper::SqlStatementOutboundMapper. It selects data from the ORDER_DATE
table and it transforms rows according to the mappings supplied in the constructor.
The bulk insert API allows for multiple rows to be mapped and inserted in a single server round trip for high-performance applications. This requires bulk DML support in the underlying DBI driver and also in SqlUtil (to determine if bulk DML support is available, call SqlUtil::AbstractTable::hasArrayBind() on the SqlUtil::AbstractTable object).
The bulk insert API consists of the following methods:
The behavior of the bulk insert API can be modified or tuned with the following options:
"insert_block"
: the number of rows inserted in a single block (default: 1000)"unstable_input"
is False and bulk DML is supported in the SqlUtil::AbstractTable objectTo queue data for bulk insert, call TableMapper::InboundTableMapper::queueData() instead of TableMapper::InboundTableMapper::insertRow(). To perform per-row actions, the TableMapper::InboundTableMapper::setRowCode() method should be called with a closure that accepts a hash representing a single row; whenever data is flushed to the database, this closure will be called with the row actually inserted (including sequence values used, etc).
Before committing the transaction, ensure that TableMapper::InboundTableMapper::flush() is called for each TableMapper::InboundTableMapper object participating in the transaction. This ensures that all data has been flushed to the database before committing the transaction.
If there are any errors, call TableMapper::InboundTableMapper::discard() before rolling the transaction back.
Consider the following example:
The mapper hash is made up of target (ie output) column names as the key values assigned to field specifications as specified in Mapper Specification Format, plus the following hash options:
"sequence"
: a name of a sequence to use to populate the column; the output buffers for this option are bound as type number, so the output type depends on the database driver's number option setting (for example, with "optimal-numbers"
, the values here are generally returned as strings; cannot be used with the upsert InboundTableMapper option)"sequence_currval"
: a name of a sequence to use to return the current value of the sequence; this is useful when assigning the same sequence value to multiple columns; the output buffers for this option are bound as type number, so the output type depends on the database driver's number option setting (for example, with "optimal-numbers"
, the values here are generally returned as strings; cannot be used with the upsert InboundTableMapper option)In both cases, the actual value inserted in the table is available in the following APIs:
Additionally, the value is provided to any row code set with TableMapper::InboundTableMapper::setRowCode(); see InboundTableMapper Bulk Insert API for more information.
AbstractMapperIterator::mapBulk()
to fail (issue 979)"table"
or "sh"
options were used and only worked with subclasses that declared these options (issue 981)"returning"
clause in insert statements; now such inserts are made in a single round trip instead of n + 1 where n is the number of sequences in the insert"unstable_input"
: to accommodate unstable input data and disable the insert optimization (default: False)"insert_block"
: for DB drivers supporting bulk DML (for use with the TableMapper::InboundTableMapper::queueData(), TableMapper::InboundTableMapper::flush(), and TableMapper::InboundTableMapper::discard() methods), the number of rows inserted at once (default: 1000, only used when "unstable_input"
is False) and bulk inserts are supported in the table object