Search in sources :

Example 91 with DatabaseRecord

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

the class AbstractTransformationMapping method readFromReturnRowIntoObject.

/**
 * INTERNAL:
 * Extracts value from return row and set the attribute to the value in the object.
 * Return row is merged into object after execution of insert or update call
 * according to ReturningPolicy.
 */
public Object readFromReturnRowIntoObject(AbstractRecord row, Object object, ReadObjectQuery query, Collection handledMappings, ObjectChangeSet changeSet) throws DatabaseException {
    int size = this.fields.size();
    AbstractRecord transformationRow = new DatabaseRecord(size);
    for (int i = 0; i < size; i++) {
        DatabaseField field = this.fields.get(i);
        Object value;
        if (row.containsKey(field)) {
            value = row.get(field);
        } else {
            value = valueFromObject(object, field, query.getSession());
        }
        transformationRow.add(field, value);
    }
    if (changeSet != null && (!changeSet.isNew() || (query.getDescriptor() != null && query.getDescriptor().shouldUseFullChangeSetsForNewObjects()))) {
        TransformationMappingChangeRecord record = (TransformationMappingChangeRecord) changeSet.getChangesForAttributeNamed(attributeName);
        if (record == null) {
            record = new TransformationMappingChangeRecord(changeSet);
            record.setAttribute(attributeName);
            record.setMapping(this);
            record.setOldValue(getAttributeValueFromObject(object));
            changeSet.addChange(record);
        }
        record.setRow(transformationRow);
    }
    Object attributeValue = readFromRowIntoObject(transformationRow, null, object, null, query, query.getSession(), true);
    if (handledMappings != null) {
        handledMappings.add(this);
    }
    return attributeValue;
}
Also used : TransformationMappingChangeRecord(org.eclipse.persistence.internal.sessions.TransformationMappingChangeRecord) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord)

Example 92 with DatabaseRecord

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

the class ReferenceMapping method valueFromRow.

/**
 * INTERNAL:
 * Return the value of the field from the row or a value holder on the query to obtain the object.
 * Check for batch + aggregation reading.
 */
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
    if (this.descriptor.getCachePolicy().isProtectedIsolation()) {
        if (this.isCacheable && isTargetProtected && cacheKey != null) {
            // cachekey will be null when isolating to uow
            // used cached collection
            Object result = null;
            Object cached = cacheKey.getObject();
            if (cached != null) {
                if (wasCacheUsed != null) {
                    wasCacheUsed[0] = Boolean.TRUE;
                }
                return this.getAttributeValueFromObject(cached);
            }
            return result;
        } else if (!this.isCacheable && !isTargetProtected && cacheKey != null) {
            return this.indirectionPolicy.buildIndirectObject(new ValueHolder<>(null));
        }
    }
    AbstractRecord targetRow = null;
    if (row.hasSopObject()) {
        Object sopAttributeValue = getAttributeValueFromObject(row.getSopObject());
        if (sopAttributeValue == null) {
            return this.indirectionPolicy.nullValueFromRow();
        }
        // As part of SOP object the indirection should be already triggered
        Object sopRealAttributeValue = getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(), sopAttributeValue);
        if (sopRealAttributeValue == null) {
            return sopAttributeValue;
        }
        targetRow = new DatabaseRecord(0);
        targetRow.setSopObject(sopRealAttributeValue);
        // As part of SOP object the indirection should be already triggered and should be no references outside of sopObject (and its privately owned (possibly nested privately owned) objects)
        return getReferenceDescriptor().getObjectBuilder().buildObject(query, targetRow, null);
    }
    Ref ref = (Ref) row.get(getField());
    if (ref == null) {
        return null;
    }
    Struct struct;
    try {
        executionSession.getAccessor().incrementCallCount(executionSession);
        java.sql.Connection connection = executionSession.getAccessor().getConnection();
        struct = (Struct) executionSession.getPlatform().getRefValue(ref, executionSession, connection);
        targetRow = ((ObjectRelationalDataTypeDescriptor) getReferenceDescriptor()).buildRowFromStructure(struct);
    } catch (java.sql.SQLException exception) {
        throw DatabaseException.sqlException(exception, executionSession, false);
    } finally {
        executionSession.getAccessor().decrementCallCount();
    }
    return getReferenceDescriptor().getObjectBuilder().buildObject(query, targetRow, joinManager);
}
Also used : Ref(java.sql.Ref) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ValueHolder(org.eclipse.persistence.indirection.ValueHolder) Struct(java.sql.Struct)

Example 93 with DatabaseRecord

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

the class EntityResult method getValueFromRecord.

