Search in sources :

Example 76 with Set

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

the class DbUpgrade method connectOrUpgrade.

/**
 * If the upgrade classes are present, upgrade the database, or connect
 * using the old version (if the parameter NO_UPGRADE is set to true). If
 * the database is upgraded, or if no upgrade is possible or needed, this
 * methods returns null.
 *
 * @param url the database URL
 * @param info the properties
 * @return the connection if connected with the old version (NO_UPGRADE)
 */
public static Connection connectOrUpgrade(String url, Properties info) throws SQLException {
    if (!UPGRADE_CLASSES_PRESENT) {
        return null;
    }
    Properties i2 = new Properties();
    i2.putAll(info);
    // clone so that the password (if set as a char array) is not cleared
    Object o = info.get("password");
    if (o instanceof char[]) {
        i2.put("password", StringUtils.cloneCharArray((char[]) o));
    }
    info = i2;
    ConnectionInfo ci = new ConnectionInfo(url, info);
    if (ci.isRemote() || !ci.isPersistent()) {
        return null;
    }
    String name = ci.getName();
    if (FileUtils.exists(name + Constants.SUFFIX_PAGE_FILE)) {
        return null;
    }
    if (!FileUtils.exists(name + Constants.SUFFIX_OLD_DATABASE_FILE)) {
        return null;
    }
    if (ci.removeProperty("NO_UPGRADE", false)) {
        return connectWithOldVersion(url, info);
    }
    synchronized (DbUpgrade.class) {
        upgrade(ci, info);
        return null;
    }
}
Also used : ConnectionInfo(org.h2.engine.ConnectionInfo) Properties(java.util.Properties)

Example 77 with Set

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

the class RunScript method execute.

/**
 * Executes the SQL commands read from the reader against a database.
 *
 * @param conn the connection to a database
 * @param reader the reader
 * @return the last result set
 */
public static ResultSet execute(Connection conn, Reader reader) throws SQLException {
    // can not close the statement because we return a result set from it
    Statement stat = conn.createStatement();
    ResultSet rs = null;
    ScriptReader r = new ScriptReader(reader);
    while (true) {
        String sql = r.readStatement();
        if (sql == null) {
            break;
        }
        if (sql.trim().length() == 0) {
            continue;
        }
        boolean resultSet = stat.execute(sql);
        if (resultSet) {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            rs = stat.getResultSet();
        }
    }
    return rs;
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ScriptReader(org.h2.util.ScriptReader)

Example 78 with Set

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

the class JdbcPreparedStatement method executeBatch.

/**
 * Executes the batch.
 * If one of the batched statements fails, this database will continue.
 *
 * @return the array of update counts
 */
@Override
public int[] executeBatch() throws SQLException {
    try {
        debugCodeCall("executeBatch");
        if (batchParameters == null) {
            // TODO batch: check what other database do if no parameters are
            // set
            batchParameters = New.arrayList();
        }
        batchIdentities = new MergedResultSet();
        int size = batchParameters.size();
        int[] result = new int[size];
        boolean error = false;
        SQLException next = null;
        checkClosedForWrite();
        try {
            for (int i = 0; i < size; i++) {
                Value[] set = batchParameters.get(i);
                ArrayList<? extends ParameterInterface> parameters = command.getParameters();
                for (int j = 0; j < set.length; j++) {
                    Value value = set[j];
                    ParameterInterface param = parameters.get(j);
                    param.setValue(value, false);
                }
                try {
                    result[i] = executeUpdateInternal();
                    // Cannot use own implementation, it returns batch identities
                    ResultSet rs = super.getGeneratedKeys();
                    batchIdentities.add(rs);
                } catch (Exception re) {
                    SQLException e = logAndConvert(re);
                    if (next == null) {
                        next = e;
                    } else {
                        e.setNextException(next);
                        next = e;
                    }
                    result[i] = Statement.EXECUTE_FAILED;
                    error = true;
                }
            }
            batchParameters = null;
            if (error) {
                throw new JdbcBatchUpdateException(next, result);
            }
            return result;
        } finally {
            afterWriting();
        }
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) MergedResultSet(org.h2.util.MergedResultSet) SQLException(java.sql.SQLException) Value(org.h2.value.Value) ResultSet(java.sql.ResultSet) MergedResultSet(org.h2.util.MergedResultSet) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 79 with Set

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

the class JdbcResultSet method updateBinaryStream.

/**
 * Updates a column in the current or insert row.
 *
 * @param columnLabel the column label
 * @param x the value
 * @param length the number of characters
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("updateBinaryStream(" + quote(columnLabel) + ", x, " + length + "L);");
        }
        checkClosed();
        Value v = conn.createBlob(x, length);
        update(columnLabel, v);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 80 with Set

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

the class JdbcResultSet method updateBlob.

/**
 * Updates a column in the current or insert row.
 *
 * @param columnLabel the column label
 * @param x the value
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateBlob(String columnLabel, Blob x) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("updateBlob(" + quote(columnLabel) + ", x);");
        }
        checkClosed();
        Value v;
        if (x == null) {
            v = ValueNull.INSTANCE;
        } else {
            v = conn.createBlob(x.getBinaryStream(), -1);
        }
        update(columnLabel, v);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

SQLException (java.sql.SQLException)107 DbException (org.h2.message.DbException)80 PreparedStatement (java.sql.PreparedStatement)78 Connection (java.sql.Connection)70 Statement (java.sql.Statement)63 ResultSet (java.sql.ResultSet)62 Value (org.h2.value.Value)51 JdbcConnection (org.h2.jdbc.JdbcConnection)48 SimpleResultSet (org.h2.tools.SimpleResultSet)36 HashSet (java.util.HashSet)28 Column (org.h2.table.Column)24 Savepoint (java.sql.Savepoint)23 ValueString (org.h2.value.ValueString)21 IOException (java.io.IOException)20 Random (java.util.Random)17 Expression (org.h2.expression.Expression)16 Task (org.h2.util.Task)16 ExpressionColumn (org.h2.expression.ExpressionColumn)14 ValueExpression (org.h2.expression.ValueExpression)13 IndexColumn (org.h2.table.IndexColumn)13