Search in sources :

Example 36 with StringExpression

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

the class StringReplaceAllMethod 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() != 2) {
        throw new NucleusException(Localiser.msg("060003", "replaceAll", "StringExpression", 2, "StringExpression/CharacterExpression"));
    }
    // {strExpr}.translate(strExpr1, strExpr2)
    SQLExpression strExpr1 = args.get(0);
    SQLExpression strExpr2 = args.get(1);
    if (!(strExpr1 instanceof StringExpression) && !(strExpr1 instanceof CharacterExpression)) {
        throw new NucleusException(Localiser.msg("060003", "replaceAll", "StringExpression", 1, "StringExpression/CharacterExpression"));
    }
    if (!(strExpr2 instanceof StringExpression) && !(strExpr2 instanceof CharacterExpression)) {
        throw new NucleusException(Localiser.msg("060003", "replaceAll", "StringExpression", 2, "StringExpression/CharacterExpression"));
    }
    // Invoke substring(startExpr, endExpr)
    List<SQLExpression> newArgs = new ArrayList<>(3);
    newArgs.add(expr);
    newArgs.add(strExpr1);
    newArgs.add(strExpr2);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    JavaTypeMapping mapping = exprFactory.getMappingForType(String.class, false);
    return new StringExpression(stmt, mapping, "replace", newArgs);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) 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 37 with StringExpression

use of org.datanucleus.store.rdbms.sql.expression.StringExpression 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 38 with StringExpression

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

the class StringEqualsIgnoreCaseMethod 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", "endsWith", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    StringExpression strExpr1 = (StringExpression) expr;
    StringExpression strExpr2 = (StringExpression) args.get(0);
    SQLExpression str1Upper = strExpr1.invoke("toUpperCase", null);
    SQLExpression str2Upper = strExpr2.invoke("toUpperCase", null);
    return str1Upper.eq(str2Upper);
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 39 with StringExpression

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

the class StringEqualsMethod 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", "endsWith", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    StringExpression strExpr1 = (StringExpression) expr;
    StringExpression strExpr2 = (StringExpression) args.get(0);
    return strExpr1.eq(strExpr2);
}
Also used : StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 40 with StringExpression

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

the class StringTranslateMethod 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() != 2) {
        throw new NucleusException(Localiser.msg("060003", "translate", "StringExpression", 2, "StringExpression/CharacterExpression"));
    }
    // {strExpr}.translate(strExpr1, strExpr2)
    SQLExpression strExpr1 = args.get(0);
    SQLExpression strExpr2 = args.get(1);
    if (!(strExpr1 instanceof StringExpression) && !(strExpr1 instanceof CharacterExpression)) {
        throw new NucleusException(Localiser.msg("060003", "translate", "StringExpression", 1, "StringExpression/CharacterExpression"));
    }
    if (!(strExpr2 instanceof StringExpression) && !(strExpr2 instanceof CharacterExpression)) {
        throw new NucleusException(Localiser.msg("060003", "translate", "StringExpression", 2, "StringExpression/CharacterExpression"));
    }
    // Invoke substring(startExpr, endExpr)
    List<SQLExpression> newArgs = new ArrayList<>(3);
    newArgs.add(expr);
    newArgs.add(strExpr1);
    newArgs.add(strExpr2);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    JavaTypeMapping mapping = exprFactory.getMappingForType(String.class, false);
    return new StringExpression(stmt, mapping, "translate", newArgs);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) 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

StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)43 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)33 ArrayList (java.util.ArrayList)29 NucleusException (org.datanucleus.exceptions.NucleusException)28 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)24 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)23 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)23 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)18 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)16 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)12 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)9 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)8 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)8 List (java.util.List)7 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)5 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)5 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)5 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)5 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)4 EnumExpression (org.datanucleus.store.rdbms.sql.expression.EnumExpression)4