Search in sources :

Example 1 with ConstructorTypeColumn

use of org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn 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.getDatastoreMapping(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.getDatastoreMappings().length && !found; l++) {
                            Column df = mapping.getDatastoreMapping(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(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.replaceFields(resultFieldNumbers, rsGetter, false);
                    }

                    public void fetchNonLoadedFields(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.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(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.replaceFields(resultFieldNumbers, rsGetter, false);
                    }

                    public void fetchNonLoadedFields(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.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) ObjectProvider(org.datanucleus.state.ObjectProvider) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FieldValues(org.datanucleus.store.FieldValues) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

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 PersistentTypeMapping (org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping)1 ObjectProvider (org.datanucleus.state.ObjectProvider)1 FieldValues (org.datanucleus.store.FieldValues)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 ResultSetGetter (org.datanucleus.store.rdbms.fieldmanager.ResultSetGetter)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 Column (org.datanucleus.store.rdbms.table.Column)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1