Search in sources :

Example 1 with GridSqlSubquery

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery 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 2 with GridSqlSubquery

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery 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)

Aggregations

GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)2 GridSqlParameter (org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)2 GridSqlSubquery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery)2 IgniteException (org.apache.ignite.IgniteException)1 GridSqlAlias (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias)1 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)1 GridSqlFunction (org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction)1 GridSqlOperation (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)1 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)1