Search in sources :

Example 51 with Alias

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

the class Parser method readSimpleTableFilter.

private TableFilter readSimpleTableFilter(int orderInFrom, Collection<String> excludeTokens) {
    Table table = readTableOrView();
    String alias = null;
    if (readIf("AS")) {
        alias = readAliasIdentifier();
    } else if (currentTokenType == IDENTIFIER) {
        if (!equalsTokenIgnoreCase(currentToken, "SET") && (excludeTokens == null || !isTokenInList(excludeTokens))) {
            // SET is not a keyword (PostgreSQL supports it as a table name)
            alias = readAliasIdentifier();
        }
    }
    return new TableFilter(session, table, alias, 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) TableFilter(org.h2.table.TableFilter) ValueString(org.h2.value.ValueString)

Example 52 with Alias

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

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf("TABLE")) {
        boolean ifExists = readIfExists(false);
        String tableName = readIdentifierWithSchema();
        DropTable command = new DropTable(session, getSchema());
        command.setTableName(tableName);
        while (readIf(",")) {
            tableName = readIdentifierWithSchema();
            DropTable next = new DropTable(session, getSchema());
            next.setTableName(tableName);
            command.addNextDropTable(next);
        }
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
            readIf("CONSTRAINTS");
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        } else if (readIf("IGNORE")) {
            command.setDropAction(ConstraintActionType.SET_DEFAULT);
        }
        return command;
    } else if (readIf("INDEX")) {
        boolean ifExists = readIfExists(false);
        String indexName = readIdentifierWithSchema();
        DropIndex command = new DropIndex(session, getSchema());
        command.setIndexName(indexName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        // Support for MySQL: DROP INDEX index_name ON tbl_name
        if (readIf("ON")) {
            readIdentifierWithSchema();
        }
        return command;
    } else if (readIf("USER")) {
        boolean ifExists = readIfExists(false);
        DropUser command = new DropUser(session);
        command.setUserName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        readIf("CASCADE");
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SEQUENCE")) {
        boolean ifExists = readIfExists(false);
        String sequenceName = readIdentifierWithSchema();
        DropSequence command = new DropSequence(session, getSchema());
        command.setSequenceName(sequenceName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("CONSTANT")) {
        boolean ifExists = readIfExists(false);
        String constantName = readIdentifierWithSchema();
        DropConstant command = new DropConstant(session, getSchema());
        command.setConstantName(constantName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("TRIGGER")) {
        boolean ifExists = readIfExists(false);
        String triggerName = readIdentifierWithSchema();
        DropTrigger command = new DropTrigger(session, getSchema());
        command.setTriggerName(triggerName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("VIEW")) {
        boolean ifExists = readIfExists(false);
        String viewName = readIdentifierWithSchema();
        DropView command = new DropView(session, getSchema());
        command.setViewName(viewName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        return command;
    } else if (readIf("ROLE")) {
        boolean ifExists = readIfExists(false);
        DropRole command = new DropRole(session);
        command.setRoleName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("ALIAS")) {
        boolean ifExists = readIfExists(false);
        String aliasName = readIdentifierWithSchema();
        DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
        command.setAliasName(aliasName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SCHEMA")) {
        boolean ifExists = readIfExists(false);
        DropSchema command = new DropSchema(session);
        command.setSchemaName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        }
        return command;
    } else if (readIf("ALL")) {
        read("OBJECTS");
        DropDatabase command = new DropDatabase(session);
        command.setDropAllObjects(true);
        if (readIf("DELETE")) {
            read("FILES");
            command.setDeleteFiles(true);
        }
        return command;
    } else if (readIf("DOMAIN")) {
        return parseDropUserDataType();
    } else if (readIf("TYPE")) {
        return parseDropUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseDropUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseDropAggregate();
    } else if (readIf("SYNONYM")) {
        boolean ifExists = readIfExists(false);
        String synonymName = readIdentifierWithSchema();
        DropSynonym command = new DropSynonym(session, getSchema());
        command.setSynonymName(synonymName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    }
    throw getSyntaxError();
}
Also used : DropConstant(org.h2.command.ddl.DropConstant) DropTrigger(org.h2.command.ddl.DropTrigger) DropView(org.h2.command.ddl.DropView) DropUser(org.h2.command.ddl.DropUser) DropSynonym(org.h2.command.ddl.DropSynonym) DropSequence(org.h2.command.ddl.DropSequence) ValueString(org.h2.value.ValueString) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) DropRole(org.h2.command.ddl.DropRole) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) DropDatabase(org.h2.command.ddl.DropDatabase) ConstraintActionType(org.h2.constraint.ConstraintActionType) DropIndex(org.h2.command.ddl.DropIndex)

Example 53 with Alias

use of org.h2.expression.Alias 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)

Example 54 with Alias

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

the class TestMultiConn method testConcurrentShutdownQuery.

private void testConcurrentShutdownQuery() throws Exception {
    Connection conn1 = getConnection("multiConn");
    Connection conn2 = getConnection("multiConn");
    final Statement stat1 = conn1.createStatement();
    stat1.execute("CREATE ALIAS SLEEP FOR \"java.lang.Thread.sleep(long)\"");
    final Statement stat2 = conn2.createStatement();
    stat1.execute("SET THROTTLE 100");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            stat2.executeQuery("CALL SLEEP(100)");
            Thread.sleep(10);
            stat2.executeQuery("CALL SLEEP(100)");
        }
    };
    t.execute();
    Thread.sleep(50);
    stat1.execute("SHUTDOWN");
    conn1.close();
    try {
        conn2.close();
    } catch (SQLException e) {
    // ignore
    }
    try {
        t.get();
    } catch (Exception e) {
    // ignore
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLException(java.sql.SQLException)

Example 55 with Alias

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

the class TestCsv method testWriteRead.

private void testWriteRead() throws SQLException {
    deleteDb("csv");
    Connection conn = getConnection("csv");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
    // int len = 100000;
    int len = 100;
    for (int i = 0; i < len; i++) {
        stat.execute("INSERT INTO TEST(NAME) VALUES('Ruebezahl')");
    }
    long time;
    time = System.nanoTime();
    new Csv().write(conn, getBaseDir() + "/testRW.csv", "SELECT X ID, 'Ruebezahl' NAME FROM SYSTEM_RANGE(1, " + len + ")", "UTF8");
    trace("write: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time));
    ResultSet rs;
    time = System.nanoTime();
    for (int i = 0; i < 30; i++) {
        rs = new Csv().read(getBaseDir() + "/testRW.csv", null, "UTF8");
        while (rs.next()) {
        // ignore
        }
    }
    trace("read: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time));
    rs = new Csv().read(getBaseDir() + "/testRW.csv", null, "UTF8");
    // stat.execute("CREATE ALIAS CSVREAD FOR \"org.h2.tools.Csv.read\"");
    ResultSetMetaData meta = rs.getMetaData();
    assertEquals(2, meta.getColumnCount());
    for (int i = 0; i < len; i++) {
        rs.next();
        assertEquals("" + (i + 1), rs.getString("ID"));
        assertEquals("Ruebezahl", rs.getString("NAME"));
    }
    assertFalse(rs.next());
    rs.close();
    conn.close();
    FileUtils.delete(getBaseDir() + "/testRW.csv");
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Csv(org.h2.tools.Csv) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Aggregations

ResultSet (java.sql.ResultSet)38 Statement (java.sql.Statement)38 Connection (java.sql.Connection)31 SimpleResultSet (org.h2.tools.SimpleResultSet)31 PreparedStatement (java.sql.PreparedStatement)28 CallableStatement (java.sql.CallableStatement)18 ValueString (org.h2.value.ValueString)17 SQLException (java.sql.SQLException)11 Column (org.h2.table.Column)10 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)6 Expression (org.h2.expression.Expression)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 GridSqlAlias (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias)5 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)4 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)4 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)4 Session (org.h2.engine.Session)4 ValueExpression (org.h2.expression.ValueExpression)4