Search in sources :

Example 1 with GridSqlParameter

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter in project ignite by apache.

the class DmlAstUtils method getFastUpdateArgs.

/**
     * @param update UPDATE statement.
     * @return {@code null} if given statement directly updates {@code _val} column with a literal or param value
     * and filters by single non expression key (and, optionally,  by single non expression value).
     */
public static FastUpdateArguments getFastUpdateArgs(GridSqlUpdate update) {
    IgnitePair<GridSqlElement> filter = findKeyValueEqualityCondition(update.where());
    if (filter == null)
        return null;
    if (update.cols().size() != 1)
        return null;
    Table tbl = update.cols().get(0).column().getTable();
    if (!(tbl instanceof GridH2Table))
        return null;
    GridH2RowDescriptor desc = ((GridH2Table) tbl).rowDescriptor();
    if (!desc.isValueColumn(update.cols().get(0).column().getColumnId()))
        return null;
    GridSqlElement set = update.set().get(update.cols().get(0).columnName());
    if (!(set instanceof GridSqlConst || set instanceof GridSqlParameter))
        return null;
    return new FastUpdateArguments(operandForElement(filter.getKey()), operandForElement(filter.getValue()), operandForElement(set));
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 2 with GridSqlParameter

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter in project ignite by apache.

the class DmlAstUtils method findParams.

/**
 * @param el Element.
 * @param params Parameters.
 * @param target Extracted parameters.
 * @param paramIdxs Parameter indexes.
 */
private static void findParams(@Nullable GridSqlElement el, Object[] params, ArrayList<Object> target, IntArray paramIdxs) {
    if (el == null)
        return;
    if (el instanceof GridSqlParameter) {
        // H2 Supports queries like "select ?5" but first 4 non-existing parameters are need to be set to any value.
        // Here we will set them to NULL.
        final int idx = ((GridSqlParameter) el).index();
        while (target.size() < idx) target.add(null);
        if (params.length <= idx)
            throw new IgniteException("Invalid number of query parameters. " + "Cannot find " + idx + " parameter.");
        Object param = params[idx];
        if (idx == target.size())
            target.add(param);
        else
            target.set(idx, param);
        paramIdxs.add(idx);
    } else if (el instanceof GridSqlSubquery)
        findParams(((GridSqlSubquery) el).subquery(), params, target, paramIdxs);
    else
        for (int i = 0; i < el.size(); i++) findParams((GridSqlElement) el.child(i), params, target, paramIdxs);
}
Also used : GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) IgniteException(org.apache.ignite.IgniteException) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)

Example 3 with GridSqlParameter

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter in project ignite by apache.

the class DmlAstUtils method selectForInsertOrMerge.

/**
 * Create SELECT on which subsequent INSERT or MERGE will be based.
 *
 * @param cols Columns to insert values into.
 * @param rows Rows to create pseudo-SELECT upon.
 * @param subQry Subquery to use rather than rows.
 * @return Subquery or pseudo-SELECT to evaluate inserted expressions, or {@code null} no query needs to be run.
 */
public static GridSqlQuery selectForInsertOrMerge(GridSqlColumn[] cols, List<GridSqlElement[]> rows, GridSqlQuery subQry) {
    if (!F.isEmpty(rows)) {
        assert !F.isEmpty(cols);
        GridSqlSelect sel = new GridSqlSelect();
        GridSqlFunction from = new GridSqlFunction(GridSqlFunctionType.TABLE);
        sel.from(from);
        GridSqlArray[] args = new GridSqlArray[cols.length];
        boolean noQry = true;
        for (int i = 0; i < cols.length; i++) {
            GridSqlArray arr = new GridSqlArray(rows.size());
            String colName = cols[i].columnName();
            GridSqlAlias alias = new GridSqlAlias(colName, arr);
            alias.resultType(cols[i].resultType());
            from.addChild(alias);
            args[i] = arr;
            GridSqlColumn newCol = new GridSqlColumn(null, from, null, "TABLE", colName);
            newCol.resultType(cols[i].resultType());
            sel.addColumn(newCol, true);
        }
        for (GridSqlElement[] row : rows) {
            assert cols.length == row.length;
            for (int i = 0; i < row.length; i++) {
                GridSqlElement el = row[i];
                noQry &= (el instanceof GridSqlConst || el instanceof GridSqlParameter);
                args[i].addChild(row[i]);
            }
        }
        if (noQry)
            return null;
        return sel;
    } else {
        assert subQry != null;
        return subQry;
    }
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter) ValueString(org.h2.value.ValueString) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction) GridSqlArray(org.apache.ignite.internal.processors.query.h2.sql.GridSqlArray)

