Search in sources :

Example 26 with ExpressionColumn

use of org.h2.expression.ExpressionColumn in project h2database by h2database.

the class Parser method readCondition.

private Expression readCondition() {
    if (readIf("NOT")) {
        return new ConditionNot(readCondition());
    }
    if (readIf("EXISTS")) {
        read("(");
        Query query = parseSelect();
        // can not reduce expression because it might be a union except
        // query with distinct
        read(")");
        return new ConditionExists(query);
    }
    if (readIf("INTERSECTS")) {
        read("(");
        Expression r1 = readConcat();
        read(",");
        Expression r2 = readConcat();
        read(")");
        return new Comparison(session, Comparison.SPATIAL_INTERSECTS, r1, r2);
    }
    Expression r = readConcat();
    while (true) {
        // special case: NOT NULL is not part of an expression (as in CREATE
        // TABLE TEST(ID INT DEFAULT 0 NOT NULL))
        int backup = parseIndex;
        boolean not = false;
        if (readIf("NOT")) {
            not = true;
            if (isToken("NULL")) {
                // this really only works for NOT NULL!
                parseIndex = backup;
                currentToken = "NOT";
                break;
            }
        }
        if (readIf("LIKE")) {
            Expression b = readConcat();
            Expression esc = null;
            if (readIf("ESCAPE")) {
                esc = readConcat();
            }
            recompileAlways = true;
            r = new CompareLike(database, r, b, esc, false);
        } else if (readIf("ILIKE")) {
            Function function = Function.getFunction(database, "CAST");
            function.setDataType(new Column("X", Value.STRING_IGNORECASE));
            function.setParameter(0, r);
            r = function;
            Expression b = readConcat();
            Expression esc = null;
            if (readIf("ESCAPE")) {
                esc = readConcat();
            }
            recompileAlways = true;
            r = new CompareLike(database, r, b, esc, false);
        } else if (readIf("REGEXP")) {
            Expression b = readConcat();
            recompileAlways = true;
            r = new CompareLike(database, r, b, null, true);
        } else if (readIf("IS")) {
            if (readIf("NOT")) {
                if (readIf("NULL")) {
                    r = new Comparison(session, Comparison.IS_NOT_NULL, r, null);
                } else if (readIf("DISTINCT")) {
                    read("FROM");
                    r = new Comparison(session, Comparison.EQUAL_NULL_SAFE, r, readConcat());
                } else {
                    r = new Comparison(session, Comparison.NOT_EQUAL_NULL_SAFE, r, readConcat());
                }
            } else if (readIf("NULL")) {
                r = new Comparison(session, Comparison.IS_NULL, r, null);
            } else if (readIf("DISTINCT")) {
                read("FROM");
                r = new Comparison(session, Comparison.NOT_EQUAL_NULL_SAFE, r, readConcat());
            } else {
                r = new Comparison(session, Comparison.EQUAL_NULL_SAFE, r, readConcat());
            }
        } else if (readIf("IN")) {
            read("(");
            if (readIf(")")) {
                if (database.getMode().prohibitEmptyInPredicate) {
                    throw getSyntaxError();
                }
                r = ValueExpression.get(ValueBoolean.FALSE);
            } else {
                if (isSelect()) {
                    Query query = parseSelect();
                    // can not be lazy because we have to call
                    // method ResultInterface.containsDistinct
                    // which is not supported for lazy execution
                    query.setNeverLazy(true);
                    r = new ConditionInSelect(database, r, query, false, Comparison.EQUAL);
                } else {
                    ArrayList<Expression> v = New.arrayList();
                    Expression last;
                    do {
                        last = readExpression();
                        v.add(last);
                    } while (readIf(","));
                    if (v.size() == 1 && (last instanceof Subquery)) {
                        Subquery s = (Subquery) last;
                        Query q = s.getQuery();
                        r = new ConditionInSelect(database, r, q, false, Comparison.EQUAL);
                    } else {
                        r = new ConditionIn(database, r, v);
                    }
                }
                read(")");
            }
        } else if (readIf("BETWEEN")) {
            Expression low = readConcat();
            read("AND");
            Expression high = readConcat();
            Expression condLow = new Comparison(session, Comparison.SMALLER_EQUAL, low, r);
            Expression condHigh = new Comparison(session, Comparison.BIGGER_EQUAL, high, r);
            r = new ConditionAndOr(ConditionAndOr.AND, condLow, condHigh);
        } else {
            int compareType = getCompareType(currentTokenType);
            if (compareType < 0) {
                break;
            }
            read();
            if (readIf("ALL")) {
                read("(");
                Query query = parseSelect();
                r = new ConditionInSelect(database, r, query, true, compareType);
                read(")");
            } else if (readIf("ANY") || readIf("SOME")) {
                read("(");
                if (currentTokenType == PARAMETER && compareType == 0) {
                    Parameter p = readParameter();
                    r = new ConditionInParameter(database, r, p);
                } else {
                    Query query = parseSelect();
                    r = new ConditionInSelect(database, r, query, false, compareType);
                }
                read(")");
            } else {
                Expression right = readConcat();
                if (SysProperties.OLD_STYLE_OUTER_JOIN && readIf("(") && readIf("+") && readIf(")")) {
                    // join with (+)
                    if (r instanceof ExpressionColumn && right instanceof ExpressionColumn) {
                        ExpressionColumn leftCol = (ExpressionColumn) r;
                        ExpressionColumn rightCol = (ExpressionColumn) right;
                        ArrayList<TableFilter> filters = currentSelect.getTopFilters();
                        for (TableFilter f : filters) {
                            while (f != null) {
                                leftCol.mapColumns(f, 0);
                                rightCol.mapColumns(f, 0);
                                f = f.getJoin();
                            }
                        }
                        TableFilter leftFilter = leftCol.getTableFilter();
                        TableFilter rightFilter = rightCol.getTableFilter();
                        r = new Comparison(session, compareType, r, right);
                        if (leftFilter != null && rightFilter != null) {
                            int idx = filters.indexOf(rightFilter);
                            if (idx >= 0) {
                                filters.remove(idx);
                                leftFilter.addJoin(rightFilter, true, r);
                            } else {
                                rightFilter.mapAndAddFilter(r);
                            }
                            r = ValueExpression.get(ValueBoolean.TRUE);
                        }
                    }
                } else {
                    r = new Comparison(session, compareType, r, right);
                }
            }
        }
        if (not) {
            r = new ConditionNot(r);
        }
    }
    return r;
}
Also used : ConditionNot(org.h2.expression.ConditionNot) Query(org.h2.command.dml.Query) Subquery(org.h2.expression.Subquery) ConditionIn(org.h2.expression.ConditionIn) ConditionAndOr(org.h2.expression.ConditionAndOr) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) CompareLike(org.h2.expression.CompareLike) ExpressionColumn(org.h2.expression.ExpressionColumn) Function(org.h2.expression.Function) TableFunction(org.h2.expression.TableFunction) JavaFunction(org.h2.expression.JavaFunction) ConditionInParameter(org.h2.expression.ConditionInParameter) ConditionInSelect(org.h2.expression.ConditionInSelect) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Comparison(org.h2.expression.Comparison) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) TableFilter(org.h2.table.TableFilter) Parameter(org.h2.expression.Parameter) ConditionInParameter(org.h2.expression.ConditionInParameter) ConditionExists(org.h2.expression.ConditionExists)

