Search in sources :

Example 1 with SQLText

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

the class StringMatchesMethod method getBooleanLikeExpression.

protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    BooleanExpression likeExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
    SQLText sql = likeExpr.toSQLText();
    sql.clearStatement();
    if (Expression.OP_LIKE.isHigherThanLeftSide(expr.getLowestOperator())) {
        sql.append("(").append(expr).append(")");
    } else {
        sql.append(expr);
    }
    sql.append(" LIKE ");
    if (Expression.OP_LIKE.isHigherThanRightSide(regExpr.getLowestOperator())) {
        sql.append("(").append(regExpr).append(")");
    } else {
        sql.append(regExpr);
    }
    BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
    if (escapeExpr != null) {
        if (escapeExpr instanceof CharacterLiteral) {
            String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
            if (chr.equals(dba.getEscapeCharacter())) {
                // If the escape character specified matches the Java character then apply the known working ESCAPE
                // This is because some datastore JDBC drivers require additional "\" characters to allow
                // for Java usage
                sql.append(dba.getEscapePatternExpression());
            } else {
                sql.append(" ESCAPE " + escapeExpr);
            }
        } else {
            sql.append(" ESCAPE " + escapeExpr);
        }
    } else {
        sql.append(" " + dba.getEscapePatternExpression());
    }
    return likeExpr;
}
Also used : BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLText(org.datanucleus.store.rdbms.sql.SQLText) CharacterLiteral(org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)

Example 2 with SQLText

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

the class StringSimilarPostgresqlMethod method getBooleanLikeExpression.

protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    BooleanExpression similarToExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
    SQLText sql = similarToExpr.toSQLText();
    sql.clearStatement();
    if (OP_SIMILAR_TO.isHigherThanLeftSide(expr.getLowestOperator())) {
        sql.append("(").append(expr).append(")");
    } else {
        sql.append(expr);
    }
    sql.append(" SIMILAR TO ");
    if (OP_SIMILAR_TO.isHigherThanRightSide(regExpr.getLowestOperator())) {
        sql.append("(").append(regExpr).append(")");
    } else {
        sql.append(regExpr);
    }
    BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
    if (escapeExpr != null) {
        if (escapeExpr instanceof CharacterLiteral) {
            String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
            if (chr.equals(dba.getEscapeCharacter())) {
                // If the escape character specified matches the Java character then apply the known working ESCAPE
                // This is because some datastore JDBC drivers require additional "\" characters to allow
                // for Java usage
                sql.append(dba.getEscapePatternExpression());
            } else {
                sql.append(" ESCAPE " + escapeExpr);
            }
        } else {
            sql.append(" ESCAPE " + escapeExpr);
        }
    } else {
        sql.append(" " + dba.getEscapePatternExpression());
    }
    return similarToExpr;
}
Also used : BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLText(org.datanucleus.store.rdbms.sql.SQLText) CharacterLiteral(org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)

Example 3 with SQLText

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

the class Concat3Operation method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.operation.SQLOperation#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, org.datanucleus.store.rdbms.sql.expression.SQLExpression)
     */
public SQLExpression getExpression(SQLExpression expr, SQLExpression expr2) {
    /*
         * We cast it to VARCHAR otherwise the concatenation in derby it is promoted to LONG VARCHAR.
         * 
         * In Derby, ? string parameters are promoted to LONG VARCHAR, and Derby
         * does not allow comparisons between LONG VARCHAR types. so the below 
         * example would be invalid in Derby
         * (THIS.FIRSTNAME||?) = ?
         * 
         * Due to that we convert it to
         * (CAST(THIS.FIRSTNAME||? AS VARCHAR(4000))) = ?
         * 
         * The only issue with this solution is for columns bigger than 4000 chars.
         * Secondly, if both operands are parameters, derby does not allow concatenation e.g.
         * ? || ? is not allowed by derby
         * 
         * so we do
         * CAST(? AS VARCHAR(4000)) || CAST(? AS VARCHAR(4000))
         *
         * If both situations happen,
         * (CAST(CAST( ? AS VARCHAR(4000) ) || CAST( ? AS VARCHAR(4000) ) AS VARCHAR(4000))) = ? 
         */
    JavaTypeMapping m = expr.getSQLStatement().getSQLExpressionFactory().getMappingForType(String.class, false);
    List types = new ArrayList();
    types.add("VARCHAR(4000)");
    List argsOp1 = new ArrayList();
    argsOp1.add(expr);
    SQLExpression firstExpr = new StringExpression(expr.getSQLStatement(), m, "CAST", argsOp1, types).encloseInParentheses();
    List argsOp2 = new ArrayList();
    argsOp2.add(expr2);
    SQLExpression secondExpr = new StringExpression(expr.getSQLStatement(), m, "CAST", argsOp2, types).encloseInParentheses();
    StringExpression concatExpr = new StringExpression(expr.getSQLStatement(), null, null);
    SQLText sql = concatExpr.toSQLText();
    sql.clearStatement();
    sql.append(firstExpr);
    sql.append("||");
    sql.append(secondExpr);
    List args = new ArrayList();
    args.add(concatExpr);
    return new StringExpression(expr.getSQLStatement(), m, "CAST", args, types);
}
Also used : 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) List(java.util.List) ArrayList(java.util.ArrayList) SQLText(org.datanucleus.store.rdbms.sql.SQLText)

Example 4 with SQLText

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

the class SQLAnywhereAdapter method getUpdateTableStatement.

/**
 * Method to return the SQLText for an UPDATE TABLE statement. Returns the SQLText for
 * <code>UPDATE T1 SET x1 = val1, x2 = val2 FROM MYTBL T1</code>. Override if the datastore doesn't
 * support that standard syntax.
 * @param tbl The primary table
 * @param setSQL The SQLText for the SET component
 * @return SQLText for the update statement
 */
public SQLText getUpdateTableStatement(SQLTable tbl, SQLText setSQL) {
    // "UPDATE T1"
    SQLText sql = new SQLText("UPDATE ").append(tbl.getAlias().toString());
    // " SET x1 = val1, x2 = val2"
    sql.append(" ").append(setSQL);
    // " FROM MYTBL T1"
    sql.append(" FROM ").append(tbl.toString());
    return sql;
}
Also used : SQLText(org.datanucleus.store.rdbms.sql.SQLText)

Example 5 with SQLText

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

the class SQLServerAdapter method getUpdateTableStatement.

/**
 * Method to return the SQLText for an UPDATE TABLE statement.
 * Returns the SQLText for <code>UPDATE T1 SET x1 = val1, x2 = val2 FROM MYTBL T1</code>.
 * Override if the datastore doesn't support that standard syntax.
 * @param tbl The primary table
 * @param setSQL The SQLText for the SET component
 * @return SQLText for the update statement
 */
public SQLText getUpdateTableStatement(SQLTable tbl, SQLText setSQL) {
    // "UPDATE T1"
    SQLText sql = new SQLText("UPDATE ").append(tbl.getAlias().toString());
    // " SET x1 = val1, x2 = val2"
    sql.append(" ").append(setSQL);
    // " FROM MYTBL T1"
    sql.append(" FROM ").append(tbl.toString());
    return sql;
}
Also used : SQLText(org.datanucleus.store.rdbms.sql.SQLText)

Aggregations

SQLText (org.datanucleus.store.rdbms.sql.SQLText)9 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BaseDatastoreAdapter (org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter)2 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)2 CharacterLiteral (org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)2 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)2 NucleusException (org.datanucleus.exceptions.NucleusException)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1