Search in sources :

Example 51 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestRecovery method testRunScript.

private void testRunScript() throws SQLException {
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
    org.h2.Driver.load();
    Connection conn = getConnection("recovery");
    Statement stat = conn.createStatement();
    stat.execute("create table \"Joe\"\"s Table\" as " + "select 1");
    stat.execute("create table test as " + "select * from system_range(1, 100)");
    stat.execute("create view \"TEST VIEW OF TABLE TEST\" as " + "select * from test");
    stat.execute("create table a(id int primary key) as " + "select * from system_range(1, 100)");
    stat.execute("create table b(id int references a(id)) as " + "select * from system_range(1, 100)");
    stat.execute("create table lob(c clob, b blob) as " + "select space(10000) || 'end', SECURE_RAND(10000)");
    stat.execute("create table d(d varchar) as " + "select space(10000) || 'end'");
    stat.execute("alter table a add foreign key(id) references b(id)");
    // all rows have the same value - so that SCRIPT can't re-order the rows
    stat.execute("create table e(id varchar) as " + "select space(10) from system_range(1, 1000)");
    stat.execute("create index idx_e_id on e(id)");
    conn.close();
    Recover rec = new Recover();
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    rec.setOut(new PrintStream(buff));
    rec.runTool("-dir", getBaseDir(), "-db", "recovery", "-trace");
    String out = new String(buff.toByteArray());
    assertContains(out, "Created file");
    Connection conn2 = getConnection("recovery2");
    Statement stat2 = conn2.createStatement();
    String name = "recovery.h2.sql";
    stat2.execute("runscript from '" + getBaseDir() + "/" + name + "'");
    stat2.execute("select * from test");
    conn2.close();
    conn = getConnection("recovery");
    stat = conn.createStatement();
    conn2 = getConnection("recovery2");
    stat2 = conn2.createStatement();
    assertEqualDatabases(stat, stat2);
    conn.close();
    conn2.close();
    Recover.execute(getBaseDir(), "recovery");
    deleteDb("recovery");
    deleteDb("recovery2");
    FileUtils.delete(getBaseDir() + "/recovery.h2.sql");
    String dir = getBaseDir() + "/recovery.lobs.db";
    FileUtils.deleteRecursive(dir, false);
}
Also used : PrintStream(java.io.PrintStream) Statement(java.sql.Statement) Connection(java.sql.Connection) Recover(org.h2.tools.Recover) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 52 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class H2Database method openDatabase.

/**
 * Open a connection to the given database.
 *
 * @param path the database file name
 * @param factory the cursor factory
 * @param flags 0, or a combination of OPEN_READONLY and CREATE_IF_NECESSARY
 * @return a connection to this database
 */
public static H2Database openDatabase(String path, H2Database.CursorFactory factory, int flags) {
    ConnectionInfo ci = new ConnectionInfo(path);
    if ((flags & OPEN_READWRITE) != 0) {
    // TODO readonly connections
    }
    if ((flags & CREATE_IF_NECESSARY) == 0) {
        ci.setProperty("IFEXISTS", "TRUE");
    }
    ci.setProperty("FILE_LOCK", "FS");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 53 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class H2Database method create.

/**
 * Create a new in-memory database.
 *
 * @param factory the cursor factory
 * @return a connection to this database
 */
public static H2Database create(H2Database.CursorFactory factory) {
    ConnectionInfo ci = new ConnectionInfo("mem:");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 54 with Db

use of org.h2.jaqu.Db 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 55 with Db

use of org.h2.jaqu.Db 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

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6