Search in sources :

Example 1 with DataRecord

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;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) Attributes(org.xml.sax.Attributes) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) ClassExtractor(org.eclipse.persistence.descriptors.ClassExtractor) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) QNameTransformer(org.eclipse.persistence.internal.xr.QNameTransformer) DataRecord(org.eclipse.persistence.sessions.DataRecord) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) Session(org.eclipse.persistence.sessions.Session)

Example 2 with DataRecord

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);
}
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 3 with DataRecord

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));
}
Also used : Order(org.eclipse.persistence.testing.models.jpa.nosql.Order) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) DataRecord(org.eclipse.persistence.sessions.DataRecord) XMLInteraction(org.eclipse.persistence.eis.interactions.XMLInteraction) Test(org.junit.Test)

Example 4 with DataRecord

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;
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Attributes(org.xml.sax.Attributes) ClassExtractor(org.eclipse.persistence.descriptors.ClassExtractor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) DataRecord(org.eclipse.persistence.sessions.DataRecord) Session(org.eclipse.persistence.sessions.Session)

Example 5 with DataRecord

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

Aggregations

DataRecord (org.eclipse.persistence.sessions.DataRecord)10 MappedInteraction (org.eclipse.persistence.eis.interactions.MappedInteraction)5 Query (jakarta.persistence.Query)4 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 List (java.util.List)3 Test (org.junit.Test)3 EntityManager (jakarta.persistence.EntityManager)2 TypedQuery (jakarta.persistence.TypedQuery)2 ArrayList (java.util.ArrayList)2 ClassExtractor (org.eclipse.persistence.descriptors.ClassExtractor)2 QueryStringInteraction (org.eclipse.persistence.eis.interactions.QueryStringInteraction)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 UnmarshalRecord (org.eclipse.persistence.oxm.record.UnmarshalRecord)2 Session (org.eclipse.persistence.sessions.Session)2 Order (org.eclipse.persistence.testing.models.jpa.mongo.Order)2 Attributes (org.xml.sax.Attributes)2 BigDecimal (java.math.BigDecimal)1 XMLInteraction (org.eclipse.persistence.eis.interactions.XMLInteraction)1 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)1 CollectionResult (org.eclipse.persistence.internal.xr.CollectionResult)1