Search in sources :

Example 46 with Column

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

the class GeneratedKeys method getKeys.

/**
 * Returns generated keys.
 *
 * @param session
 *            session
 * @return local result with generated keys
 */
public LocalResult getKeys(Session session) {
    Database db = session == null ? null : session.getDatabase();
    if (Boolean.FALSE.equals(generatedKeysRequest)) {
        clear(null);
        return new LocalResult();
    }
    ArrayList<ExpressionColumn> expressionColumns;
    if (Boolean.TRUE.equals(generatedKeysRequest)) {
        expressionColumns = new ArrayList<>(allColumns.size());
        for (Column column : allColumns) {
            expressionColumns.add(new ExpressionColumn(db, column));
        }
    } else if (generatedKeysRequest instanceof int[]) {
        if (table != null) {
            int[] indices = (int[]) generatedKeysRequest;
            Column[] columns = table.getColumns();
            int cnt = columns.length;
            allColumns.clear();
            expressionColumns = new ArrayList<>(indices.length);
            for (int idx : indices) {
                if (idx >= 1 && idx <= cnt) {
                    Column column = columns[idx - 1];
                    expressionColumns.add(new ExpressionColumn(db, column));
                    allColumns.add(column);
                }
            }
        } else {
            clear(null);
            return new LocalResult();
        }
    } else if (generatedKeysRequest instanceof String[]) {
        if (table != null) {
            String[] names = (String[]) generatedKeysRequest;
            allColumns.clear();
            expressionColumns = new ArrayList<>(names.length);
            for (String name : names) {
                Column column;
                search: if (table.doesColumnExist(name)) {
                    column = table.getColumn(name);
                } else {
                    name = StringUtils.toUpperEnglish(name);
                    if (table.doesColumnExist(name)) {
                        column = table.getColumn(name);
                    } else {
                        for (Column c : table.getColumns()) {
                            if (c.getName().equalsIgnoreCase(name)) {
                                column = c;
                                break search;
                            }
                        }
                        continue;
                    }
                }
                expressionColumns.add(new ExpressionColumn(db, column));
                allColumns.add(column);
            }
        } else {
            clear(null);
            return new LocalResult();
        }
    } else {
        clear(null);
        return new LocalResult();
    }
    int columnCount = expressionColumns.size();
    if (columnCount == 0) {
        clear(null);
        return new LocalResult();
    }
    LocalResult result = new LocalResult(session, expressionColumns.toArray(new Expression[0]), columnCount);
    for (Map<Column, Value> map : data) {
        Value[] row = new Value[columnCount];
        for (Map.Entry<Column, Value> entry : map.entrySet()) {
            int idx = allColumns.indexOf(entry.getKey());
            if (idx >= 0) {
                row[idx] = entry.getValue();
            }
        }
        for (int i = 0; i < columnCount; i++) {
            if (row[i] == null) {
                row[i] = ValueNull.INSTANCE;
            }
        }
        result.addRow(row);
    }
    clear(null);
    return result;
}
Also used : ArrayList(java.util.ArrayList) 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) Value(org.h2.value.Value) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with Column

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

the class GeneratedKeys method confirmRow.

/**
 * Saves row with generated keys if any.
 *
 * @param tableRow
 *            table row that was inserted
 */
public void confirmRow(Row tableRow) {
    if (Boolean.FALSE.equals(generatedKeysRequest)) {
        return;
    }
    int size = row.size();
    if (size > 0) {
        if (size == 1) {
            Column column = row.get(0);
            data.add(Collections.singletonMap(column, tableRow.getValue(column.getColumnId())));
            if (!allColumns.contains(column)) {
                allColumns.add(column);
            }
        } else {
            HashMap<Column, Value> map = new HashMap<>();
            for (Column column : row) {
                map.put(column, tableRow.getValue(column.getColumnId()));
                if (!allColumns.contains(column)) {
                    allColumns.add(column);
                }
            }
            data.add(map);
        }
        row.clear();
    }
}
Also used : ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) HashMap(java.util.HashMap) Value(org.h2.value.Value)

Example 48 with Column

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

the class CreateTable method update.

