use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class FKListStore method updateElementFk.
/**
* Utility to update a foreign-key in the element in the case of a unidirectional 1-N relationship.
* @param ownerOP ObjectProvider for the owner
* @param element The element to update
* @param owner The owner object to set in the FK
* @param index The index position (or -1 if not known)
* @return Whether it was performed successfully
*/
private boolean updateElementFk(ObjectProvider ownerOP, Object element, Object owner, int index) {
if (element == null) {
return false;
}
ExecutionContext ec = ownerOP.getExecutionContext();
String updateFkStmt = getUpdateFkStmt(element);
boolean retval;
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, updateFkStmt, false);
try {
ComponentInfo elemInfo = getComponentInfoForElement(element);
JavaTypeMapping ownerMapping = elemInfo.getOwnerMapping();
JavaTypeMapping elemMapping = elemInfo.getDatastoreClass().getIdMapping();
JavaTypeMapping orderMapping = elemInfo.getDatastoreClass().getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_INDEX);
int jdbcPosition = 1;
if (owner == null) {
if (ownerMemberMetaData != null) {
ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, ownerMapping), null, ownerOP, ownerMemberMetaData.getAbsoluteFieldNumber());
} else {
ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, ownerMapping), null);
}
jdbcPosition += ownerMapping.getNumberOfDatastoreMappings();
} else {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
}
if (orderMapping != null) {
jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elemMapping);
sqlControl.executeStatementUpdate(ec, mconn, updateFkStmt, ps, true);
retval = true;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056027", updateFkStmt), e);
}
return retval;
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class FKListStore method listIterator.
/**
* Accessor for an iterator through the list elements.
* @param ownerOP ObjectProvider for the owner.
* @param startIdx The start index in the list (only for indexed lists)
* @param endIdx The end index in the list (only for indexed lists)
* @return The List Iterator
*/
protected ListIterator<E> listIterator(ObjectProvider ownerOP, int startIdx, int endIdx) {
ExecutionContext ec = ownerOP.getExecutionContext();
Transaction tx = ec.getTransaction();
if (elementInfo == null || elementInfo.length == 0) {
return null;
}
// Generate the statement. Note that this is not cached since depends on the current FetchPlan and other things
IteratorStatement iterStmt = getIteratorStatement(ownerOP.getExecutionContext(), ec.getFetchPlan(), true, startIdx, endIdx);
SelectStatement sqlStmt = iterStmt.getSelectStatement();
StatementClassMapping resultMapping = iterStmt.getStatementClassMapping();
// Input parameter(s) - the owner
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
if (sqlStmt.getNumberOfUnions() > 0) {
// Add parameter occurrence for each union of statement
for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
if (tx.getSerializeRead() != null && tx.getSerializeRead()) {
sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
}
String stmt = sqlStmt.getSQLText().toSQL();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
// Create the statement
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
// Set the owner
ObjectProvider stmtOwnerOP = BackingStoreHelper.getOwnerObjectProviderForBackingStore(ownerOP);
int numParams = ownerIdx.getNumberOfParameterOccurrences();
for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), stmtOwnerOP.getObject());
}
try {
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
ResultObjectFactory rof = null;
if (elementsAreEmbedded || elementsAreSerialised) {
throw new NucleusException("Cannot have FK set with non-persistent objects");
}
rof = new PersistentClassROF(ec, rs, false, resultMapping, elementCmd, clr.classForName(elementType));
return new ListStoreIterator(ownerOP, rs, rof, this);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException | MappedDatastoreException e) {
throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
}
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class FKListStore method set.
/**
* Method to set an object in the List at a position.
* @param ownerOP ObjectProvider for the owner
* @param index The item index
* @param element What to set it to.
* @param allowDependentField Whether to enable dependent-field deletes during the set
* @return The value before setting.
*/
public E set(ObjectProvider ownerOP, int index, Object element, boolean allowDependentField) {
// Last argument means don't set the position on any INSERT
validateElementForWriting(ownerOP, element, -1);
// Find the original element at this position
E oldElement = null;
List fieldVal = (List) ownerOP.provideField(ownerMemberMetaData.getAbsoluteFieldNumber());
if (fieldVal != null && fieldVal instanceof BackedSCO && ((BackedSCO) fieldVal).isLoaded()) {
// Already loaded in the wrapper
oldElement = (E) fieldVal.get(index);
} else {
oldElement = get(ownerOP, index);
}
ManagedConnection mconn = null;
try {
ExecutionContext ec = ownerOP.getExecutionContext();
SQLController sqlControl = storeMgr.getSQLController();
mconn = storeMgr.getConnectionManager().getConnection(ec);
// Unset the existing object from this position
String theUnsetStmt = getUnsetStmt();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, theUnsetStmt, false);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
if (orderMapping != null) {
jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
sqlControl.executeStatementUpdate(ec, mconn, theUnsetStmt, ps, true);
} finally {
sqlControl.closeStatement(mconn, ps);
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056015", theUnsetStmt), e);
} finally {
}
// Set the new object at this position
String theSetStmt = getSetStmt(element);
try {
PreparedStatement ps2 = sqlControl.getStatementForUpdate(mconn, theSetStmt, false);
try {
ComponentInfo elemInfo = getComponentInfoForElement(element);
JavaTypeMapping elemMapping = this.elementMapping;
JavaTypeMapping orderMapping = this.orderMapping;
if (elemInfo != null) {
elemMapping = elemInfo.getDatastoreClass().getIdMapping();
orderMapping = elemInfo.getDatastoreClass().getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_INDEX);
}
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps2, jdbcPosition, this);
if (orderMapping != null) {
jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps2, index, jdbcPosition, orderMapping);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps2, jdbcPosition, this);
}
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps2, element, jdbcPosition, elemMapping);
sqlControl.executeStatementUpdate(ec, mconn, theSetStmt, ps2, true);
} finally {
sqlControl.closeStatement(mconn, ps2);
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056015", theSetStmt), e);
}
} finally {
if (mconn != null) {
mconn.release();
}
}
// Dependent field
boolean dependent = getOwnerMemberMetaData().getCollection().isDependentElement();
if (getOwnerMemberMetaData().isCascadeRemoveOrphans()) {
dependent = true;
}
if (dependent && allowDependentField) {
if (oldElement != null) {
// Delete the element if it is dependent and doesnt have a duplicate entry in the list
ownerOP.getExecutionContext().deleteObjectInternal(oldElement);
}
}
return oldElement;
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class FKMapStore method updateValueFkInternal.
protected boolean updateValueFkInternal(ObjectProvider op, Object value, Object owner) {
boolean retval;
ExecutionContext ec = op.getExecutionContext();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, updateFkStmt, false);
try {
int jdbcPosition = 1;
if (owner == null) {
if (ownerMemberMetaData != null) {
ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(1, ownerMapping), null, op, ownerMemberMetaData.getAbsoluteFieldNumber());
} else {
ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(1, ownerMapping), null);
}
jdbcPosition += ownerMapping.getNumberOfDatastoreMappings();
} else {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
}
jdbcPosition = BackingStoreHelper.populateValueInStatement(ec, ps, value, jdbcPosition, valueMapping);
sqlControl.executeStatementUpdate(ec, mconn, updateFkStmt, ps, true);
retval = true;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056027", updateFkStmt), e);
}
return retval;
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class FKMapStore method updateKeyFkInternal.
protected boolean updateKeyFkInternal(ObjectProvider op, Object key, Object owner) {
boolean retval;
ExecutionContext ec = op.getExecutionContext();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, updateFkStmt, false);
try {
int jdbcPosition = 1;
if (owner == null) {
if (ownerMemberMetaData != null) {
ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(1, ownerMapping), null, op, ownerMemberMetaData.getAbsoluteFieldNumber());
} else {
ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(1, ownerMapping), null);
}
jdbcPosition += ownerMapping.getNumberOfDatastoreMappings();
} else {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
}
jdbcPosition = BackingStoreHelper.populateKeyInStatement(ec, ps, key, jdbcPosition, keyMapping);
sqlControl.executeStatementUpdate(ec, mconn, updateFkStmt, ps, true);
retval = true;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056027", updateFkStmt), e);
}
return retval;
}
Aggregations