Search in sources :

Example 86 with Column

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

the class TableView method replace.

/**
 * Try to replace the SQL statement of the view and re-compile this and all
 * dependent views.
 *
 * @param querySQL the SQL statement
 * @param newColumnTemplates the columns
 * @param session the session
 * @param recursive whether this is a recursive view
 * @param force if errors should be ignored
 * @param literalsChecked if literals have been checked
 */
public void replace(String querySQL, Column[] newColumnTemplates, Session session, boolean recursive, boolean force, boolean literalsChecked) {
    String oldQuerySQL = this.querySQL;
    Column[] oldColumnTemplates = this.columnTemplates;
    boolean oldRecursive = this.allowRecursive;
    init(querySQL, null, newColumnTemplates == null ? this.columnTemplates : newColumnTemplates, session, recursive, literalsChecked, isTableExpression, isPersistent);
    DbException e = recompile(session, force, true);
    if (e != null) {
        init(oldQuerySQL, null, oldColumnTemplates, session, oldRecursive, literalsChecked, isTableExpression, isPersistent);
        recompile(session, true, false);
        throw e;
    }
}
Also used : ExpressionColumn(org.h2.expression.ExpressionColumn) DbException(org.h2.message.DbException)

Example 87 with Column

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

the class TableView method createTableViewMaybeRecursive.

/**
 * Create a view.
 *
 * @param schema the schema
 * @param id the view id
 * @param name the view name
 * @param querySQL the query
 * @param parameters the parameters
 * @param columnTemplates the columns
 * @param session the session
 * @param literalsChecked whether literals in the query are checked
 * @param isTableExpression if this is a table expression
 * @param isPersistent whether the view is persisted
 * @param db the database
 * @return the view
 */
public static TableView createTableViewMaybeRecursive(Schema schema, int id, String name, String querySQL, ArrayList<Parameter> parameters, Column[] columnTemplates, Session session, boolean literalsChecked, boolean isTableExpression, boolean isPersistent, Database db) {
    Table recursiveTable = TableView.createShadowTableForRecursiveTableExpression(isPersistent, session, name, schema, Arrays.asList(columnTemplates), db);
    List<Column> columnTemplateList;
    String[] querySQLOutput = { null };
    ArrayList<String> columnNames = new ArrayList<>();
    for (Column columnTemplate : columnTemplates) {
        columnNames.add(columnTemplate.getName());
    }
    try {
        Prepared withQuery = session.prepare(querySQL, false, false);
        if (isPersistent) {
            withQuery.setSession(session);
        }
        columnTemplateList = TableView.createQueryColumnTemplateList(columnNames.toArray(new String[1]), (Query) withQuery, querySQLOutput);
    } finally {
        TableView.destroyShadowTableForRecursiveExpression(isPersistent, session, recursiveTable);
    }
    // build with recursion turned on
    TableView view = new TableView(schema, id, name, querySQL, parameters, columnTemplateList.toArray(columnTemplates), session, true, /* try recursive */
    literalsChecked, isTableExpression, isPersistent);
    // and no recursive index
    if (!view.isRecursiveQueryDetected()) {
        if (isPersistent) {
            db.addSchemaObject(session, view);
            view.lock(session, true, true);
            session.getDatabase().removeSchemaObject(session, view);
            // during database startup - this method does not normally get called - and it
            // needs to be to correctly un-register the table which the table expression
            // uses...
            view.removeChildrenAndResources(session);
        } else {
            session.removeLocalTempTable(view);
        }
        view = new TableView(schema, id, name, querySQL, parameters, columnTemplates, session, false, /* detected not recursive */
        literalsChecked, isTableExpression, isPersistent);
    }
    return view;
}
Also used : Query(org.h2.command.dml.Query) ExpressionColumn(org.h2.expression.ExpressionColumn) ArrayList(java.util.ArrayList) Prepared(org.h2.command.Prepared)

Example 88 with Column

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

the class TableView method init.

private synchronized void init(String querySQL, ArrayList<Parameter> params, Column[] columnTemplates, Session session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression, boolean isPersistent) {
    this.querySQL = querySQL;
    this.columnTemplates = columnTemplates;
    this.allowRecursive = allowRecursive;
    this.isRecursiveQueryDetected = false;
    this.isTableExpression = isTableExpression;
    this.isPersistent = isPersistent;
    index = new ViewIndex(this, querySQL, params, allowRecursive);
    initColumnsAndTables(session, literalsChecked);
}
Also used : ViewIndex(org.h2.index.ViewIndex)

Example 89 with Column

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

the class TableView method createQueryColumnTemplateList.

/**
 * Creates a list of column templates from a query (usually from WITH query,
 * but could be any query)
 *
 * @param cols - an optional list of column names (can be specified by WITH
 *            clause overriding usual select names)
 * @param theQuery - the query object we want the column list for
 * @param querySQLOutput - array of length 1 to receive extra 'output' field
 *            in addition to return value - containing the SQL query of the
 *            Query object
 * @return a list of column object returned by withQuery
 */
