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);
}
}
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);
}
}
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;
}
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);
}
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();
}
}
}
}
Aggregations