Search in sources :

Example 71 with DataModifyQuery

use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.

the class TableTypeTestSuite method testCreateAndDeleteSQL.

@Test
public void testCreateAndDeleteSQL() {
    xrService.getORSession().getActiveSession().getIdentityMapAccessor().initializeIdentityMaps();
    DatabaseQuery createQuery = xrService.getORSession().getQuery("create_TabletypeType");
    // by default, JPA will create a DataReadQuery, but we want a DataModifyQuery
    DataModifyQuery query = new DataModifyQuery();
    query.setIsUserDefined(createQuery.isUserDefined());
    query.copyFromQuery(createQuery);
    // Need to clone call, in case was executed as read.
    query.setDatasourceCall((Call) createQuery.getDatasourceCall().clone());
    List<String> args = new ArrayList<String>();
    Vector<DatabaseField> fieldVector = new Vector<DatabaseField>(12);
    List<Object> argVals = new ArrayList<Object>();
    argVals.add(99);
    argVals.add("Joe Black");
    argVals.add("22");
    argVals.add("Janitor");
    argVals.add("q");
    argVals.add(19000);
    argVals.add(333);
    argVals.add(1.00);
    argVals.add("1111");
    argVals.add("040404040404040404040404040404");
    argVals.add("dddddddddddddddddddddddddddddd");
    argVals.add("040404");
    fieldVector.add(new DatabaseField("ID", "TABLETYPE"));
    fieldVector.add(new DatabaseField("NAME", "TABLETYPE"));
    fieldVector.add(new DatabaseField("DEPTNO", "TABLETYPE"));
    fieldVector.add(new DatabaseField("DEPTNAME", "TABLETYPE"));
    fieldVector.add(new DatabaseField("SECTION", "TABLETYPE"));
    fieldVector.add(new DatabaseField("SAL", "TABLETYPE"));
    fieldVector.add(new DatabaseField("COMMISSION", "TABLETYPE"));
    fieldVector.add(new DatabaseField("SALES", "TABLETYPE"));
    fieldVector.add(new DatabaseField("BINID", "TABLETYPE"));
    fieldVector.add(new DatabaseField("B", "TABLETYPE"));
    fieldVector.add(new DatabaseField("C", "TABLETYPE"));
    fieldVector.add(new DatabaseField("R", "TABLETYPE"));
    for (int i = 1; i <= argVals.size(); i++) {
        args.add(String.valueOf(i));
    }
    query.setArguments(args);
    query.setArgumentValues(argVals);
    ((SQLCall) query.getDatasourceCall()).setFields(fieldVector);
    // need to create/set a translation row
    AbstractRecord row = query.rowFromArguments(argVals, (AbstractSession) xrService.getORSession());
    query.setTranslationRow(row);
    query.prepareCall(xrService.getORSession(), query.getTranslationRow());
    query.setSession((AbstractSession) xrService.getORSession());
    query.executeDatabaseQuery();
    // verify create call succeeded
    Invocation invocation = new Invocation("findByPrimaryKey_TabletypeType");
    invocation.setParameter("id", 99);
    Operation op = xrService.getOperation(invocation.getName());
    Object result = op.invoke(xrService, invocation);
    assertNotNull("Result is null after create call", result);
    Document doc = xmlPlatform.createDocument();
    XMLMarshaller marshaller = xrService.getXMLContext().createMarshaller();
    marshaller.marshal(result, doc);
    Document controlDoc = xmlParser.parse(new StringReader(NEW_PERSON2_XML));
    assertTrue("Expected:\n" + documentToString(controlDoc) + "but was:\n" + documentToString(doc), comparer.isNodeEqual(controlDoc, doc));
    // delete
    DatabaseQuery deleteQuery = xrService.getORSession().getQuery("delete_TabletypeType");
    // by default, JPA will create a DataReadQuery, but we want a DataModifyQuery
    query = new DataModifyQuery();
    query.setIsUserDefined(deleteQuery.isUserDefined());
    query.copyFromQuery(deleteQuery);
    // Need to clone call, in case was executed as read.
    query.setDatasourceCall((Call) deleteQuery.getDatasourceCall().clone());
    args = new ArrayList<String>();
    fieldVector = new Vector<DatabaseField>(12);
    argVals = new ArrayList<Object>();
    argVals.add(99);
    args.add("1");
    fieldVector.add(new DatabaseField("ID", "TABLETYPE"));
    query.setArguments(args);
    query.setArgumentValues(argVals);
    ((SQLCall) query.getDatasourceCall()).setFields(fieldVector);
    // need to create/set a translation row
    row = query.rowFromArguments(argVals, (AbstractSession) xrService.getORSession());
    query.setTranslationRow(row);
    query.prepareCall(xrService.getORSession(), query.getTranslationRow());
    query.setSession((AbstractSession) xrService.getORSession());
    query.executeDatabaseQuery();
    // verify delete call succeeded
    result = op.invoke(xrService, invocation);
    assertNull("Result not null after delete call", result);
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) Invocation(org.eclipse.persistence.internal.xr.Invocation) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) Operation(org.eclipse.persistence.internal.xr.Operation) Document(org.w3c.dom.Document) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) StringReader(java.io.StringReader) Vector(java.util.Vector) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Test(org.junit.Test)

Example 72 with DataModifyQuery

use of org.eclipse.persistence.queries.DataModifyQuery in project eclipselink by eclipse-ee4j.

the class TableTypeTestSuite method testUpdateSQL.

