Search in sources :

Example 1 with PersistentTypeMapping

use of org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping in project tests by datanucleus.

the class AnnotationTest method testSqlResultSetMapping.

/**
 * Test of JPA @SqlResultSetMapping
 */
public void testSqlResultSetMapping() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    QueryResultMetaData[] queryResultMappings = cmd.getQueryResultMetaData();
    assertNotNull("LoginAccount has no QueryResultMetaData!", queryResultMappings);
    assertEquals("LoginAccount has incorrect number of query result mappings", 4, queryResultMappings.length);
    // Example 1 : Returning 2 entities
    QueryResultMetaData qrmd = null;
    for (int i = 0; i < queryResultMappings.length; i++) {
        QueryResultMetaData md = queryResultMappings[i];
        if (md.getName().equals("AN_LOGIN_PLUS_ACCOUNT")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping AN_LOGIN_PLUS_ACCOUNT is not present!");
    }
    String[] scalarCols = qrmd.getScalarColumns();
    assertNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
    PersistentTypeMapping[] sqlMappingEntities = qrmd.getPersistentTypeMappings();
    assertNotNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
    assertEquals("LoginAccount sql mapping has incorrect number of entities", 2, sqlMappingEntities.length);
    // LoginAccount
    assertEquals("LoginAccount sql mapping entity 0 has incorrect class", LoginAccount.class.getName(), sqlMappingEntities[0].getClassName());
    assertNull("LoginAccount sql mapping entity 0 has incorrect discriminator", sqlMappingEntities[0].getDiscriminatorColumn());
    // Login
    assertEquals("LoginAccount sql mapping entity 1 has incorrect class", Login.class.getName(), sqlMappingEntities[1].getClassName());
    assertNull("LoginAccount sql mapping entity 1 has incorrect discriminator", sqlMappingEntities[1].getDiscriminatorColumn());
    // Example 2 : Returning 2 scalars
    qrmd = null;
    for (int i = 0; i < queryResultMappings.length; i++) {
        QueryResultMetaData md = queryResultMappings[i];
        if (md.getName().equals("AN_ACCOUNT_NAMES")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping AN_ACCOUNT_NAMES is not present!");
    }
    scalarCols = qrmd.getScalarColumns();
    assertNotNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
    assertEquals("LoginAccount sql mapping has incorrect column name", "FIRSTNAME", scalarCols[0]);
    assertEquals("LoginAccount sql mapping has incorrect column name", "LASTNAME", scalarCols[1]);
    sqlMappingEntities = qrmd.getPersistentTypeMappings();
    assertNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) Login(org.datanucleus.samples.annotations.one_one.unidir.Login) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) LoginAccount(org.datanucleus.samples.annotations.one_one.unidir.LoginAccount) PersistentTypeMapping(org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping) ClassMetaData(org.datanucleus.metadata.ClassMetaData) QueryResultMetaData(org.datanucleus.metadata.QueryResultMetaData)

Example 2 with PersistentTypeMapping

use of org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping in project tests by datanucleus.

the class XMLTest method testSqlResultSetMapping.

/**
 * Test of JPA <sql-result-set-mapping>
 */
