org.datanucleus.store.rdbms.request.InsertRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-rdbms Show documentation
Show all versions of datanucleus-rdbms Show documentation
Plugin for DataNucleus providing persistence to RDBMS datastores.
/**********************************************************************
Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
2003 Erik Bengtson - the fields to fetch are better managed for application
identity. Their selection were moved from execute
method to the constructor
2003 Andy Jefferson - coding standards
2004 Andy Jefferson - conversion to use Logger
2004 Erik Bengtson - changed to use mapping consumer
2004 Andy Jefferson - added discriminator support
2005 Andy Jefferson - fixed 1-1 bidir order of insertion
...
**********************************************************************/
package org.datanucleus.store.rdbms.request;
import static java.util.stream.Collectors.toList;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ExecutionContext;
import org.datanucleus.exceptions.NotYetFlushedException;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.ColumnMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.metadata.MetaData;
import org.datanucleus.metadata.MetaDataUtils;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.metadata.VersionMetaData;
import org.datanucleus.state.ActivityState;
import org.datanucleus.state.ObjectProvider;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.identifier.DatastoreIdentifier;
import org.datanucleus.store.rdbms.mapping.MappingCallbacks;
import org.datanucleus.store.rdbms.mapping.MappingConsumer;
import org.datanucleus.store.rdbms.mapping.MappingType;
import org.datanucleus.store.rdbms.mapping.column.ColumnMapping;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.mapping.java.PersistableMapping;
import org.datanucleus.store.rdbms.mapping.java.ReferenceMapping;
import org.datanucleus.store.rdbms.query.StatementClassMapping;
import org.datanucleus.store.rdbms.query.StatementMappingIndex;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.SQLController;
import org.datanucleus.store.rdbms.adapter.DatastoreAdapter;
import org.datanucleus.store.rdbms.fieldmanager.ParameterSetter;
import org.datanucleus.store.rdbms.table.Column;
import org.datanucleus.store.rdbms.table.DatastoreClass;
import org.datanucleus.store.rdbms.table.SecondaryTable;
import org.datanucleus.store.schema.table.SurrogateColumnType;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
/**
* Class to provide a means of insertion of records to RDBMS.
* Extends basic request class implementing the execute method to do a JDBC insert operation.
* The SQL will be of the form
*
* INSERT INTO TBL_NAME (col1, col2, col3, ...) VALUES (?, ?, ?, ...)
*
*
* When inserting an object with inheritance this will involve 1 InsertRequest for each table involved.
* So if we have a class B that extends class A and they both use "new-table" inheritance strategy, we will have 2 InsertRequests, one for table A, and one for table B.
*
*
* When the InsertRequest starts to populate its statement and it has a PC field, this calls PersistableMapping.setObject().
* This then checks if the other PC object is yet persistent and, if not, will persist it before processing this objects INSERT.
* This forms the key to "persistence-by-reachability".
*
*/
public class InsertRequest extends Request
{
private static final int IDPARAMNUMBER = 1;
private final MappingCallbacks[] callbacks;
/** Numbers of fields in the INSERT statement (excluding PK). */
private final int[] insertFieldNumbers;
/** Numbers of Primary key fields. */
private final int[] pkFieldNumbers;
/** Numbers of fields that are reachable yet have no datastore column in this table. Used for reachability. */
private final int[] reachableFieldNumbers;
/** Numbers of fields that are relations that may be detached when persisting but not bidir so cant attach yet. */
private final int[] relationFieldNumbers;
/** SQL statement for the INSERT. */
private final String insertStmt;
/** Whether the class has an identity (auto-increment, serial etc) column */
private boolean hasIdentityColumn = false;
/** one StatementExpressionIndex for each field **/
private StatementMappingIndex[] stmtMappings;
/** StatementExpressionIndex for fields to be "retrieved" **/
private StatementMappingIndex[] retrievedStmtMappings;
/** StatementExpressionIndex for version **/
private StatementMappingIndex versionStmtMapping;
/** StatementExpressionIndex for discriminator. **/
private StatementMappingIndex discriminatorStmtMapping;
/** StatementExpressionIndex for multi-tenancy. **/
private StatementMappingIndex multitenancyStmtMapping;
/** StatementExpressionIndex for soft-delete. **/
private StatementMappingIndex softDeleteStmtMapping;
/** StatementExpressionIndex for create-user. **/
private StatementMappingIndex createUserStmtMapping;
/** StatementExpressionIndex for create-timestamp. **/
private StatementMappingIndex createTimestampStmtMapping;
/** StatementExpressionIndex for update-user. **/
private StatementMappingIndex updateUserStmtMapping;
/** StatementExpressionIndex for update-timestamp. **/
private StatementMappingIndex updateTimestampStmtMapping;
/** StatementExpressionIndex for external FKs */
private StatementMappingIndex[] externalFKStmtMappings;
/** StatementExpressionIndex for external FK discriminators (shared FKs) */
private StatementMappingIndex[] externalFKDiscrimStmtMappings;
/** StatementExpressionIndex for external indices */
private StatementMappingIndex[] externalOrderStmtMappings;
/** Whether to batch the INSERT SQL. */
private boolean batch = false;
/**
* Constructor, taking the table. Uses the structure of the datastore table to build a basic query.
* @param table The Class Table representing the datastore table to insert.
* @param cmd ClassMetaData for the object being persisted
* @param clr ClassLoader resolver
*/
public InsertRequest(DatastoreClass table, AbstractClassMetaData cmd, ClassLoaderResolver clr)
{
super(table);
InsertMappingConsumer consumer = new InsertMappingConsumer(clr, cmd, IDPARAMNUMBER);
table.provideSurrogateMapping(SurrogateColumnType.DATASTORE_ID, consumer);
table.provideNonPrimaryKeyMappings(consumer);
table.providePrimaryKeyMappings(consumer);
table.provideSurrogateMapping(SurrogateColumnType.VERSION, consumer);
table.provideSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, consumer);
table.provideSurrogateMapping(SurrogateColumnType.MULTITENANCY, consumer);
table.provideSurrogateMapping(SurrogateColumnType.SOFTDELETE, consumer);
table.provideSurrogateMapping(SurrogateColumnType.CREATE_USER, consumer);
table.provideSurrogateMapping(SurrogateColumnType.CREATE_TIMESTAMP, consumer);
table.provideSurrogateMapping(SurrogateColumnType.UPDATE_USER, consumer);
table.provideSurrogateMapping(SurrogateColumnType.UPDATE_TIMESTAMP, consumer);
table.provideExternalMappings(consumer, MappingType.EXTERNAL_FK);
table.provideExternalMappings(consumer, MappingType.EXTERNAL_FK_DISCRIMINATOR);
table.provideExternalMappings(consumer, MappingType.EXTERNAL_INDEX);
table.provideUnmappedColumns(consumer);
callbacks = (MappingCallbacks[])consumer.getMappingCallbacks().toArray(new MappingCallbacks[consumer.getMappingCallbacks().size()]);
stmtMappings = consumer.getStatementMappings();
versionStmtMapping = consumer.getVersionStatementMapping();
discriminatorStmtMapping = consumer.getDiscriminatorStatementMapping();
multitenancyStmtMapping = consumer.getMultitenancyStatementMapping();
softDeleteStmtMapping = consumer.getSoftDeleteStatementMapping();
createUserStmtMapping = consumer.getCreateUserStatementMapping();
createTimestampStmtMapping = consumer.getCreateTimestampStatementMapping();
updateUserStmtMapping = consumer.getUpdateUserStatementMapping();
updateTimestampStmtMapping = consumer.getUpdateTimestampStatementMapping();
externalFKStmtMappings = consumer.getExternalFKStatementMapping();
externalFKDiscrimStmtMappings = consumer.getExternalFKDiscrimStatementMapping();
externalOrderStmtMappings = consumer.getExternalOrderStatementMapping();
pkFieldNumbers = consumer.getPrimaryKeyFieldNumbers();
if (table.getIdentityType() == IdentityType.APPLICATION && pkFieldNumbers.length < 1 && !hasIdentityColumn)
{
throw new NucleusException(Localiser.msg("052200", cmd.getFullClassName())).setFatal();
}
insertFieldNumbers = consumer.getInsertFieldNumbers();
retrievedStmtMappings = consumer.getReachableStatementMappings();
reachableFieldNumbers = consumer.getReachableFieldNumbers();
relationFieldNumbers = consumer.getRelationFieldNumbers();
insertStmt = consumer.getInsertStmt();
// TODO Need to also check on whether there is inheritance with multiple tables
if (!hasIdentityColumn && !cmd.hasRelations(clr) && externalFKStmtMappings == null)
{
// No identity, no persistence-by-reachability and no external FKs so should be safe to batch this
batch = true;
}
}
/**
* Method performing the insertion of the record from the datastore.
* Takes the constructed insert query and populates with the specific record information.
* @param op The ObjectProvider for the record to be inserted
*/
public void execute(ObjectProvider op)
{
ExecutionContext ec = op.getExecutionContext();
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
// Debug information about what we are inserting
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052207", op.getObjectAsPrintable(), table));
}
try
{
VersionMetaData vermd = table.getVersionMetaData();
RDBMSStoreManager storeMgr = table.getStoreManager();
if (vermd != null && vermd.getFieldName() != null)
{
// Version field - Update the version in the object
AbstractMemberMetaData verfmd = ((AbstractClassMetaData)vermd.getParent()).getMetaDataForMember(vermd.getFieldName());
Object currentVersion = op.getVersion();
if (currentVersion instanceof Number)
{
// Cater for Integer based versions
currentVersion = Long.valueOf(((Number)currentVersion).longValue());
}
Object nextOptimisticVersion = ec.getLockManager().getNextVersion(vermd, currentVersion);
if (verfmd.getType() == Integer.class || verfmd.getType() == int.class)
{
// Cater for Integer based versions
nextOptimisticVersion = Integer.valueOf(((Number)nextOptimisticVersion).intValue());
}
op.replaceField(verfmd.getAbsoluteFieldNumber(), nextOptimisticVersion);
}
// Set the state to "inserting" (may already be at this state if multiple inheritance level INSERT)
op.changeActivityState(ActivityState.INSERTING);
SQLController sqlControl = storeMgr.getSQLController();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try
{
List pkColumnNames = new ArrayList<>();
if (table.getIdentityType() == IdentityType.DATASTORE)
{
JavaTypeMapping mapping = table.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, true);
ColumnMapping[] columnMappings = mapping.getColumnMappings();
pkColumnNames = Stream.of(columnMappings)
.map(cm -> cm.getColumn().getIdentifier().getName())
.collect(toList());
}
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, insertStmt, batch,
hasIdentityColumn && storeMgr.getDatastoreAdapter().supportsOption(DatastoreAdapter.GET_GENERATED_KEYS_STATEMENT),
pkColumnNames);
try
{
StatementClassMapping mappingDefinition = new StatementClassMapping();
StatementMappingIndex[] idxs = stmtMappings;
for (int i=0;i 0)
{
int numberOfFieldsToProvide = 0;
for (int i = 0; i < insertFieldNumbers.length; i++)
{
if (insertFieldNumbers[i] < op.getClassMetaData().getMemberCount())
{
AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(insertFieldNumbers[i]);
if (mmd.isCreateTimestamp())
{
// Set create timestamp to time for the start of this transaction
if (mmd.getType().isAssignableFrom(java.time.Instant.class))
{
op.replaceField(insertFieldNumbers[i], ec.getTransaction().getIsActive() ?
java.time.Instant.ofEpochMilli(ec.getTransaction().getBeginTime()) : java.time.Instant.now());
}
else
{
op.replaceField(insertFieldNumbers[i], ec.getTransaction().getIsActive() ?
new Timestamp(ec.getTransaction().getBeginTime()) : new Timestamp(System.currentTimeMillis()));
}
// TODO Throw exception if invalid member type
}
else if (mmd.isCreateUser())
{
// Set create user to current user
op.replaceField(insertFieldNumbers[i], ec.getNucleusContext().getCurrentUser(ec));
}
numberOfFieldsToProvide++;
}
}
int j = 0;
int[] fieldNums = new int[numberOfFieldsToProvide];
for (int i = 0; i < insertFieldNumbers.length; i++)
{
if (insertFieldNumbers[i] < op.getClassMetaData().getMemberCount())
{
fieldNums[j++] = insertFieldNumbers[i];
}
}
op.provideFields(fieldNums, new ParameterSetter(op, ps, mappingDefinition));
}
JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, false);
if (versionMapping != null)
{
// Surrogate version - set the new version for the object
Object currentVersion = op.getVersion();
Object nextOptimisticVersion = ec.getLockManager().getNextVersion(vermd, currentVersion);
for (int k=0;k 0)
{
int numberOfReachableFields = 0;
for (int i = 0; i < reachableFieldNumbers.length; i++)
{
if (reachableFieldNumbers[i] < op.getClassMetaData().getMemberCount())
{
numberOfReachableFields++;
}
}
int[] fieldNums = new int[numberOfReachableFields];
int j = 0;
for (int i = 0; i < reachableFieldNumbers.length; i++)
{
if (reachableFieldNumbers[i] < op.getClassMetaData().getMemberCount())
{
fieldNums[j++] = reachableFieldNumbers[i];
}
}
mappingDefinition = new StatementClassMapping();
idxs = retrievedStmtMappings;
for (int i=0;i exceptions = new ArrayList<>();
exceptions.add(e);
while ((e = e.getNextException()) != null)
{
exceptions.add(e);
}
throw new NucleusDataStoreException(msg, exceptions.toArray(new Throwable[exceptions.size()]));
}
// Execute any mapping actions now that we have inserted the element
// (things like inserting any association parent-child).
for (int i = 0; i < callbacks.length; ++i)
{
try
{
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052209", op.getObjectAsPrintable(), ((JavaTypeMapping)callbacks[i]).getMemberMetaData().getFullFieldName()));
}
callbacks[i].postInsert(op);
}
catch (NotYetFlushedException e)
{
op.updateFieldAfterInsert(e.getPersistable(), ((JavaTypeMapping) callbacks[i]).getMemberMetaData().getAbsoluteFieldNumber());
}
}
}
/**
* Method to obtain the identity attributed by the datastore when using auto-increment/IDENTITY/SERIAL.
* @param ec execution context
* @param sqlControl SQLController
* @param op ObjectProvider of the object
* @param mconn The Connection
* @param ps PreparedStatement for the INSERT
* @return The identity
* @throws SQLException Thrown if an error occurs retrieving the identity
*/
private Object getInsertedDatastoreIdentity(ExecutionContext ec, SQLController sqlControl, ObjectProvider op, ManagedConnection mconn, PreparedStatement ps)
throws SQLException
{
Object datastoreId = null;
RDBMSStoreManager storeMgr = table.getStoreManager();
if (storeMgr.getDatastoreAdapter().supportsOption(DatastoreAdapter.GET_GENERATED_KEYS_STATEMENT))
{
// Try getGeneratedKeys() method to avoid extra SQL calls (only in more recent JDBC drivers)
ResultSet rs = null;
try
{
rs = ps.getGeneratedKeys();
if (rs != null && rs.next())
{
datastoreId = rs.getObject(1);
}
}
catch (Throwable e)
{
// Not supported maybe (e.g HSQL), or the driver is too old
}
finally
{
if (rs != null)
{
rs.close();
}
}
}
if (datastoreId == null)
{
// Not found, so try the native method for retrieving it
String columnName = null;
JavaTypeMapping idMapping = table.getIdMapping();
if (idMapping != null)
{
for (int i=0;i 0)
{
for (int i=0;i 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(columnId);
columnValues.append(m.getColumnMapping(j).getInsertionInputParameter());
}
if (m.getColumnMapping(j).insertValuesOnInsert())
{
// only add fields to be replaced by the real values only if the param value has ?
Integer abs_field_num = Integer.valueOf(mmd.getAbsoluteFieldNumber());
if (mmd.isPrimaryKey())
{
if (!pkFields.contains(abs_field_num))
{
pkFields.add(abs_field_num);
}
}
else if (!insertFields.contains(abs_field_num))
{
insertFields.add(abs_field_num);
}
if (columnExists)
{
parametersIndex[j] = ((Integer)assignedColumns.get(c.getIdentifier().toString())).intValue();
}
else
{
parametersIndex[j] = paramIndex++;
}
}
if (!columnExists)
{
assignedColumns.put(c.getIdentifier().toString(), Integer.valueOf(mmd.getAbsoluteFieldNumber()));
}
}
else
{
hasIdentityColumn = true;
}
}
statementMappings[mmd.getAbsoluteFieldNumber()].addParameterOccurrence(parametersIndex);
}
}
if (m instanceof MappingCallbacks)
{
mc.add(m);
}
}
/**
* Consumes a mapping not associated to a field.
* @param m The mapping.
* @param mappingType the Mapping type
*/
public void consumeMapping(JavaTypeMapping m, MappingType mappingType)
{
if (mappingType == MappingType.VERSION)
{
// Surrogate version column
JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, false);
if (versionMapping != null)
{
String val = versionMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(versionMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
versionStatementMapping = new StatementMappingIndex(versionMapping);
int[] param = { paramIndex++ };
versionStatementMapping.addParameterOccurrence(param);
}
else
{
versionStatementMapping = null;
}
}
else if (mappingType == MappingType.DISCRIMINATOR)
{
// Surrogate discriminator column
JavaTypeMapping discrimMapping = table.getSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, false);
if (discrimMapping != null)
{
String val = discrimMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(discrimMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
discriminatorStatementMapping = new StatementMappingIndex(discrimMapping);
int[] param = { paramIndex++ };
discriminatorStatementMapping.addParameterOccurrence(param);
}
else
{
discriminatorStatementMapping = null;
}
}
else if (mappingType == MappingType.DATASTORE_ID)
{
// Surrogate datastore id column
if (table.getIdentityType() == IdentityType.DATASTORE)
{
if (!table.isObjectIdDatastoreAttributed() || !table.isBaseDatastoreClass())
{
Iterator iterator = key.getColumns().iterator();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(((Column) iterator.next()).getIdentifier().toString());
columnValues.append("?");
paramIndex++;
}
else
{
hasIdentityColumn = true;
}
}
}
else if (mappingType == MappingType.EXTERNAL_FK)
{
// External FK mapping (1-N uni)
externalFKStmtExprIndex = processExternalMapping(m, statementMappings, externalFKStmtExprIndex);
}
else if (mappingType == MappingType.EXTERNAL_FK_DISCRIMINATOR)
{
// External FK discriminator mapping (1-N uni with shared FK)
externalFKDiscrimStmtExprIndex = processExternalMapping(m, statementMappings, externalFKDiscrimStmtExprIndex);
}
else if (mappingType == MappingType.EXTERNAL_INDEX)
{
// External FK order mapping (1-N uni List)
externalOrderStmtExprIndex = processExternalMapping(m, statementMappings, externalOrderStmtExprIndex);
}
else if (mappingType == MappingType.MULTITENANCY)
{
// Multitenancy column
JavaTypeMapping multitenancyMapping = table.getSurrogateMapping(SurrogateColumnType.MULTITENANCY, false);
String val = multitenancyMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(multitenancyMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
multitenancyStatementMapping = new StatementMappingIndex(multitenancyMapping);
int[] param = { paramIndex++ };
multitenancyStatementMapping.addParameterOccurrence(param);
}
else if (mappingType == MappingType.SOFTDELETE)
{
// SoftDelete column
JavaTypeMapping softDeleteMapping = table.getSurrogateMapping(SurrogateColumnType.SOFTDELETE, false);
String val = softDeleteMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(softDeleteMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
softDeleteStatementMapping = new StatementMappingIndex(softDeleteMapping);
int[] param = { paramIndex++ };
softDeleteStatementMapping.addParameterOccurrence(param);
}
else if (mappingType == MappingType.CREATEUSER)
{
// CreateUser column
JavaTypeMapping createUserMapping = table.getSurrogateMapping(SurrogateColumnType.CREATE_USER, false);
String val = createUserMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(createUserMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
createUserStatementMapping = new StatementMappingIndex(createUserMapping);
int[] param = { paramIndex++ };
createUserStatementMapping.addParameterOccurrence(param);
}
else if (mappingType == MappingType.CREATETIMESTAMP)
{
// CreateTimestamp column
JavaTypeMapping createTimestampMapping = table.getSurrogateMapping(SurrogateColumnType.CREATE_TIMESTAMP, false);
String val = createTimestampMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(createTimestampMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
createTimestampStatementMapping = new StatementMappingIndex(createTimestampMapping);
int[] param = { paramIndex++ };
createTimestampStatementMapping.addParameterOccurrence(param);
}
else if (mappingType == MappingType.UPDATEUSER)
{
// UpdateUser column - we add it to the INSERT in case it is not nullable
JavaTypeMapping updateUserMapping = table.getSurrogateMapping(SurrogateColumnType.UPDATE_USER, false);
String val = updateUserMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(updateUserMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
updateUserStatementMapping = new StatementMappingIndex(updateUserMapping);
int[] param = { paramIndex++ };
updateUserStatementMapping.addParameterOccurrence(param);
}
else if (mappingType == MappingType.UPDATETIMESTAMP)
{
// UpdateTimestamp column - we add it to the INSERT in case it is not nullable
JavaTypeMapping updateTimestampMapping = table.getSurrogateMapping(SurrogateColumnType.UPDATE_TIMESTAMP, false);
String val = updateTimestampMapping.getColumnMapping(0).getUpdateInputParameter();
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(updateTimestampMapping.getColumnMapping(0).getColumn().getIdentifier());
columnValues.append(val);
updateTimestampStatementMapping = new StatementMappingIndex(updateTimestampMapping);
int[] param = { paramIndex++ };
updateTimestampStatementMapping.addParameterOccurrence(param);
}
}
/**
* Consumer a column without mapping.
* @param col The column
*/
public void consumeUnmappedColumn(Column col)
{
if (columnNames.length() > 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(col.getIdentifier());
ColumnMetaData colmd = col.getColumnMetaData();
String value = colmd.getInsertValue();
if (value != null && value.equalsIgnoreCase("#NULL"))
{
value = null;
}
if (MetaDataUtils.isJdbcTypeString(colmd.getJdbcType()))
{
// String-based so add quoting. Do all RDBMS accept single-quote?
if (value != null)
{
value = "'" + value + "'";
}
else if (!col.isNullable())
{
value = "''";
}
}
columnValues.append(value);
}
/**
* Convenience method to process an external mapping.
* @param mapping The external mapping
* @param fieldStmtExprIndex The indices for the fields
* @param stmtExprIndex The current external mapping indices
* @return The updated external mapping indices
*/
private StatementMappingIndex[] processExternalMapping(JavaTypeMapping mapping,
StatementMappingIndex[] fieldStmtExprIndex, StatementMappingIndex[] stmtExprIndex)
{
// Check that we dont already have this as a field
for (int i=0;i 0)
{
columnNames.append(',');
columnValues.append(',');
}
columnNames.append(mapping.getColumnMapping(i).getColumn().getIdentifier());
columnValues.append(mapping.getColumnMapping(i).getUpdateInputParameter());
param[i] = paramIndex++;
}
stmtExprIndex[pos].addParameterOccurrence(param);
return stmtExprIndex;
}
/**
* @return Returns the mappingCallbacks.
*/
public List getMappingCallbacks()
{
return mc;
}
/**
* Accessor for the numbers of the fields to be inserted (excluding PK fields).
* @return the array of field numbers
*/
public int[] getInsertFieldNumbers()
{
int[] fieldNumbers = new int[insertFields.size()];
for (int i = 0; i < insertFields.size(); ++i)
{
fieldNumbers[i] = ((Integer) insertFields.get(i)).intValue();
}
return fieldNumbers;
}
/**
* Accessor for the numbers of the PK fields.
* @return the array of primary key field numbers
*/
public int[] getPrimaryKeyFieldNumbers()
{
int[] fieldNumbers = new int[pkFields.size()];
for (int i = 0; i < pkFields.size(); i++)
{
fieldNumbers[i] = ((Integer) pkFields.get(i)).intValue();
}
return fieldNumbers;
}
/**
* Accessor for the numbers of the reachable fields (not inserted).
* @return the array of field numbers
*/
public int[] getReachableFieldNumbers()
{
int[] fieldNumbers = new int[reachableFields.size()];
for (int i = 0; i < reachableFields.size(); ++i)
{
fieldNumbers[i] = ((Integer) reachableFields.get(i)).intValue();
}
return fieldNumbers;
}
/**
* Accessor for the numbers of the relation fields (not inserted).
* @return the array of field numbers
*/
public int[] getRelationFieldNumbers()
{
int[] fieldNumbers = new int[relationFields.size()];
for (int i = 0; i < relationFields.size(); ++i)
{
fieldNumbers[i] = ((Integer) relationFields.get(i)).intValue();
}
return fieldNumbers;
}
/**
* Obtain the mappings for fields in the statement
* @return the array of StatementMappingIndex
*/
public StatementMappingIndex[] getStatementMappings()
{
return statementMappings;
}
/**
* Obtain the StatementExpressionIndex for the "reachable" fields.
* @return the array of StatementExpressionIndex indexed by absolute field numbers
*/
public StatementMappingIndex[] getReachableStatementMappings()
{
return retrievedStatementMappings;
}
/**
* Obtain the mapping for the version in the statement
* @return the StatementMappingIndex
*/
public StatementMappingIndex getVersionStatementMapping()
{
return versionStatementMapping;
}
/**
* Obtain the mapping for the discriminator in the statement
* @return the StatementMappingIndex
*/
public StatementMappingIndex getDiscriminatorStatementMapping()
{
return discriminatorStatementMapping;
}
/**
* Obtain the mapping for multitenancy in the statement
* @return the StatementMappingIndex
*/
public StatementMappingIndex getMultitenancyStatementMapping()
{
return multitenancyStatementMapping;
}
/**
* Obtain the mapping for soft-delete in the statement
* @return the StatementMappingIndex
*/
public StatementMappingIndex getSoftDeleteStatementMapping()
{
return softDeleteStatementMapping;
}
public StatementMappingIndex getCreateUserStatementMapping()
{
return createUserStatementMapping;
}
public StatementMappingIndex getCreateTimestampStatementMapping()
{
return createTimestampStatementMapping;
}
public StatementMappingIndex getUpdateUserStatementMapping()
{
return updateUserStatementMapping;
}
public StatementMappingIndex getUpdateTimestampStatementMapping()
{
return updateTimestampStatementMapping;
}
/**
* Obtain the mapping for any external FKs in the statement
* @return the StatementMappingIndex
*/
public StatementMappingIndex[] getExternalFKStatementMapping()
{
return externalFKStmtExprIndex;
}
/**
* Obtain the mapping for any external FK discriminators in the statement.
* @return the StatementMappingIndex
*/
public StatementMappingIndex[] getExternalFKDiscrimStatementMapping()
{
return externalFKDiscrimStmtExprIndex;
}
/**
* Obtain the mapping for any external indexes in the statement
* @return the StatementMappingIndex
*/
public StatementMappingIndex[] getExternalOrderStatementMapping()
{
return externalOrderStmtExprIndex;
}
/**
* Obtain the insert statement
* @return the SQL statement
*/
public String getInsertStmt()
{
// Construct the statement for the INSERT
if (columnNames.length() > 0 && columnValues.length() > 0)
{
return "INSERT INTO " + table.toString() + " (" + columnNames + ") VALUES (" + columnValues + ")";
}
// No columns in the INSERT statement
return table.getStoreManager().getDatastoreAdapter().getInsertStatementForNoColumns(table);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy