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