Search in sources :

Example 6 with DbObject

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

the class ScriptCommand method query.

@Override
public ResultInterface query(int maxrows) {
    session.getUser().checkAdmin();
    reset();
    Database db = session.getDatabase();
    if (schemaNames != null) {
        for (String schemaName : schemaNames) {
            Schema schema = db.findSchema(schemaName);
            if (schema == null) {
                throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
            }
        }
    }
    try {
        result = createResult();
        deleteStore();
        openOutput();
        if (out != null) {
            buffer = new byte[Constants.IO_BUFFER_SIZE];
        }
        if (settings) {
            for (Setting setting : db.getAllSettings()) {
                if (setting.getName().equals(SetTypes.getTypeName(SetTypes.CREATE_BUILD))) {
                    // (it is only set when creating the database)
                    continue;
                }
                add(setting.getCreateSQL(), false);
            }
        }
        if (out != null) {
            add("", true);
        }
        for (User user : db.getAllUsers()) {
            add(user.getCreateSQL(passwords), false);
        }
        for (Role role : db.getAllRoles()) {
            add(role.getCreateSQL(true), false);
        }
        for (Schema schema : db.getAllSchemas()) {
            if (excludeSchema(schema)) {
                continue;
            }
            add(schema.getCreateSQL(), false);
        }
        for (UserDataType datatype : db.getAllUserDataTypes()) {
            if (drop) {
                add(datatype.getDropSQL(), false);
            }
            add(datatype.getCreateSQL(), false);
        }
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.CONSTANT)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            Constant constant = (Constant) obj;
            add(constant.getCreateSQL(), false);
        }
        final ArrayList<Table> tables = db.getAllTablesAndViews(false);
        // sort by id, so that views are after tables and views on views
        // after the base views
        Collections.sort(tables, new Comparator<Table>() {

            @Override
            public int compare(Table t1, Table t2) {
                return t1.getId() - t2.getId();
            }
        });
        // Generate the DROP XXX  ... IF EXISTS
        for (Table table : tables) {
            if (excludeSchema(table.getSchema())) {
                continue;
            }
            if (excludeTable(table)) {
                continue;
            }
            if (table.isHidden()) {
                continue;
            }
            table.lock(session, false, false);
            String sql = table.getCreateSQL();
            if (sql == null) {
                // null for metadata tables
                continue;
            }
            if (drop) {
                add(table.getDropSQL(), false);
            }
        }
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            if (drop) {
                add(obj.getDropSQL(), false);
            }
            add(obj.getCreateSQL(), false);
        }
        for (UserAggregate agg : db.getAllAggregates()) {
            if (drop) {
                add(agg.getDropSQL(), false);
            }
            add(agg.getCreateSQL(), false);
        }
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.SEQUENCE)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            Sequence sequence = (Sequence) obj;
            if (drop) {
                add(sequence.getDropSQL(), false);
            }
            add(sequence.getCreateSQL(), false);
        }
        // Generate CREATE TABLE and INSERT...VALUES
        int count = 0;
        for (Table table : tables) {
            if (excludeSchema(table.getSchema())) {
                continue;
            }
            if (excludeTable(table)) {
                continue;
            }
            if (table.isHidden()) {
                continue;
            }
            table.lock(session, false, false);
            String createTableSql = table.getCreateSQL();
            if (createTableSql == null) {
                // null for metadata tables
                continue;
            }
            final TableType tableType = table.getTableType();
            add(createTableSql, false);
            final ArrayList<Constraint> constraints = table.getConstraints();
            if (constraints != null) {
                for (Constraint constraint : constraints) {
                    if (Constraint.Type.PRIMARY_KEY == constraint.getConstraintType()) {
                        add(constraint.getCreateSQLWithoutIndexes(), false);
                    }
                }
            }
            if (TableType.TABLE == tableType) {
                if (table.canGetRowCount()) {
                    String rowcount = "-- " + table.getRowCountApproximation() + " +/- SELECT COUNT(*) FROM " + table.getSQL();
                    add(rowcount, false);
                }
                if (data) {
                    count = generateInsertValues(count, table);
                }
            }
            final ArrayList<Index> indexes = table.getIndexes();
            for (int j = 0; indexes != null && j < indexes.size(); j++) {
                Index index = indexes.get(j);
                if (!index.getIndexType().getBelongsToConstraint()) {
                    add(index.getCreateSQL(), false);
                }
            }
        }
        if (tempLobTableCreated) {
            add("DROP TABLE IF EXISTS SYSTEM_LOB_STREAM", true);
            add("CALL SYSTEM_COMBINE_BLOB(-1)", true);
            add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_CLOB", true);
            add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_BLOB", true);
            tempLobTableCreated = false;
        }
        // Generate CREATE CONSTRAINT ...
        final ArrayList<SchemaObject> constraints = db.getAllSchemaObjects(DbObject.CONSTRAINT);
        Collections.sort(constraints, new Comparator<SchemaObject>() {

            @Override
            public int compare(SchemaObject c1, SchemaObject c2) {
                return ((Constraint) c1).compareTo((Constraint) c2);
            }
        });
        for (SchemaObject obj : constraints) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            Constraint constraint = (Constraint) obj;
            if (excludeTable(constraint.getTable())) {
                continue;
            }
            if (constraint.getTable().isHidden()) {
                continue;
            }
            if (Constraint.Type.PRIMARY_KEY != constraint.getConstraintType()) {
                add(constraint.getCreateSQLWithoutIndexes(), false);
            }
        }
        // Generate CREATE TRIGGER ...
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.TRIGGER)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            TriggerObject trigger = (TriggerObject) obj;
            if (excludeTable(trigger.getTable())) {
                continue;
            }
            add(trigger.getCreateSQL(), false);
        }
        // Generate GRANT ...
        for (Right right : db.getAllRights()) {
            DbObject object = right.getGrantedObject();
            if (object != null) {
                if (object instanceof Schema) {
                    if (excludeSchema((Schema) object)) {
                        continue;
                    }
                } else if (object instanceof Table) {
                    Table table = (Table) object;
                    if (excludeSchema(table.getSchema())) {
                        continue;
                    }
                    if (excludeTable(table)) {
                        continue;
                    }
                }
            }
            add(right.getCreateSQL(), false);
        }
        // Generate COMMENT ON ...
        for (Comment comment : db.getAllComments()) {
            add(comment.getCreateSQL(), false);
        }
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        throw DbException.convertIOException(e, getFileName());
    } finally {
        closeIO();
    }
    result.done();
    LocalResult r = result;
    reset();
    return r;
}
Also used : SchemaObject(org.h2.schema.SchemaObject) User(org.h2.engine.User) Constraint(org.h2.constraint.Constraint) Constant(org.h2.schema.Constant) Schema(org.h2.schema.Schema) Right(org.h2.engine.Right) TriggerObject(org.h2.schema.TriggerObject) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) LocalResult(org.h2.result.LocalResult) Database(org.h2.engine.Database) Comment(org.h2.engine.Comment) Table(org.h2.table.Table) TableType(org.h2.table.TableType) DbObject(org.h2.engine.DbObject) Setting(org.h2.engine.Setting) UserDataType(org.h2.engine.UserDataType) Sequence(org.h2.schema.Sequence) IOException(java.io.IOException) Constraint(org.h2.constraint.Constraint) Role(org.h2.engine.Role) UserAggregate(org.h2.engine.UserAggregate)

