Search in sources :

Example 6 with NucleusUserException

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

the class ArrayRDBMSMapping method setObject.

public void setObject(PreparedStatement ps, int param, Object value) {
    try {
        if (value == null) {
            ps.setNull(param, getJDBCType());
        } else {
            Array array = null;
            if (value.getClass().isArray()) {
                // Convert the array into a java.sql.Array
                int numElems = java.lang.reflect.Array.getLength(value);
                Object[] elems = new Object[numElems];
                for (int i = 0; i < numElems; i++) {
                    elems[i] = java.lang.reflect.Array.get(value, i);
                }
                array = ps.getConnection().createArrayOf(arrayElemSqlType, elems);
            } else if (value instanceof Collection) {
                // Convert the collection into a java.sql.Array
                Collection coll = (Collection) value;
                Object[] elems = new Object[coll.size()];
                int i = 0;
                for (Object elem : coll) {
                    elems[i++] = elem;
                }
                array = ps.getConnection().createArrayOf(arrayElemSqlType, elems);
            } else {
                throw new NucleusUserException("We do not support persisting values of type " + value.getClass().getName() + " as an ARRAY." + " Member=" + mapping.getMemberMetaData().getFullFieldName());
            }
            ps.setArray(param, array);
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("055001", "Object", "" + value, column, e.getMessage()), e);
    }
}
Also used : Array(java.sql.Array) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Collection(java.util.Collection)

Example 7 with NucleusUserException

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

the class ArrayRDBMSMapping method getObject.

