private IValue ParseSearchFunctionExpression() { NPathSearchFunction search = new NPathSearchFunction(); search.FunctionName = tokenizer.GetCurrentToken().Text; tokenizer.MoveNext(); tokenizer.GetCurrentToken("(", "("); tokenizer.MoveNext(); NPathIdentifier path = new NPathIdentifier(); path.Path = CurrentPropertyPrefix + tokenizer.GetCurrentToken("property path", "Property path").Text; // do not localize path.ReferenceLocation = IsInSelectClause() ? NPathPropertyPathReferenceLocation.SelectClause : NPathPropertyPathReferenceLocation.WhereClause; // CurrentQuery.AddPropertyPathReference(path.Path) ; search.PropertyPath = path; tokenizer.MoveNext(); tokenizer.GetCurrentToken("comma", ","); tokenizer.MoveNext(); tokenizer.GetCurrentToken("string", new string[] { "\"", "'" }); search.SearchString = (NPathStringValue)ParseValue(); tokenizer.GetCurrentToken(")", ")"); // do not localize tokenizer.MoveNext(); return(search); }
public object EvalPropertyPath(object item, NPathIdentifier propertyPath) { if (propertyPath.Path == "") { throw new Exception("Can not evaluate propertypath ''"); } if (propertyPath.IsWildcard) { throw new Exception("Can not evaluate a wildcard path"); // do not localize } object current = EvalStringPropertyPath(item, propertyPath.Path); //idiot conversion is due to roundoff errors when using convert.todouble if (IsNumber(current)) { current = double.Parse(current.ToString()); } //negate expression if (propertyPath.IsNegative) { return(NegateValue(current)); } return(current); }
protected virtual void EmitPropertyPath(NPathIdentifier propertyPath) { if (propertyPath.IsNegative) { Write("-"); } Write(propertyPath.Path); }
public virtual DataTable GetDataTable(NPathSelectQuery query, IList sourceList) { FixQuery(query); DataTable resultTable = new DataTable(); resultTable.BeginInit(); #region build columns int id = 0; foreach (NPathSelectField field in query.Select.SelectFields) { string fieldName = field.Alias; NPathIdentifier path = field.Expression as NPathIdentifier; if (path != null) { if (path.IsWildcard) { throw new Exception("this can not happen"); // do not localize } else { if (fieldName == null) { fieldName = path.Path; } resultTable.Columns.Add(path.Path, typeof(object)); } } else { if (fieldName == null) { fieldName = "col" + id.ToString(); id++; } resultTable.Columns.Add(fieldName, typeof(object)); } } #endregion resultTable.EndInit(); resultTable.BeginLoadData(); IList resultList = InternalGetTable(query, sourceList); foreach (object[] values in resultList) { resultTable.Rows.Add(values); } resultTable.EndLoadData(); return(resultTable); }
public void ExpandWildcards(NPathSelectQuery query) { ArrayList newSelectFieldList = new ArrayList(); foreach (NPathSelectField field in query.Select.SelectFields) { string fieldName = field.Alias; NPathIdentifier path = field.Expression as NPathIdentifier; if (path != null && path.IsWildcard) { string[] parts = path.Path.Split('.'); NPathClassName className = (NPathClassName)query.From.Classes[0]; IClassMap classMap = Context.DomainMap.MustGetClassMap(className.Name); int i = 0; foreach (string part in parts) { if (i == parts.Length - 1) { break; } IPropertyMap property = classMap.MustGetPropertyMap(part); classMap = Context.DomainMap.MustGetClassMap(property.DataType); i++; } ArrayList properties = classMap.GetAllPropertyMaps(); foreach (PropertyMap property in properties) { if (property.ReferenceType != ReferenceType.None) { continue; } NPathSelectField newField = new NPathSelectField(); newField.Alias = null; NPathIdentifier newPath = new NPathIdentifier(); if (parts.Length > 1) { newPath.Path = string.Join(".", parts, 0, parts.Length - 1) + "."; } newPath.Path += property.Name; newField.Expression = newPath; newSelectFieldList.Add(newField); } } else { newSelectFieldList.Add(field); } } query.Select.SelectFields = newSelectFieldList; }
protected virtual void EmitSelect(NPathSelectQuery query) { NPathSelectClause select = query.Select; Write("select "); // do not localize if (query.Select.HasTop) { Write("top {0}", query.Select.Top); // do not localize if (query.Select.Percent) { Write("% "); } } int i = 0; foreach (NPathSelectField field in select.SelectFields) { if (field.Expression is NPathIdentifier) { NPathIdentifier path = field.Expression as NPathIdentifier; Write(path.Path); } if (field.Expression is NPathFunction) { NPathFunction function = field.Expression as NPathFunction; EmitFunction(function); } if (field.Expression is NPathExpression) { NPathExpression expression = field.Expression as NPathExpression; EmitExpression(expression); } if (field.Alias != null && field.Alias != "") { WriteLine(" as [{0}]", field.Alias); // do not localize } if (i < select.SelectFields.Count - 1) { Write(","); } i++; } WriteLine(); }
protected virtual bool EvalCompareExpression(object item, NPathCompareExpression compareExpression) { object leftValue = null; //EvalValue(item, compareExpression.LeftOperand); object rightValue = null; // EvalValue(item, compareExpression.RightOperand); if (compareExpression.LeftOperand is NPathIdentifier && compareExpression.RightOperand is NPathIdentifier) { NPathIdentifier leftIdent = (NPathIdentifier)compareExpression.LeftOperand; NPathIdentifier rightIdent = (NPathIdentifier)compareExpression.RightOperand; bool rightIsEnum = false; if (rightIdent.Path.IndexOf(".") == -1) { if (item.GetType().GetProperty(rightIdent.Path) == null) { rightIsEnum = true; } } bool leftIsEnum = false; if (leftIdent.Path.IndexOf(".") == -1) { if (item.GetType().GetProperty(leftIdent.Path) == null) { leftIsEnum = true; } } if (leftIsEnum && rightIsEnum) { throw new Exception(string.Format("Property not found '{0}'", leftIdent.Path)); } if (leftIsEnum) { string enumName = leftIdent.Path; rightValue = EvalValue(item, rightIdent); if (rightValue == null) { return(false); } Type enumType = rightValue.GetType(); if (!enumType.IsEnum) { throw new Exception(string.Format("Property '{0}' is not an enum", rightIdent.Path)); } leftValue = Enum.Parse(enumType, enumName); } if (rightIsEnum) { string enumName = rightIdent.Path; leftValue = EvalValue(item, leftIdent); if (leftValue == null) { return(false); } Type enumType = leftValue.GetType(); if (!enumType.IsEnum) { throw new Exception(string.Format("Property '{0}' is not an enum", leftIdent.Path)); } rightValue = Enum.Parse(enumType, enumName); } } else { leftValue = EvalValue(item, compareExpression.LeftOperand); rightValue = EvalValue(item, compareExpression.RightOperand); } if ((leftValue is IComparable || rightValue is IComparable) || (leftValue == null || rightValue == null)) { int res = Comparer.DefaultInvariant.Compare(leftValue, rightValue); switch (compareExpression.Operator) { case "=": { return(res == 0); } case "!=": { return(res != 0); } case ">=": { return(res >= 0); } case "<=": { return(res <= 0); } case ">": { return(res > 0); } case "<": { return(res < 0); } case "like": { if (leftValue == null || rightValue == null) { return(false); } bool isLike = Like(leftValue.ToString(), rightValue.ToString()); return(isLike); } default: break; } } else { switch (compareExpression.Operator) { case "=": { return(leftValue == rightValue); } case "!=": { return(leftValue != rightValue); } default: break; } } throw new Exception(string.Format("unknown compare expression '{0}'", compareExpression.Operator)); // do not localize }
protected virtual void EmitExpression(IValue expression) { if (expression is NPathNotExpression) { NPathNotExpression value = (NPathNotExpression)expression; EmitNot(value); } if (expression is NPathFunction) { NPathFunction value = (NPathFunction)expression; EmitFunction(value); } if (expression is NPathParameter) { NPathParameter value = (NPathParameter)expression; EmitParameter(value); } if (expression is NPathNullValue) { NPathNullValue value = (NPathNullValue)expression; EmitNullValue(value); } if (expression is NPathBetweenExpression) { NPathBetweenExpression value = (NPathBetweenExpression)expression; EmitBetween(value); } if (expression is NPathBooleanValue) { NPathBooleanValue value = (NPathBooleanValue)expression; EmitBooleanValue(value); } if (expression is NPathDecimalValue) { NPathDecimalValue value = (NPathDecimalValue)expression; EmitDecimalValue(value); } if (expression is NPathDateTimeValue) { NPathDateTimeValue value = (NPathDateTimeValue)expression; EmitDateTimeValue(value); } if (expression is NPathGuidValue) { NPathGuidValue value = (NPathGuidValue)expression; EmitGuidValue(value); } if (expression is NPathStringValue) { NPathStringValue value = (NPathStringValue)expression; EmitStringValue(value); } if (expression is NPathIdentifier) { NPathIdentifier propertyPath = (NPathIdentifier)expression; EmitPropertyPath(propertyPath); } if (expression is NPathPropertyFilter) { NPathPropertyFilter propertyFilter = (NPathPropertyFilter)expression; EmitPropertyFilter(propertyFilter); } if (expression is NPathParenthesisGroup) { NPathParenthesisGroup parenthesisGroup = (NPathParenthesisGroup)expression; EmitParenthesisGroup(parenthesisGroup); } if (expression is NPathMathExpression) { NPathMathExpression mathExpression = (NPathMathExpression)expression; EmitMathExpression(mathExpression); } if (expression is NPathCompareExpression) { NPathCompareExpression compareExpression = (NPathCompareExpression)expression; EmitCompareExpression(compareExpression); } if (expression is NPathBooleanExpression) { NPathBooleanExpression boolExpression = (NPathBooleanExpression)expression; EmitBooleanExpression(boolExpression); } if (expression is NPathInExpression) { NPathInExpression value = (NPathInExpression)expression; EmitIn(value); } if (expression is NPathSearchFunction) { NPathSearchFunction value = (NPathSearchFunction)expression; EmitSearchFunction(value); } }
private IValue ParseValue() { IValue operand = null; bool isNegative = false; if (tokenizer.GetCurrentToken().IsType("sign")) { if (tokenizer.GetCurrentToken().IsType("minus")) { isNegative = true; } tokenizer.MoveNext(); } Token currentToken = tokenizer.GetCurrentToken(); #region parse value if (currentToken.IsType("null")) { NPathNullValue nullOperand = new NPathNullValue(); operand = nullOperand; tokenizer.MoveNext(); } else if (currentToken.IsType("parameter")) { NPathParameter parameterOperand = new NPathParameter(); parameterOperand.Value = parameterQueue[0]; parameterQueue.RemoveAt(0); operand = parameterOperand; tokenizer.MoveNext(); } else if (tokenizer.GetCurrentToken().IsType("textsearch")) { return(ParseSearchFunctionExpression()); } else if (currentToken.IsType("function") && IsInSelectClause() || currentToken.IsType("isnull") || currentToken.IsType("soundex")) { NPathFunction functionOperand = new NPathFunction(); if (currentToken.IsType("soundex")) { functionOperand = new NPathSoundexStatement(); } if (currentToken.IsType("sum")) { functionOperand = new NPathSumStatement(); } if (currentToken.IsType("isnull")) { functionOperand = new NPathIsNullStatement(); } if (currentToken.IsType("count")) { functionOperand = new NPathCountStatement(); } if (currentToken.IsType("avg")) { functionOperand = new NPathAvgStatement(); } if (currentToken.IsType("min")) { functionOperand = new NPathMinStatement(); } if (currentToken.IsType("max")) { functionOperand = new NPathMaxStatement(); } tokenizer.MoveNext(); tokenizer.GetCurrentToken("(", "("); tokenizer.MoveNext(); if (tokenizer.GetCurrentToken().IsType("distinct")) { functionOperand.Distinct = true; tokenizer.MoveNext(); } functionOperand.Expression = ParseBooleanExpression(); tokenizer.GetCurrentToken(")", ")"); tokenizer.MoveNext(); operand = functionOperand; } else if (currentToken.IsType("date")) { NPathDateTimeValue dateOperand = new NPathDateTimeValue(); dateOperand.Value = DateTime.Parse(currentToken.Text); operand = dateOperand; tokenizer.MoveNext(); } else if (currentToken.IsType("decimal")) { NPathDecimalValue decimalOperand = new NPathDecimalValue(); decimalOperand.Value = double.Parse(currentToken.Text, NumberFormatInfo.InvariantInfo); decimalOperand.IsNegative = isNegative; operand = decimalOperand; tokenizer.MoveNext(); } else if (currentToken.IsType("string")) { NPathStringValue stringOperand = new NPathStringValue(); string text = currentToken.Text; text = text.Substring(1, text.Length - 2); if (currentToken.IsType("string '")) // do not localize { text = text.Replace("''", "'"); } else if (currentToken.IsType("string \"")) { text = text.Replace("\"\"", "\""); // do not localize } stringOperand.Value = text; operand = stringOperand; tokenizer.MoveNext(); } else if (currentToken.IsType("boolean")) { NPathBooleanValue booleanOperand = new NPathBooleanValue(); booleanOperand.Value = bool.Parse(currentToken.Text); operand = booleanOperand; tokenizer.MoveNext(); } else if (currentToken.IsType("guid")) { NPathGuidValue guidOperand = new NPathGuidValue(); guidOperand.Value = currentToken.Text; operand = guidOperand; tokenizer.MoveNext(); } else if (currentToken.IsType("property path")) // do not localize { if (tokenizer.GetNextToken().IsType("(")) { string fullPath = currentToken.Text; string propertyPath = ""; string methodName = ""; int lastIndexOfDot = fullPath.LastIndexOf("."); if (lastIndexOfDot > 0) { propertyPath = fullPath.Substring(0, lastIndexOfDot); methodName = fullPath.Substring(lastIndexOfDot + 1); } else { methodName = fullPath; } NPathMethodCall call = new NPathMethodCall(); call.MethodName = methodName; call.PropertyPath = new NPathIdentifier(); call.PropertyPath.Path = propertyPath; call.PropertyPath.IsNegative = isNegative; //TODO:add method support here tokenizer.MoveNext(); //move past "(" tokenizer.MoveNext(); while (!tokenizer.GetCurrentToken().IsType(")")) { IValue param = ParseExpression(); call.Parameters.Add(param); if (tokenizer.GetCurrentToken().IsType("comma")) { tokenizer.MoveNext(); } else { tokenizer.GetCurrentToken(")", ")"); } } tokenizer.MoveNext(); operand = call; } else if (tokenizer.GetNextToken().IsType("[")) { CurrentPropertyPrefix = currentToken.Text + "."; NPathBracketGroup bracketGroup = new NPathBracketGroup(); tokenizer.MoveNext(); ParseBracketGroup(bracketGroup); CurrentPropertyPrefix = ""; NPathParenthesisGroup parens = new NPathParenthesisGroup(); parens.Expression = bracketGroup.Expression; operand = parens; } else { NPathIdentifier propertyOperand = new NPathIdentifier(); propertyOperand.Path = CurrentPropertyPrefix + currentToken.Text; propertyOperand.ReferenceLocation = IsInSelectClause() ? NPathPropertyPathReferenceLocation.SelectClause : NPathPropertyPathReferenceLocation.WhereClause; //CurrentQuery.AddPropertyPathReference(propertyOperand.Path) ; propertyOperand.IsNegative = isNegative; operand = propertyOperand; tokenizer.MoveNext(); } } else if (currentToken.IsType("(")) { NPathParenthesisGroup parenthesisOperand = new NPathParenthesisGroup(); ParseParenthesisGroup(parenthesisOperand); parenthesisOperand.IsNegative = isNegative; operand = parenthesisOperand; } else { //unknown value? throw GetUnknownTokenException(); } #endregion return(operand); }