Search in sources :

Example 21 with StringLiteral

use of org.datanucleus.store.rdbms.sql.expression.StringLiteral in project datanucleus-rdbms by datanucleus.

the class SQLBooleanMethod 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<SQLExpression> args) {
    if (args == null || args.size() != 1) {
        throw new NucleusUserException("Cannot invoke SQL_boolean() without a string argument");
    }
    SQLExpression expr = args.get(0);
    if (!(expr instanceof StringLiteral)) {
        throw new NucleusUserException("Cannot use SQL_boolean() without string argument");
    }
    String sql = (String) ((StringLiteral) expr).getValue();
    JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(boolean.class, false);
    BooleanExpression retExpr = new BooleanExpression(stmt, m, sql);
    return retExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 22 with StringLiteral

use of org.datanucleus.store.rdbms.sql.expression.StringLiteral in project datanucleus-rdbms by datanucleus.

the class SQLNumericMethod 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<SQLExpression> args) {
    if (args == null || args.size() != 1) {
        throw new NucleusUserException("Cannot invoke SQL_numeric() without a string argument");
    }
    SQLExpression expr = args.get(0);
    if (!(expr instanceof StringLiteral)) {
        throw new NucleusUserException("Cannot use SQL_numeric() without string argument");
    }
    String sql = (String) ((StringLiteral) expr).getValue();
    JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(boolean.class, false);
    NumericExpression retExpr = new NumericExpression(stmt, m, sql);
    return retExpr;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression)

Example 23 with StringLiteral

use of org.datanucleus.store.rdbms.sql.expression.StringLiteral in project datanucleus-rdbms by datanucleus.

