use of org.eclipse.persistence.sessions.DataRecord in project eclipselink by eclipse-ee4j.
the class DBWSModelProject method buildResultDescriptor.
// result
//
// type|attachment
protected XMLDescriptor buildResultDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(Result.class);
descriptor.setDefaultRootElement("result");
XMLTransformationMapping type = new XMLTransformationMapping();
type.setAttributeName("type");
QNameTransformer qNameTransformer = new QNameTransformer("type/text()");
type.addFieldTransformer("type/text()", qNameTransformer);
type.setAttributeTransformer(qNameTransformer);
descriptor.addMapping(type);
XMLCompositeObjectMapping attachment = new XMLCompositeObjectMapping();
attachment.setAttributeName("attachment");
attachment.setXPath("attachment");
attachment.setReferenceClass(Attachment.class);
descriptor.addMapping(attachment);
XMLCompositeObjectMapping sxf = new XMLCompositeObjectMapping();
sxf.setAttributeName("simpleXMLFormat");
sxf.setXPath("simple-xml-format");
sxf.setReferenceClass(SimpleXMLFormat.class);
descriptor.addMapping(sxf);
XMLDirectMapping isCollection = new XMLDirectMapping();
isCollection.setAttributeAccessor(new AttributeAccessor() {
@Override
public String getAttributeName() {
return "isCollection";
}
@Override
public Object getAttributeValueFromObject(Object object) throws DescriptorException {
if (object instanceof CollectionResult) {
return Boolean.TRUE;
}
return null;
}
@Override
public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
}
});
isCollection.setXPath("@isCollection");
descriptor.addMapping(isCollection);
XMLField isColl = new XMLField("@isCollection");
isColl.setSchemaType(BOOLEAN_QNAME);
descriptor.getInheritancePolicy().setClassIndicatorField(isColl);
descriptor.getInheritancePolicy().setClassExtractor(new ClassExtractor() {
@Override
@SuppressWarnings({ "unchecked" })
public <T> Class<T> extractClassFromRow(DataRecord dataRecord, Session session) {
Class<?> clz = Result.class;
UnmarshalRecord uRecord = (UnmarshalRecord) dataRecord;
Attributes attrs = uRecord.getAttributes();
if (attrs != null) {
for (int i = 0, l = attrs.getLength(); i < l; i++) {
String attributeName = attrs.getQName(i);
if (attributeName.equals("isCollection")) {
String value = attrs.getValue(i);
if (value.equalsIgnoreCase("true")) {
clz = CollectionResult.class;
break;
}
}
}
}
return (Class<T>) clz;
}
});
XMLDirectMapping jdbcTypeMapping = new XMLDirectMapping();
jdbcTypeMapping.setAttributeName("jdbcType");
jdbcTypeMapping.setXPath("@jdbcType");
descriptor.addMapping(jdbcTypeMapping);
return descriptor;
}
use of org.eclipse.persistence.sessions.DataRecord 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.sessions.DataRecord in project eclipselink by eclipse-ee4j.
the class NoSQLJPATest method testNativeQuery.
/**
* Test native query.
*/
@Test
public void testNativeQuery() {
MappedInteraction interaction = new MappedInteraction();
final Order existingOrder = getRandomOrder();
interaction.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.GET.name());
interaction.addArgumentValue("[ORDER," + existingOrder.id + "]", "");
final Query query1 = em.unwrap(JpaEntityManager.class).createQuery(interaction);
@SuppressWarnings("unchecked") final List<Object> result1 = query1.getResultList();
assertEquals(String.format("Expected result of size 1, got %d", result1.size()), 1, result1.size());
assertTrue(String.format("Result is not instance of Record but %s", result1.get(0).getClass().getSimpleName()), (result1.get(0) instanceof DataRecord));
assertTrue(String.format("Incorrect result: %s", result1), (((DataRecord) result1.get(0)).containsKey("[ORDER," + existingOrder.id + "]")));
interaction = new XMLInteraction();
interaction.setProperty(OracleNoSQLPlatform.OPERATION, OracleNoSQLOperation.GET.name());
interaction.addArgumentValue("@id", existingOrder.id);
final Query query2 = em.unwrap(JpaEntityManager.class).createQuery(interaction, Order.class);
@SuppressWarnings("unchecked") final List<Object> result2 = query2.getResultList();
assertEquals(String.format("Expected result of size 1, got %d", result2.size()), 1, result2.size());
assertTrue(String.format("Result is not instance of Order but %s", result2.get(0).getClass().getSimpleName()), (result2.get(0) instanceof Order));
}
use of org.eclipse.persistence.sessions.DataRecord in project eclipselink by eclipse-ee4j.
the class PLSQLCallModelTestProject method buildDatabaseTypeWrapperDescriptor.
protected ClassDescriptor buildDatabaseTypeWrapperDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(DatabaseTypeWrapper.class);
descriptor.getInheritancePolicy().setClassExtractor(new ClassExtractor() {
@Override
public Class<?> extractClassFromRow(DataRecord databaseRow, Session session) {
if (databaseRow instanceof UnmarshalRecord) {
UnmarshalRecord unmarshalRecord = (UnmarshalRecord) databaseRow;
Attributes attributes = unmarshalRecord.getAttributes();
for (int i = 0, l = attributes.getLength(); i < l; i++) {
String qName = attributes.getQName(i);
if (PLSQL_RECORD_INDICATOR.equals(qName)) {
return ComplexPLSQLTypeWrapper.class;
}
if (PLSQL_TYPE_INDICATOR.equals(qName)) {
return SimplePLSQLTypeWrapper.class;
}
return JDBCTypeWrapper.class;
}
}
Class<?> clz = null;
DOMRecord domRecord = (DOMRecord) databaseRow;
Object o = domRecord.get("@" + PLSQL_RECORD_INDICATOR);
if (o != null) {
clz = ComplexPLSQLTypeWrapper.class;
} else {
o = domRecord.get("@" + PLSQL_TYPE_INDICATOR);
if (o != null) {
clz = SimplePLSQLTypeWrapper.class;
} else {
o = domRecord.get("@" + JDBC_TYPE_INDICATOR);
if (o != null) {
clz = JDBCTypeWrapper.class;
}
}
}
return clz;
}
});
descriptor.descriptorIsAggregate();
return descriptor;
}
use of org.eclipse.persistence.sessions.DataRecord in project eclipselink by eclipse-ee4j.
the class QueryFrameworkTestSuite method buildGetSQLTest.
/**
* Test getting the SQL for a parameterized query.
*/
public TestCase buildGetSQLTest() {
TestCase test = new TestCase() {
@Override
public void test() {
ReadAllQuery query = new ReadAllQuery(Employee.class);
ExpressionBuilder builder = query.getExpressionBuilder();
query.setSelectionCriteria(builder.get("firstName").equal(builder.getParameter("name")));
query.addArgument("name");
DataRecord dataRecord = new DatabaseRecord();
dataRecord.put("name", "Bob");
String sql = query.getTranslatedSQLString(getSession(), dataRecord);
if (sql.indexOf("?") != -1) {
throwError("SQL was not translated.");
}
}
};
test.setName("GetSQLTest");
return test;
}
Aggregations