Search in sources :

Example 41 with Schema

use of com.predic8.schema.Schema 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 42 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method parseAlterSchema.

private Prepared parseAlterSchema() {
    boolean ifExists = readIfExists(false);
    String schemaName = readIdentifierWithSchema();
    Schema old = getSchema();
    read("RENAME");
    read("TO");
    String newName = readIdentifierWithSchema(old.getName());
    Schema schema = findSchema(schemaName);
    if (schema == null) {
        if (ifExists) {
            return new NoOperation(session);
        }
        throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
    }
    AlterSchemaRename command = new AlterSchemaRename(session);
    command.setOldSchema(schema);
    checkSchema(old);
    command.setNewName(newName);
    return command;
}
Also used : NoOperation(org.h2.command.dml.NoOperation) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) AlterSchemaRename(org.h2.command.ddl.AlterSchemaRename) ValueString(org.h2.value.ValueString)

Example 43 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method parseValuesTable.

private TableFilter parseValuesTable(int orderInFrom) {
    Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
    TableFunction tf = (TableFunction) Function.getFunction(database, "TABLE");
    ArrayList<Column> columns = New.arrayList();
    ArrayList<ArrayList<Expression>> rows = New.arrayList();
    do {
        int i = 0;
        ArrayList<Expression> row = New.arrayList();
        boolean multiColumn = readIf("(");
        do {
            Expression expr = readExpression();
            expr = expr.optimize(session);
            int type = expr.getType();
            long prec;
            int scale, displaySize;
            Column column;
            String columnName = "C" + (i + 1);
            if (rows.isEmpty()) {
                if (type == Value.UNKNOWN) {
                    type = Value.STRING;
                }
                DataType dt = DataType.getDataType(type);
                prec = dt.defaultPrecision;
                scale = dt.defaultScale;
                displaySize = dt.defaultDisplaySize;
                column = new Column(columnName, type, prec, scale, displaySize);
                columns.add(column);
            }
            prec = expr.getPrecision();
            scale = expr.getScale();
            displaySize = expr.getDisplaySize();
            if (i >= columns.size()) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            Column c = columns.get(i);
            type = Value.getHigherOrder(c.getType(), type);
            prec = Math.max(c.getPrecision(), prec);
            scale = Math.max(c.getScale(), scale);
            displaySize = Math.max(c.getDisplaySize(), displaySize);
            column = new Column(columnName, type, prec, scale, displaySize);
            columns.set(i, column);
            row.add(expr);
            i++;
        } while (multiColumn && readIfMore(true));
        rows.add(row);
    } while (readIf(","));
    int columnCount = columns.size();
    int rowCount = rows.size();
    for (ArrayList<Expression> row : rows) {
        if (row.size() != columnCount) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    for (int i = 0; i < columnCount; i++) {
        Column c = columns.get(i);
        if (c.getType() == Value.UNKNOWN) {
            c = new Column(c.getName(), Value.STRING, 0, 0, 0);
            columns.set(i, c);
        }
        Expression[] array = new Expression[rowCount];
        for (int j = 0; j < rowCount; j++) {
            array[j] = rows.get(j).get(i);
        }
        ExpressionList list = new ExpressionList(array);
        tf.setParameter(i, list);
    }
    tf.setColumns(columns);
    tf.doneWithParameters();
    Table table = new FunctionTable(mainSchema, session, tf, tf);
    return new TableFilter(session, table, null, rightsChecked, currentSelect, orderInFrom, null);
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ArrayList(java.util.ArrayList) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) 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) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) FunctionTable(org.h2.table.FunctionTable) TableFilter(org.h2.table.TableFilter) TableFunction(org.h2.expression.TableFunction) CreateUserDataType(org.h2.command.ddl.CreateUserDataType) DataType(org.h2.value.DataType) DropUserDataType(org.h2.command.ddl.DropUserDataType) UserDataType(org.h2.engine.UserDataType) ExpressionList(org.h2.expression.ExpressionList)

Example 44 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method createCTEView.

private TableView createCTEView(String cteViewName, String querySQL, List<Column> columnTemplateList, boolean allowRecursiveQueryDetection, boolean addViewToSession, boolean isPersistent, Session targetSession) {
    Database db = targetSession.getDatabase();
    Schema schema = getSchemaWithDefault();
    int id = db.allocateObjectId();
    Column[] columnTemplateArray = columnTemplateList.toArray(new Column[0]);
    // No easy way to determine if this is a recursive query up front, so we just compile
    // it twice - once without the flag set, and if we didn't see a recursive term,
    // then we just compile it again.
    TableView view;
    synchronized (targetSession) {
        view = new TableView(schema, id, cteViewName, querySQL, parameters, columnTemplateArray, targetSession, allowRecursiveQueryDetection, false, /* literalsChecked */
        true, /* isTableExpression */
        isPersistent);
        if (!view.isRecursiveQueryDetected() && allowRecursiveQueryDetection) {
            if (isPersistent) {
                db.addSchemaObject(targetSession, view);
                view.lock(targetSession, true, true);
                targetSession.getDatabase().removeSchemaObject(targetSession, view);
            } else {
                session.removeLocalTempTable(view);
            }
            view = new TableView(schema, id, cteViewName, querySQL, parameters, columnTemplateArray, targetSession, false, /* assume recursive */
            false, /* literalsChecked */
            true, /* isTableExpression */
            isPersistent);
        }
        // both removeSchemaObject and removeLocalTempTable hold meta locks
        targetSession.getDatabase().unlockMeta(targetSession);
    }
    view.setTableExpression(true);
    view.setTemporary(!isPersistent);
    view.setHidden(true);
    view.setOnCommitDrop(false);
    if (addViewToSession) {
        if (isPersistent) {
            db.addSchemaObject(targetSession, view);
            view.unlock(targetSession);
            db.unlockMeta(targetSession);
        } else {
            targetSession.addLocalTempTable(view);
        }
    }
    return view;
}
Also used : 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) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database) DropDatabase(org.h2.command.ddl.DropDatabase) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) TableView(org.h2.table.TableView)

