Search in sources :

Example 6 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.

the class IndexFunction method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression ignore, List args) {
    if (ignore == null) {
        if (args == null || args.size() != 2) {
            throw new NucleusException("INDEX can only be used with 2 arguments - the element expression, and the collection expression");
        }
        SQLExpression elemSqlExpr = (SQLExpression) args.get(0);
        SQLExpression collSqlExpr = (SQLExpression) args.get(1);
        AbstractMemberMetaData mmd = collSqlExpr.getJavaTypeMapping().getMemberMetaData();
        if (!mmd.hasCollection()) {
            throw new NucleusException("INDEX expression for field " + mmd.getFullFieldName() + " does not represent a collection!");
        } else if (!mmd.getOrderMetaData().isIndexedList()) {
            throw new NucleusException("INDEX expression for field " + mmd.getFullFieldName() + " does not represent an indexed list!");
        }
        JavaTypeMapping orderMapping = null;
        SQLTable orderTable = null;
        Table joinTbl = stmt.getRDBMSManager().getTable(mmd);
        if (joinTbl != null) {
            // 1-N via join table
            CollectionTable collTable = (CollectionTable) joinTbl;
            orderTable = stmt.getTableForDatastoreContainer(collTable);
            // TODO If the join table is not yet referenced, or referenced multiple times then fix this
            orderMapping = collTable.getOrderMapping();
        } else {
            // 1-N via FK
            orderTable = elemSqlExpr.getSQLTable();
            orderMapping = ((ClassTable) elemSqlExpr.getSQLTable().getTable()).getExternalMapping(mmd, MappingType.EXTERNAL_INDEX);
        }
        return new NumericExpression(stmt, orderTable, orderMapping);
    }
    throw new NucleusException(Localiser.msg("060002", "INDEX", ignore));
}
Also used : CollectionTable(org.datanucleus.store.rdbms.table.CollectionTable) Table(org.datanucleus.store.rdbms.table.Table) ClassTable(org.datanucleus.store.rdbms.table.ClassTable) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) CollectionTable(org.datanucleus.store.rdbms.table.CollectionTable) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 7 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.

the class BaseDatastoreAdapter method loadDatastoreMappings.

/**
 * Load all datastore mappings defined in the associated plugins via the plugin mechanism.
 * All individual DatastoreAdapters should load up their own built-in support.
 * @param mgr the PluginManager
 * @param clr the ClassLoaderResolver
 */
protected void loadDatastoreMappings(PluginManager mgr, ClassLoaderResolver clr) {
    // Load from plugin mechanism
    ConfigurationElement[] elems = mgr.getConfigurationElementsForExtension("org.datanucleus.store.rdbms.datastore_mapping", null, null);
    if (elems != null) {
        for (ConfigurationElement elem : elems) {
            String javaName = elem.getAttribute("java-type").trim();
            String rdbmsMappingClassName = elem.getAttribute("rdbms-mapping-class");
            String jdbcType = elem.getAttribute("jdbc-type");
            String sqlType = elem.getAttribute("sql-type");
            String defaultJava = elem.getAttribute("default");
            boolean defaultForJavaType = false;
            if (defaultJava != null) {
                if (defaultJava.equalsIgnoreCase("true")) {
                    defaultForJavaType = Boolean.TRUE.booleanValue();
                }
            }
            Class mappingType = null;
            if (!StringUtils.isWhitespace(rdbmsMappingClassName)) {
                try {
                    mappingType = mgr.loadClass(elem.getExtension().getPlugin().getSymbolicName(), rdbmsMappingClassName);
                } catch (NucleusException ne) {
                    NucleusLogger.DATASTORE.error(Localiser.msg("041013", rdbmsMappingClassName));
                }
                Set includes = new HashSet();
                Set excludes = new HashSet();
                for (ConfigurationElement childElem : elem.getChildren()) {
                    if (childElem.getName().equals("includes")) {
                        includes.add(childElem.getAttribute("vendor-id"));
                    } else if (childElem.getName().equals("excludes")) {
                        excludes.add(childElem.getAttribute("vendor-id"));
                    }
                }
                if (!excludes.contains(getVendorID())) {
                    if (includes.isEmpty() || includes.contains(getVendorID())) {
                        registerDatastoreMapping(javaName, mappingType, jdbcType, sqlType, defaultForJavaType);
                    }
                }
            }
        }
    }
}
Also used : ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) ResultSet(java.sql.ResultSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) NucleusException(org.datanucleus.exceptions.NucleusException) HashSet(java.util.HashSet)

Example 8 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.

the class EmbeddedMapping method setObject.

/**
 * Mutator for the embedded object in the datastore.
 * @param ec ExecutionContext
 * @param ps The Prepared Statement
 * @param value The embedded object to use
 * @param ownerSM StateManager of the owning object containing this embedded object
 * @param ownerFieldNumber Field number in the owning object where this is stored
 * @param param Param numbers in the PreparedStatement for the fields of this object
 */
