Search in sources :

Example 31 with SQLSelectStatement

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;
}
Also used : SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Example 32 with SQLSelectStatement

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());
}
Also used : FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) DirectReadQuery(org.eclipse.persistence.queries.DirectReadQuery)

Example 33 with SQLSelectStatement

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;
}
Also used : SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) ForUpdateClause(org.eclipse.persistence.internal.expressions.ForUpdateClause) DirectReadQuery(org.eclipse.persistence.queries.DirectReadQuery)

Example 34 with SQLSelectStatement

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;
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) DataReadQuery(org.eclipse.persistence.queries.DataReadQuery) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) ThreadCursoredList(org.eclipse.persistence.internal.helper.ThreadCursoredList) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 35 with SQLSelectStatement

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);
            }
        }
    });
}
Also used : CacheIndex(org.eclipse.persistence.descriptors.CacheIndex) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DescriptorEventAdapter(org.eclipse.persistence.descriptors.DescriptorEventAdapter) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey)

Aggregations

SQLSelectStatement (org.eclipse.persistence.internal.expressions.SQLSelectStatement)35 Expression (org.eclipse.persistence.expressions.Expression)13 IdentityHashMap (java.util.IdentityHashMap)10 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)10 HashMap (java.util.HashMap)9 Map (java.util.Map)8 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)8 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)8 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)8 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)7 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)7 DataExpression (org.eclipse.persistence.internal.expressions.DataExpression)6 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)6 SQLCall (org.eclipse.persistence.queries.SQLCall)6 Iterator (java.util.Iterator)5 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)5 DirectReadQuery (org.eclipse.persistence.queries.DirectReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 ArrayList (java.util.ArrayList)4