Example 7 with DbObject

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

the class Database method getDependentTable.

/**
 * Get the first table that depends on this object.
 *
 * @param obj the object to find
 * @param except the table to exclude (or null)
 * @return the first dependent table, or null
 */
public Table getDependentTable(SchemaObject obj, Table except) {
    switch(obj.getType()) {
        case DbObject.COMMENT:
        case DbObject.CONSTRAINT:
        case DbObject.INDEX:
        case DbObject.RIGHT:
        case DbObject.TRIGGER:
        case DbObject.USER:
            return null;
        default:
    }
    HashSet<DbObject> set = new HashSet<>();
    for (Table t : getAllTablesAndViews(false)) {
        if (except == t) {
            continue;
        } else if (TableType.VIEW == t.getTableType()) {
            continue;
        }
        set.clear();
        t.addDependencies(set);
        if (set.contains(obj)) {
            return t;
        }
    }
    return null;
}
Also used : MetaTable(org.h2.table.MetaTable) Table(org.h2.table.Table) HashSet(java.util.HashSet)

Example 8 with DbObject

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

the class Database method addDatabaseObject.

/**
 * Add an object to the database.
 *
 * @param session the session
 * @param obj the object to add
 */
public synchronized void addDatabaseObject(Session session, DbObject obj) {
    int id = obj.getId();
    if (id > 0 && !starting) {
        checkWritingAllowed();
    }
    HashMap<String, DbObject> map = getMap(obj.getType());
    if (obj.getType() == DbObject.USER) {
        User user = (User) obj;
        if (user.isAdmin() && systemUser.getName().equals(SYSTEM_USER_NAME)) {
            systemUser.rename(user.getName());
        }
    }
    String name = obj.getName();
    if (SysProperties.CHECK && map.get(name) != null) {
        DbException.throwInternalError("object already exists");
    }
    lockMeta(session);
    addMeta(session, obj);
    map.put(name, obj);
}
Also used : Constraint(org.h2.constraint.Constraint)

