Search in sources :

Example 71 with Column

use of org.h2.table.Column 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 72 with Column

use of org.h2.table.Column in project h2database by h2database.

the class PageStore method openMetaIndex.

private void openMetaIndex() {
    CreateTableData data = new CreateTableData();
    ArrayList<Column> cols = data.columns;
    cols.add(new Column("ID", Value.INT));
    cols.add(new Column("TYPE", Value.INT));
    cols.add(new Column("PARENT", Value.INT));
    cols.add(new Column("HEAD", Value.INT));
    cols.add(new Column("OPTIONS", Value.STRING));
    cols.add(new Column("COLUMNS", Value.STRING));
    metaSchema = new Schema(database, 0, "", null, true);
    data.schema = metaSchema;
    data.tableName = "PAGE_INDEX";
    data.id = META_TABLE_ID;
    data.temporary = false;
    data.persistData = true;
    data.persistIndexes = true;
    data.create = false;
    data.session = pageStoreSession;
    metaTable = new RegularTable(data);
    metaIndex = (PageDataIndex) metaTable.getScanIndex(pageStoreSession);
    metaObjects.clear();
    metaObjects.put(-1, metaIndex);
}
Also used : IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) Schema(org.h2.schema.Schema) RegularTable(org.h2.table.RegularTable) CreateTableData(org.h2.command.ddl.CreateTableData)

Example 73 with Column

use of org.h2.table.Column in project h2database by h2database.

the class PageStore method addMeta.

private void addMeta(Row row, Session session, boolean redo) {
    int id = row.getValue(0).getInt();
    int type = row.getValue(1).getInt();
    int parent = row.getValue(2).getInt();
    int rootPageId = row.getValue(3).getInt();
    String[] options = StringUtils.arraySplit(row.getValue(4).getString(), ',', false);
    String columnList = row.getValue(5).getString();
    String[] columns = StringUtils.arraySplit(columnList, ',', false);
    Index meta;
    if (trace.isDebugEnabled()) {
        trace.debug("addMeta id=" + id + " type=" + type + " root=" + rootPageId + " parent=" + parent + " columns=" + columnList);
    }
    if (redo && rootPageId != 0) {
        // ensure the page is empty, but not used by regular data
        writePage(rootPageId, createData());
        allocatePage(rootPageId);
    }
    metaRootPageId.put(id, rootPageId);
    if (type == META_TYPE_DATA_INDEX) {
        CreateTableData data = new CreateTableData();
        if (SysProperties.CHECK) {
            if (columns == null) {
                throw DbException.throwInternalError(row.toString());
            }
        }
        for (int i = 0, len = columns.length; i < len; i++) {
            Column col = new Column("C" + i, Value.INT);
            data.columns.add(col);
        }
        data.schema = metaSchema;
        data.tableName = "T" + id;
        data.id = id;
        data.temporary = options[2].equals("temp");
        data.persistData = true;
        data.persistIndexes = true;
        data.create = false;
        data.session = session;
        RegularTable table = new RegularTable(data);
        boolean binaryUnsigned = SysProperties.SORT_BINARY_UNSIGNED;
        if (options.length > 3) {
            binaryUnsigned = Boolean.parseBoolean(options[3]);
        }
        CompareMode mode = CompareMode.getInstance(options[0], Integer.parseInt(options[1]), binaryUnsigned);
        table.setCompareMode(mode);
        meta = table.getScanIndex(session);
    } else {
        Index p = metaObjects.get(parent);
        if (p == null) {
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Table not found:" + parent + " for " + row + " meta:" + metaObjects);
        }
        RegularTable table = (RegularTable) p.getTable();
        Column[] tableCols = table.getColumns();
        int len = columns.length;
        IndexColumn[] cols = new IndexColumn[len];
        for (int i = 0; i < len; i++) {
            String c = columns[i];
            IndexColumn ic = new IndexColumn();
            int idx = c.indexOf('/');
            if (idx >= 0) {
                String s = c.substring(idx + 1);
                ic.sortType = Integer.parseInt(s);
                c = c.substring(0, idx);
            }
            ic.column = tableCols[Integer.parseInt(c)];
            cols[i] = ic;
        }
        IndexType indexType;
        if (options[3].equals("d")) {
            indexType = IndexType.createPrimaryKey(true, false);
            Column[] tableColumns = table.getColumns();
            for (IndexColumn indexColumn : cols) {
                tableColumns[indexColumn.column.getColumnId()].setNullable(false);
            }
        } else {
            indexType = IndexType.createNonUnique(true);
        }
        meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
    }
    PageIndex index;
    if (meta instanceof MultiVersionIndex) {
        index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
    } else {
        index = (PageIndex) meta;
    }
    metaObjects.put(id, index);
}
Also used : Index(org.h2.index.Index) PageIndex(org.h2.index.PageIndex) PageDelegateIndex(org.h2.index.PageDelegateIndex) MultiVersionIndex(org.h2.index.MultiVersionIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) PageDataIndex(org.h2.index.PageDataIndex) ValueString(org.h2.value.ValueString) PageIndex(org.h2.index.PageIndex) CreateTableData(org.h2.command.ddl.CreateTableData) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) MultiVersionIndex(org.h2.index.MultiVersionIndex) RegularTable(org.h2.table.RegularTable) CompareMode(org.h2.value.CompareMode) IndexType(org.h2.index.IndexType)

