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);
}
}
}
}
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;
}
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);
}
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);
}
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);
}
Aggregations