use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractListStore method getIndicesOf.
/**
* Utility to find the indices of a collection of elements.
* The returned list are in reverse order (highest index first).
* @param op ObjectProvider
* @param elements The elements
* @return The indices of the elements in the List.
*/
protected int[] getIndicesOf(ObjectProvider op, Collection elements) {
if (elements == null || elements.isEmpty()) {
return null;
}
Iterator iter = elements.iterator();
while (iter.hasNext()) {
validateElementForReading(op, iter.next());
}
String stmt = getIndicesOfStmt(elements);
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
try {
Iterator elemIter = elements.iterator();
int jdbcPosition = 1;
while (elemIter.hasNext()) {
Object element = elemIter.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);
}
}
List<Integer> indexes = new ArrayList();
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
while (rs.next()) {
indexes.add(rs.getInt(1));
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
if (indexes.isEmpty()) {
return null;
}
int i = 0;
int[] indicesReturn = new int[indexes.size()];
for (Integer idx : indexes) {
indicesReturn[i++] = idx;
}
return indicesReturn;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056017", stmt), e);
}
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractMapStore method containsValue.
/**
* Method to check if a value exists in the Map.
* @param op ObjectProvider for the map
* @param value The value to check for.
* @return Whether the value exists in the Map.
*/
public boolean containsValue(ObjectProvider op, Object value) {
if (value == null) {
// nulls not allowed
return false;
}
if (!validateValueForReading(op, value)) {
return false;
}
boolean exists = false;
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, containsValueStmt);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
BackingStoreHelper.populateValueInStatement(ec, ps, value, jdbcPosition, getValueMapping());
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, containsValueStmt, ps);
try {
if (rs.next()) {
exists = true;
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
NucleusLogger.DATASTORE_RETRIEVE.warn("Exception during backing store select", e);
throw new NucleusDataStoreException(Localiser.msg("056019", containsValueStmt), e);
}
return exists;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractSetStore method remove.
/**
* Removes the association to one element
* @param op ObjectProvider for the container
* @param element Element to remove
* @param size Current size
* @param allowDependentField Whether to allow any cascade deletes caused by this removal
* @return Whether it was successful
*/
public boolean remove(ObjectProvider op, Object element, int size, boolean allowDependentField) {
if (!validateElementForReading(op, element)) {
NucleusLogger.DATASTORE.debug("Attempt to remove element=" + StringUtils.toJVMIDString(element) + " but doesn't exist in this Set.");
return false;
}
Object elementToRemove = element;
ExecutionContext ec = op.getExecutionContext();
if (ec.getApiAdapter().isDetached(element)) {
// Element passed in is detached so find attached version (DON'T attach this object)
elementToRemove = ec.findObject(ec.getApiAdapter().getIdForObject(element), true, false, element.getClass().getName());
}
// Remove the element
boolean modified = false;
String removeStmt = getRemoveStmt(elementToRemove);
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, false);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, elementToRemove, jdbcPosition, elementMapping);
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
int[] rowsDeleted = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, true);
modified = (rowsDeleted[0] == 1);
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
String msg = Localiser.msg("056012", removeStmt);
NucleusLogger.DATASTORE.error(msg, e);
throw new NucleusDataStoreException(msg, e);
}
CollectionMetaData collmd = ownerMemberMetaData.getCollection();
boolean dependent = collmd.isDependentElement();
if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
dependent = true;
}
if (allowDependentField && dependent && !collmd.isEmbeddedElement()) {
// Delete the element if it is dependent
op.getExecutionContext().deleteObjectInternal(elementToRemove);
}
return modified;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class ElementContainerStore method getSize.
public int getSize(ObjectProvider ownerOP) {
int numRows;
String sizeStmt = getSizeStmt();
try {
ExecutionContext ec = ownerOP.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, sizeStmt);
try {
int jdbcPosition = 1;
if (elementInfo == null) {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
} else {
if (usingJoinTable()) {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
if (elementInfo[0].getDiscriminatorMapping() != null) {
jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elementInfo[0], clr);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
} else {
for (int i = 0; i < elementInfo.length; i++) {
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
if (elementInfo[i].getDiscriminatorMapping() != null) {
jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elementInfo[i], clr);
}
if (relationDiscriminatorMapping != null) {
jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
}
}
}
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, sizeStmt, ps);
try {
if (!rs.next()) {
throw new NucleusDataStoreException(Localiser.msg("056007", sizeStmt));
}
numRows = rs.getInt(1);
if (elementInfo != null && elementInfo.length > 1) {
while (rs.next()) {
numRows = numRows + rs.getInt(1);
}
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} catch (SQLException sqle) {
NucleusLogger.GENERAL.error("Exception in size", sqle);
throw sqle;
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056007", sizeStmt), e);
}
return numRows;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class RDBMSSchemaHandler method getRDBMSSchemasInfo.
/**
* Convenience method to read the schemas information for this datastore.
* @param conn Connection to the datastore
* @param schemaName Name of the schema to check for
* @param catalogName Name of the catalog to check for
* @return The RDBMSTypesInfo
*/
protected RDBMSSchemaInfo getRDBMSSchemasInfo(Connection conn, String schemaName, String catalogName) {
try {
if (conn == null) {
// No connection provided so nothing to return
return null;
}
DatabaseMetaData dmd = conn.getMetaData();
ResultSet rs = dmd.getSchemas();
try {
String inputSchema = getNameWithoutQuotes(schemaName);
String inputCatalog = getNameWithoutQuotes(catalogName);
while (rs.next()) {
String schema = rs.getString("TABLE_SCHEM");
String foundSchema = getNameWithoutQuotes(schema);
boolean schemaCorrect = false;
if (StringUtils.isWhitespace(inputSchema) && StringUtils.isWhitespace(foundSchema)) {
schemaCorrect = true;
} else {
if (inputSchema != null && inputSchema.equals(foundSchema)) {
schemaCorrect = true;
} else if (foundSchema != null && StringUtils.isWhitespace(inputSchema) && foundSchema.equals(((RDBMSStoreManager) storeMgr).getSchemaName())) {
schemaCorrect = true;
}
}
boolean catalogCorrect = false;
String catalog = inputCatalog;
try {
catalog = rs.getString("TABLE_CATALOG");
String foundCatalog = getNameWithoutQuotes(catalog);
if (StringUtils.isWhitespace(inputCatalog) && StringUtils.isWhitespace(foundCatalog)) {
catalogCorrect = true;
} else if (inputCatalog != null && inputCatalog.equals(foundCatalog)) {
catalogCorrect = true;
} else if (foundCatalog != null && StringUtils.isWhitespace(inputCatalog) && foundCatalog.equals(((RDBMSStoreManager) storeMgr).getCatalogName())) {
catalogCorrect = true;
}
} catch (SQLException sqle) {
// This datastore doesn't return catalog (e.g Oracle)
if (catalogName == null) {
catalogCorrect = true;
}
}
if (schemaCorrect && catalogCorrect) {
return new RDBMSSchemaInfo(catalog, schema);
}
}
} finally {
rs.close();
}
} catch (SQLException sqle) {
throw new NucleusDataStoreException("Exception thrown retrieving schema information from datastore", sqle);
}
return null;
}
Aggregations