Search in sources :

Example 56 with NucleusUserException

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

the class SecondaryTable method getPrimaryKey.

/**
 * Accessor for the primary key for this table. Overrides the method in TableImpl
 * to add on any specification of PK name in the metadata.
 * @return The primary key.
 */
public PrimaryKey getPrimaryKey() {
    PrimaryKey pk = super.getPrimaryKey();
    if (joinMetaData == null) {
        // TODO Localise this message
        throw new NucleusUserException("A relationship to a secondary table requires a <join> specification. " + "The secondary table is " + this.getDatastoreIdentifierFullyQualified() + " and the primary table is " + this.getPrimaryTable() + ". The fields mapped to this secondary table are: " + memberMappingsMap.keySet().toString());
    }
    PrimaryKeyMetaData pkmd = joinMetaData.getPrimaryKeyMetaData();
    if (pkmd != null && pkmd.getName() != null) {
        pk.setName(pkmd.getName());
    }
    return pk;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData)

Example 57 with NucleusUserException

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

the class SequenceGenerator method createRepository.

/**
 * Method to create the sequence.
 * @return Whether it was created successfully.
 */
protected boolean createRepository() {
    PreparedStatement ps = null;
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    DatastoreAdapter dba = srm.getDatastoreAdapter();
    SQLController sqlControl = srm.getSQLController();
    if (!srm.getSchemaHandler().isAutoCreateTables()) {
        throw new NucleusUserException(Localiser.msg("040010", sequenceName));
    }
    Integer min = properties.containsKey(ValueGenerator.PROPERTY_KEY_MIN_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_MIN_VALUE)) : null;
    Integer max = properties.containsKey(ValueGenerator.PROPERTY_KEY_MAX_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_MAX_VALUE)) : null;
    Integer start = properties.containsKey(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE)) : null;
    Integer incr = properties.containsKey(ValueGenerator.PROPERTY_KEY_CACHE_SIZE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_CACHE_SIZE)) : null;
    Integer cacheSize = properties.containsKey(ValueGenerator.PROPERTY_KEY_DATABASE_CACHE_SIZE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_DATABASE_CACHE_SIZE)) : null;
    String stmt = dba.getSequenceCreateStmt(sequenceName, min, max, start, incr, cacheSize);
    try {
        ps = sqlControl.getStatementForUpdate(connection, stmt, false);
        sqlControl.executeStatementUpdate(null, connection, stmt, ps, true);
    } catch (SQLException e) {
        NucleusLogger.DATASTORE.error(e);
        throw new ValueGenerationException(Localiser.msg("061000", e.getMessage()) + stmt);
    } finally {
        try {
            if (ps != null) {
                sqlControl.closeStatement(connection, ps);
            }
        } catch (SQLException e) {
        // non-recoverable error
        }
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PreparedStatement(java.sql.PreparedStatement) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 58 with NucleusUserException

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

the class RDBMSStoreManager method getTableForStrategy.

private DatastoreClass getTableForStrategy(AbstractClassMetaData cmd, int fieldNumber, ClassLoaderResolver clr) {
    DatastoreClass t = getDatastoreClass(cmd.getFullClassName(), clr);
    if (t == null && cmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
        throw new NucleusUserException(Localiser.msg("032013", cmd.getFullClassName()));
    }
    if (t == null) {
        throw new NucleusUserException("Attempt to find table for class=" + cmd.getFullClassName() + " but no table found! Check the metadata");
    }
    if (fieldNumber >= 0) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        t = t.getBaseDatastoreClassWithMember(mmd);
    } else {
        // Go up to overall superclass to find id for that class.
        boolean hasSuperclass = true;
        while (hasSuperclass) {
            DatastoreClass supert = t.getSuperDatastoreClass();
            if (supert != null) {
                t = supert;
            } else {
                hasSuperclass = false;
            }
        }
    }
    return t;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 59 with NucleusUserException

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

the class RDBMSStoreManager method initialiseIdentifierFactory.

/**
 * Method to create the IdentifierFactory to be used by this store.
 * Relies on the datastore adapter existing before creation
 * @param nucleusContext context
 */
protected void initialiseIdentifierFactory(NucleusContext nucleusContext) {
    if (dba == null) {
        throw new NucleusException("DatastoreAdapter not yet created so cannot create IdentifierFactory!");
    }
    String idFactoryName = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_FACTORY);
    try {
        // Create the control properties for identifier generation
        Map props = new HashMap();
        if (catalogName != null) {
            props.put("DefaultCatalog", catalogName);
        }
        if (schemaName != null) {
            props.put("DefaultSchema", schemaName);
        }
        String val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_CASE);
        props.put("RequiredCase", val != null ? val : getDefaultIdentifierCase());
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_WORD_SEPARATOR);
        if (val != null) {
            props.put("WordSeparator", val);
        }
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_PREFIX);
        if (val != null) {
            props.put("TablePrefix", val);
        }
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_SUFFIX);
        if (val != null) {
            props.put("TableSuffix", val);
        }
        props.put("NamingFactory", getNamingFactory());
        // Create the IdentifierFactory
        ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
        if ("datanucleus2".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new DN2IdentifierFactory(dba, clr, props);
        } else if ("jpa".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new JPAIdentifierFactory(dba, clr, props);
        } else if ("datanucleus1".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new DNIdentifierFactory(dba, clr, props);
        } else if ("jpox".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new JPOXIdentifierFactory(dba, clr, props);
        } else {
            // Fallback to the plugin mechanism
            Class[] argTypes = new Class[] { DatastoreAdapter.class, ClassConstants.CLASS_LOADER_RESOLVER, Map.class };
            Object[] args = new Object[] { dba, nucleusContext.getClassLoaderResolver(null), props };
            identifierFactory = (IdentifierFactory) nucleusContext.getPluginManager().createExecutableExtension("org.datanucleus.store.rdbms.identifierfactory", "name", idFactoryName, "class-name", argTypes, args);
        }
    } catch (ClassNotFoundException cnfe) {
        throw new NucleusUserException(Localiser.msg("039004", idFactoryName), cnfe).setFatal();
    } catch (Exception e) {
        NucleusLogger.PERSISTENCE.error("Exception creating IdentifierFactory", e);
        throw new NucleusException(Localiser.msg("039005", idFactoryName), e).setFatal();
    }
}
Also used : JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DN2IdentifierFactory(org.datanucleus.store.rdbms.identifier.DN2IdentifierFactory) MacroString(org.datanucleus.util.MacroString) DN2IdentifierFactory(org.datanucleus.store.rdbms.identifier.DN2IdentifierFactory) JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) DNIdentifierFactory(org.datanucleus.store.rdbms.identifier.DNIdentifierFactory) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) JPAIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPAIdentifierFactory) SQLException(java.sql.SQLException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) IOException(java.io.IOException) NucleusException(org.datanucleus.exceptions.NucleusException) UnsupportedDataTypeException(org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) IncompatibleFieldTypeException(org.datanucleus.store.types.IncompatibleFieldTypeException) DNIdentifierFactory(org.datanucleus.store.rdbms.identifier.DNIdentifierFactory) JPAIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPAIdentifierFactory) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map) MultiMap(org.datanucleus.util.MultiMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 60 with NucleusUserException

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

the class RDBMSStoreManager method executeScript.

/* (non-Javadoc)
     * @see org.datanucleus.store.schema.SchemaScriptAwareStoreManager#executeScript(java.lang.String)
     */
public void executeScript(String script) {
    script = StringUtils.replaceAll(script, "\n", " ");
    script = StringUtils.replaceAll(script, "\t", " ");
    ManagedConnection mc = connectionMgr.getConnection(-1);
    try {
        // Execute the script on this datastore
        // Note that we simply split the script at line delimiter (";")
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        try {
            StringTokenizer tokeniser = new StringTokenizer(script, ";");
            while (tokeniser.hasMoreTokens()) {
                String token = tokeniser.nextToken().trim();
                if (!StringUtils.isWhitespace(token)) {
                    NucleusLogger.DATASTORE_NATIVE.debug("Executing script statement : " + token);
                    stmt.execute(token + ";");
                }
            }
        } finally {
            stmt.close();
        }
    } catch (SQLException e) {
        NucleusLogger.DATASTORE_NATIVE.error("Exception executing user script", e);
        throw new NucleusUserException("Exception executing user script. See nested exception for details", e);
    } finally {
        mc.release();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Connection(java.sql.Connection) NucleusConnection(org.datanucleus.store.NucleusConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) MacroString(org.datanucleus.util.MacroString)

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