Search in sources :

Example 51 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class NijiojiNiTestSet method runQuery.

@SuppressWarnings("unchecked")
@Test
public void runQuery() {
    DatabaseSession s = project.createDatabaseSession();
    s.dontLogMessages();
    s.login();
    Object o = null;
    Vector queryArgs = new NonSynchronizedVector();
    queryArgs.add(105);
    queryArgs.add(106);
    queryArgs.add("BLAHZOO");
    queryArgs.add(0);
    boolean worked = false;
    String msg = null;
    try {
        o = s.executeQuery("NijiojiNi", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertTrue("invocation NijiojiNi failed: " + msg, worked);
    Vector results = (Vector) o;
    DatabaseRecord record = (DatabaseRecord) results.get(0);
    BigDecimal y = (BigDecimal) record.get("Y");
    assertTrue("wrong y value", y.intValue() == 155);
    s.logout();
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 52 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class NojijioNoTestSet method runQuery.

@SuppressWarnings("unchecked")
@Test
public void runQuery() {
    DatabaseSession s = project.createDatabaseSession();
    s.dontLogMessages();
    s.login();
    Object o = null;
    Vector queryArgs = new NonSynchronizedVector();
    queryArgs.add(104);
    queryArgs.add("FOO");
    boolean worked = false;
    String msg = null;
    try {
        o = s.executeQuery("NojijioNo", Empty.class, queryArgs);
        worked = true;
    } catch (Exception e) {
        msg = e.getMessage();
    }
    assertTrue("invocation NojijioNo failed: " + msg, worked);
    Vector results = (Vector) o;
    DatabaseRecord record = (DatabaseRecord) results.get(0);
    BigDecimal x = (BigDecimal) record.get("X");
    assertTrue("wrong x value", x.intValue() == 48);
    String z = (String) record.get("Z");
    assertTrue("wrong z value", z.equals("FOOBAR"));
    s.logout();
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 53 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class QueryOperation method createSimpleXMLFormat.

@SuppressWarnings({ "unchecked" })
public Object createSimpleXMLFormat(XRServiceAdapter xrService, Object value) {
    XMLRoot xmlRoot = new XMLRoot();
    SimpleXMLFormat simpleXMLFormat = result.getSimpleXMLFormat();
    String tempSimpleXMLFormatTag = SimpleXMLFormat.DEFAULT_SIMPLE_XML_FORMAT_TAG;
    String simpleXMLFormatTag = simpleXMLFormat.getSimpleXMLFormatTag();
    if (simpleXMLFormatTag != null && !EMPTY_STR.equals(simpleXMLFormatTag)) {
        tempSimpleXMLFormatTag = simpleXMLFormatTag;
    }
    xmlRoot.setLocalName(tempSimpleXMLFormatTag);
    String tempXMLTag = DEFAULT_SIMPLE_XML_TAG;
    String xmlTag = simpleXMLFormat.getXMLTag();
    if (xmlTag != null && !EMPTY_STR.equals(xmlTag)) {
        tempXMLTag = xmlTag;
    }
    Vector<DatabaseRecord> records = null;
    if (value instanceof ArrayList) {
        // JPA query results in a list of raw values
        // Here we have raw values returned as opposed to DatabaseRecords - this means
        // we need to figure out the tag names based on the call's output parameters.
        // assumes JPAQuery
        JPAQuery jpaQuery = (JPAQuery) queryHandler.getDatabaseQuery();
        // to match field names with results, we need to gather the database fields from each of the Output parameters
        List<DatabaseField> paramFlds = new ArrayList<>();
        DatasourceCall dsCall = (DatasourceCall) jpaQuery.getDatabaseQuery().getDatasourceCall();
        for (Object obj : dsCall.getParameters()) {
            if (obj instanceof OutputParameterForCallableStatement) {
                paramFlds.add(((OutputParameterForCallableStatement) obj).getOutputField());
            } else if (obj instanceof Object[]) {
                Object[] objArray = (Object[]) obj;
                for (int i = 0; i < objArray.length; i++) {
                    Object o = objArray[i];
                    if (o instanceof OutputParameterForCallableStatement) {
                        paramFlds.add(((OutputParameterForCallableStatement) o).getOutputField());
                    }
                }
            }
        }
        // now create a record using DatabaseField/value pairs
        DatabaseRecord dr = new DatabaseRecord();
        if (paramFlds.size() > 0) {
            for (int i = 0; i < ((ArrayList<?>) value).size(); i++) {
                dr.add(paramFlds.get(i), ((ArrayList<?>) value).get(i));
            }
        } else {
            dr.add(new DatabaseField(RESULT_STR), ((ArrayList<?>) value).get(0));
        }
        records = new Vector<>();
        records.add(dr);
    } else if (value instanceof Vector) {
        Class<?> vectorContent = ((Vector<?>) value).firstElement().getClass();
        if (DatabaseRecord.class.isAssignableFrom(vectorContent)) {
            records = (Vector<DatabaseRecord>) value;
        } else {
            records = new Vector<>();
            DatabaseRecord dr = new DatabaseRecord();
            dr.add(new DatabaseField(RESULT_STR), ((Vector<?>) value).firstElement());
            records.add(dr);
        }
    } else {
        records = new Vector<>();
        DatabaseRecord dr = new DatabaseRecord();
        dr.add(new DatabaseField(RESULT_STR), value);
        records.add(dr);
    }
    SimpleXMLFormatModel simpleXMLFormatModel = new SimpleXMLFormatModel();
    XMLConversionManager conversionManager = (XMLConversionManager) xrService.getOXSession().getDatasourcePlatform().getConversionManager();
    SessionLog log = xrService.getOXSession().getSessionLog();
    for (DatabaseRecord dr : records) {
        Element rowElement = TEMP_DOC.createElement(tempXMLTag);
        for (DatabaseField field : dr.getFields()) {
            // handle complex types, i.e. ones we have a descriptor for
            if (field instanceof ObjectRelationalDatabaseField) {
                ObjectRelationalDatabaseField ordtField = (ObjectRelationalDatabaseField) field;
                if (xrService.getOXSession().getDescriptor(ordtField.getType()) != null) {
                    xrService.getXMLContext().createMarshaller().marshal(dr.get(field), rowElement);
                    continue;
                }
            }
            Object fieldValue = dr.get(field);
            if (fieldValue != null) {
                if (fieldValue instanceof Calendar) {
                    Calendar cValue = (Calendar) fieldValue;
                    fieldValue = conversionManager.convertObject(cValue, STRING, DATE_TIME_QNAME);
                }
                if (fieldValue instanceof Date) {
                    Date dValue = (Date) fieldValue;
                    fieldValue = conversionManager.convertObject(dValue, STRING, DATE_QNAME);
                } else if (fieldValue instanceof Time) {
                    Time tValue = (Time) fieldValue;
                    fieldValue = conversionManager.convertObject(tValue, STRING, TIME_QNAME);
                } else if (fieldValue instanceof Timestamp) {
                    Timestamp tsValue = (Timestamp) fieldValue;
                    fieldValue = conversionManager.convertObject(tsValue, STRING, DATE_TIME_QNAME);
                } else if (fieldValue instanceof Blob) {
                    fieldValue = conversionManager.convertObject(fieldValue, ClassConstants.APBYTE);
                } else if (SQLXML.class.isAssignableFrom(fieldValue.getClass())) {
                    // handle XMLType case where an oracle.jdbc.driver.OracleSQLXML instance was returned
                    SQLXML sqlXml = (SQLXML) fieldValue;
                    try {
                        String str = sqlXml.getString();
                        sqlXml.free();
                        // Oracle 12c appends a \n character to the xml string
                        fieldValue = str.endsWith("\n") ? str.substring(0, str.length() - 1) : str;
                    } catch (SQLException e) {
                        log.logThrowable(SessionLog.FINE, SessionLog.DBWS, e);
                    }
                } else if (fieldValue.getClass().getName().equalsIgnoreCase(ORACLEOPAQUE_STR)) {
                    // handle XMLType case where an oracle.sql.OPAQUE instance was returned
                    try {
                        Class<?> oracleOPAQUE;
                        Class<?> xmlTypeFactoryClass;
                        Constructor<?> xmlTypeFactoryConstructor;
                        Object xmlTypeFactory;
                        Method getStringMethod;
                        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                            oracleOPAQUE = AccessController.doPrivileged(new PrivilegedClassForName<>(IORACLEOPAQUE_STR, true, this.getClass().getClassLoader()));
                            xmlTypeFactoryClass = AccessController.doPrivileged(new PrivilegedClassForName<>(XMLTYPEFACTORY_STR, true, this.getClass().getClassLoader()));
                            xmlTypeFactoryConstructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(xmlTypeFactoryClass, new Class<?>[0], true));
                            xmlTypeFactory = AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(xmlTypeFactoryConstructor, new Object[0]));
                            getStringMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(xmlTypeFactoryClass, GETSTRING_METHOD, new Class<?>[] { oracleOPAQUE }));
                            fieldValue = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(getStringMethod, xmlTypeFactory, new Object[] { fieldValue }));
                        } else {
                            oracleOPAQUE = PrivilegedAccessHelper.getClassForName(IORACLEOPAQUE_STR, false, this.getClass().getClassLoader());
                            xmlTypeFactoryClass = PrivilegedAccessHelper.getClassForName(XMLTYPEFACTORY_STR, true, this.getClass().getClassLoader());
                            xmlTypeFactoryConstructor = PrivilegedAccessHelper.getConstructorFor(xmlTypeFactoryClass, new Class<?>[0], true);
                            xmlTypeFactory = PrivilegedAccessHelper.invokeConstructor(xmlTypeFactoryConstructor, new Object[0]);
                            getStringMethod = PrivilegedAccessHelper.getDeclaredMethod(xmlTypeFactoryClass, GETSTRING_METHOD, new Class<?>[] { oracleOPAQUE });
                            fieldValue = PrivilegedAccessHelper.invokeMethod(getStringMethod, xmlTypeFactory, new Object[] { fieldValue });
                        }
                    } catch (ReflectiveOperationException | PrivilegedActionException e) {
                        // if the required resources are not available there's nothing we can do...
                        log.logThrowable(SessionLog.FINE, SessionLog.DBWS, e);
                    }
                }
                String elementName;
                if (field.getName() == null || (elementName = sqlToXmlName(field.getName())).equals(EMPTY_STR)) {
                    // return arg from stored function has no name
                    elementName = RESULT_STR;
                }
                Element columnElement = TEMP_DOC.createElement(elementName);
                rowElement.appendChild(columnElement);
                String fieldValueString = fieldValue.toString();
                // handle binary content - attachments dealt with in invoke() above
                if (result.getType().equals(BASE_64_BINARY_QNAME)) {
                    fieldValueString = Helper.buildHexStringFromBytes(Base64.base64Encode((byte[]) fieldValue));
                    columnElement.setAttributeNS(XMLNS_URL, XSD_STR, SCHEMA_URL);
                    columnElement.setAttributeNS(XMLNS_URL, XSI_STR, SCHEMA_INSTANCE_URL);
                    columnElement.setAttributeNS(SCHEMA_INSTANCE_URL, XSITYPE_STR, BASE64_BINARY_STR);
                }
                columnElement.appendChild(TEMP_DOC.createTextNode(fieldValueString));
            }
        }
        simpleXMLFormatModel.simpleXML.add(rowElement);
    }
    xmlRoot.setObject(simpleXMLFormatModel);
    return xmlRoot;
}
Also used : SQLException(java.sql.SQLException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Time(java.sql.Time) SimpleXMLFormat(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormat) DatasourceCall(org.eclipse.persistence.internal.databaseaccess.DatasourceCall) Timestamp(java.sql.Timestamp) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) Vector(java.util.Vector) ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) Blob(java.sql.Blob) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) PrivilegedInvokeConstructor(org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor) Constructor(java.lang.reflect.Constructor) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) JPAQuery(org.eclipse.persistence.internal.jpa.JPAQuery) SimpleXMLFormatModel(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatModel) Date(java.sql.Date) SessionLog(org.eclipse.persistence.logging.SessionLog) SQLXML(java.sql.SQLXML) OutputParameterForCallableStatement(org.eclipse.persistence.internal.databaseaccess.OutputParameterForCallableStatement) ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 54 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class PLSQLTestModel method getRecordTestSuite.