@Override
public int update() {
    if (!transactional) {
        session.commit(true);
    }
    Database db = session.getDatabase();
    if (!db.isPersistent()) {
        data.persistIndexes = false;
    }
    boolean isSessionTemporary = data.temporary && !data.globalTemporary;
    if (!isSessionTemporary) {
        db.lockMeta(session);
    }
    if (getSchema().resolveTableOrView(session, data.tableName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, data.tableName);
    }
    if (asQuery != null) {
        asQuery.prepare();
        if (data.columns.isEmpty()) {
            generateColumnsFromQuery();
        } else if (data.columns.size() != asQuery.getColumnCount()) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    changePrimaryKeysToNotNull(data.columns);
    data.id = getObjectId();
    data.create = create;
    data.session = session;
    Table table = getSchema().createTable(data);
    ArrayList<Sequence> sequences = generateSequences(data.columns, data.temporary);
    table.setComment(comment);
    if (isSessionTemporary) {
        if (onCommitDrop) {
            table.setOnCommitDrop(true);
        }
        if (onCommitTruncate) {
            table.setOnCommitTruncate(true);
        }
        session.addLocalTempTable(table);
    } else {
        db.lockMeta(session);
        db.addSchemaObject(session, table);
    }
    try {
        for (Column c : data.columns) {
            c.prepareExpression(session);
        }
        for (Sequence sequence : sequences) {
            table.addSequence(sequence);
        }
        createConstraints();
        if (asQuery != null) {
            boolean old = session.isUndoLogEnabled();
            try {
                session.setUndoLogEnabled(false);
                session.startStatementWithinTransaction();
                Insert insert = new Insert(session);
                insert.setSortedInsertMode(sortedInsertMode);
                insert.setQuery(asQuery);
                insert.setTable(table);
                insert.setInsertFromSelect(true);
                insert.prepare();
                insert.update();
            } finally {
                session.setUndoLogEnabled(old);
            }
        }
        HashSet<DbObject> set = new HashSet<>();
        set.clear();
        table.addDependencies(set);
        for (DbObject obj : set) {
            if (obj == table) {
                continue;
            }
            if (obj.getType() == DbObject.TABLE_OR_VIEW) {
                if (obj instanceof Table) {
                    Table t = (Table) obj;
                    if (t.getId() > table.getId()) {
                        throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, "Table depends on another table " + "with a higher ID: " + t + ", this is currently not supported, " + "as it would prevent the database from " + "being re-opened");
                    }
                }
            }
        }
    } catch (DbException e) {
        db.checkPowerOff();
        db.removeSchemaObject(session, table);
        if (!transactional) {
            session.commit(true);
        }
        throw e;
    }
    return 0;
}
Also used : Table(org.h2.table.Table) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) DbObject(org.h2.engine.DbObject) Database(org.h2.engine.Database) Sequence(org.h2.schema.Sequence) Insert(org.h2.command.dml.Insert) HashSet(java.util.HashSet) DbException(org.h2.message.DbException)

Example 49 with Column

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

the class ConstraintReferential method checkRowRefTable.

private void checkRowRefTable(Session session, Row oldRow, Row newRow) {
    if (oldRow == null) {
        // this is an insert
        return;
    }
    if (newRow != null && isEqual(oldRow, newRow)) {
        // on an update, if both old and new are the same, don't do anything
        return;
    }
    if (newRow == null) {
        // this is a delete
        if (deleteAction == ConstraintActionType.RESTRICT) {
            checkRow(session, oldRow);
        } else {
            int i = deleteAction == ConstraintActionType.CASCADE ? 0 : columns.length;
            Prepared deleteCommand = getDelete(session);
            setWhere(deleteCommand, i, oldRow);
            updateWithSkipCheck(deleteCommand);
        }
    } else {
        // this is an update
        if (updateAction == ConstraintActionType.RESTRICT) {
            checkRow(session, oldRow);
        } else {
            Prepared updateCommand = getUpdate(session);
            if (updateAction == ConstraintActionType.CASCADE) {
                ArrayList<Parameter> params = updateCommand.getParameters();
                for (int i = 0, len = columns.length; i < len; i++) {
                    Parameter param = params.get(i);
                    Column refCol = refColumns[i].column;
                    param.setValue(newRow.getValue(refCol.getColumnId()));
                }
            }
            setWhere(updateCommand, columns.length, oldRow);
            updateWithSkipCheck(updateCommand);
        }
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Prepared(org.h2.command.Prepared) Parameter(org.h2.expression.Parameter)

Example 50 with Column

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

the class ConstraintReferential method prepare.

private Prepared prepare(Session session, String sql, ConstraintActionType action) {
    Prepared command = session.prepare(sql);
    if (action != ConstraintActionType.CASCADE) {
        ArrayList<Parameter> params = command.getParameters();
        for (int i = 0, len = columns.length; i < len; i++) {
            Column column = columns[i].column;
            Parameter param = params.get(i);
            Value value;
            if (action == ConstraintActionType.SET_NULL) {
                value = ValueNull.INSTANCE;
            } else {
                Expression expr = column.getDefaultExpression();
                if (expr == null) {
                    throw DbException.get(ErrorCode.NO_DEFAULT_SET_1, column.getName());
                }
                value = expr.getValue(session);
            }
            param.setValue(value);
        }
    }
    return command;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) Prepared(org.h2.command.Prepared) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

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