Example 74 with Column

use of org.h2.table.Column in project h2database by h2database.

the class MVSecondaryIndex method convertToSearchRow.

/**
 * Convert array of values to a SearchRow.
 *
 * @param key the index key
 * @return the row
 */
SearchRow convertToSearchRow(ValueArray key) {
    Value[] array = key.getList();
    SearchRow searchRow = mvTable.getTemplateRow();
    searchRow.setKey((array[array.length - 1]).getLong());
    Column[] cols = getColumns();
    for (int i = 0; i < array.length - 1; i++) {
        Column c = cols[i];
        int idx = c.getColumnId();
        Value v = array[i];
        searchRow.setValue(idx, v);
    }
    return searchRow;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow)

Example 75 with Column

use of org.h2.table.Column in project h2database by h2database.

the class SortOrder method getColumn.

/**
 * Get the column for the given table filter, if the sort column is for this
 * filter.
 *
 * @param index the column index (0, 1,..)
 * @param filter the table filter
 * @return the column, or null
 */
public Column getColumn(int index, TableFilter filter) {
    if (orderList == null) {
        return null;
    }
    SelectOrderBy order = orderList.get(index);
    Expression expr = order.expression;
    if (expr == null) {
        return null;
    }
    expr = expr.getNonAliasExpression();
    if (expr.isConstant()) {
        return null;
    }
    if (!(expr instanceof ExpressionColumn)) {
        return null;
    }
    ExpressionColumn exprCol = (ExpressionColumn) expr;
    if (exprCol.getTableFilter() != filter) {
        return null;
    }
    return exprCol.getColumn();
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) Expression(org.h2.expression.Expression) ExpressionColumn(org.h2.expression.ExpressionColumn)

Aggregations

Column (org.h2.table.Column)146 Value (org.h2.value.Value)83 IndexColumn (org.h2.table.IndexColumn)79 DbException (org.h2.message.DbException)64 SQLException (java.sql.SQLException)60 Expression (org.h2.expression.Expression)55 ExpressionColumn (org.h2.expression.ExpressionColumn)51 ValueString (org.h2.value.ValueString)34 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)32 ValueExpression (org.h2.expression.ValueExpression)30 ArrayList (java.util.ArrayList)27 PreparedStatement (java.sql.PreparedStatement)26 Index (org.h2.index.Index)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)25 Table (org.h2.table.Table)25 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)22 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)21 StatementBuilder (org.h2.util.StatementBuilder)19 Constraint (org.h2.constraint.Constraint)18