Search in sources :

Example 36 with Insert

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

the class TestFileLockSerialized method testLeftLogFiles.

private void testLeftLogFiles() throws Exception {
    deleteDb("fileLockSerialized");
    // without serialized
    String url;
    url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int)");
    stat.execute("insert into test values(0)");
    conn.close();
    List<String> filesWithoutSerialized = FileUtils.newDirectoryStream(getBaseDir());
    deleteDb("fileLockSerialized");
    // with serialized
    url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED";
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    Thread.sleep(500);
    stat.execute("insert into test values(0)");
    conn.close();
    List<String> filesWithSerialized = FileUtils.newDirectoryStream(getBaseDir());
    if (filesWithoutSerialized.size() != filesWithSerialized.size()) {
        for (int i = 0; i < filesWithoutSerialized.size(); i++) {
            if (!filesWithSerialized.contains(filesWithoutSerialized.get(i))) {
                System.out.println("File left from 'without serialized' mode: " + filesWithoutSerialized.get(i));
            }
        }
        for (int i = 0; i < filesWithSerialized.size(); i++) {
            if (!filesWithoutSerialized.contains(filesWithSerialized.get(i))) {
                System.out.println("File left from 'with serialized' mode: " + filesWithSerialized.get(i));
            }
        }
        fail("With serialized it must create the same files than without serialized");
    }
    deleteDb("fileLockSerialized");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 37 with Insert

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

the class TestFileLockSerialized method testConcurrentReadWrite.

private void testConcurrentReadWrite() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
    // ;TRACE_LEVEL_SYSTEM_OUT=3
    // String readUrl = writeUrl + ";ACCESS_MODE_DATA=R";
    trace(" create database");
    Class.forName("org.h2.Driver");
    Connection conn = getConnection(writeUrl, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    Connection conn3 = getConnection(writeUrl, "sa", "sa");
    PreparedStatement prep3 = conn3.prepareStatement("insert into test values(?)");
    Connection conn2 = getConnection(writeUrl, "sa", "sa");
    Statement stat2 = conn2.createStatement();
    printResult(stat2, "select * from test");
    stat2.execute("create local temporary table temp(name varchar) not persistent");
    printResult(stat2, "select * from temp");
    trace(" insert row 1");
    stat.execute("insert into test values(1)");
    trace(" insert row 2");
    prep3.setInt(1, 2);
    prep3.execute();
    printResult(stat2, "select * from test");
    printResult(stat2, "select * from temp");
    conn.close();
    conn2.close();
    conn3.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Example 38 with Insert

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

the class TestFileLockSerialized method testCache.

/**
 * Caches must be cleared. Session.reconnect only closes the DiskFile (which
 * is associated with the cache) if there is one session
 */
private void testCache() throws Exception {
    deleteDb("fileLockSerialized");
    String urlShared = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED";
    Connection connShared1 = getConnection(urlShared);
    Statement statement1 = connShared1.createStatement();
    Connection connShared2 = getConnection(urlShared);
    Statement statement2 = connShared2.createStatement();
    statement1.execute("create table test1(id int)");
    statement1.execute("insert into test1 values(1)");
    ResultSet rs = statement1.executeQuery("select id from test1");
    rs.close();
    rs = statement2.executeQuery("select id from test1");
    rs.close();
    statement1.execute("update test1 set id=2");
    Thread.sleep(500);
    rs = statement2.executeQuery("select id from test1");
    assertTrue(rs.next());
    assertEquals(2, rs.getInt(1));
    rs.close();
    connShared1.close();
    connShared2.close();
    deleteDb("fileLockSerialized");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 39 with Insert

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

the class Test method main.

public static void main(String... args) throws Exception {
    H2Database db = H2Utils.openOrCreateDatabase("helloWorld.db", MODE_PRIVATE, null);
    log("opened ps=" + db.getPageSize());
    try {
        // db.execSQL("DROP TABLE IF EXISTS test");
        // log("dropped");
        db.execSQL("CREATE TABLE if not exists test(ID INTEGER PRIMARY KEY, NAME VARCHAR)");
        log("created");
        for (int j = 0; j < 10; j++) {
            Cursor c = db.rawQuery("select * from test", new String[0]);
            int count = c.getCount();
            for (int i = 0; i < count; i++) {
                c.move(1);
                c.getInt(0);
                c.getString(1);
            }
            c.close();
        }
        // log("select " + count);
        db.execSQL("delete from test");
        log("delete");
        db.beginTransaction();
        for (int i = 0; i < 1000; i++) {
            db.execSQL("INSERT INTO TEST VALUES(?, 'Hello')", new Object[] { i });
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        log("inserted");
        for (int i = 0; i < 10; i++) {
            Cursor c = db.rawQuery("select * from test where id=?", new String[] { "" + i });
            int count = c.getCount();
            if (count > 0) {
                c.move(1);
                c.getInt(0);
                c.getString(1);
            }
            c.close();
        }
        log("select");
    } finally {
        db.close();
        log("closed");
    }
}
Also used : Cursor(android.database.Cursor) H2Database(org.h2.android.H2Database)

Example 40 with Insert

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

the class TableDefinition method insert.

long insert(Db db, Object obj, boolean returnKey) {
    SQLStatement stat = new SQLStatement(db);
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(db.getDialect().getTableName(schemaName, tableName)).append('(');
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append(field.columnName);
    }
    buff.append(") VALUES(");
    buff.resetCount();
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append('?');
        Object value = getValue(obj, field);
        stat.addParameter(value);
    }
    buff.append(')');
    stat.setSQL(buff.toString());
    StatementLogger.insert(stat.getSQL());
    if (returnKey) {
        return stat.executeInsert();
    }
    return stat.executeUpdate();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Aggregations

Statement (java.sql.Statement)215 ResultSet (java.sql.ResultSet)204 PreparedStatement (java.sql.PreparedStatement)201 Connection (java.sql.Connection)199 JdbcConnection (org.h2.jdbc.JdbcConnection)97 SimpleResultSet (org.h2.tools.SimpleResultSet)64 SQLException (java.sql.SQLException)56 JdbcStatement (org.h2.jdbc.JdbcStatement)46 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)35 Savepoint (java.sql.Savepoint)32 Random (java.util.Random)28 Value (org.h2.value.Value)28 DbException (org.h2.message.DbException)27 Task (org.h2.util.Task)17 Column (org.h2.table.Column)16 ValueString (org.h2.value.ValueString)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 StringReader (java.io.StringReader)12 InputStream (java.io.InputStream)11 ArrayList (java.util.ArrayList)11