Example 4 with GridSqlParameter

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter in project ignite by apache.

the class DmlAstUtils method injectKeysFilterParam.

/**
 * Append additional condition to WHERE for it to select only specific keys.
 *
 * @param where Initial condition.
 * @param keyCol Column to base the new condition on.
 * @return New condition.
 */
private static GridSqlElement injectKeysFilterParam(GridSqlElement where, GridSqlColumn keyCol, int paramIdx) {
    // Yes, we need a subquery for "WHERE _key IN ?" to work with param being an array without dirty query rewriting.
    GridSqlSelect sel = new GridSqlSelect();
    GridSqlFunction from = new GridSqlFunction(GridSqlFunctionType.TABLE);
    sel.from(from);
    GridSqlColumn col = new GridSqlColumn(null, from, null, "TABLE", "_IGNITE_ERR_KEYS");
    sel.addColumn(col, true);
    GridSqlAlias alias = new GridSqlAlias("_IGNITE_ERR_KEYS", new GridSqlParameter(paramIdx));
    alias.resultType(keyCol.resultType());
    from.addChild(alias);
    GridSqlElement e = new GridSqlOperation(GridSqlOperationType.IN, keyCol, new GridSqlSubquery(sel));
    if (where == null)
        return e;
    else
        return new GridSqlOperation(GridSqlOperationType.AND, where, e);
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Example 5 with GridSqlParameter

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter in project ignite by apache.

the class DmlAstUtils method getFastUpdateArgs.

/**
 * @param update UPDATE statement.
 * @return {@code null} if given statement directly updates {@code _val} column with a literal or param value
 * and filters by single non expression key (and, optionally,  by single non expression value).
 */
public static FastUpdate getFastUpdateArgs(GridSqlUpdate update) {
    IgnitePair<GridSqlElement> filter = findKeyValueEqualityCondition(update.where());
    if (filter == null)
        return null;
    if (update.cols().size() != 1)
        return null;
    Table tbl = update.cols().get(0).column().getTable();
    if (!(tbl instanceof GridH2Table))
        return null;
    GridH2RowDescriptor desc = ((GridH2Table) tbl).rowDescriptor();
    if (!desc.isValueColumn(update.cols().get(0).column().getColumnId()))
        return null;
    GridSqlElement set = update.set().get(update.cols().get(0).columnName());
    if (!(set instanceof GridSqlConst || set instanceof GridSqlParameter))
        return null;
    return FastUpdate.create(filter.getKey(), filter.getValue(), set);
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)

Aggregations

GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)5 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)5 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)5 GridSqlParameter (org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)5 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)3 GridSqlConst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst)3 IgniteException (org.apache.ignite.IgniteException)2 GridSqlAlias (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias)2 GridSqlFunction (org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction)2 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)2 GridSqlSubquery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery)2 IndexColumn (org.h2.table.IndexColumn)2 Table (org.h2.table.Table)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 CacheQueryPartitionInfo (org.apache.ignite.internal.processors.cache.query.CacheQueryPartitionInfo)1 FastUpdateArguments (org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)1 GridSqlArray (org.apache.ignite.internal.processors.query.h2.sql.GridSqlArray)1 GridSqlOperation (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)1 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)1 GridSqlType.fromColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn)1