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