Search in sources :

Example 76 with Delete

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();
}
Also used : Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 77 with Delete

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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 78 with Delete

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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 79 with Delete

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;
            }
        }
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 80 with Delete

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();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Aggregations

Connection (java.sql.Connection)40 PreparedStatement (java.sql.PreparedStatement)39 Statement (java.sql.Statement)38 ResultSet (java.sql.ResultSet)36 JdbcConnection (org.h2.jdbc.JdbcConnection)25 SQLException (java.sql.SQLException)17 SimpleResultSet (org.h2.tools.SimpleResultSet)14 Savepoint (java.sql.Savepoint)13 StatementBuilder (org.h2.util.StatementBuilder)9 DbException (org.h2.message.DbException)8 Column (org.h2.table.Column)8 ValueString (org.h2.value.ValueString)7 Random (java.util.Random)6 Expression (org.h2.expression.Expression)5 ValueExpression (org.h2.expression.ValueExpression)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4