Search in sources :

Example 71 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class PrepareProcedure method update.

@Override
public int update() {
    Procedure proc = new Procedure(procedureName, prepared);
    prepared.setParameterList(parameters);
    prepared.setPrepareAlways(prepareAlways);
    prepared.prepare();
    session.addProcedure(proc);
    return 0;
}
Also used : Procedure(org.h2.engine.Procedure)

Example 72 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class JdbcConnection method prepareStatement.

/**
 * Creates a new prepared statement.
 *
 * @param sql the SQL statement
 * @return the prepared statement
 * @throws SQLException if the connection is closed
 */
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
    try {
        int id = getNextId(TraceObject.PREPARED_STATEMENT);
        if (isDebugEnabled()) {
            debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, "prepareStatement(" + quote(sql) + ")");
        }
        checkClosed();
        sql = translateSQL(sql);
        return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY, Constants.DEFAULT_RESULT_SET_CONCURRENCY, false, false);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 73 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class JdbcConnection method prepareStatement.

/**
 * Creates a new prepared statement.
 *
 * @param sql the SQL statement
 * @param columnIndexes
 *            an array of column indexes indicating the columns with generated
 *            keys that should be returned from the inserted row
 * @return the prepared statement
 * @throws SQLException if the connection is closed
 */
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
    try {
        int id = getNextId(TraceObject.PREPARED_STATEMENT);
        if (isDebugEnabled()) {
            debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, "prepareStatement(" + quote(sql) + ", " + quoteIntArray(columnIndexes) + ");");
        }
        checkClosed();
        sql = translateSQL(sql);
        return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY, Constants.DEFAULT_RESULT_SET_CONCURRENCY, false, columnIndexes);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 74 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class ViewIndex method prepareSubQuery.

private static Query prepareSubQuery(String sql, Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder) {
    Prepared p;
    session.pushSubQueryInfo(masks, filters, filter, sortOrder);
    try {
        p = session.prepare(sql, true, true);
    } finally {
        session.popSubQueryInfo();
    }
    return (Query) p;
}
Also used : Query(org.h2.command.dml.Query) Prepared(org.h2.command.Prepared)

Example 75 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class TestFuzzOptimizations method testIn.

/*
        drop table test0;
        drop table test1;
        create table test0(a int, b int, c int);
        create index idx_1 on test0(a);
        create index idx_2 on test0(b, a);
        create table test1(a int, b int, c int);
        insert into test0 select x / 100,
            mod(x / 10, 10), mod(x, 10)
            from system_range(0, 999);
        update test0 set a = null where a = 9;
        update test0 set b = null where b = 9;
        update test0 set c = null where c = 9;
        insert into test1 select * from test0;

        select * from test0 where
        b in(null, 0) and a in(2, null, null)
        order by 1, 2, 3;

        select * from test1 where
        b in(null, 0) and a in(2, null, null)
        order by 1, 2, 3;
     */
private void testIn() throws SQLException {
    Db db = new Db(conn);
    db.execute("create table test0(a int, b int, c int)");
    db.execute("create index idx_1 on test0(a)");
    db.execute("create index idx_2 on test0(b, a)");
    db.execute("create table test1(a int, b int, c int)");
    db.execute("insert into test0 select x / 100, " + "mod(x / 10, 10), mod(x, 10) from system_range(0, 999)");
    db.execute("update test0 set a = null where a = 9");
    db.execute("update test0 set b = null where b = 9");
    db.execute("update test0 set c = null where c = 9");
    db.execute("insert into test1 select * from test0");
    // this failed at some point
    Prepared p = db.prepare("select * from test0 where b in(" + "select a from test1 where a <? and a not in(" + "select c from test1 where b <=10 and a in(" + "select a from test1 where b =1 or b =2 and b not in(2))) or c <>a) " + "and c in(0, 10) and c in(10, 0, 0) order by 1, 2, 3");
    p.set(1);
    p.execute();
    Random seedGenerator = new Random();
    String[] columns = new String[] { "a", "b", "c" };
    String[] values = new String[] { null, "0", "0", "1", "2", "10", "a", "?" };
    String[] compares = new String[] { "in(", "not in(", "=", "=", ">", "<", ">=", "<=", "<>", "in(select", "not in(select" };
    int size = getSize(100, 1000);
    for (int i = 0; i < size; i++) {
        long seed = seedGenerator.nextLong();
        println("seed: " + seed);
        Random random = new Random(seed);
        ArrayList<String> params = New.arrayList();
        String condition = getRandomCondition(random, params, columns, compares, values);
        String message = "seed: " + seed + " " + condition;
        executeAndCompare(condition, params, message);
        if (params.size() > 0) {
            for (int j = 0; j < params.size(); j++) {
                String value = values[random.nextInt(values.length - 2)];
                params.set(j, value);
            }
            executeAndCompare(condition, params, message);
        }
    }
    executeAndCompare("a >=0 and b in(?, 2) and a in(1, ?, null)", Arrays.asList("10", "2"), "seed=-6191135606105920350L");
    db.execute("drop table test0, test1");
}
Also used : Random(java.util.Random) Prepared(org.h2.test.db.Db.Prepared) Db(org.h2.test.db.Db)

Aggregations

Prepared (org.h2.command.Prepared)32 ValueString (org.h2.value.ValueString)16 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)11 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)11 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)10 Parameter (org.h2.expression.Parameter)10 DbException (org.h2.message.DbException)10 PreparedStatement (java.sql.PreparedStatement)9 Value (org.h2.value.Value)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 Query (org.h2.command.dml.Query)7 Expression (org.h2.expression.Expression)7 Column (org.h2.table.Column)7 Connection (java.sql.Connection)6 IndexColumn (org.h2.table.IndexColumn)6 ResultSet (java.sql.ResultSet)5 SQLClientInfoException (java.sql.SQLClientInfoException)5 Savepoint (java.sql.Savepoint)5