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