Search in sources :

Example 6 with Explain

use of org.h2.command.dml.Explain in project ignite by apache.

the class GridSqlUpdate method getSQL.

/**
 * {@inheritDoc}
 */
@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN " : "");
    buff.append("UPDATE ").append(target.getSQL()).append("\nSET\n");
    for (GridSqlColumn c : cols) {
        GridSqlElement e = set.get(c.columnName());
        buff.appendExceptFirst(",\n    ");
        buff.append(c.columnName()).append(" = ").append(e != null ? e.getSQL() : "DEFAULT");
    }
    if (where != null)
        buff.append("\nWHERE ").append(StringUtils.unEnclose(where.getSQL()));
    if (limit != null)
        buff.append("\nLIMIT ").append(StringUtils.unEnclose(limit.getSQL()));
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 7 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class Parser method parsePrepared.

private Prepared parsePrepared() {
    int start = lastParseIndex;
    Prepared c = null;
    String token = currentToken;
    if (token.length() == 0) {
        c = new NoOperation(session);
    } else {
        char first = token.charAt(0);
        switch(first) {
            case '?':
                // read the ? as a parameter
                readTerm();
                // this is an 'out' parameter - set a dummy value
                parameters.get(0).setValue(ValueNull.INSTANCE);
                read("=");
                read("CALL");
                c = parseCall();
                break;
            case '(':
                c = parseSelect();
                break;
            case 'a':
            case 'A':
                if (readIf("ALTER")) {
                    c = parseAlter();
                } else if (readIf("ANALYZE")) {
                    c = parseAnalyze();
                }
                break;
            case 'b':
            case 'B':
                if (readIf("BACKUP")) {
                    c = parseBackup();
                } else if (readIf("BEGIN")) {
                    c = parseBegin();
                }
                break;
            case 'c':
            case 'C':
                if (readIf("COMMIT")) {
                    c = parseCommit();
                } else if (readIf("CREATE")) {
                    c = parseCreate();
                } else if (readIf("CALL")) {
                    c = parseCall();
                } else if (readIf("CHECKPOINT")) {
                    c = parseCheckpoint();
                } else if (readIf("COMMENT")) {
                    c = parseComment();
                }
                break;
            case 'd':
            case 'D':
                if (readIf("DELETE")) {
                    c = parseDelete();
                } else if (readIf("DROP")) {
                    c = parseDrop();
                } else if (readIf("DECLARE")) {
                    // support for DECLARE GLOBAL TEMPORARY TABLE...
                    c = parseCreate();
                } else if (readIf("DEALLOCATE")) {
                    c = parseDeallocate();
                }
                break;
            case 'e':
            case 'E':
                if (readIf("EXPLAIN")) {
                    c = parseExplain();
                } else if (readIf("EXECUTE")) {
                    c = parseExecute();
                }
                break;
            case 'f':
            case 'F':
                if (isToken("FROM")) {
                    c = parseSelect();
                }
                break;
            case 'g':
            case 'G':
                if (readIf("GRANT")) {
                    c = parseGrantRevoke(CommandInterface.GRANT);
                }
                break;
            case 'h':
            case 'H':
                if (readIf("HELP")) {
                    c = parseHelp();
                }
                break;
            case 'i':
            case 'I':
                if (readIf("INSERT")) {
                    c = parseInsert();
                }
                break;
            case 'm':
            case 'M':
                if (readIf("MERGE")) {
                    c = parseMerge();
                }
                break;
            case 'p':
            case 'P':
                if (readIf("PREPARE")) {
                    c = parsePrepare();
                }
                break;
            case 'r':
            case 'R':
                if (readIf("ROLLBACK")) {
                    c = parseRollback();
                } else if (readIf("REVOKE")) {
                    c = parseGrantRevoke(CommandInterface.REVOKE);
                } else if (readIf("RUNSCRIPT")) {
                    c = parseRunScript();
                } else if (readIf("RELEASE")) {
                    c = parseReleaseSavepoint();
                } else if (readIf("REPLACE")) {
                    c = parseReplace();
                }
                break;
            case 's':
            case 'S':
                if (isToken("SELECT")) {
                    c = parseSelect();
                } else if (readIf("SET")) {
                    c = parseSet();
                } else if (readIf("SAVEPOINT")) {
                    c = parseSavepoint();
                } else if (readIf("SCRIPT")) {
                    c = parseScript();
                } else if (readIf("SHUTDOWN")) {
                    c = parseShutdown();
                } else if (readIf("SHOW")) {
                    c = parseShow();
                }
                break;
            case 't':
            case 'T':
                if (readIf("TRUNCATE")) {
                    c = parseTruncate();
                }
                break;
            case 'u':
            case 'U':
                if (readIf("UPDATE")) {
                    c = parseUpdate();
                } else if (readIf("USE")) {
                    c = parseUse();
                }
                break;
            case 'v':
            case 'V':
                if (readIf("VALUES")) {
                    c = parseValues();
                }
                break;
            case 'w':
            case 'W':
                if (readIf("WITH")) {
                    c = parseWithStatementOrQuery();
                }
                break;
            case ';':
                c = new NoOperation(session);
                break;
            default:
                throw getSyntaxError();
        }
        if (indexedParameterList != null) {
            for (int i = 0, size = indexedParameterList.size(); i < size; i++) {
                if (indexedParameterList.get(i) == null) {
                    indexedParameterList.set(i, new Parameter(i));
                }
            }
            parameters = indexedParameterList;
        }
        if (readIf("{")) {
            do {
                int index = (int) readLong() - 1;
                if (index < 0 || index >= parameters.size()) {
                    throw getSyntaxError();
                }
                Parameter p = parameters.get(index);
                if (p == null) {
                    throw getSyntaxError();
                }
                read(":");
                Expression expr = readExpression();
                expr = expr.optimize(session);
                p.setValue(expr.getValue(session));
            } while (readIf(","));
            read("}");
            for (Parameter p : parameters) {
                p.checkSet();
            }
            parameters.clear();
        }
    }
    if (c == null) {
        throw getSyntaxError();
    }
    setSQL(c, null, start);
    return c;
}
Also used : NoOperation(org.h2.command.dml.NoOperation) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Parameter(org.h2.expression.Parameter) ConditionInParameter(org.h2.expression.ConditionInParameter) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 8 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class TestSpatial method testExplainSpatialIndexWithPk.

