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