Search in sources :

Example 26 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class DirectCollectionMapping method initializeUpdateAtIndexQuery.

protected void initializeUpdateAtIndexQuery(AbstractSession session) {
    if (!getUpdateAtIndexQuery().hasSessionName()) {
        getUpdateAtIndexQuery().setSessionName(session.getName());
    }
    if (getUpdateAtIndexQuery().getPartitioningPolicy() == null) {
        getUpdateAtIndexQuery().setPartitioningPolicy(getPartitioningPolicy());
    }
    if (hasCustomUpdateAtIndexQuery()) {
        return;
    }
    SQLUpdateStatement statement = new SQLUpdateStatement();
    ExpressionBuilder builder = new ExpressionBuilder();
    Expression expression = createWhereClauseForDeleteQuery(builder);
    expression = expression.and(builder.getField(this.listOrderField).equal(builder.getParameter(this.listOrderField)));
    statement.setWhereClause(expression);
    statement.setTable(getReferenceTable());
    AbstractRecord modifyRow = new DatabaseRecord();
    modifyRow.add(this.listOrderField, null);
    statement.setModifyRow(modifyRow);
    getUpdateAtIndexQuery().setSQLStatement(statement);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) Expression(org.eclipse.persistence.expressions.Expression) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 27 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class DirectCollectionMapping method initializeInsertQuery.

/**
 * Initialize insert query. This query is used to insert the collection of objects into the
 * reference table.
 */
protected void initializeInsertQuery(AbstractSession session) {
    if (!getInsertQuery().hasSessionName()) {
        getInsertQuery().setSessionName(session.getName());
    }
    if (getInsertQuery().getPartitioningPolicy() == null) {
        getInsertQuery().setPartitioningPolicy(getPartitioningPolicy());
    }
    if (hasCustomInsertQuery()) {
        return;
    }
    SQLInsertStatement statement = new SQLInsertStatement();
    statement.setTable(getReferenceTable());
    AbstractRecord directRow = new DatabaseRecord();
    for (Enumeration<DatabaseField> referenceEnum = getReferenceKeyFields().elements(); referenceEnum.hasMoreElements(); ) {
        directRow.put(referenceEnum.nextElement(), null);
    }
    directRow.put(getDirectField(), null);
    if (listOrderField != null) {
        directRow.put(listOrderField, null);
    }
    statement.setModifyRow(directRow);
    getInsertQuery().setSQLStatement(statement);
    getInsertQuery().setModifyRow(directRow);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement)

Example 28 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class AggregateCollectionMapping method getAggregateRow.

/**
 * INTERNAL:
 * return the aggregate Record with the primary keys from the source table and target table
 */
public AbstractRecord getAggregateRow(ObjectLevelModifyQuery query, Object object) {
    Vector referenceObjectKeys = getReferenceObjectKeys(query);
    AbstractRecord aggregateRow = new DatabaseRecord();
    Vector<DatabaseField> keys = getTargetForeignKeyFields();
    for (int keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
        aggregateRow.put(keys.elementAt(keyIndex), referenceObjectKeys.elementAt(keyIndex));
    }
    getReferenceDescriptor(object.getClass(), query.getSession()).getObjectBuilder().buildRow(aggregateRow, object, query.getSession(), WriteType.UNDEFINED);
    return aggregateRow;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector)

Example 29 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class AggregateCollectionMapping method compareListsAndWrite_NonUpdatableListOrderField.

/**
 * INTERNAL:
 * Old and new lists are compared and only the changes are written to the database.
 * Called only if listOrderField != null
 */
