Search in sources :

Example 11 with DatabaseRecord

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

the class PLSQLTest method test.

/**
 * Execute the named query and compare the result with the expected result.
 */
@Override
public void test() {
    Object queryResult = null;
    try {
        queryResult = getSession().executeQuery(this.queryName, this.queryClass, this.queryArguments);
    } catch (RuntimeException exception) {
        if (this.result instanceof EclipseLinkException) {
            if (exception.getClass() == this.result.getClass() && (((EclipseLinkException) exception).getErrorCode() == ((EclipseLinkException) this.result).getErrorCode())) {
                return;
            }
        }
        throw exception;
    }
    if (this.result == null) {
        return;
    }
    if (this.result.getClass() != queryResult.getClass()) {
        if (queryResult instanceof List) {
            queryResult = ((List) queryResult).get(0);
        }
        if (this.result.getClass() != queryResult.getClass()) {
            throwError("Results do not match: " + queryResult + " expected: " + this.result);
        }
    }
    if (this.result instanceof DatabaseRecord) {
        DatabaseRecord record = (DatabaseRecord) this.result;
        DatabaseRecord queryRecord = (DatabaseRecord) queryResult;
        for (Iterator<DatabaseField> iterator = record.getFields().iterator(); iterator.hasNext(); ) {
            DatabaseField field = iterator.next();
            Object value = record.get(field);
            Object queryValue = queryRecord.get(field);
            if (value instanceof Number) {
                // Avoid Java number type in-equality.
                value = value.toString();
                queryValue = queryValue.toString();
            }
            if (!value.equals(queryValue)) {
                throwError("Results do not match: " + queryValue + " expected: " + value);
            }
        }
    }
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) List(java.util.List)

Example 12 with DatabaseRecord

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

the class PLSQLTestModel method getFunctionTestSuite.

public static TestSuite getFunctionTestSuite() {
    TestSuite suite = new TestSuite();
    suite.setName("PLSQLFunctionTestSuite");
    suite.setDescription("This suite tests calling PLSQL functions.");
    List args = new ArrayList();
    args.add("varchar");
    args.add(1);
    args.add(123);
    args.add(new BigDecimal("123.6"));
    args.add(1);
    args.add(1);
    args.add(1);
    args.add(1);
    args.add(1);
    args.add(1);
    args.add(1);
    args.add(new BigDecimal("123.5"));
    PLSQLTest test = new PLSQLTest("SimpleInFunc", Address.class, args);
    test.setName("SimpleInFuncTest");
    suite.addTest(test);
    Address resultAddress = new Address();
    resultAddress.setId(new BigDecimal(1234));
    resultAddress.setNumber(17);
    resultAddress.setStreet("Bank");
    resultAddress.setCity("Ottawa");
    resultAddress.setState("ON");
    args = new ArrayList();
    DatabaseRecord result = new DatabaseRecord();
    result.put("RESULT", resultAddress);
    test = new PLSQLTest("AddressOutObjectFunc", Address.class, args, result);
    test.setName("AddressOutFuncTest");
    suite.addTest(test);
    return suite;
}
Also used : Address(org.eclipse.persistence.testing.models.plsql.Address) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal)

Example 13 with DatabaseRecord

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

the class OneToManyMapping method initializeChangeOrderTargetQuery.

/**
 * INTERNAL:
 * Initialize changeOrderTargetQuery.
 */