Example 27 with ExpressionColumn

use of org.h2.expression.ExpressionColumn in project h2database by h2database.

the class Parser method readTermObjectDot.

private Expression readTermObjectDot(String objectName) {
    Expression expr = readWildcardOrSequenceValue(null, objectName);
    if (expr != null) {
        return expr;
    }
    String name = readColumnIdentifier();
    Schema s = database.findSchema(objectName);
    if ((!SysProperties.OLD_STYLE_OUTER_JOIN || s != null) && readIf("(")) {
        // if the old style outer joins are not supported
        return readFunction(s, name);
    } else if (readIf(".")) {
        String schema = objectName;
        objectName = name;
        expr = readWildcardOrSequenceValue(schema, objectName);
        if (expr != null) {
            return expr;
        }
        name = readColumnIdentifier();
        if (readIf("(")) {
            String databaseName = schema;
            if (!equalsToken(database.getShortName(), databaseName)) {
                throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, databaseName);
            }
            schema = objectName;
            return readFunction(database.getSchema(schema), name);
        } else if (readIf(".")) {
            String databaseName = schema;
            if (!equalsToken(database.getShortName(), databaseName)) {
                throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, databaseName);
            }
            schema = objectName;
            objectName = name;
            expr = readWildcardOrSequenceValue(schema, objectName);
            if (expr != null) {
                return expr;
            }
            name = readColumnIdentifier();
            return new ExpressionColumn(database, schema, objectName, name);
        }
        return new ExpressionColumn(database, schema, objectName, name);
    }
    return new ExpressionColumn(database, null, objectName, name);
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 28 with ExpressionColumn

use of org.h2.expression.ExpressionColumn in project h2database by h2database.

the class CreateTable method generateColumnsFromQuery.