/**
 * INTERNAL:
 * This method is a convenience method for extracting values from Results
 */
@Override
public Object getValueFromRecord(DatabaseRecord record, ResultSetMappingQuery query) {
    // From the row data build result entity.
    // To do this let's collect the column based data for this entity from
    // the results and call build object with this new row.
    ClassDescriptor descriptor = query.getSession().getDescriptor(this.entityClass);
    if (descriptor == null) {
        throw new IllegalArgumentException("@EntityResult: entityClass points to unknown entity: " + (this.entityClass != null ? this.entityClass.getName() : "null"));
    }
    DatabaseRecord entityRecord = new DatabaseRecord(descriptor.getFields().size());
    if (descriptor.hasInheritance()) {
        Object value = null;
        if (this.discriminatorColumn == null) {
            value = record.get(descriptor.getInheritancePolicy().getClassIndicatorField());
        } else {
            value = record.getIndicatingNoEntry(this.discriminatorColumn);
            if (value == AbstractRecord.noEntry) {
                throw QueryException.discriminatorColumnNotSelected(this.discriminatorColumn.getName(), getSQLResultMapping().getName());
            }
        }
        entityRecord.put(descriptor.getInheritancePolicy().getClassIndicatorField(), value);
        // if multiple types may have been read get the correct descriptor.
        if (descriptor.getInheritancePolicy().shouldReadSubclasses()) {
            Class<?> classValue = descriptor.getInheritancePolicy().classFromRow(entityRecord, query.getSession());
            descriptor = query.getSession().getDescriptor(classValue);
        }
    }
    for (Iterator<DatabaseMapping> mappings = descriptor.getMappings().iterator(); mappings.hasNext(); ) {
        DatabaseMapping mapping = mappings.next();
        FieldResult fieldResult = (FieldResult) this.getFieldResults().get(mapping.getAttributeName());
        if (fieldResult != null) {
            if (mapping.getFields().size() == 1) {
                entityRecord.put(mapping.getFields().firstElement(), record.get(fieldResult.getColumn()));
            } else if (mapping.getFields().size() > 1) {
                getValueFromRecordForMapping(entityRecord, mapping, fieldResult, record);
            }
        } else {
            for (Iterator<DatabaseField> fields = mapping.getFields().iterator(); fields.hasNext(); ) {
                DatabaseField field = fields.next();
                entityRecord.put(field, record.get(field));
            }
        }
    }
    query.setReferenceClass(this.entityClass);
    query.setDescriptor(descriptor);
    // TODO : support prefetchedCacheKeys in ResultSetMappingQuery
    return descriptor.getObjectBuilder().buildObject(query, entityRecord, null);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 94 with DatabaseRecord

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

the class joNijoTestSet method runQuery.

@SuppressWarnings("unchecked")
@Test
public void runQuery() {
    DatabaseSession s = project.createDatabaseSession();
    s.dontLogMessages();
    s.login();
    Object o = null;
    Vector queryArgs = new NonSynchronizedVector();
    queryArgs.add(1);
    boolean worked = false;
    String msg = null;
    try {
        o = s.executeQuery("joNijo", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertTrue("invocation joNijo failed: " + msg, worked);
    Vector results = (Vector) o;
    DatabaseRecord record = (DatabaseRecord) results.get(0);
    String y = (String) record.get("X");
    assertTrue("wrong y value", y.equals("test"));
    BigDecimal z = (BigDecimal) record.get("Z");
    assertTrue("wrong z value", z.intValue() == 46);
    s.logout();
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 95 with DatabaseRecord

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

the class AddTargetCustomizer method customize.

@Override
public void customize(ClassDescriptor descriptor) {
    OneToManyMapping dealers = (OneToManyMapping) descriptor.getMappingForAttributeName("dealers");
    DataModifyQuery query = new DataModifyQuery();
    query.setName("CustomAddTargetQuery");
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(descriptor.getPrimaryKeyFields().get(0).getTable());
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    int size = dealers.getSourceKeyFields().size();
    for (int index = 0; index < size; index++) {
        DatabaseField key = dealers.getSourceKeyFields().get(index);
        Expression expression = builder.getField(key).equal(builder.getParameter(dealers.getTargetForeignKeyFields().get(index)));
        whereClause = expression.and(whereClause);
    }
    statement.setWhereClause(whereClause);
    AbstractRecord modifyRow = new DatabaseRecord();
    modifyRow.add(descriptor.getMappingForAttributeName("firstName").getField(), null);
    statement.setModifyRow(modifyRow);
    query.setSQLStatement(statement);
    dealers.setCustomAddTargetQuery(query);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) Expression(org.eclipse.persistence.expressions.Expression) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

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