private void testExplainSpatialIndexWithPk() throws SQLException {
    deleteDb("spatial");
    try (Connection conn = getConnection(URL)) {
        Statement stat = conn.createStatement();
        stat.execute("drop table if exists pt_cloud;");
        stat.execute("CREATE TABLE PT_CLOUD(id serial, the_geom geometry) AS " + "SELECT null, CONCAT('POINT(',A.X,' ',B.X,')')::geometry the_geom " + "from system_range(0,120) A,system_range(0,10) B;");
        stat.execute("create spatial index on pt_cloud(the_geom);");
        try (ResultSet rs = stat.executeQuery("explain select * from  PT_CLOUD " + "where the_geom && 'POINT(1 1)'")) {
            assertTrue(rs.next());
            assertFalse("H2 should use spatial index got this explain:\n" + rs.getString(1), rs.getString(1).contains("tableScan"));
        }
    }
    deleteDb("spatial");
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 9 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class TestSpatial method testSpatialIndexWithOrder.

private void testSpatialIndexWithOrder() throws SQLException {
    deleteDb("spatial");
    try (Connection conn = getConnection(URL)) {
        Statement stat = conn.createStatement();
        stat.execute("DROP TABLE IF EXISTS BUILDINGS;" + "CREATE TABLE BUILDINGS (PK serial, THE_GEOM geometry);" + "insert into buildings(the_geom) SELECT 'POINT(1 1)" + "'::geometry from SYSTEM_RANGE(1,10000);\n" + "CREATE SPATIAL INDEX ON PUBLIC.BUILDINGS(THE_GEOM);\n");
        try (ResultSet rs = stat.executeQuery("EXPLAIN SELECT * FROM " + "BUILDINGS ORDER BY PK LIMIT 51;")) {
            assertTrue(rs.next());
            assertTrue(rs.getString(1).contains("PRIMARY_KEY"));
        }
    }
    deleteDb("spatial");
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 10 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class TestSpatial method testScanIndexOnNonSpatialQuery.

private void testScanIndexOnNonSpatialQuery() throws SQLException {
    deleteDb("spatial");
    try (Connection conn = getConnection(URL)) {
        Statement stat = conn.createStatement();
        stat.execute("drop table if exists test");
        stat.execute("create table test(id serial primary key, " + "value double, the_geom geometry)");
        stat.execute("create spatial index spatial on test(the_geom)");
        ResultSet rs = stat.executeQuery("explain select * from test where _ROWID_ = 5");
        assertTrue(rs.next());
        assertFalse(rs.getString(1).contains("/* PUBLIC.SPATIAL: _ROWID_ = " + "5 */"));
    }
    deleteDb("spatial");
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Aggregations

ResultSet (java.sql.ResultSet)26 Statement (java.sql.Statement)24 Connection (java.sql.Connection)23 PreparedStatement (java.sql.PreparedStatement)21 SimpleResultSet (org.h2.tools.SimpleResultSet)21 StatementBuilder (org.h2.util.StatementBuilder)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CacheException (javax.cache.CacheException)3 GridCacheSqlQuery (org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)3 GridCacheTwoStepQuery (org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery)3 Query (org.h2.command.dml.Query)3 ValueString (org.h2.value.ValueString)3 Collections.singletonList (java.util.Collections.singletonList)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 IgniteException (org.apache.ignite.IgniteException)2 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 GridIntList (org.apache.ignite.internal.util.GridIntList)2