use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class JoinArrayStore method iterator.
/**
* Method to return an iterator to the array.
* @param ownerOP ObjectProvider for the owner of the array
*/
public Iterator<E> iterator(ObjectProvider ownerOP) {
ExecutionContext ec = ownerOP.getExecutionContext();
// Generate the statement, and statement mapping/parameter information
IteratorStatement iterStmt = getIteratorStatement(ec, ec.getFetchPlan(), true);
SelectStatement sqlStmt = iterStmt.sqlStmt;
StatementClassMapping iteratorMappingClass = iterStmt.stmtClassMapping;
// 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 < paramPositions.length; k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < paramPositions.length; k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
StatementParameterMapping iteratorMappingParams = new StatementParameterMapping();
iteratorMappingParams.addMappingForParameter("owner", ownerIdx);
if (ec.getTransaction().getSerializeRead() != null && ec.getTransaction().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 {
if (elementsAreEmbedded || elementsAreSerialised) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new ArrayStoreIterator(ownerOP, rs, null, this);
} else if (elementMapping instanceof ReferenceMapping) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new ArrayStoreIterator(ownerOP, rs, null, this);
} else {
ResultObjectFactory rof = new PersistentClassROF(ec, rs, false, iteratorMappingClass, elementCmd, clr.classForName(elementType));
return new ArrayStoreIterator(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.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class JoinMapStore method getNextIDForAdapterColumn.
/**
* Accessor for the higher id when elements primary key can't be part of
* the primary key by datastore limitations like BLOB types can't be primary keys.
* @param op ObjectProvider for container
* @return The next id
*/
private int getNextIDForAdapterColumn(ObjectProvider op) {
int nextID;
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
String stmt = getMaxAdapterColumnIdStmt();
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
try {
int jdbcPosition = 1;
BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
if (!rs.next()) {
nextID = 1;
} else {
nextID = rs.getInt(1) + 1;
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056020", getMaxAdapterColumnIdStmt()), e);
}
return nextID;
}
use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class JoinMapStore method put.
/**
* Method to put an item in the Map.
* @param op ObjectProvider for the map.
* @param key The key to store the value against
* @param value The value to store.
* @return The value stored.
*/
public V put(ObjectProvider op, K key, V value) {
validateKeyForWriting(op, key);
validateValueForWriting(op, value);
boolean exists = false;
V oldValue;
try {
oldValue = getValue(op, key);
exists = true;
} catch (NoSuchElementException e) {
oldValue = null;
exists = false;
}
if (oldValue != value) {
// Value changed so update the map
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
if (exists) {
internalUpdate(op, mconn, false, key, value, true);
} else {
internalPut(op, mconn, false, key, value, true);
}
} finally {
mconn.release();
}
} catch (MappedDatastoreException e) {
throw new NucleusDataStoreException(Localiser.msg("056016", e.getMessage()), e);
}
}
MapMetaData mapmd = ownerMemberMetaData.getMap();
if (mapmd.isDependentValue() && !mapmd.isEmbeddedValue() && oldValue != null) {
// Delete the old value if it is no longer contained and is dependent
if (!containsValue(op, oldValue)) {
op.getExecutionContext().deleteObjectInternal(oldValue);
}
}
return oldValue;
}
use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class JoinPersistableRelationStore method remove.
/* (non-Javadoc)
* @see org.datanucleus.store.scostore.PersistableRelationStore#remove(org.datanucleus.store.ObjectProvider)
*/
public boolean remove(ObjectProvider op) {
String removeStmt = getRemoveStmt();
ExecutionContext ec = op.getExecutionContext();
SQLController sqlControl = storeMgr.getSQLController();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, false);
try {
// Update the join table row
int jdbcPosition = 1;
populateOwnerInStatement(op, ec, ps, jdbcPosition, joinTable);
// Execute the statement
int[] nums = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, true);
return (nums != null && nums.length == 1 && nums[0] == 1);
} finally {
sqlControl.closeStatement(mconn, ps);
mconn.release();
}
} catch (SQLException sqle) {
throw new NucleusDataStoreException("Exception thrown deleting row from persistable relation join table", sqle);
}
}
use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class JoinSetStore method removeAllInternal.
protected boolean removeAllInternal(ObjectProvider op, Collection elements, int size) {
boolean modified = false;
String removeAllStmt = getRemoveAllStmt(op, elements);
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeAllStmt, false);
try {
int jdbcPosition = 1;
Iterator iter = elements.iterator();
while (iter.hasNext()) {
Object element = iter.next();
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elementMapping);
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
}
int[] number = sqlControl.executeStatementUpdate(ec, mconn, removeAllStmt, ps, true);
if (number[0] > 0) {
modified = true;
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
NucleusLogger.DATASTORE.error("Exception on removeAll", e);
throw new NucleusDataStoreException(Localiser.msg("056012", removeAllStmt), e);
}
return modified;
}
Aggregations