Search in sources :

Example 26 with Right

use of org.h2.engine.Right 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 Right

use of org.h2.engine.Right in project h2database by h2database.

the class GrantRevoke method grantRole.

private void grantRole(Role grantedRole) {
    if (grantedRole != grantee && grantee.isRoleGranted(grantedRole)) {
        return;
    }
    if (grantee instanceof Role) {
        Role granteeRole = (Role) grantee;
        if (grantedRole.isRoleGranted(granteeRole)) {
            // cyclic role grants are not allowed
            throw DbException.get(ErrorCode.ROLE_ALREADY_GRANTED_1, grantedRole.getSQL());
        }
    }
    Database db = session.getDatabase();
    int id = getObjectId();
    Right right = new Right(db, id, grantee, grantedRole);
    db.addDatabaseObject(session, right);
    grantee.grantRole(grantedRole, right);
}
Also used : Role(org.h2.engine.Role) Database(org.h2.engine.Database) Right(org.h2.engine.Right)

Example 28 with Right

use of org.h2.engine.Right in project h2database by h2database.

the class GrantRevoke method revokeRight.

private void revokeRight(DbObject object) {
    Right right = grantee.getRightForObject(object);
    if (right == null) {
        return;
    }
    int mask = right.getRightMask();
    int newRight = mask & ~rightMask;
    Database db = session.getDatabase();
    if (newRight == 0) {
        db.removeDatabaseObject(session, right);
    } else {
        right.setRightMask(newRight);
        db.updateMeta(session, right);
    }
}
Also used : Right(org.h2.engine.Right) Database(org.h2.engine.Database)

Example 29 with Right

use of org.h2.engine.Right in project h2database by h2database.

the class CreateSchema method update.

@Override
public int update() {
    session.getUser().checkSchemaAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    User user = db.getUser(authorization);
    // during DB startup, the Right/Role records have not yet been loaded
    if (!db.isStarting()) {
        user.checkSchemaAdmin();
    }
    if (db.findSchema(schemaName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.SCHEMA_ALREADY_EXISTS_1, schemaName);
    }
    int id = getObjectId();
    Schema schema = new Schema(db, id, schemaName, user, false);
    schema.setTableEngineParams(tableEngineParams);
    db.addDatabaseObject(session, schema);
    return 0;
}
Also used : User(org.h2.engine.User) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database)

Example 30 with Right

use of org.h2.engine.Right in project h2database by h2database.

the class Operation method getValue.

@Override
public Value getValue(Session session) {
    Value l = left.getValue(session).convertTo(dataType);
    Value r;
    if (right == null) {
        r = null;
    } else {
        r = right.getValue(session);
        if (convertRight) {
            r = r.convertTo(dataType);
        }
    }
    switch(opType) {
        case NEGATE:
            return l == ValueNull.INSTANCE ? l : l.negate();
        case CONCAT:
            {
                Mode mode = session.getDatabase().getMode();
                if (l == ValueNull.INSTANCE) {
                    if (mode.nullConcatIsNull) {
                        return ValueNull.INSTANCE;
                    }
                    return r;
                } else if (r == ValueNull.INSTANCE) {
                    if (mode.nullConcatIsNull) {
                        return ValueNull.INSTANCE;
                    }
                    return l;
                }
                String s1 = l.getString(), s2 = r.getString();
                StringBuilder buff = new StringBuilder(s1.length() + s2.length());
                buff.append(s1).append(s2);
                return ValueString.get(buff.toString());
            }
        case PLUS:
            if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
                return ValueNull.INSTANCE;
            }
            return l.add(r);
        case MINUS:
            if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
                return ValueNull.INSTANCE;
            }
            return l.subtract(r);
        case MULTIPLY:
            if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
                return ValueNull.INSTANCE;
            }
            return l.multiply(r);
        case DIVIDE:
            if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
                return ValueNull.INSTANCE;
            }
            return l.divide(r);
        case MODULUS:
            if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
                return ValueNull.INSTANCE;
            }
            return l.modulus(r);
        default:
            throw DbException.throwInternalError("type=" + opType);
    }
}
Also used : Mode(org.h2.engine.Mode) Value(org.h2.value.Value) ValueString(org.h2.value.ValueString)

Aggregations

Right (org.h2.engine.Right)9 Database (org.h2.engine.Database)7 Expression (org.h2.expression.Expression)7 ValueExpression (org.h2.expression.ValueExpression)7 Value (org.h2.value.Value)7 Comparison (org.h2.expression.Comparison)6 ConditionAndOr (org.h2.expression.ConditionAndOr)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 ValueString (org.h2.value.ValueString)6 Column (org.h2.table.Column)5 IOException (java.io.IOException)4 Constraint (org.h2.constraint.Constraint)4 IndexColumn (org.h2.table.IndexColumn)4 ArrayList (java.util.ArrayList)3 DbObject (org.h2.engine.DbObject)3 Index (org.h2.index.Index)3 Sequence (org.h2.schema.Sequence)3 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 ResultSet (java.sql.ResultSet)2