Search in sources :

Example 76 with Database

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

the class AggregateDataHistogram method getValue.

@Override
Value getValue(Database database, int dataType, boolean distinct) {
    if (distinct) {
        count = 0;
        groupDistinct(database, dataType);
    }
    ValueArray[] values = new ValueArray[distinctValues.size()];
    int i = 0;
    for (Value dv : distinctValues.keys()) {
        AggregateDataHistogram d = distinctValues.get(dv);
        values[i] = ValueArray.get(new Value[] { dv, ValueLong.get(d.count) });
        i++;
    }
    final CompareMode compareMode = database.getCompareMode();
    Arrays.sort(values, new Comparator<ValueArray>() {

        @Override
        public int compare(ValueArray v1, ValueArray v2) {
            Value a1 = v1.getList()[0];
            Value a2 = v2.getList()[0];
            return a1.compareTo(a2, compareMode);
        }
    });
    Value v = ValueArray.get(values);
    return v.convertTo(dataType);
}
Also used : Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode) ValueArray(org.h2.value.ValueArray)

Example 77 with Database

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

the class WebApp method tables.

private String tables() {
    DbContents contents = session.getContents();
    boolean isH2 = false;
    try {
        String url = (String) session.get("url");
        Connection conn = session.getConnection();
        contents.readContents(url, conn);
        session.loadBnf();
        isH2 = contents.isH2();
        StringBuilder buff = new StringBuilder().append("setNode(0, 0, 0, 'database', '").append(PageParser.escapeJavaScript(url)).append("', null);\n");
        int treeIndex = 1;
        DbSchema defaultSchema = contents.getDefaultSchema();
        treeIndex = addTablesAndViews(defaultSchema, true, buff, treeIndex);
        DbSchema[] schemas = contents.getSchemas();
        for (DbSchema schema : schemas) {
            if (schema == defaultSchema || schema == null) {
                continue;
            }
            buff.append("setNode(").append(treeIndex).append(", 0, 1, 'folder', '").append(PageParser.escapeJavaScript(schema.name)).append("', null);\n");
            treeIndex++;
            treeIndex = addTablesAndViews(schema, false, buff, treeIndex);
        }
        if (isH2) {
            try (Statement stat = conn.createStatement()) {
                ResultSet rs = stat.executeQuery("SELECT * FROM " + "INFORMATION_SCHEMA.SEQUENCES ORDER BY SEQUENCE_NAME");
                for (int i = 0; rs.next(); i++) {
                    if (i == 0) {
                        buff.append("setNode(").append(treeIndex).append(", 0, 1, 'sequences', '${text.tree.sequences}', null);\n");
                        treeIndex++;
                    }
                    String name = rs.getString("SEQUENCE_NAME");
                    String current = rs.getString("CURRENT_VALUE");
                    String increment = rs.getString("INCREMENT");
                    buff.append("setNode(").append(treeIndex).append(", 1, 1, 'sequence', '").append(PageParser.escapeJavaScript(name)).append("', null);\n");
                    treeIndex++;
                    buff.append("setNode(").append(treeIndex).append(", 2, 2, 'type', '${text.tree.current}: ").append(PageParser.escapeJavaScript(current)).append("', null);\n");
                    treeIndex++;
                    if (!"1".equals(increment)) {
                        buff.append("setNode(").append(treeIndex).append(", 2, 2, 'type', '${text.tree.increment}: ").append(PageParser.escapeJavaScript(increment)).append("', null);\n");
                        treeIndex++;
                    }
                }
                rs.close();
                rs = stat.executeQuery("SELECT * FROM " + "INFORMATION_SCHEMA.USERS ORDER BY NAME");
                for (int i = 0; rs.next(); i++) {
                    if (i == 0) {
                        buff.append("setNode(").append(treeIndex).append(", 0, 1, 'users', '${text.tree.users}', null);\n");
                        treeIndex++;
                    }
                    String name = rs.getString("NAME");
                    String admin = rs.getString("ADMIN");
                    buff.append("setNode(").append(treeIndex).append(", 1, 1, 'user', '").append(PageParser.escapeJavaScript(name)).append("', null);\n");
                    treeIndex++;
                    if (admin.equalsIgnoreCase("TRUE")) {
                        buff.append("setNode(").append(treeIndex).append(", 2, 2, 'type', '${text.tree.admin}', null);\n");
                        treeIndex++;
                    }
                }
                rs.close();
            }
        }
        DatabaseMetaData meta = session.getMetaData();
        String version = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion();
        buff.append("setNode(").append(treeIndex).append(", 0, 0, 'info', '").append(PageParser.escapeJavaScript(version)).append("', null);\n").append("refreshQueryTables();");
        session.put("tree", buff.toString());
    } catch (Exception e) {
        session.put("tree", "");
        session.put("error", getStackTrace(0, e, isH2));
    }
    return "tables.jsp";
}
Also used : DbContents(org.h2.bnf.context.DbContents) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) DbSchema(org.h2.bnf.context.DbSchema) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) DbException(org.h2.message.DbException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 78 with Database

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

the class LobStorageBackend method init.

@Override
public void init() {
    if (init) {
        return;
    }
    synchronized (database) {
        // thread
        if (init) {
            return;
        }
        init = true;
        conn = database.getLobConnectionForRegularUse();
        JdbcConnection initConn = database.getLobConnectionForInit();
        try {
            Statement stat = initConn.createStatement();
            // stat.execute("SET UNDO_LOG 0");
            // stat.execute("SET REDO_LOG_BINARY 0");
            boolean create = true;
            PreparedStatement prep = initConn.prepareStatement("SELECT ZERO() FROM INFORMATION_SCHEMA.COLUMNS WHERE " + "TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?");
            prep.setString(1, "INFORMATION_SCHEMA");
            prep.setString(2, "LOB_MAP");
            prep.setString(3, "POS");
            ResultSet rs;
            rs = prep.executeQuery();
            if (rs.next()) {
                prep = initConn.prepareStatement("SELECT ZERO() FROM INFORMATION_SCHEMA.TABLES WHERE " + "TABLE_SCHEMA=? AND TABLE_NAME=?");
                prep.setString(1, "INFORMATION_SCHEMA");
                prep.setString(2, "LOB_DATA");
                rs = prep.executeQuery();
                if (rs.next()) {
                    create = false;
                }
            }
            if (create) {
                stat.execute("CREATE CACHED TABLE IF NOT EXISTS " + LOBS + "(ID BIGINT PRIMARY KEY, BYTE_COUNT BIGINT, TABLE INT) HIDDEN");
                stat.execute("CREATE INDEX IF NOT EXISTS " + "INFORMATION_SCHEMA.INDEX_LOB_TABLE ON " + LOBS + "(TABLE)");
                stat.execute("CREATE CACHED TABLE IF NOT EXISTS " + LOB_MAP + "(LOB BIGINT, SEQ INT, POS BIGINT, HASH INT, " + "BLOCK BIGINT, PRIMARY KEY(LOB, SEQ)) HIDDEN");
                stat.execute("ALTER TABLE " + LOB_MAP + " RENAME TO " + LOB_MAP + " HIDDEN");
                stat.execute("ALTER TABLE " + LOB_MAP + " ADD IF NOT EXISTS POS BIGINT BEFORE HASH");
                // TODO the column name OFFSET was used in version 1.3.156,
                // so this can be remove in a later version
                stat.execute("ALTER TABLE " + LOB_MAP + " DROP COLUMN IF EXISTS \"OFFSET\"");
                stat.execute("CREATE INDEX IF NOT EXISTS " + "INFORMATION_SCHEMA.INDEX_LOB_MAP_DATA_LOB ON " + LOB_MAP + "(BLOCK, LOB)");
                stat.execute("CREATE CACHED TABLE IF NOT EXISTS " + LOB_DATA + "(BLOCK BIGINT PRIMARY KEY, COMPRESSED INT, DATA BINARY) HIDDEN");
            }
            rs = stat.executeQuery("SELECT MAX(BLOCK) FROM " + LOB_DATA);
            rs.next();
            nextBlock = rs.getLong(1) + 1;
            stat.close();
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Example 79 with Database

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

the class LobStorageBackend method copyLob.

@Override
public ValueLobDb copyLob(ValueLobDb old, int tableId, long length) {
    int type = old.getType();
    long oldLobId = old.getLobId();
    assertNotHolds(conn.getSession());
    // see locking discussion at the top
    synchronized (database) {
        synchronized (conn.getSession()) {
            try {
                init();
                ValueLobDb v = null;
                if (!old.isRecoveryReference()) {
                    long lobId = getNextLobId();
                    String sql = "INSERT INTO " + LOB_MAP + "(LOB, SEQ, POS, HASH, BLOCK) " + "SELECT ?, SEQ, POS, HASH, BLOCK FROM " + LOB_MAP + " WHERE LOB = ?";
                    PreparedStatement prep = prepare(sql);
                    prep.setLong(1, lobId);
                    prep.setLong(2, oldLobId);
                    prep.executeUpdate();
                    reuse(sql, prep);
                    sql = "INSERT INTO " + LOBS + "(ID, BYTE_COUNT, TABLE) " + "SELECT ?, BYTE_COUNT, ? FROM " + LOBS + " WHERE ID = ?";
                    prep = prepare(sql);
                    prep.setLong(1, lobId);
                    prep.setLong(2, tableId);
                    prep.setLong(3, oldLobId);
                    prep.executeUpdate();
                    reuse(sql, prep);
                    v = ValueLobDb.create(type, database, tableId, lobId, null, length);
                } else {
                    // Recovery process, no need to copy LOB using normal
                    // infrastructure
                    v = ValueLobDb.create(type, database, tableId, oldLobId, null, length);
                }
                return v;
            } catch (SQLException e) {
                throw DbException.convert(e);
            }
        }
    }
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 80 with Database

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

the class LobStorageMap method createLob.

private ValueLobDb createLob(InputStream in, int type) throws IOException {
    byte[] streamStoreId;
    try {
        streamStoreId = streamStore.put(in);
    } catch (Exception e) {
        throw DbException.convertToIOException(e);
    }
    long lobId = generateLobId();
    long length = streamStore.length(streamStoreId);
    int tableId = LobStorageFrontend.TABLE_TEMP;
    Object[] value = { streamStoreId, tableId, length, 0 };
    lobMap.put(lobId, value);
    Object[] key = { streamStoreId, lobId };
    refMap.put(key, Boolean.TRUE);
    ValueLobDb lob = ValueLobDb.create(type, database, tableId, lobId, null, length);
    if (TRACE) {
        trace("create " + tableId + "/" + lobId);
    }
    return lob;
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) IOException(java.io.IOException) DbException(org.h2.message.DbException)

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