public static TestSuite getRecordTestSuite() {
    TestSuite suite = new TestSuite();
    suite.setName("PLSQLRecordTestSuite");
    suite.setDescription("This suite tests calling PLSQL procedures that take records.");
    Address address = new Address();
    address.setId(new BigDecimal(123));
    address.setNumber(17);
    address.setStreet("Bank");
    address.setCity("Ottawa");
    address.setState("ON");
    Address resultAddress = new Address();
    resultAddress.setId(new BigDecimal(1234));
    resultAddress.setNumber(17);
    resultAddress.setStreet("Bank");
    resultAddress.setCity("Ottawa");
    resultAddress.setState("ON");
    Phone phone = new Phone();
    phone.setAreaCode("613");
    phone.setNumber("7927711");
    Employee employee = new Employee();
    employee.setId(new BigDecimal(123));
    employee.setName("Bob Jones");
    employee.setAddress(address);
    employee.getPhones().add(phone);
    List args = new ArrayList();
    args.add(address);
    DatabaseRecord result = new DatabaseRecord();
    result.put("P_ADDRESS", resultAddress);
    PLSQLTest test = new PLSQLTest("AddressInOutObject", Address.class, args, result);
    test.setName("AddressInOutObjectTest");
    suite.addTest(test);
    args = new ArrayList();
    args.add(address.getId());
    args.add(address.getNumber());
    args.add(address.getStreet());
    args.add(address.getCity());
    args.add(address.getState());
    args.add("Local");
    result = new DatabaseRecord();
    result.put("ADDRESS_ID", resultAddress.getId());
    result.put("ADDRESS_NUM", resultAddress.getNumber());
    result.put("STREET", resultAddress.getStreet());
    result.put("CITY", resultAddress.getCity());
    result.put("STATE", resultAddress.getState());
    result.put("P_LOCAL", "Nepean");
    test = new PLSQLTest("AddressInOutData", Address.class, args);
    test.setName("AddressInOutDataTest");
    suite.addTest(test);
    args = new ArrayList();
    args.add(address.getId());
    args.add(address.getNumber());
    args.add(address.getStreet());
    args.add(address.getCity());
    args.add(address.getState());
    args.add("Local");
    test = new PLSQLTest("AddressInData", Address.class, args);
    test.setName("AddressInDataTest");
    suite.addTest(test);
    args = new ArrayList();
    args.add(address);
    test = new PLSQLTest("AddressInObject", Address.class, args);
    test.setName("AddressInObjectTest");
    suite.addTest(test);
    args = new ArrayList();
    result = new DatabaseRecord();
    result.put("P_ADDRESS", resultAddress);
    test = new PLSQLTest("AddressOutObject", Address.class, args, result);
    test.setName("AddressOutTest");
    suite.addTest(test);
    args = new ArrayList();
    result = new DatabaseRecord();
    result.put("ADDRESS_ID", resultAddress.getId());
    result.put("STREET_NUM", resultAddress.getNumber());
    result.put("STREET", resultAddress.getStreet());
    result.put("CITY", resultAddress.getCity());
    result.put("STATE", resultAddress.getState());
    result.put("P_LOCAL", "Local");
    test = new PLSQLTest("AddressOutData", Address.class, args, result);
    test.setName("AddressOutDataTest");
    suite.addTest(test);
    args = new ArrayList();
    args.add(employee);
    args.add("Nepean");
    test = new PLSQLTest("EmployeeInObject", Employee.class, args);
    test.setName("EmployeeInObjectTest");
    suite.addTest(test);
    args = new ArrayList();
    result = new DatabaseRecord();
    result.put("P_EMP", new Employee());
    result.put("P_CITY", "Nepean");
    test = new PLSQLTest("EmployeeOutObject", Employee.class, args, result);
    test.setName("EmployeeOutTest");
    suite.addTest(test);
    args = new ArrayList();
    result = new DatabaseRecord();
    result.put("P_EMP", employee);
    result.put("P_CITY", "Nepean");
    args.add(employee);
    args.add("Nepean");
    test = new PLSQLTest("EmployeeInOutObject", Employee.class, args, result);
    test.setName("EmployeeInOutObjectTest");
    suite.addTest(test);
    test = new PLSQLTest("AddressOutCursor", Address.class, new ArrayList(), new Vector());
    test.setName("AddressOutCursorTest");
    suite.addTest(test);
    test = new PLSQLTest("AddressRecOutCursor", Address.class, new ArrayList(), new Vector());
    test.setName("AddressRecOutCursorTest");
    suite.addTest(test);
    return suite;
}
Also used : Employee(org.eclipse.persistence.testing.models.plsql.Employee) Address(org.eclipse.persistence.testing.models.plsql.Address) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) Phone(org.eclipse.persistence.testing.models.plsql.Phone) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Vector(java.util.Vector) BigDecimal(java.math.BigDecimal)

