use of org.h2.command.dml.Delete in project h2database by h2database.
the class TestLob method testLobRollbackStop.
private void testLobRollbackStop() throws SQLException {
deleteDb("lob");
Connection conn = reconnect(null);
conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB)");
conn.createStatement().execute("INSERT INTO TEST VALUES(1, SPACE(10000))");
conn.setAutoCommit(false);
conn.createStatement().execute("DELETE FROM TEST");
conn.createStatement().execute("CHECKPOINT");
conn.createStatement().execute("SHUTDOWN IMMEDIATELY");
conn = reconnect(conn);
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
assertTrue(rs.next());
rs.getInt(1);
assertEquals(10000, rs.getString(2).length());
conn.close();
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class TestLob method testConcurrentRemoveRead.
private void testConcurrentRemoveRead() throws Exception {
if (config.lazy) {
return;
}
deleteDb("lob");
final String url = getURL("lob", true);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("set max_length_inplace_lob 5");
stat.execute("create table lob(data clob)");
stat.execute("insert into lob values(space(100))");
Connection conn2 = getConnection(url);
Statement stat2 = conn2.createStatement();
ResultSet rs = stat2.executeQuery("select data from lob");
rs.next();
stat.execute("delete lob");
InputStream in = rs.getBinaryStream(1);
in.read();
conn2.close();
conn.close();
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class TestLob method testCopyLob.
private void testCopyLob() throws Exception {
if (config.memory) {
return;
}
deleteDb("lob");
Connection conn;
Statement stat;
ResultSet rs;
conn = getConnection("lob");
stat = conn.createStatement();
stat.execute("create table test(id identity, data clob) " + "as select 1, space(10000)");
stat.execute("insert into test(id, data) select 2, data from test");
stat.execute("delete from test where id = 1");
conn.close();
conn = getConnection("lob");
stat = conn.createStatement();
rs = stat.executeQuery("select * from test");
rs.next();
assertEquals(10000, rs.getString(2).length());
conn.close();
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class TestConcurrent method test.
@Override
public void test() throws Exception {
String url = "jdbc:h2:mem:";
for (int i = 0; i < 50; i++) {
final int x = i % 4;
final Connection conn = DriverManager.getConnection(url);
final Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key)");
String sql = "";
switch(x % 6) {
case 0:
sql = "select 1";
break;
case 1:
case 2:
sql = "delete from test";
break;
}
final PreparedStatement prep = conn.prepareStatement(sql);
Task t = new Task() {
@Override
public void call() throws SQLException {
while (!conn.isClosed()) {
switch(x % 6) {
case 0:
prep.executeQuery();
break;
case 1:
prep.execute();
break;
case 2:
prep.executeUpdate();
break;
case 3:
stat.executeQuery("select 1");
break;
case 4:
stat.execute("select 1");
break;
case 5:
stat.execute("delete from test");
break;
}
}
}
};
t.execute();
Thread.sleep(100);
conn.close();
SQLException e = (SQLException) t.getException();
if (e != null) {
if (ErrorCode.OBJECT_CLOSED != e.getErrorCode() && ErrorCode.STATEMENT_WAS_CANCELED != e.getErrorCode()) {
throw e;
}
}
}
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class TableDefinition method delete.
void delete(Db db, Object obj) {
if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass() + " - no update possible");
}
SQLStatement stat = new SQLStatement(db);
StatementBuilder buff = new StatementBuilder("DELETE FROM ");
buff.append(db.getDialect().getTableName(schemaName, tableName));
buff.resetCount();
Object alias = ClassUtils.newObject(obj.getClass());
Query<Object> query = Query.from(db, alias);
boolean firstCondition = true;
for (FieldDefinition field : fields) {
if (field.isPrimaryKey) {
Object aliasValue = field.getValue(alias);
Object value = field.getValue(obj);
if (!firstCondition) {
query.addConditionToken(ConditionAndOr.AND);
}
firstCondition = false;
query.addConditionToken(new Condition<>(aliasValue, value, CompareType.EQUAL));
}
}
stat.setSQL(buff.toString());
query.appendWhere(stat);
StatementLogger.delete(stat.getSQL());
stat.executeUpdate();
}
Aggregations