public static List<Column> createQueryColumnTemplateList(String[] cols, Query theQuery, String[] querySQLOutput) {
    List<Column> columnTemplateList = new ArrayList<>();
    theQuery.prepare();
    // String array of length 1 is to receive extra 'output' field in addition to
    // return value
    querySQLOutput[0] = StringUtils.cache(theQuery.getPlanSQL());
    ColumnNamer columnNamer = new ColumnNamer(theQuery.getSession());
    ArrayList<Expression> withExpressions = theQuery.getExpressions();
    for (int i = 0; i < withExpressions.size(); ++i) {
        Expression columnExp = withExpressions.get(i);
        // use the passed in column name if supplied, otherwise use alias
        // (if found) otherwise use column name derived from column
        // expression
        String columnName = columnNamer.getColumnName(columnExp, i, cols);
        columnTemplateList.add(new Column(columnName, columnExp.getType()));
    }
    return columnTemplateList;
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) ExpressionColumn(org.h2.expression.ExpressionColumn) Expression(org.h2.expression.Expression) ArrayList(java.util.ArrayList)

Example 90 with Column

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

the class TableView method initColumnsAndTables.

private void initColumnsAndTables(Session session, boolean literalsChecked) {
    Column[] cols;
    removeCurrentViewFromOtherTables();
    setTableExpression(isTableExpression);
    try {
        Query compiledQuery = compileViewQuery(session, querySQL, literalsChecked, getName());
        this.querySQL = compiledQuery.getPlanSQL();
        tables = new ArrayList<>(compiledQuery.getTables());
        ArrayList<Expression> expressions = compiledQuery.getExpressions();
        ArrayList<Column> list = New.arrayList();
        ColumnNamer columnNamer = new ColumnNamer(session);
        for (int i = 0, count = compiledQuery.getColumnCount(); i < count; i++) {
            Expression expr = expressions.get(i);
            String name = null;
            int type = Value.UNKNOWN;
            if (columnTemplates != null && columnTemplates.length > i) {
                name = columnTemplates[i].getName();
                type = columnTemplates[i].getType();
            }
            if (name == null) {
                name = expr.getAlias();
            }
            name = columnNamer.getColumnName(expr, i, name);
            if (type == Value.UNKNOWN) {
                type = expr.getType();
            }
            long precision = expr.getPrecision();
            int scale = expr.getScale();
            int displaySize = expr.getDisplaySize();
            String[] enumerators = null;
            if (type == Value.ENUM) {
                if (expr instanceof ExpressionColumn) {
                    enumerators = ((ExpressionColumn) expr).getColumn().getEnumerators();
                }
            }
            Column col = new Column(name, type, precision, scale, displaySize, enumerators);
            col.setTable(this, i);
            // Fetch check constraint from view column source
            ExpressionColumn fromColumn = null;
            if (expr instanceof ExpressionColumn) {
                fromColumn = (ExpressionColumn) expr;
            } else if (expr instanceof Alias) {
                Expression aliasExpr = expr.getNonAliasExpression();
                if (aliasExpr instanceof ExpressionColumn) {
                    fromColumn = (ExpressionColumn) aliasExpr;
                }
            }
            if (fromColumn != null) {
                Expression checkExpression = fromColumn.getColumn().getCheckConstraint(session, name);
                if (checkExpression != null) {
                    col.addCheckConstraint(session, checkExpression);
                }
            }
            list.add(col);
        }
        cols = list.toArray(new Column[0]);
        createException = null;
        viewQuery = compiledQuery;
    } catch (DbException e) {
        e.addSQL(getCreateSQL());
        createException = e;
        // table expression query.
        if (isRecursiveQueryExceptionDetected(createException)) {
            this.isRecursiveQueryDetected = true;
        }
        tables = New.arrayList();
        cols = new Column[0];
        if (allowRecursive && columnTemplates != null) {
            cols = new Column[columnTemplates.length];
            for (int i = 0; i < columnTemplates.length; i++) {
                cols[i] = columnTemplates[i].getClone();
            }
            index.setRecursive(true);
            createException = null;
        }
    }
    setColumns(cols);
    if (getId() != 0) {
        addDependentViewToTables();
    }
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) Query(org.h2.command.dml.Query) ExpressionColumn(org.h2.expression.ExpressionColumn) DbException(org.h2.message.DbException) ExpressionColumn(org.h2.expression.ExpressionColumn) Expression(org.h2.expression.Expression) Alias(org.h2.expression.Alias)

Aggregations

Column (org.h2.table.Column)146 Value (org.h2.value.Value)83 IndexColumn (org.h2.table.IndexColumn)79 DbException (org.h2.message.DbException)64 SQLException (java.sql.SQLException)60 Expression (org.h2.expression.Expression)55 ExpressionColumn (org.h2.expression.ExpressionColumn)51 ValueString (org.h2.value.ValueString)34 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)32 ValueExpression (org.h2.expression.ValueExpression)30 ArrayList (java.util.ArrayList)27 PreparedStatement (java.sql.PreparedStatement)26 Index (org.h2.index.Index)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)25 Table (org.h2.table.Table)25 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)22 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)21 StatementBuilder (org.h2.util.StatementBuilder)19 Constraint (org.h2.constraint.Constraint)18