Search in sources :

Example 41 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class Engine method createSessionAndValidate.

private Session createSessionAndValidate(ConnectionInfo ci) {
    try {
        ConnectionInfo backup = null;
        String lockMethodName = ci.getProperty("FILE_LOCK", null);
        FileLockMethod fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
        if (fileLockMethod == FileLockMethod.SERIALIZED) {
            // In serialized mode, database instance sharing is not possible
            ci.setProperty("OPEN_NEW", "TRUE");
            try {
                backup = ci.clone();
            } catch (CloneNotSupportedException e) {
                throw DbException.convert(e);
            }
        }
        Session session = openSession(ci);
        validateUserAndPassword(true);
        if (backup != null) {
            session.setConnectionInfo(backup);
        }
        return session;
    } catch (DbException e) {
        if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
            validateUserAndPassword(false);
        }
        throw e;
    }
}
Also used : FileLockMethod(org.h2.store.FileLockMethod) DbException(org.h2.message.DbException)

Example 42 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class AlterSequence method update.

@Override
public int update() {
    Database db = session.getDatabase();
    if (sequence == null) {
        sequence = getSchema().findSequence(sequenceName);
        if (sequence == null) {
            if (!ifExists) {
                throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
            }
            return 0;
        }
    }
    if (table != null) {
        session.getUser().checkRight(table, Right.ALL);
    }
    if (cycle != null) {
        sequence.setCycle(cycle);
    }
    if (cacheSize != null) {
        long size = cacheSize.optimize(session).getValue(session).getLong();
        sequence.setCacheSize(size);
    }
    if (start != null || minValue != null || maxValue != null || increment != null) {
        Long startValue = getLong(start);
        Long min = getLong(minValue);
        Long max = getLong(maxValue);
        Long inc = getLong(increment);
        sequence.modify(startValue, min, max, inc);
    }
    db.updateMeta(session, sequence);
    return 0;
}
Also used : Database(org.h2.engine.Database)

Example 43 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class BackupCommand method backupTo.

