Search in sources :

Example 11 with Comment

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

the class CreateSynonym method createTableSynonym.

private int createTableSynonym(Database db) {
    TableSynonym old = getSchema().getSynonym(data.synonymName);
    if (old != null) {
        if (orReplace) {
        // ok, we replacing the existing synonym
        } else if (ifNotExists) {
            return 0;
        } else {
            throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, data.synonymName);
        }
    }
    TableSynonym table;
    if (old != null) {
        table = old;
        data.schema = table.getSchema();
        table.updateData(data);
        table.setComment(comment);
        table.setModified();
        db.updateMeta(session, table);
    } else {
        data.id = getObjectId();
        table = getSchema().createSynonym(data);
        table.setComment(comment);
        db.addSchemaObject(session, table);
    }
    table.updateSynonymFor();
    return 0;
}
Also used : TableSynonym(org.h2.table.TableSynonym)

Example 12 with Comment

use of org.h2.engine.Comment 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 13 with Comment

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

the class ConstraintReferential method getCreateSQLForCopy.

/**
 * Create the SQL statement of this object so a copy of the table can be
 * made.
 *
 * @param forTable the table to create the object for
 * @param forRefTable the referenced table
 * @param quotedName the name of this object (quoted if necessary)
 * @param internalIndex add the index name to the statement
 * @return the SQL statement
 */
public String getCreateSQLForCopy(Table forTable, Table forRefTable, String quotedName, boolean internalIndex) {
    StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
    String mainTable = forTable.getSQL();
    buff.append(mainTable).append(" ADD CONSTRAINT ");
    if (forTable.isHidden()) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(quotedName);
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    IndexColumn[] cols = columns;
    IndexColumn[] refCols = refColumns;
    buff.append(" FOREIGN KEY(");
    for (IndexColumn c : cols) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    if (internalIndex && indexOwner && forTable == this.table) {
        buff.append(" INDEX ").append(index.getSQL());
    }
    buff.append(" REFERENCES ");
    String quotedRefTable;
    if (this.table == this.refTable) {
        // self-referencing constraints: need to use new table
        quotedRefTable = forTable.getSQL();
    } else {
        quotedRefTable = forRefTable.getSQL();
    }
    buff.append(quotedRefTable).append('(');
    buff.resetCount();
    for (IndexColumn r : refCols) {
        buff.appendExceptFirst(", ");
        buff.append(r.getSQL());
    }
    buff.append(')');
    if (internalIndex && refIndexOwner && forTable == this.table) {
        buff.append(" INDEX ").append(refIndex.getSQL());
    }
    if (deleteAction != ConstraintActionType.RESTRICT) {
        buff.append(" ON DELETE ").append(deleteAction.getSqlName());
    }
    if (updateAction != ConstraintActionType.RESTRICT) {
        buff.append(" ON UPDATE ").append(updateAction.getSqlName());
    }
    return buff.append(" NOCHECK").toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Example 14 with Comment

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

the class CreateUser method update.

@Override
public int update() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    if (db.findRole(userName) != null) {
        throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName);
    }
    if (db.findUser(userName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName);
    }
    int id = getObjectId();
    User user = new User(db, id, userName, false);
    user.setAdmin(admin);
    user.setComment(comment);
    if (hash != null && salt != null) {
        setSaltAndHash(user, session, salt, hash);
    } else if (password != null) {
        setPassword(user, session, password);
    } else {
        throw DbException.throwInternalError();
    }
    db.addDatabaseObject(session, user);
    return 0;
}
Also used : User(org.h2.engine.User) Database(org.h2.engine.Database)

Example 15 with Comment

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

the class TableView method getCreateSQL.

private String getCreateSQL(boolean orReplace, boolean force, String quotedName) {
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (orReplace) {
        buff.append("OR REPLACE ");
    }
    if (force) {
        buff.append("FORCE ");
    }
    buff.append("VIEW ");
    if (isTableExpression) {
        buff.append("TABLE_EXPRESSION ");
    }
    buff.append(quotedName);
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    if (columns != null && columns.length > 0) {
        buff.append('(');
        for (Column c : columns) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    } else if (columnTemplates != null) {
        buff.append('(');
        for (Column c : columnTemplates) {
            buff.appendExceptFirst(", ");
            buff.append(c.getName());
        }
        buff.append(')');
    }
    return buff.append(" AS\n").append(querySQL).toString();
}
Also used : ExpressionColumn(org.h2.expression.ExpressionColumn) StatementBuilder(org.h2.util.StatementBuilder)

Aggregations

Database (org.h2.engine.Database)9 ValueString (org.h2.value.ValueString)8 Table (org.h2.table.Table)7 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)5 StatementBuilder (org.h2.util.StatementBuilder)5 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)4 Constraint (org.h2.constraint.Constraint)4 ExpressionColumn (org.h2.expression.ExpressionColumn)4 DbException (org.h2.message.DbException)4 Column (org.h2.table.Column)4 IndexColumn (org.h2.table.IndexColumn)4 PreparedStatement (java.sql.PreparedStatement)3 DbObject (org.h2.engine.DbObject)3 Expression (org.h2.expression.Expression)3 ValueExpression (org.h2.expression.ValueExpression)3 Index (org.h2.index.Index)3 Sequence (org.h2.schema.Sequence)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2