Example 9 with DbObject

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

the class Database method renameDatabaseObject.

/**
 * Rename a database object.
 *
 * @param session the session
 * @param obj the object
 * @param newName the new name
 */
public synchronized void renameDatabaseObject(Session session, DbObject obj, String newName) {
    checkWritingAllowed();
    int type = obj.getType();
    HashMap<String, DbObject> map = getMap(type);
    if (SysProperties.CHECK) {
        if (!map.containsKey(obj.getName())) {
            DbException.throwInternalError("not found: " + obj.getName());
        }
        if (obj.getName().equals(newName) || map.containsKey(newName)) {
            DbException.throwInternalError("object already exists: " + newName);
        }
    }
    obj.checkRename();
    int id = obj.getId();
    lockMeta(session);
    removeMeta(session, id);
    map.remove(obj.getName());
    obj.rename(newName);
    map.put(newName, obj);
    updateMetaAndFirstLevelChildren(session, obj);
}
Also used : Constraint(org.h2.constraint.Constraint)

Example 10 with DbObject

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

the class Database method addMeta.

private synchronized void addMeta(Session session, DbObject obj) {
    int id = obj.getId();
    if (id > 0 && !starting && !obj.isTemporary()) {
        Row r = meta.getTemplateRow();
        MetaRecord rec = new MetaRecord(obj);
        rec.setRecord(r);
        objectIds.set(id);
        if (SysProperties.CHECK) {
            verifyMetaLocked(session);
        }
        meta.addRow(session, r);
        if (isMultiVersion()) {
            // TODO this should work without MVCC, but avoid risks at the
            // moment
            session.log(meta, UndoLogRecord.INSERT, r);
        }
    }
}
Also used : Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow) Constraint(org.h2.constraint.Constraint)

Aggregations

DbObject (org.h2.engine.DbObject)12 Constraint (org.h2.constraint.Constraint)11 Table (org.h2.table.Table)9 Database (org.h2.engine.Database)8 Right (org.h2.engine.Right)5 Index (org.h2.index.Index)5 Sequence (org.h2.schema.Sequence)5 Schema (org.h2.schema.Schema)4 SchemaObject (org.h2.schema.SchemaObject)4 Column (org.h2.table.Column)4 TableView (org.h2.table.TableView)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Role (org.h2.engine.Role)3 User (org.h2.engine.User)3 DbException (org.h2.message.DbException)3 TriggerObject (org.h2.schema.TriggerObject)3 IOException (java.io.IOException)2 ConstraintReferential (org.h2.constraint.ConstraintReferential)2 Comment (org.h2.engine.Comment)2