private void generateColumnsFromQuery() {
    int columnCount = asQuery.getColumnCount();
    ArrayList<Expression> expressions = asQuery.getExpressions();
    ColumnNamer columnNamer = new ColumnNamer(session);
    for (int i = 0; i < columnCount; i++) {
        Expression expr = expressions.get(i);
        int type = expr.getType();
        String name = columnNamer.getColumnName(expr, i, expr.getAlias());
        long precision = expr.getPrecision();
        int displaySize = expr.getDisplaySize();
        DataType dt = DataType.getDataType(type);
        if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) {
            // dont' set precision to MAX_VALUE if this is the default
            precision = dt.defaultPrecision;
        }
        int scale = expr.getScale();
        if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) {
            scale = dt.defaultScale;
        }
        if (scale > precision) {
            precision = scale;
        }
        String[] enumerators = null;
        if (dt.type == Value.ENUM) {
            /**
             * Only columns of tables may be enumerated.
             */
            if (!(expr instanceof ExpressionColumn)) {
                throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unable to resolve enumerators of expression");
            }
            enumerators = ((ExpressionColumn) expr).getColumn().getEnumerators();
        }
        Column col = new Column(name, type, precision, scale, displaySize, enumerators);
        addColumn(col);
    }
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) Expression(org.h2.expression.Expression) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) DataType(org.h2.value.DataType) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 29 with ExpressionColumn

use of org.h2.expression.ExpressionColumn in project h2database by h2database.

the class Expression method getExpressionColumns.

/**
 * Extracts expression columns from the given result set.
 *
 * @param session the session
 * @param rs the result set
 * @return an array of expression columns
 */
public static Expression[] getExpressionColumns(Session session, ResultSet rs) {
    try {
        ResultSetMetaData meta = rs.getMetaData();
        int columnCount = meta.getColumnCount();
        Expression[] expressions = new Expression[columnCount];
        Database db = session == null ? null : session.getDatabase();
        for (int i = 0; i < columnCount; i++) {
            String name = meta.getColumnLabel(i + 1);
            int type = DataType.getValueTypeFromResultSet(meta, i + 1);
            int precision = meta.getPrecision(i + 1);
            int scale = meta.getScale(i + 1);
            int displaySize = meta.getColumnDisplaySize(i + 1);
            Column col = new Column(name, type, precision, scale, displaySize);
            Expression expr = new ExpressionColumn(db, col);
            expressions[i] = expr;
        }
        return expressions;
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Column(org.h2.table.Column) SQLException(java.sql.SQLException) Database(org.h2.engine.Database)

Example 30 with ExpressionColumn

use of org.h2.expression.ExpressionColumn in project ignite by apache.

the class CollocationModel method isAffinityColumn.

/**
 * @param f Table filter.
 * @param expCol Expression column.
 * @param validate Query validation flag.
 * @return {@code true} It it is an affinity column.
 */
@SuppressWarnings("IfMayBeConditional")
private static boolean isAffinityColumn(TableFilter f, ExpressionColumn expCol, boolean validate) {
    Column col = expCol.getColumn();
    if (col == null)
        return false;
    Table t = col.getTable();
    if (t.isView()) {
        Query qry;
        if (f.getIndex() != null)
            qry = getSubQuery(f);
        else
            qry = GridSqlQueryParser.VIEW_QUERY.get((TableView) t);
        return isAffinityColumn(qry, expCol, validate);
    }
    if (t instanceof GridH2Table) {
        GridH2Table t0 = (GridH2Table) t;
        if (validate && t0.isCustomAffinityMapper())
            throw customAffinityError((t0).cacheName());
        IndexColumn affCol = t0.getAffinityKeyColumn();
        return affCol != null && col.getColumnId() == affCol.column.getColumnId();
    }
    return false;
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) Query(org.h2.command.dml.Query) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IndexColumn(org.h2.table.IndexColumn)

Aggregations

ExpressionColumn (org.h2.expression.ExpressionColumn)21 Column (org.h2.table.Column)18 Expression (org.h2.expression.Expression)17 ValueExpression (org.h2.expression.ValueExpression)9 IndexColumn (org.h2.table.IndexColumn)9 TableFilter (org.h2.table.TableFilter)8 Query (org.h2.command.dml.Query)6 Comparison (org.h2.expression.Comparison)6 ConditionAndOr (org.h2.expression.ConditionAndOr)6 ValueString (org.h2.value.ValueString)6 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)4 Database (org.h2.engine.Database)4 Index (org.h2.index.Index)4 Value (org.h2.value.Value)4 Parser (org.h2.command.Parser)3 LocalResult (org.h2.result.LocalResult)3 Table (org.h2.table.Table)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 CacheException (javax.cache.CacheException)2