Search in sources :

Example 66 with Db

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

the class MVTableEngine method createTable.

@Override
public TableBase createTable(CreateTableData data) {
    Database db = data.session.getDatabase();
    Store store = init(db);
    MVTable table = new MVTable(data, store);
    table.init(data.session);
    store.tableMap.put(table.getMapName(), table);
    return table;
}
Also used : Database(org.h2.engine.Database) MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore)

Example 67 with Db

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

the class MVTableEngine method init.

/**
 * Initialize the MVStore.
 *
 * @param db the database
 * @return the store
 */
public static Store init(final Database db) {
    Store store = db.getMvStore();
    if (store != null) {
        return store;
    }
    byte[] key = db.getFileEncryptionKey();
    String dbPath = db.getDatabasePath();
    MVStore.Builder builder = new MVStore.Builder();
    store = new Store();
    boolean encrypted = false;
    if (dbPath != null) {
        String fileName = dbPath + Constants.SUFFIX_MV_FILE;
        MVStoreTool.compactCleanUp(fileName);
        builder.fileName(fileName);
        builder.pageSplitSize(db.getPageSize());
        if (db.isReadOnly()) {
            builder.readOnly();
        } else {
            // possibly create the directory
            boolean exists = FileUtils.exists(fileName);
            if (exists && !FileUtils.canWrite(fileName)) {
            // read only
            } else {
                String dir = FileUtils.getParent(fileName);
                FileUtils.createDirectories(dir);
            }
        }
        if (key != null) {
            encrypted = true;
            char[] password = new char[key.length / 2];
            for (int i = 0; i < password.length; i++) {
                password[i] = (char) (((key[i + i] & 255) << 16) | ((key[i + i + 1]) & 255));
            }
            builder.encryptionKey(password);
        }
        if (db.getSettings().compressData) {
            builder.compress();
            // use a larger page split size to improve the compression ratio
            builder.pageSplitSize(64 * 1024);
        }
        builder.backgroundExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                db.setBackgroundException(DbException.convert(e));
            }
        });
    }
    store.open(db, builder, encrypted);
    db.setMvStore(store);
    return store;
}
Also used : MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore) MVStore(org.h2.mvstore.MVStore) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 68 with Db

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

the class TestRecoverKillLoop method runTest.

private void runTest(int count) throws Exception {
    FileUtils.deleteRecursive("data/db", false);
    Random random = new Random(1);
    for (int i = 0; i < count; i++) {
        String[] procDef = { "java", "-cp", getClassPath(), "-Dtest.dir=data/db", TestRecover.class.getName() };
        Process p = Runtime.getRuntime().exec(procDef);
        InputStream in = p.getInputStream();
        OutputCatcher catcher = new OutputCatcher(in);
        catcher.start();
        while (true) {
            String s = catcher.readLine(60 * 1000);
            // System.out.println("> " + s);
            if (s == null) {
                fail("No reply from process");
            } else if (s.startsWith("testing...")) {
                int sleep = random.nextInt(10000);
                Thread.sleep(sleep);
                printTime("killing");
                p.destroy();
                p.waitFor();
                break;
            } else if (s.startsWith("error!")) {
                fail("Failed: " + s);
            }
        }
    }
}
Also used : Random(java.util.Random) InputStream(java.io.InputStream) OutputCatcher(org.h2.test.synth.OutputCatcher)

Example 69 with Db

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

the class ModelsTest method testModelGeneration.

private void testModelGeneration() {
    DbInspector inspector = new DbInspector(db);
    List<String> models = inspector.generateModel(null, "SupportedTypes", "org.h2.test.jaqu", true, true);
    assertEquals(1, models.size());
    // a poor test, but a start
    assertEquals(1364, models.get(0).length());
}
Also used : DbInspector(org.h2.jaqu.DbInspector)

Example 70 with Db

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

the class AliasMapTest method test.

@Override
public void test() throws Exception {
    Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
    db.insertAll(Product.getList());
    Product p = new Product();
    List<Product> products = db.from(p).where(p.unitsInStock).is(9).orderBy(p.productId).select();
    assertEquals("[]", products.toString());
    db.close();
}
Also used : Db(org.h2.jaqu.Db)

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