public void testSqlResultSetMapping() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    QueryResultMetaData[] queryResultMappings = cmd.getQueryResultMetaData();
    assertNotNull("LoginAccount has no QueryResultMetaData!", queryResultMappings);
    assertEquals("LoginAccount has incorrect number of query result mappings", 2, queryResultMappings.length);
    // Example 1 : Returning 2 entities
    QueryResultMetaData qrmd = null;
    for (int i = 0; i < queryResultMappings.length; i++) {
        QueryResultMetaData md = queryResultMappings[i];
        if (md.getName().equals("MD_LOGIN_PLUS_ACCOUNT")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping MD_LOGIN_PLUS_ACCOUNT is not present!");
    }
    String[] scalarCols = qrmd.getScalarColumns();
    assertNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
    PersistentTypeMapping[] sqlMappingEntities = qrmd.getPersistentTypeMappings();
    assertNotNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
    assertEquals("LoginAccount sql mapping has incorrect number of entities", 2, sqlMappingEntities.length);
    // LoginAccount
    assertEquals("LoginAccount sql mapping entity 0 has incorrect class", LoginAccount.class.getName(), sqlMappingEntities[0].getClassName());
    assertNull("LoginAccount sql mapping entity 0 has incorrect discriminator", sqlMappingEntities[0].getDiscriminatorColumn());
    // Login
    assertEquals("LoginAccount sql mapping entity 1 has incorrect class", Login.class.getName(), sqlMappingEntities[1].getClassName());
    assertNull("LoginAccount sql mapping entity 1 has incorrect discriminator", sqlMappingEntities[1].getDiscriminatorColumn());
    // Example 2 : Returning 2 scalars
    qrmd = null;
    for (int i = 0; i < queryResultMappings.length; i++) {
        QueryResultMetaData md = queryResultMappings[i];
        if (md.getName().equals("MD_ACCOUNT_NAMES")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping MD_ACCOUNT_NAMES is not present!");
    }
    scalarCols = qrmd.getScalarColumns();
    assertNotNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
    assertEquals("LoginAccount sql mapping has incorrect column name", "FIRSTNAME", scalarCols[0]);
    assertEquals("LoginAccount sql mapping has incorrect column name", "LASTNAME", scalarCols[1]);
    sqlMappingEntities = qrmd.getPersistentTypeMappings();
    assertNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) Login(org.jpox.samples.one_one.unidir.Login) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) PersistentTypeMapping(org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping) ClassMetaData(org.datanucleus.metadata.ClassMetaData) QueryResultMetaData(org.datanucleus.metadata.QueryResultMetaData)

Example 3 with PersistentTypeMapping

use of org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping in project datanucleus-rdbms by datanucleus.

the class ResultMetaDataROF method getObject.

/**
 * Accessor for the object(s) from the current row of the ResultSet.
 * @return The object(s) for this row of the ResultSet.
 */