private void backupTo(String fileName) {
    Database db = session.getDatabase();
    if (!db.isPersistent()) {
        throw DbException.get(ErrorCode.DATABASE_IS_NOT_PERSISTENT);
    }
    try {
        Store mvStore = db.getMvStore();
        if (mvStore != null) {
            mvStore.flush();
        }
        String name = db.getName();
        name = FileUtils.getName(name);
        try (OutputStream zip = FileUtils.newOutputStream(fileName, false)) {
            ZipOutputStream out = new ZipOutputStream(zip);
            db.flush();
            if (db.getPageStore() != null) {
                String fn = db.getName() + Constants.SUFFIX_PAGE_FILE;
                backupPageStore(out, fn, db.getPageStore());
            }
            // synchronize on the database, to avoid concurrent temp file
            // creation / deletion / backup
            String base = FileUtils.getParent(db.getName());
            synchronized (db.getLobSyncObject()) {
                String prefix = db.getDatabasePath();
                String dir = FileUtils.getParent(prefix);
                dir = FileLister.getDir(dir);
                ArrayList<String> fileList = FileLister.getDatabaseFiles(dir, name, true);
                for (String n : fileList) {
                    if (n.endsWith(Constants.SUFFIX_LOB_FILE)) {
                        backupFile(out, base, n);
                    }
                    if (n.endsWith(Constants.SUFFIX_MV_FILE) && mvStore != null) {
                        MVStore s = mvStore.getStore();
                        boolean before = s.getReuseSpace();
                        s.setReuseSpace(false);
                        try {
                            InputStream in = mvStore.getInputStream();
                            backupFile(out, base, n, in);
                        } finally {
                            s.setReuseSpace(before);
                        }
                    }
                }
            }
            out.close();
        }
    } catch (IOException e) {
        throw DbException.convertIOException(e, fileName);
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) Database(org.h2.engine.Database) Store(org.h2.mvstore.db.MVTableEngine.Store) PageStore(org.h2.store.PageStore) MVStore(org.h2.mvstore.MVStore) IOException(java.io.IOException)

Example 44 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class BackupCommand method backupPageStore.

private void backupPageStore(ZipOutputStream out, String fileName, PageStore store) throws IOException {
    Database db = session.getDatabase();
    fileName = FileUtils.getName(fileName);
    out.putNextEntry(new ZipEntry(fileName));
    int pos = 0;
    try {
        store.setBackup(true);
        while (true) {
            pos = store.copyDirect(pos, out);
            if (pos < 0) {
                break;
            }
            int max = store.getPageCount();
            db.setProgress(DatabaseEventListener.STATE_BACKUP_FILE, fileName, pos, max);
        }
    } finally {
        store.setBackup(false);
    }
    out.closeEntry();
}
Also used : ZipEntry(java.util.zip.ZipEntry) Database(org.h2.engine.Database)

Example 45 with Database

use of org.h2.engine.Database in project h2database by h2database.

the class Explain method query.

@Override
public ResultInterface query(int maxrows) {
    Column column = new Column("PLAN", Value.STRING);
    Database db = session.getDatabase();
    ExpressionColumn expr = new ExpressionColumn(db, column);
    Expression[] expressions = { expr };
    result = new LocalResult(session, expressions, 1);
    if (maxrows >= 0) {
        String plan;
        if (executeCommand) {
            PageStore store = null;
            Store mvStore = null;
            if (db.isPersistent()) {
                store = db.getPageStore();
                if (store != null) {
                    store.statisticsStart();
                }
                mvStore = db.getMvStore();
                if (mvStore != null) {
                    mvStore.statisticsStart();
                }
            }
            if (command.isQuery()) {
                command.query(maxrows);
            } else {
                command.update();
            }
            plan = command.getPlanSQL();
            Map<String, Integer> statistics = null;
            if (store != null) {
                statistics = store.statisticsEnd();
            } else if (mvStore != null) {
                statistics = mvStore.statisticsEnd();
            }
            if (statistics != null) {
                int total = 0;
                for (Entry<String, Integer> e : statistics.entrySet()) {
                    total += e.getValue();
                }
                if (total > 0) {
                    statistics = new TreeMap<>(statistics);
                    StringBuilder buff = new StringBuilder();
                    if (statistics.size() > 1) {
                        buff.append("total: ").append(total).append('\n');
                    }
                    for (Entry<String, Integer> e : statistics.entrySet()) {
                        int value = e.getValue();
                        int percent = (int) (100L * value / total);
                        buff.append(e.getKey()).append(": ").append(value);
                        if (statistics.size() > 1) {
                            buff.append(" (").append(percent).append("%)");
                        }
                        buff.append('\n');
                    }
                    plan += "\n/*\n" + buff.toString() + "*/";
                }
            }
        } else {
            plan = command.getPlanSQL();
        }
        add(plan);
    }
    result.done();
    return result;
}
Also used : Store(org.h2.mvstore.db.MVTableEngine.Store) PageStore(org.h2.store.PageStore) PageStore(org.h2.store.PageStore) ValueString(org.h2.value.ValueString) ExpressionColumn(org.h2.expression.ExpressionColumn) LocalResult(org.h2.result.LocalResult) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Database(org.h2.engine.Database)

Aggregations

Database (org.h2.engine.Database)79 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)38 DbException (org.h2.message.DbException)37 ResultSet (java.sql.ResultSet)34 Statement (java.sql.Statement)32 SimpleResultSet (org.h2.tools.SimpleResultSet)32 Connection (java.sql.Connection)27 Table (org.h2.table.Table)25 Value (org.h2.value.Value)25 Column (org.h2.table.Column)22 IOException (java.io.IOException)19 Constraint (org.h2.constraint.Constraint)18 Expression (org.h2.expression.Expression)17 ExpressionColumn (org.h2.expression.ExpressionColumn)17 ValueString (org.h2.value.ValueString)15 ValueExpression (org.h2.expression.ValueExpression)14 Session (org.h2.engine.Session)13 JdbcConnection (org.h2.jdbc.JdbcConnection)13 Schema (org.h2.schema.Schema)13