use of org.eclipse.persistence.internal.expressions.SQLSelectStatement in project eclipselink by eclipse-ee4j.
the class OneToOneMapping method buildSelectionQueryForDirectCollectionKeyMapping.
/**
* INTERNAL:
* Certain key mappings favor different types of selection query. Return the appropriate
* type of selectionQuery
*/
@Override
public ReadQuery buildSelectionQueryForDirectCollectionKeyMapping(ContainerPolicy containerPolicy) {
DataReadQuery query = new DataReadQuery();
query.setSQLStatement(new SQLSelectStatement());
query.setContainerPolicy(containerPolicy);
return query;
}
use of org.eclipse.persistence.internal.expressions.SQLSelectStatement in project eclipselink by eclipse-ee4j.
the class RelationTableMechanism method initializeLockRelationTableQuery.
/**
* INTERNAL:
* Initialize lockRelationTableQuery.
*/
protected void initializeLockRelationTableQuery(AbstractSession session, ForeignReferenceMapping mapping, Expression startCriteria) {
lockRelationTableQuery = new DirectReadQuery();
Expression criteria = buildSelectionCriteriaAndAddFieldsToQueryInternal(mapping, startCriteria, false, false);
SQLSelectStatement statement = new SQLSelectStatement();
statement.addTable(this.relationTable);
statement.addField(this.sourceRelationKeyFields.get(0).clone());
statement.setWhereClause(criteria);
statement.normalize(session, null);
lockRelationTableQuery.setSQLStatement(statement);
lockRelationTableQuery.setSessionName(session.getName());
}
use of org.eclipse.persistence.internal.expressions.SQLSelectStatement in project eclipselink by eclipse-ee4j.
the class RelationTableMechanism method getLockRelationTableQueryClone.
/**
* INTERNAL:
* Returns a query that
*/
ReadQuery getLockRelationTableQueryClone(AbstractSession session, short lockMode) {
DirectReadQuery lockRelationTableQueryClone = (DirectReadQuery) lockRelationTableQuery.clone();
SQLSelectStatement statement = new SQLSelectStatement();
statement.addTable(this.relationTable);
statement.addField(this.sourceRelationKeyFields.get(0).clone());
statement.setWhereClause((Expression) lockRelationTableQuery.getSelectionCriteria().clone());
statement.setLockingClause(new ForUpdateClause(lockMode));
statement.normalize(session, null);
lockRelationTableQueryClone.setSQLStatement(statement);
lockRelationTableQueryClone.setIsExecutionClone(true);
return lockRelationTableQueryClone;
}
use of org.eclipse.persistence.internal.expressions.SQLSelectStatement in project eclipselink by eclipse-ee4j.
the class ObjectBuilder method verifyDelete.
/**
* Verify that an object has been deleted from the database.
* An object can span multiple tables. A query is performed on each of
* these tables using the primary key values of the object as the selection
* criteria. If the query returns a result then the object has not been
* deleted from the database. If no result is returned then each of the
* mappings is asked to verify that the object has been deleted. If all mappings
* answer true then the result is true.
*/
public boolean verifyDelete(Object object, AbstractSession session) {
AbstractRecord translationRow = buildRowForTranslation(object, session);
// If a call is used generated SQL cannot be executed, the call must be used.
if ((this.descriptor.getQueryManager().getReadObjectQuery() != null) && this.descriptor.getQueryManager().getReadObjectQuery().isCallQuery()) {
Object result = session.readObject(object);
if (result != null) {
return false;
}
} else {
for (Enumeration<DatabaseTable> tables = this.descriptor.getTables().elements(); tables.hasMoreElements(); ) {
DatabaseTable table = tables.nextElement();
SQLSelectStatement sqlStatement = new SQLSelectStatement();
sqlStatement.addTable(table);
if (table == this.descriptor.getTables().firstElement()) {
sqlStatement.setWhereClause((Expression) getPrimaryKeyExpression().clone());
} else {
sqlStatement.setWhereClause(buildPrimaryKeyExpression(table));
}
DatabaseField all = new DatabaseField("*");
all.setTable(table);
sqlStatement.addField(all);
sqlStatement.normalize(session, null);
DataReadQuery dataReadQuery = new DataReadQuery();
dataReadQuery.setSQLStatement(sqlStatement);
dataReadQuery.setSessionName(this.descriptor.getSessionName());
// execute the query and check if there is a valid result
List queryResults = (List) session.executeQuery(dataReadQuery, translationRow);
if (!queryResults.isEmpty()) {
return false;
}
}
}
// now ask each of the mappings to verify that the object has been deleted.
for (Enumeration<DatabaseMapping> mappings = this.descriptor.getMappings().elements(); mappings.hasMoreElements(); ) {
DatabaseMapping mapping = mappings.nextElement();
if (!mapping.verifyDelete(object, session)) {
return false;
}
}
return true;
}
use of org.eclipse.persistence.internal.expressions.SQLSelectStatement in project eclipselink by eclipse-ee4j.
the class OracleChangeNotificationListener method initialize.
/**
* Initialize the descriptor to receive database change events.
* This is called when the descriptor is initialized.
*/
@Override
public void initialize(final ClassDescriptor descriptor, AbstractSession session) {
if (descriptor.getOptimisticLockingPolicy() == null) {
boolean requiresLocking = descriptor.hasMultipleTables();
for (DatabaseMapping mapping : descriptor.getMappings()) {
if (mapping.isCollectionMapping()) {
requiresLocking = true;
}
}
if (requiresLocking) {
session.log(SessionLog.WARNING, SessionLog.METADATA, "locking_required_for_database_change_notification", descriptor.getJavaClass());
}
}
final DatabaseField rowId = descriptor.buildField(new DatabaseField(ROWID));
final List<DatabaseField> fields = new ArrayList<DatabaseField>();
fields.add(rowId);
// May already have the index if has inheritance.
CacheIndex existingIndex = descriptor.getCachePolicy().getCacheIndex(fields);
if (existingIndex == null) {
if (descriptor.isChildDescriptor()) {
existingIndex = descriptor.getInheritancePolicy().getRootParentDescriptor().getCachePolicy().getCacheIndex(fields);
}
if (existingIndex == null) {
existingIndex = new CacheIndex(fields);
existingIndex.setIsUpdateable(false);
existingIndex.setIsInsertable(false);
}
descriptor.getCachePolicy().addCacheIndex(existingIndex);
}
final CacheIndex index = existingIndex;
rowId.setInsertable(false);
rowId.setUpdatable(false);
rowId.setCreatable(false);
descriptor.getFields().add(rowId);
descriptor.getAllFields().add(rowId);
final ValueReadQuery rowIdQuery = new ValueReadQuery();
rowIdQuery.setName(ROWID);
SQLSelectStatement sqlStatement = new SQLSelectStatement();
sqlStatement.setWhereClause(descriptor.getObjectBuilder().getPrimaryKeyExpression());
sqlStatement.addField(rowId);
sqlStatement.addTable(descriptor.getTables().get(0));
rowIdQuery.setSQLStatement(sqlStatement);
sqlStatement.normalize(session, null);
descriptor.getEventManager().addListener(new DescriptorEventAdapter() {
@Override
public void postMerge(DescriptorEvent event) {
if ((event.getChangeSet() != null) && event.getChangeSet().hasChanges()) {
Object id = event.getChangeSet().getId();
CacheKey cacheKey = event.getChangeSet().getActiveCacheKey();
if (cacheKey == null) {
cacheKey = event.getSession().getParent().getIdentityMapAccessorInstance().getIdentityMapManager().getCacheKeyForObject(id, descriptor.getJavaClass(), descriptor, false);
}
cacheKey.setTransactionId(event.getSession().getProperty(ORA_TRANSACTION_ID));
if (event.getChangeSet().isNew()) {
AbstractRecord row = descriptor.getObjectBuilder().buildRowFromPrimaryKeyValues(id, event.getSession());
Object rowid = event.getSession().executeQuery(rowIdQuery, row);
CacheId indexValue = new CacheId(new Object[] { rowid });
event.getSession().getParent().getIdentityMapAccessorInstance().getIdentityMapManager().putCacheKeyByIndex(index, indexValue, cacheKey, descriptor);
}
}
}
@Override
public void postUpdate(DescriptorEvent event) {
Object txId = event.getSession().getProperty(ORA_TRANSACTION_ID);
if (txId == null) {
txId = event.getSession().executeQuery(transactionIdQuery);
event.getSession().setProperty(ORA_TRANSACTION_ID, txId);
}
}
});
}
Aggregations