Search in sources :

Example 1 with CreateView

use of org.h2.command.ddl.CreateView in project h2database by h2database.

the class Parser method parseCreateView.

private CreateView parseCreateView(boolean force, boolean orReplace) {
    boolean ifNotExists = readIfNotExists();
    boolean isTableExpression = readIf("TABLE_EXPRESSION");
    String viewName = readIdentifierWithSchema();
    CreateView command = new CreateView(session, getSchema());
    this.createView = command;
    command.setViewName(viewName);
    command.setIfNotExists(ifNotExists);
    command.setComment(readCommentIf());
    command.setOrReplace(orReplace);
    command.setForce(force);
    command.setTableExpression(isTableExpression);
    if (readIf("(")) {
        String[] cols = parseColumnList();
        command.setColumnNames(cols);
    }
    String select = StringUtils.cache(sqlCommand.substring(parseIndex));
    read("AS");
    try {
        Query query;
        session.setParsingCreateView(true, viewName);
        try {
            query = parseSelect();
            query.prepare();
        } finally {
            session.setParsingCreateView(false, viewName);
        }
        command.setSelect(query);
    } catch (DbException e) {
        if (force) {
            command.setSelectSQL(select);
            while (currentTokenType != END) {
                read();
            }
        } else {
            throw e;
        }
    }
    return command;
}
Also used : Query(org.h2.command.dml.Query) ValueString(org.h2.value.ValueString) CreateView(org.h2.command.ddl.CreateView) DbException(org.h2.message.DbException)

Example 2 with CreateView

use of org.h2.command.ddl.CreateView 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

Query (org.h2.command.dml.Query)2 ValueString (org.h2.value.ValueString)2 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)1 CreateSchema (org.h2.command.ddl.CreateSchema)1 CreateTable (org.h2.command.ddl.CreateTable)1 CreateView (org.h2.command.ddl.CreateView)1 DropSchema (org.h2.command.ddl.DropSchema)1 DropTable (org.h2.command.ddl.DropTable)1 TruncateTable (org.h2.command.ddl.TruncateTable)1 Session (org.h2.engine.Session)1 Expression (org.h2.expression.Expression)1 FunctionCall (org.h2.expression.FunctionCall)1 ValueExpression (org.h2.expression.ValueExpression)1 DbException (org.h2.message.DbException)1 Schema (org.h2.schema.Schema)1 FunctionTable (org.h2.table.FunctionTable)1 IndexHints (org.h2.table.IndexHints)1 RangeTable (org.h2.table.RangeTable)1 Table (org.h2.table.Table)1 TableFilter (org.h2.table.TableFilter)1