Search in sources :

Example 16 with TableView

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

the class Parser method parseAlterView.

private AlterView parseAlterView() {
    AlterView command = new AlterView(session);
    boolean ifExists = readIfExists(false);
    command.setIfExists(ifExists);
    String viewName = readIdentifierWithSchema();
    Table tableView = getSchema().findTableOrView(session, viewName);
    if (!(tableView instanceof TableView) && !ifExists) {
        throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName);
    }
    TableView view = (TableView) tableView;
    command.setView(view);
    read("RECOMPILE");
    return command;
}
Also used : AlterView(org.h2.command.ddl.AlterView) RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) ValueString(org.h2.value.ValueString) TableView(org.h2.table.TableView)

Example 17 with TableView

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

the class Parser method createCTEView.

private TableView createCTEView(String cteViewName, String querySQL, List<Column> columnTemplateList, boolean allowRecursiveQueryDetection, boolean addViewToSession, boolean isPersistent, Session targetSession) {
    Database db = targetSession.getDatabase();
    Schema schema = getSchemaWithDefault();
    int id = db.allocateObjectId();
    Column[] columnTemplateArray = columnTemplateList.toArray(new Column[0]);
    // No easy way to determine if this is a recursive query up front, so we just compile
    // it twice - once without the flag set, and if we didn't see a recursive term,
    // then we just compile it again.
    TableView view;
    synchronized (targetSession) {
        view = new TableView(schema, id, cteViewName, querySQL, parameters, columnTemplateArray, targetSession, allowRecursiveQueryDetection, false, /* literalsChecked */
        true, /* isTableExpression */
        isPersistent);
        if (!view.isRecursiveQueryDetected() && allowRecursiveQueryDetection) {
            if (isPersistent) {
                db.addSchemaObject(targetSession, view);
                view.lock(targetSession, true, true);
                targetSession.getDatabase().removeSchemaObject(targetSession, view);
            } else {
                session.removeLocalTempTable(view);
            }
            view = new TableView(schema, id, cteViewName, querySQL, parameters, columnTemplateArray, targetSession, false, /* assume recursive */
            false, /* literalsChecked */
            true, /* isTableExpression */
            isPersistent);
        }
        // both removeSchemaObject and removeLocalTempTable hold meta locks
        targetSession.getDatabase().unlockMeta(targetSession);
    }
    view.setTableExpression(true);
    view.setTemporary(!isPersistent);
    view.setHidden(true);
    view.setOnCommitDrop(false);
    if (addViewToSession) {
        if (isPersistent) {
            db.addSchemaObject(targetSession, view);
            view.unlock(targetSession);
            db.unlockMeta(targetSession);
        } else {
            targetSession.addLocalTempTable(view);
        }
    }
    return view;
}
Also used : AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database) DropDatabase(org.h2.command.ddl.DropDatabase) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) TableView(org.h2.table.TableView)

Example 18 with TableView

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

the class DropView method update.

@Override
public int update() {
    session.commit(true);
    Table view = getSchema().findTableOrView(session, viewName);
    if (view == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName);
        }
    } else {
        if (TableType.VIEW != view.getTableType()) {
            throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName);
        }
        session.getUser().checkRight(view, Right.ALL);
        if (dropAction == ConstraintActionType.RESTRICT) {
            for (DbObject child : view.getChildren()) {
                if (child instanceof TableView) {
                    throw DbException.get(ErrorCode.CANNOT_DROP_2, viewName, child.getName());
                }
            }
        }
        // TODO: Where is the ConstraintReferential.CASCADE style drop processing ? It's
        // supported from imported keys - but not for dependent db objects
        TableView tableView = (TableView) view;
        ArrayList<Table> copyOfDependencies = new ArrayList<>(tableView.getTables());
        view.lock(session, true, true);
        session.getDatabase().removeSchemaObject(session, view);
        // remove dependent table expressions
        for (Table childTable : copyOfDependencies) {
            if (TableType.VIEW == childTable.getTableType()) {
                TableView childTableView = (TableView) childTable;
                if (childTableView.isTableExpression() && childTableView.getName() != null) {
                    session.getDatabase().removeSchemaObject(session, childTableView);
                }
            }
        }
        // make sure its all unlocked
        session.getDatabase().unlockMeta(session);
    }
    return 0;
}
Also used : Table(org.h2.table.Table) DbObject(org.h2.engine.DbObject) ArrayList(java.util.ArrayList) TableView(org.h2.table.TableView)

Example 19 with TableView

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

the class CreateView method update.

