Search in sources :

Example 91 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class UpdatableRow method insertRow.

/**
 * Insert a new row into the database.
 *
 * @param row the new row
 * @throws SQLException if the row could not be inserted
 */
public void insertRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    appendTableName(buff);
    buff.append('(');
    appendColumnList(buff, false);
    buff.append(")VALUES(");
    buff.resetCount();
    for (int i = 0; i < columnCount; i++) {
        buff.appendExceptFirst(",");
        Value v = row[i];
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
        }
    }
    buff.append(')');
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    for (int i = 0, j = 0; i < columnCount; i++) {
        Value v = row[i];
        if (v != null) {
            v.set(prep, j++ + 1);
        }
    }
    int count = prep.executeUpdate();
    if (count != 1) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 92 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class UpdatableRow method deleteRow.

/**
 * Delete the given row in the database.
 *
 * @param current the row
 * @throws SQLException if this row has already been deleted
 */
public void deleteRow(Value[] current) throws SQLException {
    StatementBuilder buff = new StatementBuilder("DELETE FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has already been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) PreparedStatement(java.sql.PreparedStatement)

Example 93 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class UpdatableRow method readRow.

/**
 * Re-reads a row from the database and updates the values in the array.
 *
 * @param row the values that contain the key
 * @return the row
 */
public Value[] readRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("SELECT ");
    appendColumnList(buff, false);
    buff.append(" FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, row);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
    Value[] newRow = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        int type = result.getColumnType(i);
        newRow[i] = DataType.readValue(conn.getSession(), rs, i + 1, type);
    }
    return newRow;
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) ResultSet(java.sql.ResultSet) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 94 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class WebServer method getConnection.

/**
 * Open a database connection.
 *
 * @param driver the driver class name
 * @param databaseUrl the database URL
 * @param user the user name
 * @param password the password
 * @return the database connection
 */
Connection getConnection(String driver, String databaseUrl, String user, String password) throws SQLException {
    driver = driver.trim();
    databaseUrl = databaseUrl.trim();
    org.h2.Driver.load();
    Properties p = new Properties();
    p.setProperty("user", user.trim());
    // do not trim the password, otherwise an
    // encrypted H2 database with empty user password doesn't work
    p.setProperty("password", password);
    if (databaseUrl.startsWith("jdbc:h2:")) {
        if (ifExists) {
            databaseUrl += ";IFEXISTS=TRUE";
        }
        // Properties
        return org.h2.Driver.load().connect(databaseUrl, p);
    }
    // }
    return JdbcUtils.getConnection(driver, databaseUrl, p);
}
Also used : SortedProperties(org.h2.util.SortedProperties) SysProperties(org.h2.engine.SysProperties) Properties(java.util.Properties)

Example 95 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class FileLister method tryUnlockDatabase.

/**
 * Try to lock the database, and then unlock it. If this worked, the
 * .lock.db file will be removed.
 *
 * @param files the database files to check
 * @param message the text to include in the error message
 * @throws SQLException if it failed
 */
public static void tryUnlockDatabase(List<String> files, String message) throws SQLException {
    for (String fileName : files) {
        if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
            FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
            try {
                lock.lock(FileLockMethod.FILE);
                lock.unlock();
            } catch (DbException e) {
                throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, message).getSQLException();
            }
        } else if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
            try (FileChannel f = FilePath.get(fileName).open("r")) {
                java.nio.channels.FileLock lock = f.tryLock(0, Long.MAX_VALUE, true);
                lock.release();
            } catch (Exception e) {
                throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, e, message).getSQLException();
            }
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) TraceSystem(org.h2.message.TraceSystem) SQLException(java.sql.SQLException) DbException(org.h2.message.DbException) DbException(org.h2.message.DbException)

Aggregations

Database (org.h2.engine.Database)79 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)38 DbException (org.h2.message.DbException)37 ResultSet (java.sql.ResultSet)34 Statement (java.sql.Statement)32 SimpleResultSet (org.h2.tools.SimpleResultSet)32 Connection (java.sql.Connection)27 Table (org.h2.table.Table)25 Value (org.h2.value.Value)25 Column (org.h2.table.Column)22 IOException (java.io.IOException)19 Constraint (org.h2.constraint.Constraint)18 Expression (org.h2.expression.Expression)17 ExpressionColumn (org.h2.expression.ExpressionColumn)17 ValueString (org.h2.value.ValueString)15 ValueExpression (org.h2.expression.ValueExpression)14 Session (org.h2.engine.Session)13 JdbcConnection (org.h2.jdbc.JdbcConnection)13 Schema (org.h2.schema.Schema)13