@Override
public void setObject(ExecutionContext ec, PreparedStatement ps, int[] param, Object value, DNStateManager ownerSM, int ownerFieldNumber) {
    if (value == null) {
        String nullColumn = (emd != null) ? emd.getNullIndicatorColumn() : null;
        String nullValue = (emd != null) ? emd.getNullIndicatorValue() : null;
        int n = 0;
        if (discrimMapping != null) {
            discrimMapping.setObject(ec, ps, new int[] { param[n] }, null);
            n++;
        }
        int numJavaMappings = javaTypeMappings.size();
        for (int i = 0; i < numJavaMappings; i++) {
            JavaTypeMapping mapping = javaTypeMappings.get(i);
            int[] posMapping = new int[mapping.getNumberOfColumnMappings()];
            for (int j = 0; j < posMapping.length; j++) {
                posMapping[j] = param[n++];
            }
            // in which case apply the required value
            if (nullColumn != null && nullValue != null && mapping.getMemberMetaData().getColumnMetaData().length > 0 && mapping.getMemberMetaData().getColumnMetaData()[0].getName().equals(nullColumn)) {
                // Try to cater for user having an integer based column and value
                if (mapping instanceof IntegerMapping || mapping instanceof BigIntegerMapping || mapping instanceof LongMapping || mapping instanceof ShortMapping) {
                    Object convertedValue = null;
                    try {
                        if (mapping instanceof IntegerMapping || mapping instanceof ShortMapping) {
                            convertedValue = Integer.valueOf(nullValue);
                        } else if (mapping instanceof LongMapping || mapping instanceof BigIntegerMapping) {
                            convertedValue = Long.valueOf(nullValue);
                        }
                    } catch (Exception e) {
                    }
                    mapping.setObject(ec, ps, posMapping, convertedValue);
                } else {
                    mapping.setObject(ec, ps, posMapping, nullValue);
                }
            } else {
                if (mapping.getNumberOfColumnMappings() > 0) {
                    mapping.setObject(ec, ps, posMapping, null);
                }
            }
        }
    } else {
        ApiAdapter api = ec.getApiAdapter();
        if (!api.isPersistable(value)) {
            throw new NucleusException(Localiser.msg("041016", value.getClass(), value)).setFatal();
        }
        AbstractClassMetaData embCmd = ec.getMetaDataManager().getMetaDataForClass(value.getClass(), ec.getClassLoaderResolver());
        DNStateManager embSM = ec.findStateManager(value);
        if (embSM == null || api.getExecutionContext(value) == null) {
            // Assign a StateManager to manage our embedded object
            embSM = ec.getNucleusContext().getStateManagerFactory().newForEmbedded(ec, value, false, ownerSM, ownerFieldNumber, objectType);
        }
        int n = 0;
        if (discrimMapping != null) {
            if (discrimMetaData.getStrategy() != DiscriminatorStrategy.NONE) {
                discrimMapping.setObject(ec, ps, new int[] { param[n] }, embCmd.getDiscriminatorValue());
            }
            n++;
        }
        int numJavaMappings = javaTypeMappings.size();
        for (int i = 0; i < numJavaMappings; i++) {
            JavaTypeMapping mapping = javaTypeMappings.get(i);
            int[] posMapping = new int[mapping.getNumberOfColumnMappings()];
            for (int j = 0; j < posMapping.length; j++) {
                posMapping[j] = param[n++];
            }
            // Retrieve value of member from Embedded StateManager
            int embAbsFieldNum = embCmd.getAbsolutePositionOfMember(mapping.getMemberMetaData().getName());
            if (embAbsFieldNum >= 0) {
                // Member is present in this embedded type
                Object fieldValue = embSM.provideField(embAbsFieldNum);
                if (mapping instanceof EmbeddedPCMapping) {
                    mapping.setObject(ec, ps, posMapping, fieldValue, embSM, embAbsFieldNum);
                } else {
                    if (mapping.getNumberOfColumnMappings() > 0) {
                        mapping.setObject(ec, ps, posMapping, fieldValue);
                    }
                }
            } else {
                mapping.setObject(ec, ps, posMapping, null);
            }
        }
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) DNStateManager(org.datanucleus.state.DNStateManager) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 9 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.

the class MultiMapping method getColumnMapping.

/**
 * Accessor for a datastore mapping.
 * @param index The position of the mapping to return
 * @return The datastore mapping
 */
public ColumnMapping getColumnMapping(int index) {
    if (index >= getNumberOfColumnMappings()) {
        throw new NucleusException("Attempt to get ColumnMapping with index " + index + " when total number of mappings is " + numberOfColumnMappings + " for field=" + mmd).setFatal();
    }
    int currentIndex = 0;
    int numberJavaMappings = javaTypeMappings.length;
    for (int i = 0; i < numberJavaMappings; i++) {
        int numberColumnMappings = javaTypeMappings[i].getNumberOfColumnMappings();
        for (int j = 0; j < numberColumnMappings; j++) {
            if (currentIndex == index) {
                return javaTypeMappings[i].getColumnMapping(j);
            }
            currentIndex++;
        }
    }
    // TODO Should never happen
    throw new NucleusException("Invalid index " + index + " for ColumnMapping (numColumns=" + getNumberOfColumnMappings() + "), for field=" + mmd).setFatal();
}
Also used : NucleusException(org.datanucleus.exceptions.NucleusException)

Example 10 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.

the class StringConcat2Method method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    if (args == null || args.size() != 1) {
        throw new NucleusException(Localiser.msg("060003", "concat", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    SQLExpression otherExpr = args.get(0);
    if (!(otherExpr instanceof StringExpression) && !(otherExpr instanceof CharacterExpression) && !(otherExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "concat", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(expr);
    funcArgs.add(otherExpr);
    return new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class), "CONCAT", funcArgs);
}
Also used : ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)326 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)71 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)67 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)62 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)53 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)52 ArrayList (java.util.ArrayList)48 Literal (org.datanucleus.query.expression.Literal)47 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)44 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)43 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)40 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)37 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)36 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)35 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)35 Expression (org.datanucleus.query.expression.Expression)32 HashMap (java.util.HashMap)31 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)31 VariableExpression (org.datanucleus.query.expression.VariableExpression)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)26