Example 55 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class PLSQLTestModel method getErrorTestSuite.

public static TestSuite getErrorTestSuite() {
    TestSuite suite = new TestSuite();
    suite.setName("PLSQLErrorTestSuite");
    suite.setDescription("This suite tests calling PLSQL procedures with invalid arguments.");
    List args = new ArrayList();
    PLSQLTest test = new PLSQLTest("BadAddressOut", Address.class, args, QueryException.typeNameNotSet(null));
    test.setName("BadAddressOutTest");
    suite.addTest(test);
    args = new ArrayList();
    test = new PLSQLTest("MissingTypeAddressListOut", Address.class, args, QueryException.compatibleTypeNotSet(null));
    test.setName("MissingTypeAddressListOutTest");
    suite.addTest(test);
    Employee employee = new Employee();
    employee.setId(new BigDecimal(123));
    employee.setName("Bad Jones");
    args = new ArrayList();
    DatabaseRecord result = new DatabaseRecord();
    result.put("P_EMP", employee);
    result.put("P_CITY", "Nepean");
    args.add(employee);
    args.add("Nepean");
    test = new PLSQLTest("EmployeeInOutObject", Employee.class, args, result);
    test.setName("EmptyEmployeeInOutObjectTest");
    return suite;
}
Also used : Employee(org.eclipse.persistence.testing.models.plsql.Employee) Address(org.eclipse.persistence.testing.models.plsql.Address) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal)

Aggregations

DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)110 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)44 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)43 Vector (java.util.Vector)31 ArrayList (java.util.ArrayList)17 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)17 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Test (org.junit.Test)14 Expression (org.eclipse.persistence.expressions.Expression)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 HashMap (java.util.HashMap)11 Map (java.util.Map)10 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)7 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)6 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)6 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)6