the class SimpleOrderableAggregateMethod 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));
    } else 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();
        if (argExpr instanceof TemporalExpression) {
            return new AggregateTemporalExpression(stmt, m, getFunctionName(), args);
        } else if (argExpr instanceof StringExpression) {
            return new AggregateStringExpression(stmt, m, getFunctionName(), args);
        }
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    // Handle as Subquery "SELECT AVG(expr) FROM tbl"
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    SQLExpression argExpr = args.get(0);
    SelectStatement subStmt = new SelectStatement(stmt, stmt.getRDBMSManager(), argExpr.getSQLTable().getTable(), argExpr.getSQLTable().getAlias(), null);
    subStmt.setClassLoaderResolver(clr);
    JavaTypeMapping mapping = stmt.getRDBMSManager().getMappingManager().getMappingWithDatastoreMapping(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 = null;
    if (argExpr instanceof TemporalExpression) {
        subqExpr = new TemporalSubqueryExpression(stmt, subStmt);
    } else if (argExpr instanceof StringExpression) {
        subqExpr = new StringSubqueryExpression(stmt, subStmt);
    } else {
        subqExpr = new NumericSubqueryExpression(stmt, subStmt);
    }
    subqExpr.setJavaTypeMapping(subqMapping);
    return subqExpr;
}
Also used : AggregateStringExpression(org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression) AggregateTemporalExpression(org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) AggregateStringExpression(org.datanucleus.store.rdbms.sql.expression.AggregateStringExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) AggregateTemporalExpression(org.datanucleus.store.rdbms.sql.expression.AggregateTemporalExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 24 with StringLiteral

use of org.datanucleus.store.rdbms.sql.expression.StringLiteral in project datanucleus-rdbms by datanucleus.

the class CollectionSizeMethod 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() > 0) {
        throw new NucleusException(Localiser.msg("060015", "size", "CollectionExpression"));
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (expr instanceof CollectionLiteral) {
        // Just return the collection size since we have the value
        Collection coll = (Collection) ((CollectionLiteral) expr).getValue();
        return exprFactory.newLiteral(stmt, exprFactory.getMappingForType(int.class, false), Integer.valueOf(coll.size()));
    }
    AbstractMemberMetaData mmd = expr.getJavaTypeMapping().getMemberMetaData();
    if (mmd.isSerialized()) {
        throw new NucleusUserException("Cannot perform Collection.size when the collection is being serialised");
    }
    ApiAdapter api = stmt.getRDBMSManager().getApiAdapter();
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    Class elementCls = clr.classForName(mmd.getCollection().getElementType());
    if (!api.isPersistable(elementCls) && mmd.getJoinMetaData() == null) {
        throw new NucleusUserException("Cannot perform Collection.size when the collection<Non-Persistable> is not in a join table");
    }
    String elementType = mmd.getCollection().getElementType();
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    // TODO Allow for interface elements, etc
    JavaTypeMapping ownerMapping = null;
    Table collectionTbl = null;
    if (mmd.getMappedBy() != null) {
        // Bidirectional
        AbstractMemberMetaData elementMmd = mmd.getRelatedMemberMetaData(clr)[0];
        if (mmd.getJoinMetaData() != null || elementMmd.getJoinMetaData() != null) {
            // JoinTable
            collectionTbl = storeMgr.getTable(mmd);
            ownerMapping = ((JoinTable) collectionTbl).getOwnerMapping();
        } else {
            // ForeignKey
            collectionTbl = storeMgr.getDatastoreClass(elementType, clr);
            ownerMapping = collectionTbl.getMemberMapping(elementMmd);
        }
    } else {
        // Unidirectional
        if (mmd.getJoinMetaData() != null) {
            // JoinTable
            collectionTbl = storeMgr.getTable(mmd);
            ownerMapping = ((JoinTable) collectionTbl).getOwnerMapping();
        } else {
            // ForeignKey
            collectionTbl = storeMgr.getDatastoreClass(elementType, clr);
            ownerMapping = ((DatastoreClass) collectionTbl).getExternalMapping(mmd, MappingType.EXTERNAL_FK);
        }
    }
    SelectStatement subStmt = new SelectStatement(stmt, storeMgr, collectionTbl, null, null);
    subStmt.setClassLoaderResolver(clr);
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
    SQLExpression countExpr = exprFactory.newLiteral(subStmt, mapping, "COUNT(*)");
    ((StringLiteral) countExpr).generateStatementWithoutQuotes();
    subStmt.select(countExpr, null);
    SQLExpression elementOwnerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
    SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), expr.getSQLTable().getTable().getIdMapping());
    subStmt.whereAnd(elementOwnerExpr.eq(ownerIdExpr), true);
    JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
    SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
    subqExpr.setJavaTypeMapping(subqMapping);
    return subqExpr;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ApiAdapter(org.datanucleus.api.ApiAdapter) Table(org.datanucleus.store.rdbms.table.Table) JoinTable(org.datanucleus.store.rdbms.table.JoinTable) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) CollectionLiteral(org.datanucleus.store.rdbms.sql.expression.CollectionLiteral) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) Collection(java.util.Collection) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 25 with StringLiteral

use of org.datanucleus.store.rdbms.sql.expression.StringLiteral in project datanucleus-rdbms by datanucleus.

the class EnumToStringMethod 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 EnumLiteral) {
        Enum val = (Enum) ((EnumLiteral) expr).getValue();
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return new StringLiteral(stmt, exprFactory.getMappingForType(String.class, false), val.toString(), null);
    } else if (expr instanceof EnumExpression) {
        EnumExpression enumExpr = (EnumExpression) expr;
        JavaTypeMapping m = enumExpr.getJavaTypeMapping();
        if (m.getJavaTypeForDatastoreMapping(0).equals(ClassNameConstants.JAVA_LANG_STRING)) {
            return enumExpr.getDelegate();
        }
        throw new NucleusException("EnumExpression.toString is not supported when the enum is stored as a numeric");
    } else {
        throw new NucleusException(Localiser.msg("060001", "ordinal", expr));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EnumExpression(org.datanucleus.store.rdbms.sql.expression.EnumExpression) EnumLiteral(org.datanucleus.store.rdbms.sql.expression.EnumLiteral) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)25 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)21 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)16 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)14 ArrayList (java.util.ArrayList)13 NucleusException (org.datanucleus.exceptions.NucleusException)13 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)12 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)8 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)7 List (java.util.List)6 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)6 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)6 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)6 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 NumericSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression)5 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)5 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)4 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)4 Arrays.asList (java.util.Arrays.asList)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3