use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class MapValueCollectionStore method iterator.
/**
* Accessor for an iterator for the set.
* @param ownerOP ObjectProvider for the set.
* @return Iterator for the set.
*/
public Iterator<V> iterator(ObjectProvider ownerOP) {
ExecutionContext ec = ownerOP.getExecutionContext();
if (iteratorStmtLocked == null) {
synchronized (// Make sure this completes in case another thread needs the same info
this) {
// Generate the statement, and statement mapping/parameter information
SQLStatement sqlStmt = getSQLStatementForIterator(ownerOP);
iteratorStmtUnlocked = sqlStmt.getSQLText().toSQL();
sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
iteratorStmtLocked = sqlStmt.getSQLText().toSQL();
}
}
Transaction tx = ec.getTransaction();
String stmt = (tx.getSerializeRead() != null && tx.getSerializeRead() ? iteratorStmtLocked : iteratorStmtUnlocked);
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
// Create the statement and set the owner
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
StatementMappingIndex ownerIdx = iteratorMappingParams.getMappingForParameter("owner");
int numParams = ownerIdx.getNumberOfParameterOccurrences();
for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerOP.getObject());
}
try {
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
ResultObjectFactory rof = null;
if (elementsAreEmbedded || elementsAreSerialised) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new CollectionStoreIterator(ownerOP, rs, null, this);
}
rof = new PersistentClassROF(ec, rs, false, iteratorMappingDef, elementCmd, clr.classForName(elementType));
return new CollectionStoreIterator(ownerOP, rs, rof, this);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
} catch (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 MapValueCollectionStore method remove.
protected boolean remove(ObjectProvider op, Object value) {
if (findKeyStmt == null) {
throw new UnsupportedOperationException("Cannot remove from a map through its values collection");
}
Object key = null;
boolean keyExists = false;
ExecutionContext ec = op.getExecutionContext();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, findKeyStmt);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
BackingStoreHelper.populateElementInStatement(ec, ps, value, jdbcPosition, elementMapping);
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, findKeyStmt, ps);
try {
if (rs.next()) {
key = keyMapping.getObject(ec, rs, MappingHelper.getMappingIndices(1, keyMapping));
keyExists = true;
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException("Request failed to check if set contains an element: " + findKeyStmt, e);
}
if (keyExists) {
mapStore.remove(op, key);
return true;
}
return false;
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class AbstractCollectionStore method contains.
/**
* Method to verify if the specified element is contained in this collection.
* @param op ObjectProvider
* @param element The element
* @return Whether it contains the element
*/
public boolean contains(ObjectProvider op, Object element) {
if (!validateElementForReading(op, element)) {
return false;
}
boolean retval;
String stmt = getContainsStmt(element);
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elementMapping);
// TODO Remove the containerTable == part of this so that the discrim restriction applies to JoinTable case too
// Needs to pass TCK M-N relation test
boolean usingJoinTable = usingJoinTable();
ComponentInfo elemInfo = getComponentInfoForElement(element);
if (!usingJoinTable && elemInfo != null && elemInfo.getDiscriminatorMapping() != null) {
jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elemInfo, clr);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
retval = rs.next();
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056008", stmt), e);
}
return retval;
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method internalRemoveAt.
/**
* Internal method to remove an object at a location in the List.
* @param op ObjectProvider
* @param index The location
* @param stmt The statement to remove the element from the List
* @param size Current list size (if known). -1 if not known
*/
protected void internalRemoveAt(ObjectProvider op, int index, String stmt, int size) {
int currentListSize = 0;
if (size < 0) {
// Get the current size from the datastore
currentListSize = size(op);
} else {
currentListSize = size;
}
ExecutionContext ec = op.getExecutionContext();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
int[] rowsDeleted = sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
if (rowsDeleted[0] == 0) {
// ?? throw exception??
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
// shift down
if (index != currentListSize - 1) {
for (int i = index + 1; i < currentListSize; i++) {
// Shift this index down 1
internalShift(op, mconn, false, i, -1, true);
}
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056012", stmt), e);
} catch (MappedDatastoreException e) {
throw new NucleusDataStoreException(Localiser.msg("056012", stmt), e);
}
}
use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method internalIndexOf.
/**
* Internal method to find the index of an element.
* @param op ObjectProvider
* @param element The element
* @param stmt The statement to find the element.
* @return The index of the element in the List.
*/
protected int internalIndexOf(ObjectProvider op, Object element, String stmt) {
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
try {
int jdbcPosition = 1;
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);
}
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
boolean found = rs.next();
if (!found) {
JDBCUtils.logWarnings(rs);
return -1;
}
int index = rs.getInt(1);
JDBCUtils.logWarnings(rs);
return index;
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056017", stmt), e);
}
}
Aggregations