Search in sources :

Example 66 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class TableView method createTableViewMaybeRecursive.

/**
 * Create a view.
 *
 * @param schema the schema
 * @param id the view id
 * @param name the view name
 * @param querySQL the query
 * @param parameters the parameters
 * @param columnTemplates the columns
 * @param session the session
 * @param literalsChecked whether literals in the query are checked
 * @param isTableExpression if this is a table expression
 * @param isPersistent whether the view is persisted
 * @param db the database
 * @return the view
 */
public static TableView createTableViewMaybeRecursive(Schema schema, int id, String name, String querySQL, ArrayList<Parameter> parameters, Column[] columnTemplates, Session session, boolean literalsChecked, boolean isTableExpression, boolean isPersistent, Database db) {
    Table recursiveTable = TableView.createShadowTableForRecursiveTableExpression(isPersistent, session, name, schema, Arrays.asList(columnTemplates), db);
    List<Column> columnTemplateList;
    String[] querySQLOutput = { null };
    ArrayList<String> columnNames = new ArrayList<>();
    for (Column columnTemplate : columnTemplates) {
        columnNames.add(columnTemplate.getName());
    }
    try {
        Prepared withQuery = session.prepare(querySQL, false, false);
        if (isPersistent) {
            withQuery.setSession(session);
        }
        columnTemplateList = TableView.createQueryColumnTemplateList(columnNames.toArray(new String[1]), (Query) withQuery, querySQLOutput);
    } finally {
        TableView.destroyShadowTableForRecursiveExpression(isPersistent, session, recursiveTable);
    }
    // build with recursion turned on
    TableView view = new TableView(schema, id, name, querySQL, parameters, columnTemplateList.toArray(columnTemplates), session, true, /* try recursive */
    literalsChecked, isTableExpression, isPersistent);
    // and no recursive index
    if (!view.isRecursiveQueryDetected()) {
        if (isPersistent) {
            db.addSchemaObject(session, view);
            view.lock(session, true, true);
            session.getDatabase().removeSchemaObject(session, view);
            // during database startup - this method does not normally get called - and it
            // needs to be to correctly un-register the table which the table expression
            // uses...
            view.removeChildrenAndResources(session);
        } else {
            session.removeLocalTempTable(view);
        }
        view = new TableView(schema, id, name, querySQL, parameters, columnTemplates, session, false, /* detected not recursive */
        literalsChecked, isTableExpression, isPersistent);
    }
    return view;
}
Also used : Query(org.h2.command.dml.Query) ExpressionColumn(org.h2.expression.ExpressionColumn) ArrayList(java.util.ArrayList) Prepared(org.h2.command.Prepared)

Example 67 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class Db method upgradeDb.

Db upgradeDb() {
    if (!upgradeChecked.contains(dbUpgrader.getClass())) {
        // flag as checked immediately because calls are nested.
        upgradeChecked.add(dbUpgrader.getClass());
        JQDatabase model = dbUpgrader.getClass().getAnnotation(JQDatabase.class);
        if (model.version() > 0) {
            DbVersion v = new DbVersion();
            DbVersion dbVersion = // (SCHEMA="" && TABLE="") == DATABASE
            from(v).where(v.schema).is("").and(v.table).is("").selectFirst();
            if (dbVersion == null) {
                // database has no version registration, but model specifies
                // version: insert DbVersion entry and return.
                DbVersion newDb = new DbVersion(model.version());
                insert(newDb);
            } else {
                // check to see if upgrade is required.
                if ((model.version() > dbVersion.version) && (dbUpgrader != null)) {
                    // database is an older version than the model
                    boolean success = dbUpgrader.upgradeDatabase(this, dbVersion.version, model.version());
                    if (success) {
                        dbVersion.version = model.version();
                        update(dbVersion);
                    }
                }
            }
        }
    }
    return this;
}
Also used : JQDatabase(org.h2.jaqu.Table.JQDatabase)

Example 68 with Db

use of org.h2.test.db.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 69 with Db

use of org.h2.test.db.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 70 with Db

use of org.h2.test.db.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)

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