@Override
protected void initializeChangeOrderTargetQuery(AbstractSession session) {
    boolean hasChangeOrderTargetQuery = changeOrderTargetQuery != null;
    if (!hasChangeOrderTargetQuery) {
        changeOrderTargetQuery = new DataModifyQuery();
    }
    changeOrderTargetQuery = new DataModifyQuery();
    if (!changeOrderTargetQuery.hasSessionName()) {
        changeOrderTargetQuery.setSessionName(session.getName());
    }
    if (hasChangeOrderTargetQuery) {
        return;
    }
    DatabaseTable table = this.listOrderField.getTable();
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    int size = targetPrimaryKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index);
        Expression expression = builder.getField(targetPrimaryKey).equal(builder.getParameter(targetPrimaryKey));
        whereClause = expression.and(whereClause);
    }
    AbstractRecord modifyRow = new DatabaseRecord();
    modifyRow.add(this.listOrderField, null);
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(table);
    statement.setWhereClause(whereClause);
    statement.setModifyRow(modifyRow);
    changeOrderTargetQuery.setSQLStatement(statement);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 14 with DatabaseRecord

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

the class OneToManyMapping method initializeRemoveAllTargetsQuery.

/**
 * INTERNAL:
 * Initialize removeAllTargetsQuery.
 */
protected void initializeRemoveAllTargetsQuery(AbstractSession session) {
    if (!removeAllTargetsQuery.hasSessionName()) {
        removeAllTargetsQuery.setSessionName(session.getName());
    }
    if (hasCustomRemoveAllTargetsQuery) {
        return;
    }
    // All targetForeignKeys should have the same table
    DatabaseTable table = targetForeignKeyFields.get(0).getTable();
    // Build where clause expression.
    Expression whereClause = null;
    Expression builder = new ExpressionBuilder();
    AbstractRecord modifyRow = new DatabaseRecord();
    int size = targetForeignKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetForeignKey = targetForeignKeyFields.get(index);
        if (shouldRemoveTargetQueryModifyTargetForeignKey()) {
            modifyRow.put(targetForeignKey, null);
        }
        Expression expression = builder.getField(targetForeignKey).equal(builder.getParameter(targetForeignKey));
        whereClause = expression.and(whereClause);
    }
    if (this.listOrderField != null) {
        // targetForeignKeys and listOrderField should have the same table
        modifyRow.add(this.listOrderField, null);
    }
    SQLUpdateStatement statement = new SQLUpdateStatement();
    statement.setTable(table);
    statement.setWhereClause(whereClause);
    statement.setModifyRow(modifyRow);
    removeAllTargetsQuery.setSQLStatement(statement);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) SQLUpdateStatement(org.eclipse.persistence.internal.expressions.SQLUpdateStatement) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 15 with DatabaseRecord

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

the class OneToManyMapping method updateTargetRowPreDeleteSource.

/**
 * INTERNAL:
 * Update target foreign key after a target object was removed from the source. This follows following steps.
 * <p>- Extract primary key and its value from the source object.
 * <p>- Extract target key and its value from the target object.
 * <p>- Construct an update statement with above fields and values for target table.
 * <p>- execute the statement.
 */
public void updateTargetRowPreDeleteSource(ObjectLevelModifyQuery query) throws DatabaseException {
    if (this.isReadOnly) {
        return;
    }
    // Extract primary key and value from the source.
    int size = this.sourceKeyFields.size();
    AbstractRecord translationRow = new DatabaseRecord(size);
    AbstractRecord modifyRow = new DatabaseRecord(size);
    for (int index = 0; index < size; index++) {
        DatabaseField sourceKey = this.sourceKeyFields.get(index);
        DatabaseField targetForeignKey = this.targetForeignKeyFields.get(index);
        Object sourceKeyValue = query.getTranslationRow().get(sourceKey);
        translationRow.add(targetForeignKey, sourceKeyValue);
        // Need to set this value to null in the modify row.
        modifyRow.add(targetForeignKey, null);
    }
    if (listOrderField != null) {
        modifyRow.add(listOrderField, null);
    }
    // Need a different modify row than translation row, as the same field has different values in each.
    DataModifyQuery removeQuery = (DataModifyQuery) this.removeAllTargetsQuery.clone();
    removeQuery.setModifyRow(modifyRow);
    removeQuery.setHasModifyRow(true);
    removeQuery.setIsExecutionClone(true);
    query.getSession().executeQuery(removeQuery, translationRow);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) 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