Search in sources :

Example 26 with DataModifyQuery

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);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) 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) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 27 with DataModifyQuery

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()));
    }
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DataReadQuery(org.eclipse.persistence.queries.DataReadQuery) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException)

Example 28 with DataModifyQuery

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();
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException)

Example 29 with DataModifyQuery

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);
}
Also used : MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) DataReadQuery(org.eclipse.persistence.queries.DataReadQuery) List(java.util.List) DataRecord(org.eclipse.persistence.sessions.DataRecord) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) Test(org.junit.Test)

Example 30 with DataModifyQuery

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);
        }
    }
}
Also used : DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Aggregations

DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)72 PLSQLStoredProcedureCall (org.eclipse.persistence.platform.database.oracle.plsql.PLSQLStoredProcedureCall)32 Test (org.junit.Test)24 StringReader (java.io.StringReader)23 Project (org.eclipse.persistence.sessions.Project)22 ObjectPersistenceWorkbenchXMLProject (org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject)21 XMLContext (org.eclipse.persistence.oxm.XMLContext)21 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)21 Document (org.w3c.dom.Document)21 NonJDBCTestHelper.buildTestProject (org.eclipse.persistence.testing.tests.nonJDBC.NonJDBCTestHelper.buildTestProject)17 NonJDBCTestHelper.buildWorkbenchXMLProject (org.eclipse.persistence.testing.tests.nonJDBC.NonJDBCTestHelper.buildWorkbenchXMLProject)17 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)12 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)10 PLSQLrecord (org.eclipse.persistence.platform.database.oracle.plsql.PLSQLrecord)8 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)7 Expression (org.eclipse.persistence.expressions.Expression)5 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)4 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)4 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)4