Search in sources :

Example 6 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method parseAlterIndex.

private AlterIndexRename parseAlterIndex() {
    boolean ifExists = readIfExists(false);
    String indexName = readIdentifierWithSchema();
    Schema old = getSchema();
    AlterIndexRename command = new AlterIndexRename(session);
    command.setOldSchema(old);
    command.setOldName(indexName);
    command.setIfExists(ifExists);
    read("RENAME");
    read("TO");
    String newName = readIdentifierWithSchema(old.getName());
    checkSchema(old);
    command.setNewName(newName);
    return command;
}
Also used : AlterIndexRename(org.h2.command.ddl.AlterIndexRename) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString)

Example 7 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class AlterTableAlterColumn method cloneTableStructure.

private Table cloneTableStructure(Table table, Column[] columns, Database db, String tempName, ArrayList<Column> newColumns) {
    for (Column col : columns) {
        newColumns.add(col.getClone());
    }
    if (type == CommandInterface.ALTER_TABLE_DROP_COLUMN) {
        for (Column removeCol : columnsToRemove) {
            Column foundCol = null;
            for (Column newCol : newColumns) {
                if (newCol.getName().equals(removeCol.getName())) {
                    foundCol = newCol;
                    break;
                }
            }
            if (foundCol == null) {
                throw DbException.throwInternalError(removeCol.getCreateSQL());
            }
            newColumns.remove(foundCol);
        }
    } else if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN) {
        int position;
        if (addFirst) {
            position = 0;
        } else if (addBefore != null) {
            position = table.getColumn(addBefore).getColumnId();
        } else if (addAfter != null) {
            position = table.getColumn(addAfter).getColumnId() + 1;
        } else {
            position = columns.length;
        }
        if (columnsToAdd != null) {
            for (Column column : columnsToAdd) {
                newColumns.add(position++, column);
            }
        }
    } else if (type == CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE) {
        int position = oldColumn.getColumnId();
        newColumns.set(position, newColumn);
    }
    // create a table object in order to get the SQL statement
    // can't just use this table, because most column objects are 'shared'
    // with the old table
    // still need a new id because using 0 would mean: the new table tries
    // to use the rows of the table 0 (the meta table)
    int id = db.allocateObjectId();
    CreateTableData data = new CreateTableData();
    data.tableName = tempName;
    data.id = id;
    data.columns = newColumns;
    data.temporary = table.isTemporary();
    data.persistData = table.isPersistData();
    data.persistIndexes = table.isPersistIndexes();
    data.isHidden = table.isHidden();
    data.create = true;
    data.session = session;
    Table newTable = getSchema().createTable(data);
    newTable.setComment(table.getComment());
    StringBuilder buff = new StringBuilder();
    buff.append(newTable.getCreateSQL());
    StringBuilder columnList = new StringBuilder();
    for (Column nc : newColumns) {
        if (columnList.length() > 0) {
            columnList.append(", ");
        }
        if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN && columnsToAdd != null && columnsToAdd.contains(nc)) {
            Expression def = nc.getDefaultExpression();
            columnList.append(def == null ? "NULL" : def.getSQL());
        } else {
            columnList.append(nc.getSQL());
        }
    }
    buff.append(" AS SELECT ");
    if (columnList.length() == 0) {
        // special case: insert into test select * from
        buff.append('*');
    } else {
        buff.append(columnList);
    }
    buff.append(" FROM ").append(table.getSQL());
    String newTableSQL = buff.toString();
    String newTableName = newTable.getName();
    Schema newTableSchema = newTable.getSchema();
    newTable.removeChildrenAndResources(session);
    execute(newTableSQL, true);
    newTable = newTableSchema.getTableOrView(session, newTableName);
    ArrayList<String> triggers = New.arrayList();
    for (DbObject child : table.getChildren()) {
        if (child instanceof Sequence) {
            continue;
        } else if (child instanceof Index) {
            Index idx = (Index) child;
            if (idx.getIndexType().getBelongsToConstraint()) {
                continue;
            }
        }
        String createSQL = child.getCreateSQL();
        if (createSQL == null) {
            continue;
        }
        if (child instanceof TableView) {
            continue;
        } else if (child.getType() == DbObject.TABLE_OR_VIEW) {
            DbException.throwInternalError();
        }
        String quotedName = Parser.quoteIdentifier(tempName + "_" + child.getName());
        String sql = null;
        if (child instanceof ConstraintReferential) {
            ConstraintReferential r = (ConstraintReferential) child;
            if (r.getTable() != table) {
                sql = r.getCreateSQLForCopy(r.getTable(), newTable, quotedName, false);
            }
        }
        if (sql == null) {
            sql = child.getCreateSQLForCopy(newTable, quotedName);
        }
        if (sql != null) {
            if (child instanceof TriggerObject) {
                triggers.add(sql);
            } else {
                execute(sql, true);
            }
        }
    }
    table.setModified();
    // otherwise the sequence is dropped if the table is dropped
    for (Column col : newColumns) {
        Sequence seq = col.getSequence();
        if (seq != null) {
            table.removeSequence(seq);
            col.setSequence(null);
        }
    }
    for (String sql : triggers) {
        execute(sql, true);
    }
    return newTable;
}
Also used : Table(org.h2.table.Table) DbObject(org.h2.engine.DbObject) Schema(org.h2.schema.Schema) TriggerObject(org.h2.schema.TriggerObject) Index(org.h2.index.Index) Sequence(org.h2.schema.Sequence) ConstraintReferential(org.h2.constraint.ConstraintReferential) Constraint(org.h2.constraint.Constraint) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) TableView(org.h2.table.TableView)

