Search in sources :

Example 51 with Prepared

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

the class MetaRecord method execute.

/**
 * Execute the meta data statement.
 *
 * @param db the database
 * @param systemSession the system session
 * @param listener the database event listener
 */
void execute(Database db, Session systemSession, DatabaseEventListener listener) {
    try {
        Prepared command = systemSession.prepare(sql);
        command.setObjectId(id);
        command.update();
    } catch (DbException e) {
        e = e.addSQL(sql);
        SQLException s = e.getSQLException();
        db.getTrace(Trace.DATABASE).error(s, sql);
        if (listener != null) {
            listener.exceptionThrown(s, sql);
        // continue startup in this case
        } else {
            throw e;
        }
    }
}
Also used : SQLException(java.sql.SQLException) Prepared(org.h2.command.Prepared) DbException(org.h2.message.DbException)

Example 52 with Prepared

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

the class WebApp method executeLoop.

private String executeLoop(Connection conn, int count, String sql) throws SQLException {
    ArrayList<Integer> params = New.arrayList();
    int idx = 0;
    while (!stop) {
        idx = sql.indexOf('?', idx);
        if (idx < 0) {
            break;
        }
        if (isBuiltIn(sql.substring(idx), "?/*rnd*/")) {
            params.add(1);
            sql = sql.substring(0, idx) + "?" + sql.substring(idx + "/*rnd*/".length() + 1);
        } else {
            params.add(0);
        }
        idx++;
    }
    boolean prepared;
    Random random = new Random(1);
    long time = System.currentTimeMillis();
    if (isBuiltIn(sql, "@statement")) {
        sql = sql.substring("@statement".length()).trim();
        prepared = false;
        Statement stat = conn.createStatement();
        for (int i = 0; !stop && i < count; i++) {
            String s = sql;
            for (Integer type : params) {
                idx = s.indexOf('?');
                if (type.intValue() == 1) {
                    s = s.substring(0, idx) + random.nextInt(count) + s.substring(idx + 1);
                } else {
                    s = s.substring(0, idx) + i + s.substring(idx + 1);
                }
            }
            if (stat.execute(s)) {
                ResultSet rs = stat.getResultSet();
                while (!stop && rs.next()) {
                // maybe get the data as well
                }
                rs.close();
            }
        }
    } else {
        prepared = true;
        PreparedStatement prep = conn.prepareStatement(sql);
        for (int i = 0; !stop && i < count; i++) {
            for (int j = 0; j < params.size(); j++) {
                Integer type = params.get(j);
                if (type.intValue() == 1) {
                    prep.setInt(j + 1, random.nextInt(count));
                } else {
                    prep.setInt(j + 1, i);
                }
            }
            if (session.getContents().isSQLite()) {
                // SQLite currently throws an exception on prep.execute()
                prep.executeUpdate();
            } else {
                if (prep.execute()) {
                    ResultSet rs = prep.getResultSet();
                    while (!stop && rs.next()) {
                    // maybe get the data as well
                    }
                    rs.close();
                }
            }
        }
    }
    time = System.currentTimeMillis() - time;
    StatementBuilder buff = new StatementBuilder();
    buff.append(time).append(" ms: ").append(count).append(" * ");
    if (prepared) {
        buff.append("(Prepared) ");
    } else {
        buff.append("(Statement) ");
    }
    buff.append('(');
    for (int p : params) {
        buff.appendExceptFirst(", ");
        buff.append(p == 0 ? "i" : "rnd");
    }
    return buff.append(") ").append(sql).toString();
}
Also used : Random(java.util.Random) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) StatementBuilder(org.h2.util.StatementBuilder) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 53 with Prepared

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

the class TableLink method updateRows.

@Override
public void updateRows(Prepared prepared, Session session, RowList rows) {
    boolean deleteInsert;
    checkReadOnly();
    if (emitUpdates) {
        for (rows.reset(); rows.hasNext(); ) {
            prepared.checkCanceled();
            Row oldRow = rows.next();
            Row newRow = rows.next();
            linkedIndex.update(oldRow, newRow);
            session.log(this, UndoLogRecord.DELETE, oldRow);
            session.log(this, UndoLogRecord.INSERT, newRow);
        }
        deleteInsert = false;
    } else {
        deleteInsert = true;
    }
    if (deleteInsert) {
        super.updateRows(prepared, session, rows);
    }
}
Also used : Row(org.h2.result.Row)

Example 54 with Prepared

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

the class TableLink method execute.

/**
 * Execute a SQL statement using the given parameters. Prepared
 * statements are kept in a hash map to avoid re-creating them.
 *
 * @param sql the SQL statement
 * @param params the parameters or null
 * @param reusePrepared if the prepared statement can be re-used immediately
 * @return the prepared statement, or null if it is re-used
 */
public PreparedStatement execute(String sql, ArrayList<Value> params, boolean reusePrepared) {
    if (conn == null) {
        throw connectException;
    }
    for (int retry = 0; ; retry++) {
        try {
            synchronized (conn) {
                PreparedStatement prep = preparedMap.remove(sql);
                if (prep == null) {
                    prep = conn.getConnection().prepareStatement(sql);
                }
                if (trace.isDebugEnabled()) {
                    StatementBuilder buff = new StatementBuilder();
                    buff.append(getName()).append(":\n").append(sql);
                    if (params != null && !params.isEmpty()) {
                        buff.append(" {");
                        int i = 1;
                        for (Value v : params) {
                            buff.appendExceptFirst(", ");
                            buff.append(i++).append(": ").append(v.getSQL());
                        }
                        buff.append('}');
                    }
                    buff.append(';');
                    trace.debug(buff.toString());
                }
                if (params != null) {
                    for (int i = 0, size = params.size(); i < size; i++) {
                        Value v = params.get(i);
                        v.set(prep, i + 1);
                    }
                }
                prep.execute();
                if (reusePrepared) {
                    reusePreparedStatement(prep, sql);
                    return null;
                }
                return prep;
            }
        } catch (SQLException e) {
            if (retry >= MAX_RETRY) {
                throw DbException.convert(e);
            }
            conn.close(true);
            connect();
        }
    }
}
Also used : SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 55 with Prepared

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

the class TableView method compileViewQuery.

private Query compileViewQuery(Session session, String sql, boolean literalsChecked, String viewName) {
    Prepared p;
    session.setParsingCreateView(true, viewName);
    try {
        p = session.prepare(sql, false, literalsChecked);
    } finally {
        session.setParsingCreateView(false, viewName);
    }
    if (!(p instanceof Query)) {
        throw DbException.getSyntaxError(sql, 0);
    }
    Query q = (Query) p;
    // only potentially recursive cte queries need to be non-lazy
    if (isTableExpression && allowRecursive) {
        q.setNeverLazy(true);
    }
    return q;
}
Also used : Query(org.h2.command.dml.Query) Prepared(org.h2.command.Prepared)

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