@SuppressWarnings("rawtypes")
@Test
public void testUpdateSQL() {
    xrService.getORSession().getActiveSession().getIdentityMapAccessor().initializeIdentityMaps();
    XMLUnmarshaller unmarshaller = xrService.getXMLContext().createUnmarshaller();
    InputSource inputSource = new InputSource(new StringReader(ONE_PERSON_XML));
    XRDynamicEntity firstPerson = (XRDynamicEntity) unmarshaller.unmarshal(inputSource);
    Float originalSal = firstPerson.get("sal");
    String originalC = firstPerson.get("c");
    firstPerson.set("sal", 112000.99);
    firstPerson.set("c", "ababababababababababababababab");
    DatabaseQuery updateQuery = xrService.getORSession().getQuery("update_TabletypeType");
    // map property names to parameter binding values
    Map<String, Integer> propOrder = new HashMap<String, Integer>();
    propOrder.put("id", 1);
    propOrder.put("name", 2);
    propOrder.put("deptno", 3);
    propOrder.put("deptname", 4);
    propOrder.put("section", 5);
    propOrder.put("sal", 6);
    propOrder.put("commission", 7);
    propOrder.put("sales", 8);
    propOrder.put("binid", 9);
    propOrder.put("b", 10);
    propOrder.put("c", 11);
    propOrder.put("r", 12);
    List<String> args = new ArrayList<String>();
    Vector<DatabaseField> fieldVector = new Vector<DatabaseField>(12);
    List<Object> argVals = new ArrayList<Object>();
    for (String prop : firstPerson.getPropertiesMap().keySet()) {
        args.add(propOrder.get(prop).toString());
        argVals.add(firstPerson.get(prop));
        fieldVector.add(new DatabaseField(prop.toUpperCase(), "TABLETYPE"));
    }
    // by default, JPA will create a DataReadQuery, but we want a DataModifyQuery
    DataModifyQuery query = new DataModifyQuery();
    query.setIsUserDefined(updateQuery.isUserDefined());
    query.copyFromQuery(updateQuery);
    // Need to clone call, in case was executed as read.
    query.setDatasourceCall((Call) updateQuery.getDatasourceCall().clone());
    query.setArguments(args);
    query.setArgumentValues(argVals);
    ((SQLCall) query.getDatasourceCall()).setFields(fieldVector);
    // need to create/set a translation row
    AbstractRecord row = query.rowFromArguments(argVals, (AbstractSession) xrService.getORSession());
    query.setTranslationRow(row);
    query.prepareCall(xrService.getORSession().getActiveSession(), row);
    query.setSession((AbstractSession) xrService.getORSession().getActiveSession());
    // execute the update
    query.executeDatabaseQuery();
    // verify update operation
    DatabaseQuery findQuery = xrService.getORSession().getActiveSession().getQuery("findByPrimaryKey_TabletypeType");
    findQuery.setIsPrepared(false);
    args = new ArrayList<String>();
    fieldVector = new Vector<DatabaseField>(12);
    argVals = new ArrayList<Object>();
    argVals.add(1);
    args.add("1");
    fieldVector.add(new DatabaseField("ID", "TABLETYPE"));
    findQuery.setArguments(args);
    findQuery.setArgumentValues(argVals);
    ((SQLCall) findQuery.getDatasourceCall()).setFields(fieldVector);
    // need to create/set a translation row
    row = findQuery.rowFromArguments(argVals, (AbstractSession) xrService.getORSession().getActiveSession());
    findQuery.setTranslationRow(row);
    findQuery.prepareCall(xrService.getORSession().getActiveSession(), row);
    findQuery.setSession((AbstractSession) xrService.getORSession().getActiveSession());
    xrService.getORSession().getActiveSession().getIdentityMapAccessor().initializeIdentityMaps();
    // execute the FindByPk
    Object result = findQuery.executeDatabaseQuery();
    assertTrue("Expected Vector, but result was " + result.getClass().getName(), result instanceof Vector);
    Vector resultVector = (Vector) result;
    assertTrue("Expected vector of size 1, but was " + resultVector.size(), resultVector.size() == 1);
    result = resultVector.get(0);
    assertTrue("Expected TableType (XRDynamicEntity) but was " + result.getClass().getName(), result instanceof XRDynamicEntity);
    // verify that 'sal' and 'c' fields were updated successfully
    XRDynamicEntity tableTypeEntity = (XRDynamicEntity) result;
    assertTrue("Expected [sal] '112000.99' but was '" + tableTypeEntity.get("sal") + "'", Float.compare(tableTypeEntity.get("sal"), 112000.99f) == 0);
    Character[] chars = tableTypeEntity.get("c");
    StringBuilder sb = new StringBuilder(chars.length);
    for (Character c : chars) {
        sb.append(c.charValue());
    }
    String charStr = sb.toString();
    assertTrue("Expected [c] 'ababababababababababababababab' but was '" + tableTypeEntity.get("c") + "'", charStr.equals("ababababababababababababababab"));
    // reset original value
    firstPerson.set("sal", originalSal);
    firstPerson.set("c", originalC);
    Invocation invocation = new Invocation("update_TabletypeType");
    invocation.setParameter("theInstance", firstPerson);
    Operation op = xrService.getOperation(invocation.getName());
    op.invoke(xrService, invocation);
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) InputSource(org.xml.sax.InputSource) Invocation(org.eclipse.persistence.internal.xr.Invocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(org.eclipse.persistence.internal.xr.Operation) StringReader(java.io.StringReader) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Vector(java.util.Vector) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) XRDynamicEntity(org.eclipse.persistence.internal.xr.XRDynamicEntity) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Test(org.junit.Test)

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