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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations