Search in sources :

Example 46 with Schema

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

the class DropDatabase method dropAllObjects.

private void dropAllObjects() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    db.lockMeta(session);
    // There can be dependencies between tables e.g. using computed columns,
    // so we might need to loop over them multiple times.
    boolean runLoopAgain;
    do {
        ArrayList<Table> tables = db.getAllTablesAndViews(false);
        ArrayList<Table> toRemove = New.arrayList();
        for (Table t : tables) {
            if (t.getName() != null && TableType.VIEW == t.getTableType()) {
                toRemove.add(t);
            }
        }
        for (Table t : tables) {
            if (t.getName() != null && TableType.TABLE_LINK == t.getTableType()) {
                toRemove.add(t);
            }
        }
        for (Table t : tables) {
            if (t.getName() != null && TableType.TABLE == t.getTableType() && !t.isHidden()) {
                toRemove.add(t);
            }
        }
        for (Table t : tables) {
            if (t.getName() != null && TableType.EXTERNAL_TABLE_ENGINE == t.getTableType() && !t.isHidden()) {
                toRemove.add(t);
            }
        }
        runLoopAgain = false;
        for (Table t : toRemove) {
            if (t.getName() == null) {
            // ignore, already dead
            } else if (db.getDependentTable(t, t) == null) {
                db.removeSchemaObject(session, t);
            } else {
                runLoopAgain = true;
            }
        }
    } while (runLoopAgain);
    // TODO session-local temp tables are not removed
    for (Schema schema : db.getAllSchemas()) {
        if (schema.canDrop()) {
            db.removeDatabaseObject(session, schema);
        }
    }
    ArrayList<SchemaObject> list = New.arrayList();
    for (SchemaObject obj : db.getAllSchemaObjects(DbObject.SEQUENCE)) {
        // ones that belong to session-local temp tables.
        if (!((Sequence) obj).getBelongsToTable()) {
            list.add(obj);
        }
    }
    // maybe constraints and triggers on system tables will be allowed in
    // the future
    list.addAll(db.getAllSchemaObjects(DbObject.CONSTRAINT));
    list.addAll(db.getAllSchemaObjects(DbObject.TRIGGER));
    list.addAll(db.getAllSchemaObjects(DbObject.CONSTANT));
    list.addAll(db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS));
    for (SchemaObject obj : list) {
        if (obj.isHidden()) {
            continue;
        }
        db.removeSchemaObject(session, obj);
    }
    for (User user : db.getAllUsers()) {
        if (user != session.getUser()) {
            db.removeDatabaseObject(session, user);
        }
    }
    for (Role role : db.getAllRoles()) {
        String sql = role.getCreateSQL();
        // the role PUBLIC must not be dropped
        if (sql != null) {
            db.removeDatabaseObject(session, role);
        }
    }
    ArrayList<DbObject> dbObjects = New.arrayList();
    dbObjects.addAll(db.getAllRights());
    dbObjects.addAll(db.getAllAggregates());
    dbObjects.addAll(db.getAllUserDataTypes());
    for (DbObject obj : dbObjects) {
        String sql = obj.getCreateSQL();
        // the role PUBLIC must not be dropped
        if (sql != null) {
            db.removeDatabaseObject(session, obj);
        }
    }
}
Also used : Role(org.h2.engine.Role) SchemaObject(org.h2.schema.SchemaObject) Table(org.h2.table.Table) User(org.h2.engine.User) DbObject(org.h2.engine.DbObject) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database)

Example 47 with Schema

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

the class CreateSchema method update.

@Override
public int update() {
    session.getUser().checkSchemaAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    User user = db.getUser(authorization);
    // during DB startup, the Right/Role records have not yet been loaded
    if (!db.isStarting()) {
        user.checkSchemaAdmin();
    }
    if (db.findSchema(schemaName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.SCHEMA_ALREADY_EXISTS_1, schemaName);
    }
    int id = getObjectId();
    Schema schema = new Schema(db, id, schemaName, user, false);
    schema.setTableEngineParams(tableEngineParams);
    db.addDatabaseObject(session, schema);
    return 0;
}
Also used : User(org.h2.engine.User) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database)

Example 48 with Schema

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

the class ExpressionColumn method optimize.

@Override
public Expression optimize(Session session) {
    if (columnResolver == null) {
        Schema schema = session.getDatabase().findSchema(tableAlias == null ? session.getCurrentSchemaName() : tableAlias);
        if (schema != null) {
            Constant constant = schema.findConstant(columnName);
            if (constant != null) {
                return constant.getValue();
            }
        }
        String name = columnName;
        if (tableAlias != null) {
            name = tableAlias + "." + name;
            if (schemaName != null) {
                name = schemaName + "." + name;
            }
        }
        throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, name);
    }
    return columnResolver.optimize(this, column);
}
Also used : Constant(org.h2.schema.Constant) Schema(org.h2.schema.Schema)

Example 49 with Schema

use of com.predic8.schema.Schema in project ignite by apache.

the class GridSqlQueryParser method parseCreateIndex.

/**
 * Parse {@code CREATE INDEX} statement.
 *
 * @param createIdx {@code CREATE INDEX} statement.
 * @see <a href="http://h2database.com/html/grammar.html#create_index">H2 {@code CREATE INDEX} spec.</a>
 */
private GridSqlCreateIndex parseCreateIndex(CreateIndex createIdx) {
    if (CREATE_INDEX_HASH.get(createIdx) || CREATE_INDEX_PRIMARY_KEY.get(createIdx) || CREATE_INDEX_UNIQUE.get(createIdx)) {
        throw new IgniteSQLException("Only SPATIAL modifier is supported for CREATE INDEX", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    GridSqlCreateIndex res = new GridSqlCreateIndex();
    Schema schema = SCHEMA_COMMAND_SCHEMA.get(createIdx);
    String tblName = CREATE_INDEX_TABLE_NAME.get(createIdx);
    res.schemaName(schema.getName());
    res.tableName(tblName);
    res.ifNotExists(CREATE_INDEX_IF_NOT_EXISTS.get(createIdx));
    QueryIndex idx = new QueryIndex();
    idx.setName(CREATE_INDEX_NAME.get(createIdx));
    idx.setIndexType(CREATE_INDEX_SPATIAL.get(createIdx) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
    IndexColumn[] cols = CREATE_INDEX_COLUMNS.get(createIdx);
    LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>(cols.length);
    for (IndexColumn col : CREATE_INDEX_COLUMNS.get(createIdx)) {
        int sortType = INDEX_COLUMN_SORT_TYPE.get(col);
        if ((sortType & SortOrder.NULLS_FIRST) != 0 || (sortType & SortOrder.NULLS_LAST) != 0) {
            throw new IgniteSQLException("NULLS FIRST and NULLS LAST modifiers are not supported for index columns", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        }
        Boolean prev = flds.put(INDEX_COLUMN_NAME.get(col), (sortType & SortOrder.DESCENDING) == 0);
        if (prev != null) {
            String prevCol = INDEX_COLUMN_NAME.get(col) + " " + (prev ? "ASC" : "DESC");
            throw new IgniteSQLException("Already defined column in index: " + prevCol, IgniteQueryErrorCode.COLUMN_ALREADY_EXISTS);
        }
    }
    idx.setFields(flds);
    res.index(idx);
    return res;
}
Also used : Schema(org.h2.schema.Schema) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryIndex(org.apache.ignite.cache.QueryIndex) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) IndexColumn(org.h2.table.IndexColumn) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with Schema

use of com.predic8.schema.Schema in project core by s4.

the class EventClock method update.

public void update(EventWrapper eventWrapper) {
    long eventTime = -1;
    String streamName = eventWrapper.getStreamName();
    String fieldName = eventClockStreamsMap.get(streamName);
    if (fieldName != null) {
        Object event = eventWrapper.getEvent();
        Schema schema = schemaContainer.getSchema(event.getClass());
        Property property = schema.getProperties().get(fieldName);
        if (property != null && (property.getType().equals(Long.TYPE) || property.getType().equals(Long.class))) {
            try {
                eventTime = (Long) property.getGetterMethod().invoke(event);
                updateTime(eventTime);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : Schema(io.s4.schema.Schema) Property(io.s4.schema.Schema.Property)

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