public FilterItem setQuery(Query query) { _query = query; if (_childItems == null) { if (_expression == null) { if (_selectItem.getQuery() == null) { _selectItem.setQuery(_query); } if (_operand is SelectItem) { SelectItem operand = (SelectItem)_operand; if (operand.getQuery() == null) { operand.setQuery(_query); } } } } else { foreach (FilterItem item in _childItems) { if (item.getQuery() == null) { item.setQuery(_query); } } } return(this); }
public _QueryPartProcessor_FindFilterItem_impl_(Query query_arg, List <Object> list_arg, object operand_arg, SelectItem select_item_arg) { _query = query_arg; _list = list_arg; _operand = operand_arg; _select_item = select_item_arg; } // constructor
/** * Creates a clone of the {@link SelectItem} for use within a cloned * {@link Query}. * * @param clonedQuery * a new {@link Query} object that represents the clone-to-be of * a query. It is expected that {@link FromItem}s have already * been cloned in this {@link Query}. * @return */ public SelectItem clone(Query clonedQuery) { SelectItem subQuerySelectItem = (_subQuerySelectItem == null ? null : _subQuerySelectItem.clone()); FromItem fromItem; if (_fromItem == null) { fromItem = null; } else if (clonedQuery != null && _query != null) { int indexOfFromItem = _query.getFromClause().indexOf(_fromItem); if (indexOfFromItem != -1) { fromItem = clonedQuery.getFromClause().getItem(indexOfFromItem); } else { fromItem = _fromItem.clone(); } } else { fromItem = _fromItem.clone(); } SelectItem s = new SelectItem(_column, fromItem, _function, _functionParameters, _expression, subQuerySelectItem, _alias, _functionApproximationAllowed); return(s); } // clone()
} // toStringNoAlias() private void appendJoinOnItem(StringBuilder sb, String sideAlias, SelectItem onItem) { FromItem fromItem = onItem.getFromItem(); if (fromItem != null && fromItem.getSubQuery() != null && fromItem.getAlias() != null) { // there's a corner case scenario where an ON item references a // subquery being joined. In that case the getSuperQueryAlias() // method will include the subquery alias. string super_query_alias = onItem.getSuperQueryAlias(); sb.Append(super_query_alias); return; } if (_join != JoinType.None && _leftSide.getJoin() != JoinType.None) { sb.Append(onItem.toSql()); return; } if (sideAlias != null) { sb.Append(sideAlias); sb.Append('.'); } String superQueryAlias = onItem.getSuperQueryAlias(); sb.Append(superQueryAlias); } // appendJoinOnItem()
} // from() public Query from(Table leftTable, Table rightTable, JoinType joinType, Column leftOnColumn, Column rightOnColumn) { SelectItem[] leftOn = new SelectItem[] { new SelectItem(leftOnColumn) }; SelectItem[] rightOn = new SelectItem[] { new SelectItem(rightOnColumn) }; FromItem fromItem = new FromItem(joinType, new FromItem(leftTable), new FromItem(rightTable), leftOn, rightOn); return(from(fromItem)); } // from()
} // from() public Query groupBy(params String[] groupByTokens) { foreach (string groupByToken in groupByTokens) { SelectItem selectItem = findSelectItem(groupByToken, true); groupBy(new GroupByItem(selectItem)); } return(this); } // groupBy()
} // groupBy() public Query groupBy(params Column [] columns) { foreach (Column column in columns) { SelectItem selectItem = (SelectItem) new SelectItem(column).setQuery(this); _groupByClause.addItem(new GroupByItem(selectItem)); } return(this); } // groupBy()
protected object validateOperand(object operand) { if (operand is Column) { // gracefully convert to a select item. operand = new SelectItem((Column)operand); } return(operand); }
} // where() public Query where (Column column, OperatorType operatorType, object operand) { SelectItem selectItem = _selectClause.getSelectItem(column); if (selectItem == null) { selectItem = new SelectItem(column); } return(where (selectItem, operatorType, operand)); } // where()
} // having() public Query having(Column column, OperatorType operatorType, object operand) { SelectItem selectItem = _selectClause.getSelectItem(column); if (selectItem == null) { selectItem = new SelectItem(column); } return(having(new FilterItem(selectItem, operatorType, operand))); } // having()
} // select() public Query select(params Column[] columns) { foreach (Column column in columns) { SelectItem selectItem = new SelectItem(column); selectItem.setQuery(this); _selectClause.addItem(selectItem); } return(this); } // select()
} // getCountAllItem() public static bool isCountAllItem(SelectItem item) { if (item != null && item.getFunction() != null && item.getFunction().ToString().Equals("COUNT") && item.getExpression() == "*") { return(true); } return(false); } // isCountAllItem()
} // orderBy() public Query orderBy(Column column, OrderByItem.Direction direction) { SelectItem selectItem = _selectClause.getSelectItem(column); if (selectItem == null) { selectItem = new SelectItem(column); } return(orderBy(new OrderByItem(selectItem, direction))); } // orderBy()
/** * Private constructor, used for cloning */ private FilterItem(SelectItem selectItem, OperatorType operator_arg, object operand, NList <FilterItem> orItems, string expression, LogicalOperator logicalOperator) { _selectItem = selectItem; _operator = operator_arg; //_operand = validateOperand(operand); _childItems = orItems; _expression = expression; _logicalOperator = logicalOperator; } // private constructor
} // select() protected object createOperand(string token, SelectItem leftSelectItem, bool searchSelectItems) { if (token.Equals("NULL", StringComparison.CurrentCultureIgnoreCase)) { return(null); } if (token.StartsWith("'") && token.EndsWith("'") && token.Length > 2) { string stringOperand = token.Substring(1, token.Length - 1); //[J2N] replaceAll <=> Replace stringOperand = stringOperand.Replace("\\\\'", "'"); return(stringOperand); } if (searchSelectItems) { SelectItem selectItem = findSelectItem(token, false); if (selectItem != null) { return(selectItem); } } ColumnType expectedColumnType = leftSelectItem.getExpectedColumnType(); object result = null; if (expectedColumnType == null) { // We're assuming number here, but it could also be boolean or a // time based type. But anyways, this should not happen since // expected column type should be available. result = NumberComparator.toNumber(token); } else if (expectedColumnType.isBoolean()) { result = BooleanComparator.toBoolean(token); } // else if (expectedColumnType.isTimeBased()) // { // result = FormatHelper.parseSqlTime(expectedColumnType, token); // } // else // { // result = NumberComparator.toNumber(token); // } // if (result == null) // { // // shouldn't happen since only "NULL" is parsed as null. // throw new QueryParserException("Could not parse operand: " + token); // } return(result); } // createOperand()
public String toStringNoAlias(bool?includeSchemaInColumnPaths) { if (_expression != null) { return(_expression); } StringBuilder sb = new StringBuilder(); if (_table != null) { if (_table.getSchema() != null && _table.getSchema().getName() != null) { sb.Append(_table.getSchema().getName()); sb.Append('.'); } sb.Append(_table.getQuotedName()); } else if (_subQuery != null) { sb.Append('('); sb.Append(_subQuery.toSql(includeSchemaInColumnPaths)); sb.Append(')'); } else if (_join != JoinType.None) { String leftSideAlias = _leftSide.getSameQueryAlias(); String rightSideAlias = _rightSide.getSameQueryAlias(); sb.Append(_leftSide.toSql()); sb.Append(' '); sb.Append(_join); sb.Append(" JOIN "); sb.Append(_rightSide.toSql()); for (int i = 0; i < _leftOn.Length; i++) { if (i == 0) { sb.Append(" ON "); } else { sb.Append(" AND "); } SelectItem primary = _leftOn[i]; appendJoinOnItem(sb, leftSideAlias, primary); sb.Append(" = "); SelectItem foreign = _rightOn[i]; appendJoinOnItem(sb, rightSideAlias, foreign); } } return(sb.ToString()); } // toStringNoAlias()
} // groupBy() public Query groupBy(params GroupByItem[] items) { foreach (GroupByItem item in items) { SelectItem selectItem = item.getSelectItem(); if (selectItem != null && selectItem.getQuery() == null) { selectItem.setQuery(this); } } _groupByClause.addItems(items); return(this); } // groupBy()
/** * All-arguments constructor * * @param column * @param fromItem * @param function * @param functionParameters * @param expression * @param subQuerySelectItem * @param alias * @param functionApproximationAllowed */ private SelectItem(Column column, FromItem fromItem, FunctionType function, object[] functionParameters, string expression, SelectItem subQuerySelectItem, string alias, bool functionApproximationAllowed) : base() { _column = column; _fromItem = fromItem; _function = function; _functionParameters = functionParameters; _expression = expression; _subQuerySelectItem = subQuerySelectItem; _alias = alias; _functionApproximationAllowed = functionApproximationAllowed; } // constructor
} // constructor /** * Creates a SelectItem that references another select item in a subquery * * @param subQuerySelectItem * @param subQueryFromItem * the FromItem that holds the sub-query */ public SelectItem(SelectItem subQuerySelectItem, FromItem subQueryFromItem) : this(null, subQueryFromItem, null, null, null, subQuerySelectItem, null, false) { if (subQueryFromItem.getSubQuery() == null) { throw new ArgumentException("Only sub-query based FromItems allowed."); } if (subQuerySelectItem.getQuery() != null && !subQuerySelectItem.getQuery().Equals(subQueryFromItem.getSubQuery())) { throw new ArgumentException("The SelectItem must exist in the sub-query"); } } // constructor
/** * Constructor for join FROM clauses that join two tables using their * relationship. The primary table of the relationship will be the left side * of the join and the foreign table of the relationship will be the right * side of the join. * * @param join * the join type to use * @param relationship * the relationship to use for joining the tables */ public FromItem(JoinType join, Relationship relationship) { _join = join; _leftSide = new FromItem(relationship.getPrimaryTable()); Column[] columns = relationship.getPrimaryColumns(); _leftOn = new SelectItem[columns.Length]; for (int i = 0; i < columns.Length; i++) { _leftOn[i] = new SelectItem(columns[i]); } _rightSide = new FromItem(relationship.getForeignTable()); columns = relationship.getForeignColumns(); _rightOn = new SelectItem[columns.Length]; for (int i = 0; i < columns.Length; i++) { _rightOn[i] = new SelectItem(columns[i]); } }
/** * Creates a single filter item based on a SelectItem, an operator and an * operand. * * @param selectItem * the selectItem to put constraints on, cannot be null * @param operator * The operator to use. Can be OperatorType.EQUALS_TO, * OperatorType.DIFFERENT_FROM, * OperatorType.GREATER_THAN,OperatorType.LESS_THAN * OperatorType.GREATER_THAN_OR_EQUAL, * OperatorType.LESS_THAN_OR_EQUAL * @param operand * The operand. Can be a constant like null or a String, a * Number, a Boolean, a Date, a Time, a DateTime. Or another * SelectItem * @throws IllegalArgumentException * if the SelectItem is null or if the combination of operator * and operand does not make sense. */ public FilterItem(SelectItem selectItem, OperatorType operator_arg, object operand) // throws IllegalArgumentException : this(selectItem, operator_arg, operand, null, null, LogicalOperator.None) { if (_operand == null) { require("Can only use EQUALS or DIFFERENT_FROM operator with null-operand", _operator == OperatorType.DIFFERENT_FROM || _operator == OperatorType.EQUALS_TO); } if (_operator == OperatorType.LIKE || _operator == OperatorType.NOT_LIKE) { ColumnType type = _selectItem.getColumn().getType(); if (type != null) { require("Can only use LIKE operator with strings", type.isLiteral() && (_operand is String || _operand is SelectItem)); } } require("SelectItem cannot be null", _selectItem != null); } // constructor
public bool equalsIgnoreAlias(SelectItem that, bool exactColumnCompare) { if (that == null) { return(false); } if (that == this) { return(true); } EqualsBuilder eb = new EqualsBuilder(); if (exactColumnCompare) { eb.append(this._column == that._column); eb.append(this._fromItem, that._fromItem); } else { eb.append(this._column, that._column); } eb.append(this._function, that._function); eb.append(this._functionApproximationAllowed, that._functionApproximationAllowed); eb.append(this._expression, that._expression); if (_subQuerySelectItem != null) { eb.append(_subQuerySelectItem.equalsIgnoreAlias(that._subQuerySelectItem)); } else { if (that._subQuerySelectItem != null) { eb.append(false); } } return(eb.isEquals()); } // equalsIgnoreAlias()
} // having() public Query having(FunctionType function, Column column, OperatorType operatorType, object operand) { SelectItem selectItem = new SelectItem(function, column); return(having(new FilterItem(selectItem, operatorType, operand))); } // having()
} // having() public Query select(Column column, FromItem fromItem) { SelectItem selectItem = new SelectItem(column, fromItem); return(select(selectItem)); } // select()
//private string getToStringColumnPrefix(bool includeSchemaInColumnPath) //{ // StringBuilder sb = new StringBuilder(); // if (_fromItem != null && _fromItem.getAlias() != null) // { // sb.Append(_fromItem.getAlias()); // sb.Append('.'); // } // else // { // Table table = _column.getTable(); // string tableLabel; // if (_query == null) // { // tableLabel = null; // } // else // { // tableLabel = _query.getFromClause().getAlias(table); // } // if (table != null) // { // if (tableLabel == null) // { // tableLabel = table.getQuotedName(); // if (includeSchemaInColumnPath) // { // Schema schema = table.getSchema(); // if (schema != null) // { // tableLabel = schema.getQuotedName() + "." + tableLabel; // } // } // } // sb.Append(tableLabel); // sb.Append('.'); // } // } // return sb.ToString(); //} // getToStringColumnPrefix() public bool equalsIgnoreAlias(SelectItem that) { return(equalsIgnoreAlias(that, false)); }
} // select() public Query where (SelectItem selectItem, OperatorType operatorType, object operand) { return(where (new FilterItem(selectItem, operatorType, operand))); } // where()
public abstract object evaluate(Row row, object[] parameters, SelectItem operandItem);
public Query selectCount() { return(select(SelectItem.getCountAllItem())); }
/** * Does a "manual" evaluation, useful for CSV data and alike, where queries * cannot be created. */ public bool evaluate(Row row) { require("Expression-based filters cannot be manually evaluated", _expression == null); if (_childItems == null) { // Evaluate a single constraint Object selectItemValue = row.getValue(_selectItem); Object operandValue = _operand; if (_operand is SelectItem) { SelectItem selectItem = (SelectItem)_operand; operandValue = row.getValue(selectItem); } if (operandValue == null) { if (_operator == OperatorType.DIFFERENT_FROM) { return(selectItemValue != null); } else if (_operator == OperatorType.EQUALS_TO) { return(selectItemValue == null); } else { return(false); } } else if (selectItemValue == null) { if (_operator == OperatorType.DIFFERENT_FROM) { return(true); } else { return(false); } } else { return(compare(selectItemValue, operandValue)); } } else { // Evaluate several constraints if (_logicalOperator == LogicalOperator.AND) { // require all results to be true foreach (FilterItem item in _childItems) { bool result = item.evaluate(row); if (!result) { return(false); } } return(true); } else { // require at least one result to be true foreach (FilterItem item in _childItems) { bool result = item.evaluate(row); if (result) { return(true); } } return(false); } } }
} // _QueryPartProcessor_FindFilterItem_impl_ class private FilterItem findFilterItem(string expression) { string upperExpression = expression.ToUpper(); QueryPartCollectionProcessor collectionProcessor = new QueryPartCollectionProcessor(); new QueryPartParser(collectionProcessor, expression, " AND ", " OR ").parse(); List <String> tokens = collectionProcessor.getTokens(); List <String> delims = collectionProcessor.getDelims(); if (tokens.Count == 1) { expression = tokens[0]; upperExpression = expression.ToUpper(); } else { //[J2N] LogicalOperator.valueOf() <=> Enum.Parse(typeof(LogicalOperator), value) LogicalOperator logicalOperator = (LogicalOperator)Enum.Parse(typeof(LogicalOperator), delims[1].Trim()); NList <FilterItem> filterItems = new NList <FilterItem>(); for (int i = 0; i < tokens.Count; i++) { string token = tokens[i]; FilterItem filterItem = findFilterItem(token); filterItems.Add(filterItem); } return(new FilterItem(logicalOperator, filterItems)); } //[J2Cc] operator identifier (a keyword in C#) => operator_var OperatorType operator_var = null; string leftSide = null; string rightSide; { string rightSideCandidate = null; OperatorType[] operators = OperatorType.BUILT_IN_OPERATORS; foreach (OperatorType operatorCandidate in operators) { string searchStr; if (operatorCandidate.isSpaceDelimited()) { searchStr = ' ' + operatorCandidate.toSql() + ' '; } else { searchStr = operatorCandidate.toSql(); } int operatorIndex = upperExpression.IndexOf(searchStr); if (operatorIndex > 0) { operator_var = operatorCandidate; leftSide = expression.Substring(0, operatorIndex).Trim(); rightSideCandidate = expression.Substring(operatorIndex + searchStr.Length).Trim(); break; } } if (operator_var == null) { // check special cases for IS NULL and IS NOT NULL if (expression.EndsWith(" IS NOT NULL")) { operator_var = OperatorType.DIFFERENT_FROM; leftSide = expression.Substring(0, expression.LastIndexOf(" IS NOT NULL")).Trim(); rightSideCandidate = "NULL"; } else if (expression.EndsWith(" IS NULL")) { operator_var = OperatorType.EQUALS_TO; leftSide = expression.Substring(0, expression.LastIndexOf(" IS NULL")).Trim(); rightSideCandidate = "NULL"; } } rightSide = rightSideCandidate; } if (operator_var == null) { return(new FilterItem(expression)); } SelectItem selectItem = findSelectItem(leftSide, false); if (selectItem == null) { return(new FilterItem(expression)); } Object operand = null; if (operator_var == OperatorType.IN) { List <Object> list = new List <Object>(); new QueryPartParser(new _QueryPartProcessor_FindFilterItem_impl_(this, list, operand, selectItem), rightSide, ",").parse(); operand = list; } else { operand = createOperand(rightSide, selectItem, true); } return(new FilterItem(selectItem, operator_var, operand)); } // findFilterItem()