Search in sources :

Example 86 with Database

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

the class PageStore method writeStaticHeader.

private void writeStaticHeader() {
    Data page = Data.create(database, new byte[pageSize - FileStore.HEADER_LENGTH]);
    page.writeInt(pageSize);
    page.writeByte((byte) WRITE_VERSION);
    page.writeByte((byte) READ_VERSION);
    file.seek(FileStore.HEADER_LENGTH);
    file.write(page.getBytes(), 0, pageSize - FileStore.HEADER_LENGTH);
    writeCount++;
}
Also used : CreateTableData(org.h2.command.ddl.CreateTableData)

Example 87 with Database

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

the class Sequence method flush.

/**
 * Flush the current value, including the margin, to disk.
 *
 * @param session the session
 */
public void flush(Session session) {
    if (isTemporary()) {
        return;
    }
    if (session == null || !database.isSysTableLockedBy(session)) {
        // This session may not lock the sys table (except if it has already
        // locked it) because it must be committed immediately, otherwise
        // other threads can not access the sys table.
        Session sysSession = database.getSystemSession();
        synchronized (database.isMultiThreaded() ? sysSession : database) {
            flushInternal(sysSession);
            sysSession.commit(false);
        }
    } else {
        synchronized (database.isMultiThreaded() ? session : database) {
            flushInternal(session);
        }
    }
}
Also used : Session(org.h2.engine.Session)

Example 88 with Database

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

the class PgServerThread method initDb.

private void initDb() throws SQLException {
    Statement stat = null;
    try {
        synchronized (server) {
            // better would be: set the database to exclusive mode
            boolean tableFound;
            try (ResultSet rs = conn.getMetaData().getTables(null, "PG_CATALOG", "PG_VERSION", null)) {
                tableFound = rs.next();
            }
            stat = conn.createStatement();
            if (!tableFound) {
                installPgCatalog(stat);
            }
            try (ResultSet rs = stat.executeQuery("select * from pg_catalog.pg_version")) {
                if (!rs.next() || rs.getInt(1) < 2) {
                    // installation incomplete, or old version
                    installPgCatalog(stat);
                } else {
                    // version 2 or newer: check the read version
                    int versionRead = rs.getInt(2);
                    if (versionRead > 2) {
                        throw DbException.throwInternalError("Incompatible PG_VERSION");
                    }
                }
            }
        }
        stat.execute("set search_path = PUBLIC, pg_catalog");
        HashSet<Integer> typeSet = server.getTypeSet();
        if (typeSet.isEmpty()) {
            try (ResultSet rs = stat.executeQuery("select oid from pg_catalog.pg_type")) {
                while (rs.next()) {
                    typeSet.add(rs.getInt(1));
                }
            }
        }
    } finally {
        JdbcUtils.closeSilently(stat);
    }
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet) JdbcResultSet(org.h2.jdbc.JdbcResultSet)

Example 89 with Database

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

the class ResultTempTable method find.

private Cursor find(Row row) {
    if (index == null) {
        // for the case "in(select ...)", the query might
        // use an optimization and not create the index
        // up front
        createIndex();
    }
    Cursor cursor = index.find(session, row, row);
    while (cursor.next()) {
        SearchRow found = cursor.getSearchRow();
        boolean ok = true;
        Database db = session.getDatabase();
        for (int i = 0; i < row.getColumnCount(); i++) {
            if (!db.areEqual(row.getValue(i), found.getValue(i))) {
                ok = false;
                break;
            }
        }
        if (ok) {
            return cursor;
        }
    }
    return null;
}
Also used : Database(org.h2.engine.Database) Cursor(org.h2.index.Cursor)

Example 90 with Database

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

the class RowList method writeAllRows.

private void writeAllRows() {
    if (file == null) {
        Database db = session.getDatabase();
        String fileName = db.createTempFile();
        file = db.openFile(fileName, "rw", false);
        file.setCheckedWriting(false);
        file.seek(FileStore.HEADER_LENGTH);
        rowBuff = Data.create(db, Constants.DEFAULT_PAGE_SIZE);
        file.seek(FileStore.HEADER_LENGTH);
    }
    Data buff = rowBuff;
    initBuffer(buff);
    for (int i = 0, size = list.size(); i < size; i++) {
        if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) {
            flushBuffer(buff);
            initBuffer(buff);
        }
        Row r = list.get(i);
        writeRow(buff, r);
    }
    flushBuffer(buff);
    file.autoDelete();
    list.clear();
    memory = 0;
}
Also used : Database(org.h2.engine.Database) Data(org.h2.store.Data)

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