public Object getObject(ResultSet rs, int param) {
    Object value = null;
    try {
        Array arr = rs.getArray(param);
        if (!rs.wasNull()) {
            Object javaArray = arr.getArray();
            int length = java.lang.reflect.Array.getLength(javaArray);
            AbstractMemberMetaData mmd = mapping.getMemberMetaData();
            if (mmd.getType().isArray()) {
                // Copy in to an array of the same type as the member
                value = java.lang.reflect.Array.newInstance(mmd.getType().getComponentType(), length);
                for (int i = 0; i < length; i++) {
                    java.lang.reflect.Array.set(value, i, java.lang.reflect.Array.get(javaArray, i));
                }
            } else if (Collection.class.isAssignableFrom(mmd.getType())) {
                Collection<Object> coll;
                try {
                    Class instanceType = SCOUtils.getContainerInstanceType(mmd.getType(), mmd.getOrderMetaData() != null);
                    coll = (Collection<Object>) instanceType.newInstance();
                } catch (Exception e) {
                    throw new NucleusDataStoreException(e.getMessage(), e);
                }
                for (int i = 0; i < length; i++) {
                    coll.add(java.lang.reflect.Array.get(javaArray, i));
                }
                value = coll;
            } else {
                throw new NucleusUserException("We do not support retrieving values of type " + mmd.getTypeName() + " as an ARRAY." + " Member=" + mapping.getMemberMetaData().getFullFieldName());
            }
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
    }
    return value;
}
Also used : Array(java.sql.Array) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Collection(java.util.Collection) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 8 with NucleusUserException

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

the class EmbeddedMapping method initialize.

/**
 * Initialize for the specified member.
 * @param mmd metadata for the embedded member
 * @param table Table for persisting this field
 * @param clr The ClassLoaderResolver
 * @param emd Embedded MetaData for the object being embedded
 * @param typeName type of the embedded PC object being stored
 * @param objectType Object type of the PC object being embedded (see StateManagerImpl object types)
 */
public void initialize(AbstractMemberMetaData mmd, Table table, ClassLoaderResolver clr, EmbeddedMetaData emd, String typeName, int objectType) {
    super.initialize(mmd, table, clr);
    this.clr = clr;
    this.emd = emd;
    this.typeName = typeName;
    this.objectType = (short) objectType;
    // Find the MetaData for the embedded PC class
    MetaDataManager mmgr = table.getStoreManager().getMetaDataManager();
    AbstractClassMetaData rootEmbCmd = mmgr.getMetaDataForClass(typeName, clr);
    if (rootEmbCmd == null) {
        // Not found so must be an interface
        // Try using the fieldTypes on the field/property - we support it if only 1 implementation
        String[] fieldTypes = mmd.getFieldTypes();
        if (fieldTypes != null && fieldTypes.length == 1) {
            rootEmbCmd = mmgr.getMetaDataForClass(fieldTypes[0], clr);
        } else if (fieldTypes != null && fieldTypes.length > 1) {
            // TODO Cater for multiple implementations
            throw new NucleusUserException("Field " + mmd.getFullFieldName() + " is a reference field that is embedded with multiple possible implementations. " + "DataNucleus doesnt support embedded reference fields that have more than 1 implementation");
        }
        if (rootEmbCmd == null) {
            // Try a persistent interface
            rootEmbCmd = mmgr.getMetaDataForInterface(clr.classForName(typeName), clr);
            if (rootEmbCmd == null && mmd.getFieldTypes() != null && mmd.getFieldTypes().length == 1) {
                // No MetaData for the type so try "fieldType" specified on the field
                rootEmbCmd = mmgr.getMetaDataForInterface(clr.classForName(mmd.getFieldTypes()[0]), clr);
            }
        }
    }
    if (rootEmbCmd == null) {
        throw new NucleusUserException("Unable to find root class embedded metadata for field=" + mmd.getFullFieldName());
    }
    embCmd = rootEmbCmd;
    AbstractMemberMetaData[] embFmds = null;
    if (emd == null && rootEmbCmd.isEmbeddedOnly()) {
        // No <embedded> block yet the class is defined as embedded-only so just use its own definition of fields
        embFmds = rootEmbCmd.getManagedMembers();
    } else if (emd != null) {
        // <embedded> block so use those field definitions
        embFmds = emd.getMemberMetaData();
    }
    String[] subclasses = mmgr.getSubclassesForClass(rootEmbCmd.getFullClassName(), true);
    if (subclasses != null && subclasses.length > 0) {
        if (rootEmbCmd.hasDiscriminatorStrategy()) {
            // Fabricate a DiscriminatorMetaData to use for the embedded object
            discrimMetaData = new DiscriminatorMetaData();
            InheritanceMetaData embInhMd = new InheritanceMetaData();
            embInhMd.setParent(rootEmbCmd);
            discrimMetaData.setParent(embInhMd);
            // Set strategy based on the inheritance of the embedded object, otherwise class name.
            DiscriminatorMetaData dismd = rootEmbCmd.getDiscriminatorMetaDataRoot();
            if (dismd.getStrategy() != null && dismd.getStrategy() != DiscriminatorStrategy.NONE) {
                discrimMetaData.setStrategy(dismd.getStrategy());
            } else {
                // Fallback to class name
                discrimMetaData.setStrategy(DiscriminatorStrategy.CLASS_NAME);
            }
            // Set column for discriminator
            ColumnMetaData disColmd = new ColumnMetaData();
            disColmd.setAllowsNull(Boolean.TRUE);
            DiscriminatorMetaData embDismd = (emd != null) ? emd.getDiscriminatorMetaData() : null;
            if (embDismd != null && embDismd.getColumnMetaData() != null) {
                disColmd.setName(embDismd.getColumnMetaData().getName());
            } else {
                ColumnMetaData colmd = dismd.getColumnMetaData();
                if (colmd != null && colmd.getName() != null) {
                    disColmd.setName(colmd.getName());
                }
            }
            discrimMetaData.setColumnMetaData(disColmd);
            discrimMapping = DiscriminatorMapping.createDiscriminatorMapping(table, discrimMetaData);
            addDatastoreMapping(discrimMapping.getDatastoreMapping(0));
        } else {
            NucleusLogger.PERSISTENCE.info("Member " + mmd.getFullFieldName() + " is embedded and the type " + "(" + rootEmbCmd.getFullClassName() + ") has potential subclasses." + " Impossible to detect which is stored embedded. Add a discriminator to the embedded type");
        }
    }
    // Add all fields of the embedded class (that are persistent)
    int[] pcFieldNumbers = rootEmbCmd.getAllMemberPositions();
    for (int i = 0; i < pcFieldNumbers.length; i++) {
        AbstractMemberMetaData rootEmbMmd = rootEmbCmd.getMetaDataForManagedMemberAtAbsolutePosition(pcFieldNumbers[i]);
        if (rootEmbMmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
            addMappingForMember(rootEmbCmd, rootEmbMmd, embFmds);
        }
    }
    // Add fields for any subtypes (that are persistent)
    if (discrimMapping != null && subclasses != null && subclasses.length > 0) {
        for (int i = 0; i < subclasses.length; i++) {
            AbstractClassMetaData subEmbCmd = storeMgr.getMetaDataManager().getMetaDataForClass(subclasses[i], clr);
            AbstractMemberMetaData[] subEmbMmds = subEmbCmd.getManagedMembers();
            if (subEmbMmds != null) {
                for (int j = 0; j < subEmbMmds.length; j++) {
                    if (subEmbMmds[j].getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
                        addMappingForMember(subEmbCmd, subEmbMmds[j], embFmds);
                    }
                }
            }
        }
    }
}
Also used : DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) MetaDataManager(org.datanucleus.metadata.MetaDataManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) InheritanceMetaData(org.datanucleus.metadata.InheritanceMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 9 with NucleusUserException

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

the class BlobImpl method getObject.

/**
 * Accessor for the Object.
 * @return The object.
 * @throws SQLException if an error occurs
 */
public Object getObject() throws SQLException {
    if (freed) {
        throw new SQLException("free() has been called");
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois;
    try {
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (StreamCorruptedException e) {
        String msg = "StreamCorruptedException: object is corrupted";
        NucleusLogger.DATASTORE.error(msg);
        throw new NucleusUserException(msg, e).setFatal();
    } catch (IOException e) {
        String msg = "IOException: error when reading object";
        NucleusLogger.DATASTORE.error(msg);
        throw new NucleusUserException(msg, e).setFatal();
    } catch (ClassNotFoundException e) {
        String msg = "ClassNotFoundException: error when creating object";
        NucleusLogger.DATASTORE.error(msg);
        throw new NucleusUserException(msg, e).setFatal();
    }
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with NucleusUserException

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

the class QueryToSQLMapper method processBitOrExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processBitOrExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processBitOrExpression(Expression expr) {
    SQLExpression rightExpr = stack.pop();
    SQLExpression leftExpr = stack.pop();
    if (rightExpr instanceof BooleanExpression && leftExpr instanceof BooleanExpression) {
        // Handle as Boolean logical OR
        stack.push(leftExpr);
        stack.push(rightExpr);
        return processOrExpression(expr);
    } else if (rightExpr instanceof NumericExpression && leftExpr instanceof NumericExpression) {
        if (storeMgr.getDatastoreAdapter().supportsOption(DatastoreAdapter.OPERATOR_BITWISE_OR)) {
            SQLExpression bitExpr = new NumericExpression(leftExpr, Expression.OP_BIT_OR, rightExpr).encloseInParentheses();
            stack.push(bitExpr);
            return bitExpr;
        }
    }
    // TODO Support BITWISE OR for more cases
    throw new NucleusUserException("Operation BITWISE OR is not supported for " + leftExpr + " and " + rightExpr + " is not supported by this datastore");
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression)

Aggregations

NucleusUserException (org.datanucleus.exceptions.NucleusUserException)258 NucleusException (org.datanucleus.exceptions.NucleusException)65 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)51 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)46 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)46 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)36 ArrayList (java.util.ArrayList)34 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)30 ObjectProvider (org.datanucleus.state.ObjectProvider)30 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)26 Expression (org.datanucleus.query.expression.Expression)24 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)23 SQLException (java.sql.SQLException)22 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)21 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)21 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)21 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)20 BigInteger (java.math.BigInteger)19