public Object getObject() {
    List returnObjects = new ArrayList();
    // A). Process persistent types
    PersistentTypeMapping[] persistentTypes = queryResultMetaData.getPersistentTypeMappings();
    if (persistentTypes != null) {
        if (persistentTypeResultSetGetters == null) {
            persistentTypeResultSetGetters = new ResultSetGetter[persistentTypes.length];
        }
        int startColumnIndex = 0;
        for (int i = 0; i < persistentTypes.length; i++) {
            Set<String> columnsInThisType = new HashSet<>();
            AbstractMemberMetaData[] mmds = new AbstractMemberMetaData[columnNames.length];
            Map<String, AbstractMemberMetaData> fieldColumns = new HashMap<>();
            DatastoreClass dc = ((RDBMSStoreManager) ec.getStoreManager()).getDatastoreClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
            AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
            Object id = null;
            // and two columns with similar names e.g "Col1" and "col1". Until that situation comes up we ignore it :-)
            for (int j = startColumnIndex; j < columnNames.length; j++) {
                if (columnsInThisType.contains(columnNames[j])) {
                    // already added this column, so must be another persistent type
                    startColumnIndex = j;
                    break;
                }
                boolean found = false;
                if (acmd.getIdentityType() == IdentityType.DATASTORE) {
                    JavaTypeMapping datastoreIdMapping = dc.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false);
                    Column df = datastoreIdMapping.getColumnMapping(0).getColumn();
                    if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
                        // add +1 because result sets in jdbc starts with 1
                        int datastoreIdentityExpressionIndex = j + 1;
                        id = datastoreIdMapping.getObject(ec, rs, new int[] { datastoreIdentityExpressionIndex });
                        found = true;
                    }
                }
                for (int k = 0; k < acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers() && !found; k++) {
                    AbstractMemberMetaData apmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(k);
                    if (persistentTypes[i].getColumnForField(apmd.getName()) != null) {
                        if (persistentTypes[i].getColumnForField(apmd.getName()).equalsIgnoreCase(columnNames[j])) {
                            fieldColumns.put(columnNames[j], apmd);
                            columnsInThisType.add(columnNames[j]);
                            mmds[j] = apmd;
                            found = true;
                        }
                    } else {
                        JavaTypeMapping mapping = dc.getMemberMapping(apmd);
                        for (int l = 0; l < mapping.getColumnMappings().length && !found; l++) {
                            Column df = mapping.getColumnMapping(l).getColumn();
                            if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
                                fieldColumns.put(columnNames[j], apmd);
                                columnsInThisType.add(columnNames[j]);
                                mmds[j] = apmd;
                                found = true;
                            }
                        }
                    }
                }
                if (!columnsInThisType.contains(columnNames[j])) {
                    // column not found in this type, so must be another persistent type
                    startColumnIndex = j;
                    break;
                }
            }
            // Build fields and mappings in the results
            StatementMappingIndex[] stmtMappings = new StatementMappingIndex[acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers()];
            Set<AbstractMemberMetaData> resultMmds = new HashSet<>();
            resultMmds.addAll(fieldColumns.values());
            int[] resultFieldNumbers = new int[resultMmds.size()];
            int j = 0;
            for (AbstractMemberMetaData apmd : resultMmds) {
                StatementMappingIndex stmtMapping = new StatementMappingIndex(dc.getMemberMapping(apmd));
                resultFieldNumbers[j] = apmd.getAbsoluteFieldNumber();
                List indexes = new ArrayList();
                for (int k = 0; k < mmds.length; k++) {
                    if (mmds[k] == apmd) {
                        indexes.add(Integer.valueOf(k));
                    }
                }
                int[] indxs = new int[indexes.size()];
                for (int k = 0; k < indxs.length; k++) {
                    // add +1 because result sets in JDBC starts with 1
                    indxs[k] = ((Integer) indexes.get(k)).intValue() + 1;
                }
                stmtMapping.setColumnPositions(indxs);
                stmtMappings[resultFieldNumbers[j]] = stmtMapping;
                j++;
            }
            Object obj = null;
            Class type = ec.getClassLoaderResolver().classForName(persistentTypes[i].getClassName());
            if (acmd.getIdentityType() == IdentityType.APPLICATION) {
                if (persistentTypeResultSetGetters[i] == null) {
                    final StatementClassMapping resultMappings = new StatementClassMapping();
                    for (int k = 0; k < resultFieldNumbers.length; k++) {
                        resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
                    }
                    persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
                }
                ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
                // TODO Make use of discriminator like in PersistentClassROF and set the pcClass in this?
                id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, acmd, type, false, rsGetter);
                obj = ec.findObject(id, new FieldValues() {

                    public void fetchFields(DNStateManager sm) {
                        rsGetter.setStateManager(sm);
                        sm.replaceFields(resultFieldNumbers, rsGetter, false);
                    }

                    public void fetchNonLoadedFields(DNStateManager sm) {
                        rsGetter.setStateManager(sm);
                        sm.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
                    }

                    public FetchPlan getFetchPlanForLoading() {
                        return null;
                    }
                }, type, ignoreCache, false);
            } else if (acmd.getIdentityType() == IdentityType.DATASTORE) {
                if (persistentTypeResultSetGetters[i] == null) {
                    final StatementClassMapping resultMappings = new StatementClassMapping();
                    for (int k = 0; k < resultFieldNumbers.length; k++) {
                        resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
                    }
                    persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
                }
                ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
                obj = ec.findObject(id, new FieldValues() {

                    public void fetchFields(DNStateManager sm) {
                        rsGetter.setStateManager(sm);
                        sm.replaceFields(resultFieldNumbers, rsGetter, false);
                    }

                    public void fetchNonLoadedFields(DNStateManager sm) {
                        rsGetter.setStateManager(sm);
                        sm.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
                    }

                    public FetchPlan getFetchPlanForLoading() {
                        return null;
                    }
                }, type, ignoreCache, false);
            } else {
                // TODO Handle non-durable
                NucleusLogger.QUERY.warn("We do not currently support non-durable objects in the results of this type of query.");
            }
            returnObjects.add(obj);
        }
    }
    // B). Process simple columns
    String[] columns = queryResultMetaData.getScalarColumns();
    if (columns != null) {
        for (int i = 0; i < columns.length; i++) {
            try {
                Object obj = rs.getObject(columns[i]);
                returnObjects.add(obj);
            } catch (SQLException sqe) {
                String msg = Localiser.msg("059027", sqe.getMessage());
                NucleusLogger.QUERY.error(msg);
                throw new NucleusUserException(msg, sqe);
            }
        }
    }
    // C). Process constructor type mappings
    ConstructorTypeMapping[] ctrTypeMappings = queryResultMetaData.getConstructorTypeMappings();
    if (ctrTypeMappings != null) {
        for (int i = 0; i < ctrTypeMappings.length; i++) {
            String ctrClassName = ctrTypeMappings[i].getClassName();
            Class ctrCls = ec.getClassLoaderResolver().classForName(ctrClassName);
            List<ConstructorTypeColumn> ctrColumns = ctrTypeMappings[i].getColumnsForConstructor();
            Class[] ctrArgTypes = null;
            Object[] ctrArgVals = null;
            if (ctrColumns != null && ctrColumns.size() > 0) {
                int j = 0;
                ctrArgTypes = new Class[ctrColumns.size()];
                ctrArgVals = new Object[ctrColumns.size()];
                Iterator<ConstructorTypeColumn> colIter = ctrColumns.iterator();
                while (colIter.hasNext()) {
                    ConstructorTypeColumn ctrCol = colIter.next();
                    try {
                        Object colVal = rs.getObject(ctrCol.getColumnName());
                        ctrArgTypes[j] = colVal.getClass();
                        if (ctrCol.getJavaType() != null) {
                            // Attempt to convert to the type requested
                            ctrArgTypes[j] = ctrCol.getJavaType();
                            ctrArgVals[j] = TypeConversionHelper.convertTo(colVal, ctrArgTypes[j]);
                        } else {
                            ctrArgTypes[j] = colVal.getClass();
                            ctrArgVals[j] = colVal;
                        }
                    } catch (SQLException sqle) {
                    // TODO Handle this
                    }
                    j++;
                }
            }
            returnObjects.add(ClassUtils.newInstance(ctrCls, ctrArgTypes, ctrArgVals));
        }
    }
    if (returnObjects.size() == 0) {
        // No objects so user must have supplied incorrect MetaData
        return null;
    } else if (returnObjects.size() == 1) {
        // Return Object
        return returnObjects.get(0);
    } else {
        // Return Object[]
        return returnObjects.toArray(new Object[returnObjects.size()]);
    }
}
Also used : HashMap(java.util.HashMap) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) FetchPlan(org.datanucleus.FetchPlan) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ResultSetGetter(org.datanucleus.store.rdbms.fieldmanager.ResultSetGetter) ConstructorTypeColumn(org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn) Column(org.datanucleus.store.rdbms.table.Column) ArrayList(java.util.ArrayList) List(java.util.List) PersistentTypeMapping(org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping) HashSet(java.util.HashSet) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) ConstructorTypeColumn(org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn) ConstructorTypeMapping(org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) DNStateManager(org.datanucleus.state.DNStateManager) FieldValues(org.datanucleus.store.FieldValues) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

PersistentTypeMapping (org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusContext (org.datanucleus.NucleusContext)2 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)2 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)2 ClassMetaData (org.datanucleus.metadata.ClassMetaData)2 MetaDataManager (org.datanucleus.metadata.MetaDataManager)2 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)2 QueryResultMetaData (org.datanucleus.metadata.QueryResultMetaData)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 FetchPlan (org.datanucleus.FetchPlan)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ConstructorTypeColumn (org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn)1 ConstructorTypeMapping (org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeMapping)1