Example 45 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method readTableFilter.

private TableFilter readTableFilter() {
    Table table;
    String alias = null;
    if (readIf("(")) {
        if (isSelect()) {
            Query query = parseSelectUnion();
            read(")");
            query.setParameterList(new ArrayList<>(parameters));
            query.init();
            Session s;
            if (createView != null) {
                s = database.getSystemSession();
            } else {
                s = session;
            }
            alias = session.getNextSystemIdentifier(sqlCommand);
            table = TableView.createTempView(s, session.getUser(), alias, query, currentSelect);
        } else {
            TableFilter top;
            top = readTableFilter();
            top = readJoin(top);
            read(")");
            alias = readFromAlias(null);
            if (alias != null) {
                top.setAlias(alias);
                ArrayList<String> derivedColumnNames = readDerivedColumnNames();
                if (derivedColumnNames != null) {
                    top.setDerivedColumns(derivedColumnNames);
                }
            }
            return top;
        }
    } else if (readIf("VALUES")) {
        table = parseValuesTable(0).getTable();
    } else {
        String tableName = readIdentifierWithSchema(null);
        Schema schema = getSchema();
        boolean foundLeftBracket = readIf("(");
        if (foundLeftBracket && readIf("INDEX")) {
            // Sybase compatibility with
            // "select * from test (index table1_index)"
            readIdentifierWithSchema(null);
            read(")");
            foundLeftBracket = false;
        }
        if (foundLeftBracket) {
            Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
            if (equalsToken(tableName, RangeTable.NAME) || equalsToken(tableName, RangeTable.ALIAS)) {
                Expression min = readExpression();
                read(",");
                Expression max = readExpression();
                if (readIf(",")) {
                    Expression step = readExpression();
                    read(")");
                    table = new RangeTable(mainSchema, min, max, step, false);
                } else {
                    read(")");
                    table = new RangeTable(mainSchema, min, max, false);
                }
            } else {
                Expression expr = readFunction(schema, tableName);
                if (!(expr instanceof FunctionCall)) {
                    throw getSyntaxError();
                }
                FunctionCall call = (FunctionCall) expr;
                if (!call.isDeterministic()) {
                    recompileAlways = true;
                }
                table = new FunctionTable(mainSchema, session, expr, call);
            }
        } else if (equalsToken("DUAL", tableName)) {
            table = getDualTable(false);
        } else if (database.getMode().sysDummy1 && equalsToken("SYSDUMMY1", tableName)) {
            table = getDualTable(false);
        } else {
            table = readTableOrView(tableName);
        }
    }
    ArrayList<String> derivedColumnNames = null;
    IndexHints indexHints = null;
    // for backward compatibility, handle case where USE is a table alias
    if (readIf("USE")) {
        if (readIf("INDEX")) {
            indexHints = parseIndexHints(table);
        } else {
            alias = "USE";
            derivedColumnNames = readDerivedColumnNames();
        }
    } else {
        alias = readFromAlias(alias);
        if (alias != null) {
            derivedColumnNames = readDerivedColumnNames();
            // if alias present, a second chance to parse index hints
            if (readIf("USE")) {
                read("INDEX");
                indexHints = parseIndexHints(table);
            }
        }
    }
    // inherit alias for CTE as views from table name
    if (table.isView() && table.isTableExpression() && alias == null) {
        alias = table.getName();
    }
    TableFilter filter = new TableFilter(session, table, alias, rightsChecked, currentSelect, orderInFrom++, indexHints);
    if (derivedColumnNames != null) {
        filter.setDerivedColumns(derivedColumnNames);
    }
    return filter;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) Query(org.h2.command.dml.Query) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) IndexHints(org.h2.table.IndexHints) RangeTable(org.h2.table.RangeTable) TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) FunctionTable(org.h2.table.FunctionTable) FunctionCall(org.h2.expression.FunctionCall) Session(org.h2.engine.Session)

Aggregations

Schema (org.h2.schema.Schema)35 CreateSchema (org.h2.command.ddl.CreateSchema)16 DropSchema (org.h2.command.ddl.DropSchema)16 ValueString (org.h2.value.ValueString)16 Column (org.h2.table.Column)10 IndexColumn (org.h2.table.IndexColumn)10 Table (org.h2.table.Table)10 Expression (org.h2.expression.Expression)8 ExpressionColumn (org.h2.expression.ExpressionColumn)8 ValueExpression (org.h2.expression.ValueExpression)8 Schema (io.s4.schema.Schema)7 ArrayList (java.util.ArrayList)7 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)7 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)7 CreateTable (org.h2.command.ddl.CreateTable)7 RangeTable (org.h2.table.RangeTable)7 Property (io.s4.schema.Schema.Property)6 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)6 DropTable (org.h2.command.ddl.DropTable)6 TruncateTable (org.h2.command.ddl.TruncateTable)6