Example 8 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method parseCreateConstant.

private CreateConstant parseCreateConstant() {
    boolean ifNotExists = readIfNotExists();
    String constantName = readIdentifierWithSchema();
    Schema schema = getSchema();
    if (isKeyword(constantName)) {
        throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
    }
    read("VALUE");
    Expression expr = readExpression();
    CreateConstant command = new CreateConstant(session, schema);
    command.setConstantName(constantName);
    command.setExpression(expr);
    command.setIfNotExists(ifNotExists);
    return command;
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) CreateConstant(org.h2.command.ddl.CreateConstant)

Example 9 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method parseCreateTrigger.

private CreateTrigger parseCreateTrigger(boolean force) {
    boolean ifNotExists = readIfNotExists();
    String triggerName = readIdentifierWithSchema(null);
    Schema schema = getSchema();
    boolean insteadOf, isBefore;
    if (readIf("INSTEAD")) {
        read("OF");
        isBefore = true;
        insteadOf = true;
    } else if (readIf("BEFORE")) {
        insteadOf = false;
        isBefore = true;
    } else {
        read("AFTER");
        insteadOf = false;
        isBefore = false;
    }
    int typeMask = 0;
    boolean onRollback = false;
    do {
        if (readIf("INSERT")) {
            typeMask |= Trigger.INSERT;
        } else if (readIf("UPDATE")) {
            typeMask |= Trigger.UPDATE;
        } else if (readIf("DELETE")) {
            typeMask |= Trigger.DELETE;
        } else if (readIf("SELECT")) {
            typeMask |= Trigger.SELECT;
        } else if (readIf("ROLLBACK")) {
            onRollback = true;
        } else {
            throw getSyntaxError();
        }
    } while (readIf(","));
    read("ON");
    String tableName = readIdentifierWithSchema();
    checkSchema(schema);
    CreateTrigger command = new CreateTrigger(session, getSchema());
    command.setForce(force);
    command.setTriggerName(triggerName);
    command.setIfNotExists(ifNotExists);
    command.setInsteadOf(insteadOf);
    command.setBefore(isBefore);
    command.setOnRollback(onRollback);
    command.setTypeMask(typeMask);
    command.setTableName(tableName);
    if (readIf("FOR")) {
        read("EACH");
        read("ROW");
        command.setRowBased(true);
    } else {
        command.setRowBased(false);
    }
    if (readIf("QUEUE")) {
        command.setQueueSize(readPositiveInt());
    }
    command.setNoWait(readIf("NOWAIT"));
    if (readIf("AS")) {
        command.setTriggerSource(readString());
    } else {
        read("CALL");
        command.setTriggerClassName(readUniqueIdentifier());
    }
    return command;
}
Also used : CreateTrigger(org.h2.command.ddl.CreateTrigger) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 10 with Schema

use of com.predic8.schema.Schema in project h2database by h2database.

the class Parser method getDualTable.

private Table getDualTable(boolean noColumns) {
    Schema main = database.findSchema(Constants.SCHEMA_MAIN);
    Expression one = ValueExpression.get(ValueLong.get(1));
    return new RangeTable(main, one, one, noColumns);
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) RangeTable(org.h2.table.RangeTable)

Aggregations

Schema (org.h2.schema.Schema)35 CreateSchema (org.h2.command.ddl.CreateSchema)16 DropSchema (org.h2.command.ddl.DropSchema)16 ValueString (org.h2.value.ValueString)16 Column (org.h2.table.Column)10 IndexColumn (org.h2.table.IndexColumn)10 Table (org.h2.table.Table)10 Expression (org.h2.expression.Expression)8 ExpressionColumn (org.h2.expression.ExpressionColumn)8 ValueExpression (org.h2.expression.ValueExpression)8 Schema (io.s4.schema.Schema)7 ArrayList (java.util.ArrayList)7 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)7 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)7 CreateTable (org.h2.command.ddl.CreateTable)7 RangeTable (org.h2.table.RangeTable)7 Property (io.s4.schema.Schema.Property)6 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)6 DropTable (org.h2.command.ddl.DropTable)6 TruncateTable (org.h2.command.ddl.TruncateTable)6