protected void compareListsAndWrite_NonUpdatableListOrderField(List previousList, List currentList, WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    boolean shouldRepairOrder = false;
    if (currentList instanceof IndirectList) {
        shouldRepairOrder = ((IndirectList) currentList).isListOrderBrokenInDb();
    }
    HashMap<Object, Object[]> previousAndCurrentByKey = new HashMap<>();
    int pkSize = getReferenceDescriptor().getPrimaryKeyFields().size();
    // First index the current objects by their primary key.
    for (int i = 0; i < currentList.size(); i++) {
        Object currentObject = currentList.get(i);
        try {
            CacheId primaryKey = (CacheId) getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(currentObject, query.getSession());
            primaryKey.add(i);
            Object[] previousAndCurrent = new Object[] { null, currentObject };
            previousAndCurrentByKey.put(primaryKey, previousAndCurrent);
        } catch (NullPointerException e) {
            // ideally the customer should check for these themselves.
            if (currentObject != null) {
                throw e;
            }
        }
    }
    if (shouldRepairOrder) {
        DeleteAllQuery deleteAllQuery = (DeleteAllQuery) this.deleteAllQuery;
        if (this.isCascadeOnDeleteSetOnDatabase) {
            deleteAllQuery = (DeleteAllQuery) deleteAllQuery.clone();
            deleteAllQuery.setIsInMemoryOnly(false);
        }
        deleteAllQuery.executeDeleteAll(query.getSession().getSessionForClass(getReferenceClass()), query.getTranslationRow(), new Vector(previousList));
    } else {
        // Next index the previous objects (read from db or from backup in uow)
        for (int i = 0; i < previousList.size(); i++) {
            Object previousObject = previousList.get(i);
            CacheId primaryKey = (CacheId) getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(previousObject, query.getSession());
            primaryKey.add(i);
            Object[] previousAndCurrent = previousAndCurrentByKey.get(primaryKey);
            if (previousAndCurrent == null) {
                // there's no current object - that means that previous object should be deleted
                DatabaseRecord extraData = new DatabaseRecord(1);
                extraData.put(this.listOrderField, i);
                objectRemovedDuringUpdate(query, previousObject, extraData);
            } else {
                previousAndCurrent[0] = previousObject;
            }
        }
    }
    Iterator<Map.Entry<Object, Object[]>> it = previousAndCurrentByKey.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Object, Object[]> entry = it.next();
        Object key = entry.getKey();
        Object[] previousAndCurrent = entry.getValue();
        // previousObject may be null, meaning currentObject has been added to the list
        Object previousObject = previousAndCurrent[0];
        // currentObject is not null
        Object currentObject = previousAndCurrent[1];
        if (previousObject == null) {
            // there's no previous object - that means that current object should be added.
            // index of currentObject in currentList
            int iCurrent = (Integer) ((CacheId) key).getPrimaryKey()[pkSize];
            DatabaseRecord extraData = new DatabaseRecord(1);
            extraData.put(this.listOrderField, iCurrent);
            objectAddedDuringUpdate(query, currentObject, null, extraData);
        } else {
            if (!this.isEntireObjectPK) {
                objectUnchangedDuringUpdate(query, currentObject, previousObject);
            }
        }
    }
    if (shouldRepairOrder) {
        ((IndirectList) currentList).setIsListOrderBrokenInDb(false);
    }
}
Also used : DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) HashMap(java.util.HashMap) IndirectList(org.eclipse.persistence.indirection.IndirectList) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 30 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class AggregateCollectionMapping method getAndPrepareModifyQueryForInsert.

/**
 * INTERNAL:
 * setup the modifyQuery for post insert/update and pre delete
 */
public InsertObjectQuery getAndPrepareModifyQueryForInsert(ObjectLevelModifyQuery originalQuery, Object object) {
    AbstractSession session = originalQuery.getSession();
    ClassDescriptor objReferenceDescriptor = getReferenceDescriptor(object.getClass(), session);
    InsertObjectQuery insertQuery = getInsertObjectQuery(session, objReferenceDescriptor);
    insertQuery.setObject(object);
    insertQuery.setDescriptor(objReferenceDescriptor);
    AbstractRecord targetForeignKeyRow = new DatabaseRecord();
    Vector referenceObjectKeys = getReferenceObjectKeys(originalQuery);
    for (int keyIndex = 0; keyIndex < getTargetForeignKeyFields().size(); keyIndex++) {
        targetForeignKeyRow.put(getTargetForeignKeyFields().elementAt(keyIndex), referenceObjectKeys.elementAt(keyIndex));
    }
    insertQuery.setModifyRow(targetForeignKeyRow);
    insertQuery.setTranslationRow(targetForeignKeyRow);
    insertQuery.setSession(session);
    insertQuery.setCascadePolicy(originalQuery.getCascadePolicy());
    insertQuery.dontMaintainCache();
    // way to get a backup directly from a clone.
    if (session.isUnitOfWork()) {
        Object backupAttributeValue = getReferenceDescriptor(object.getClass(), session).getObjectBuilder().buildNewInstance();
        insertQuery.setBackupClone(backupAttributeValue);
    }
    return insertQuery;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) InsertObjectQuery(org.eclipse.persistence.queries.InsertObjectQuery) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)110 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)44 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)43 Vector (java.util.Vector)31 ArrayList (java.util.ArrayList)17 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)17 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Test (org.junit.Test)14 Expression (org.eclipse.persistence.expressions.Expression)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 HashMap (java.util.HashMap)11 Map (java.util.Map)10 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)7 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)6 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)6 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)6