@Override
public int update() {
    session.commit(true);
    session.getUser().checkAdmin();
    Database db = session.getDatabase();
    TableView view = null;
    Table old = getSchema().findTableOrView(session, viewName);
    if (old != null) {
        if (ifNotExists) {
            return 0;
        }
        if (!orReplace || TableType.VIEW != old.getTableType()) {
            throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName);
        }
        view = (TableView) old;
    }
    int id = getObjectId();
    String querySQL;
    if (select == null) {
        querySQL = selectSQL;
    } else {
        ArrayList<Parameter> params = select.getParameters();
        if (params != null && !params.isEmpty()) {
            throw DbException.getUnsupportedException("parameters in views");
        }
        querySQL = select.getPlanSQL();
    }
    Column[] columnTemplatesAsUnknowns = null;
    Column[] columnTemplatesAsStrings = null;
    if (columnNames != null) {
        columnTemplatesAsUnknowns = new Column[columnNames.length];
        columnTemplatesAsStrings = new Column[columnNames.length];
        for (int i = 0; i < columnNames.length; ++i) {
            // non table expressions are fine to use unknown column type
            columnTemplatesAsUnknowns[i] = new Column(columnNames[i], Value.UNKNOWN);
            // table expressions can't have unknown types - so we use string instead
            columnTemplatesAsStrings[i] = new Column(columnNames[i], Value.STRING);
        }
    }
    if (view == null) {
        if (isTableExpression) {
            view = TableView.createTableViewMaybeRecursive(getSchema(), id, viewName, querySQL, null, columnTemplatesAsStrings, session, false, /* literalsChecked */
            isTableExpression, true, /* isPersistent */
            db);
        } else {
            view = new TableView(getSchema(), id, viewName, querySQL, null, columnTemplatesAsUnknowns, session, false, /* allow recursive */
            false, /* literalsChecked */
            isTableExpression, true);
        }
    } else {
        // TODO support isTableExpression in replace function...
        view.replace(querySQL, columnTemplatesAsUnknowns, session, false, force, false);
        view.setModified();
    }
    if (comment != null) {
        view.setComment(comment);
    }
    if (old == null) {
        db.addSchemaObject(session, view);
        db.unlockMeta(session);
    } else {
        db.updateMeta(session, view);
    }
    return 0;
}
Also used : Table(org.h2.table.Table) Column(org.h2.table.Column) Database(org.h2.engine.Database) Parameter(org.h2.expression.Parameter) TableView(org.h2.table.TableView)

Example 20 with TableView

use of org.h2.table.TableView in project ignite by apache.

the class CollocationModel method isAffinityColumn.

/**
 * @param f Table filter.
 * @param expCol Expression column.
 * @param validate Query validation flag.
 * @return {@code true} It it is an affinity column.
 */
@SuppressWarnings("IfMayBeConditional")
private static boolean isAffinityColumn(TableFilter f, ExpressionColumn expCol, boolean validate) {
    Column col = expCol.getColumn();
    if (col == null)
        return false;
    Table t = col.getTable();
    if (t.isView()) {
        Query qry;
        if (f.getIndex() != null)
            qry = getSubQuery(f);
        else
            qry = GridSqlQueryParser.VIEW_QUERY.get((TableView) t);
        return isAffinityColumn(qry, expCol, validate);
    }
    if (t instanceof GridH2Table) {
        GridH2Table t0 = (GridH2Table) t;
        if (validate && t0.isCustomAffinityMapper())
            throw customAffinityError((t0).cacheName());
        IndexColumn affCol = t0.getAffinityKeyColumn();
        return affCol != null && col.getColumnId() == affCol.column.getColumnId();
    }
    return false;
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) Query(org.h2.command.dml.Query) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IndexColumn(org.h2.table.IndexColumn)

Aggregations

TableView (org.h2.table.TableView)14 Table (org.h2.table.Table)10 Column (org.h2.table.Column)8 ArrayList (java.util.ArrayList)6 Query (org.h2.command.dml.Query)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 Constraint (org.h2.constraint.Constraint)5 DbObject (org.h2.engine.DbObject)5 Schema (org.h2.schema.Schema)5 IndexColumn (org.h2.table.IndexColumn)5 Sequence (org.h2.schema.Sequence)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)3 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)3 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)3 CreateTable (org.h2.command.ddl.CreateTable)3 DropTable (org.h2.command.ddl.DropTable)3 Index (org.h2.index.Index)3 DbException (org.h2.message.DbException)3 TriggerObject (org.h2.schema.TriggerObject)3 FunctionTable (org.h2.table.FunctionTable)3