Search in sources :

Example 21 with NucleusException

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

the class StringLength3Method 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 (expr instanceof StringLiteral) {
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
        String val = (String) ((StringLiteral) expr).getValue();
        return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
    } else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
        ArrayList funcArgs = new ArrayList();
        funcArgs.add(expr);
        return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LENGTH", funcArgs);
    } else {
        throw new NucleusException(Localiser.msg("060001", "length", expr));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 22 with NucleusException

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

the class SimpleNumericAggregateMethod 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 (expr != null) {
        throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
    }
    if (args == null || args.size() != 1) {
        throw new NucleusException(getFunctionName() + " is only supported with a single argument");
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
        // FUNC(argExpr)
        // Use same java type as the argument
        SQLExpression argExpr = args.get(0);
        JavaTypeMapping m = argExpr.getJavaTypeMapping();
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    // Handle as Subquery "SELECT AVG(expr) FROM tbl"
    SQLExpression argExpr = args.get(0);
    SelectStatement subStmt = new SelectStatement(stmt, stmt.getRDBMSManager(), argExpr.getSQLTable().getTable(), argExpr.getSQLTable().getAlias(), null);
    subStmt.setClassLoaderResolver(clr);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    JavaTypeMapping mapping = stmt.getRDBMSManager().getMappingManager().getMappingWithColumnMapping(String.class, false, false, clr);
    String aggregateString = getFunctionName() + "(" + argExpr.toSQLText() + ")";
    SQLExpression aggExpr = exprFactory.newLiteral(subStmt, mapping, aggregateString);
    ((StringLiteral) aggExpr).generateStatementWithoutQuotes();
    subStmt.select(aggExpr, null);
    JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
    SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
    subqExpr.setJavaTypeMapping(subqMapping);
    return subqExpr;
}
Also used : SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NucleusException(org.datanucleus.exceptions.NucleusException) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression)

Example 23 with NucleusException

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

the class StringConcat1Method 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(expr, Expression.OP_CONCAT, otherExpr);
}
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)

Example 24 with NucleusException

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

the class RDBMSStoreManager method deleteSchemaForClasses.

public void deleteSchemaForClasses(Set<String> classNames, Properties props) {
    if (!classNames.isEmpty()) {
        // Delete the tables
        String ddlFilename = props != null ? props.getProperty("ddlFilename") : null;
        String completeDdlProp = props != null ? props.getProperty("completeDdl") : null;
        boolean completeDdl = completeDdlProp != null && completeDdlProp.equalsIgnoreCase("true");
        String autoStartProp = props != null ? props.getProperty("autoStartTable") : null;
        boolean autoStart = autoStartProp != null && autoStartProp.equalsIgnoreCase("true");
        ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
        FileWriter ddlFileWriter = null;
        try {
            performingDeleteSchemaForClasses = true;
            if (ddlFilename != null) {
                // Open the DDL file for writing
                File ddlFile = StringUtils.getFileForFilename(ddlFilename);
                if (ddlFile.exists()) {
                    // Delete existing file
                    ddlFile.delete();
                }
                if (ddlFile.getParentFile() != null && !ddlFile.getParentFile().exists()) {
                    // Make sure the directory exists
                    ddlFile.getParentFile().mkdirs();
                }
                ddlFile.createNewFile();
                ddlFileWriter = new FileWriter(ddlFile);
                SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                ddlFileWriter.write("------------------------------------------------------------------\n");
                ddlFileWriter.write("-- DataNucleus SchemaTool " + "(ran at " + fmt.format(new java.util.Date()) + ")\n");
                ddlFileWriter.write("------------------------------------------------------------------\n");
                ddlFileWriter.write("-- Delete schema required for the following classes:-\n");
                Iterator classNameIter = classNames.iterator();
                while (classNameIter.hasNext()) {
                    ddlFileWriter.write("--     " + classNameIter.next() + "\n");
                }
                ddlFileWriter.write("--\n");
            }
            try {
                if (ddlFileWriter != null) {
                    this.ddlWriter = ddlFileWriter;
                    this.completeDDL = completeDdl;
                    this.writtenDdlStatements = new HashSet();
                }
                // Generate the tables/constraints for these classes (so we know the tables to delete)
                // TODO This will add CREATE to the DDL, need to be able to omit this
                String[] classNameArray = classNames.toArray(new String[classNames.size()]);
                // Add them to mgr first
                manageClasses(clr, classNameArray);
                // Delete the tables of the required classes TODO Why use READ_COMMITTED for delete but SERIALIZABLE for add?
                int isolationLevel = hasProperty(PropertyNames.PROPERTY_SCHEMA_TXN_ISOLATION) ? TransactionUtils.getTransactionIsolationLevelForName(getStringProperty(PropertyNames.PROPERTY_SCHEMA_TXN_ISOLATION)) : Connection.TRANSACTION_READ_COMMITTED;
                DeleteTablesSchemaTransaction deleteTablesTxn = new DeleteTablesSchemaTransaction(this, isolationLevel, storeDataMgr);
                deleteTablesTxn.setWriter(ddlWriter);
                boolean success = true;
                try {
                    deleteTablesTxn.execute(clr);
                } catch (NucleusException ne) {
                    success = false;
                    throw ne;
                } finally {
                    if (success) {
                        clearSchemaData();
                    }
                }
                if (autoStart) {
                // TODO Delete the SchemaTable auto-starter table
                }
            // TODO Delete sequences and sequenceTables
            } finally {
                performingDeleteSchemaForClasses = false;
                if (ddlFileWriter != null) {
                    this.ddlWriter = null;
                    this.completeDDL = false;
                    this.writtenDdlStatements.clear();
                    this.writtenDdlStatements = null;
                    ddlFileWriter.close();
                }
            }
        } catch (IOException ioe) {
        // Error in writing DDL file
        // TODO Handle this
        }
    } else {
        String msg = Localiser.msg("014039");
        NucleusLogger.DATASTORE_SCHEMA.error(msg);
        System.out.println(msg);
        throw new NucleusException(msg);
    }
}
Also used : FileWriter(java.io.FileWriter) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MacroString(org.datanucleus.util.MacroString) IOException(java.io.IOException) Date(java.util.Date) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) NucleusException(org.datanucleus.exceptions.NucleusException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet)

Example 25 with NucleusException

use of org.datanucleus.exceptions.NucleusException 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(IdentifierFactory.PROPERTY_DEFAULT_CATALOG, catalogName);
        }
        if (schemaName != null) {
            props.put(IdentifierFactory.PROPERTY_DEFAULT_SCHEMA, schemaName);
        }
        String val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_CASE);
        props.put(IdentifierFactory.PROPERTY_REQUIRED_CASE, val != null ? val : getDefaultIdentifierCase());
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_WORD_SEPARATOR);
        if (val != null) {
            props.put(IdentifierFactory.PROPERTY_WORD_SEPARATOR, val);
        }
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_PREFIX);
        if (val != null) {
            props.put(IdentifierFactory.PROPERTY_TABLE_PREFIX, val);
        }
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_SUFFIX);
        if (val != null) {
            props.put(IdentifierFactory.PROPERTY_TABLE_SUFFIX, val);
        }
        props.put(IdentifierFactory.PROPERTY_NAMING_FACTORY, getNamingFactory());
        // Create the IdentifierFactory
        ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
        if ("datanucleus2".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new DN2IdentifierFactory(dba, clr, props);
        } else if ("jpa".equalsIgnoreCase(idFactoryName) || "jakarta".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)

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