use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.
the class AggregateCollectionMapping method initializeUpdateListOrderQuery.
protected void initializeUpdateListOrderQuery(AbstractSession session, String queryType) {
DataModifyQuery query = new DataModifyQuery();
if (queryType == pk) {
this.pkUpdateListOrderFieldQuery = query;
} else if (queryType == bulk) {
this.bulkUpdateListOrderFieldQuery = query;
} else {
this.updateListOrderFieldQuery = query;
}
query.setSessionName(session.getName());
// Build where clause expression.
Expression whereClause = null;
Expression builder = new ExpressionBuilder();
AbstractRecord modifyRow = new DatabaseRecord();
if (queryType == pk) {
Iterator<DatabaseField> it = getReferenceDescriptor().getPrimaryKeyFields().iterator();
while (it.hasNext()) {
DatabaseField pkField = it.next();
DatabaseField sourceField = targetForeignKeyToSourceKeys.get(pkField);
DatabaseField parameterField = sourceField != null ? sourceField : pkField;
Expression expression = builder.getField(pkField).equal(builder.getParameter(parameterField));
whereClause = expression.and(whereClause);
}
modifyRow.add(this.listOrderField, null);
} else {
Iterator<Map.Entry<DatabaseField, DatabaseField>> it = targetForeignKeyToSourceKeys.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<DatabaseField, DatabaseField> entry = it.next();
Expression expression = builder.getField(entry.getKey()).equal(builder.getParameter(entry.getValue()));
whereClause = expression.and(whereClause);
}
Expression listOrderExpression;
if (queryType == bulk) {
listOrderExpression = builder.getField(this.listOrderField).between(builder.getParameter(min), builder.getParameter(max));
modifyRow.add(this.listOrderField, ExpressionMath.add(builder.getField(this.listOrderField), builder.getParameter(shift)));
} else {
listOrderExpression = builder.getField(this.listOrderField).equal(builder.getParameter(min));
modifyRow.add(this.listOrderField, null);
}
whereClause = listOrderExpression.and(whereClause);
}
SQLUpdateStatement statement = new SQLUpdateStatement();
statement.setTable(getReferenceDescriptor().getDefaultTable());
statement.setWhereClause(whereClause);
statement.setModifyRow(modifyRow);
query.setSQLStatement(statement);
}
use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.
the class QueryTimeoutBatchTestCase method initializeDatabase.
protected void initializeDatabase(UnitOfWork uow) {
try {
// Add expected inserts to sequence
DataModifyQuery modifyQuery = new DataModifyQuery();
String sequenceTableName = "SEQUENCE";
if (getSession().getPlatform().getDefaultSequence().isTable()) {
sequenceTableName = getSession().getPlatform().getQualifiedSequenceTableName();
}
modifyQuery.setSQLString("UPDATE " + sequenceTableName + " SET SEQ_COUNT = SEQ_COUNT + 10 WHERE SEQ_NAME = 'EMP_SEQ'");
modifyQuery.setForceBatchStatementExecution(true);
uow.addQuery("modify1", modifyQuery);
uow.executeQuery(modifyQuery);
// uow.commit();
// Get next sequence
DataReadQuery readQuery = new DataReadQuery();
readQuery.setSQLString("SELECT SEQ_COUNT FROM " + sequenceTableName + " WHERE SEQ_NAME = 'EMP_SEQ'");
uow.addQuery("read1", readQuery);
Object resultFromRead = uow.executeQuery(readQuery);
DatabaseRecord dbRecord = (DatabaseRecord) ((Vector) resultFromRead).get(0);
setCurrentIDSequence(((BigDecimal) dbRecord.get("SEQ_COUNT")).longValue());
uow.commit();
} catch (Exception e) {
System.out.println("QueryTimeoutBatchTest could not get EMP_SEQ sequence");
setCurrentIDSequence(40000 + Math.round(Math.random()));
}
}
use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.
the class QueryTimeoutBatchDatabaseQueryTest method test.
@Override
public void test() {
UnitOfWork uow = null;
try {
uow = setupPlatform();
initializeDatabase(uow);
// Get new UOW
uow = getSession().acquireUnitOfWork();
int queryCount = 0;
for (int i = 0; i < getNumberOfInserts(); i++) {
DataModifyQuery query = new DataModifyQuery();
queryCount++;
// The following insert will take around 5.2 seconds per row (the last (address c) is significant)
// insert into employee (emp_id, version) SELECT 40003, SUM(e.address_id) as version from address e, address b, address b, address c, address c;
StringBuffer sBuffer = new StringBuffer(getQuerySQLPrefix());
sBuffer.append(getCurrentIDSequence() + i);
// sBuffer.append(i);
sBuffer.append(getQuerySQLPostfix());
query.setSQLString(sBuffer.toString());
// set different query timeouts - the largest will be used
query.setQueryTimeout(getChildQueryTimeout());
// session.executeQuery(query);
StringBuilder queryName = new StringBuilder("query");
queryName.append(i);
// Force the last query to execute
if (queryCount == getNumberOfInserts()) {
query.setForceBatchStatementExecution(true);
// clear last queryTimeout - so we pick up one from a previous appendCall
// query.setQueryTimeout(0);
}
uow.addQuery(queryName.toString(), query);
uow.executeQuery(query);
}
uow.commit();
} catch (Exception e) {
// System.err.print(e.getMessage());
if (e instanceof DatabaseException) {
limitExceeded = true;
vendorErrorCodeEncountered = ((DatabaseException) e).getDatabaseErrorCode();
// System.out.println("test completed with timeout of " + getChildQueryTimeout() + " seconds and exception: " + vendorErrorCodeEncountered);
} else {
// e.printStackTrace();
}
// Release transaction mutex
((AbstractSession) uow).rollbackTransaction();
} finally {
resetPlatform();
}
}
use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.
the class NoSQLSimpleTest method testNative.
/**
* Test native Oracle NoSQL queries.
*/
@Test
public void testNative() throws Exception {
final MappedInteraction insertCall = new MappedInteraction();
insertCall.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.PUT.name());
insertCall.addArgumentValue("Order/1234", "foo");
final DataModifyQuery insert = new DataModifyQuery(insertCall);
session.executeQuery(insert);
final MappedInteraction readCall = new MappedInteraction();
readCall.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.GET.name());
readCall.addArgumentValue("Order/1234", "");
final DataReadQuery read = new DataReadQuery(readCall);
@SuppressWarnings("unchecked") final List<DataRecord> result = (List<DataRecord>) session.executeQuery(read);
final String value = new String((byte[]) result.get(0).get("Order/1234"));
assertEquals("foo expected: " + value, "foo", value);
}
use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.
the class DbChangeNotificationAdapter method execute.
protected void execute(Session session, String str, boolean shouldThrowException) {
try {
// For some reason DML must not usee binding on Oracle.
DataModifyQuery query = new DataModifyQuery(str);
query.setShouldBindAllParameters(false);
session.executeQuery(query);
} catch (Exception e) {
if (shouldThrowException) {
throw new TestErrorException("FAILED: " + str, e);
}
}
}
Aggregations