/// <summary> /// Creates a predicate from the passed in data. /// </summary> /// <param name="field">The field.</param> /// <param name="operatorID">The operator ID.</param> /// <param name="negate">if set to <c>true</c> [negate].</param> /// <param name="fromValue">From value.</param> /// <param name="toValue">To value.</param> /// <returns> /// Predicate object which filters on the field with the specified values, or null if no predicate could be created. /// </returns> public static IPredicate CreatePredicate(EntityField2 field, int operatorID, bool negate, object fromValue, object toValue) { IPredicate toReturn = null; switch (operatorID) { case 0: // equal toReturn = new FieldCompareValuePredicate(field, null, ComparisonOperator.Equal, fromValue, negate); break; case 1: // greater toReturn = new FieldCompareValuePredicate(field, null, ComparisonOperator.GreaterThan, fromValue, negate); break; case 2: // greater / equal toReturn = new FieldCompareValuePredicate(field, null, ComparisonOperator.GreaterEqual, fromValue, negate); break; case 3: // lesser toReturn = new FieldCompareValuePredicate(field, null, ComparisonOperator.LesserThan, fromValue, negate); break; case 4: // lesser / equal toReturn = new FieldCompareValuePredicate(field, null, ComparisonOperator.LessEqual, fromValue, negate); break; case 5: // between if (toValue == null) { // invalid return(null); } toReturn = new FieldBetweenPredicate(field, null, fromValue, toValue, negate); break; case 6: // null toReturn = new FieldCompareNullPredicate(field, null, negate); break; case 7: // like toReturn = new FieldLikePredicate(field, null, (string)fromValue, negate); break; default: // invalid operator return(null); } return(toReturn); }
private static Predicate CreatePredicateForType(EntityField2 filterField, string data) { Predicate predicate = null; if (filterField.DataType == typeof(string)) { FieldLikePredicate likePredicate = new FieldLikePredicate(filterField, null, string.Format("{0}%", data.ToUpper())); likePredicate.CaseSensitiveCollation = true; predicate = likePredicate; } else if (filterField.DataType == typeof(bool)) { bool boolData; if (bool.TryParse(data, out boolData)) { predicate = filterField == boolData; } } else if (filterField.DataType == typeof(byte)) { byte byteData; if (byte.TryParse(data, out byteData)) { predicate = filterField == byteData; } } else if (filterField.DataType == typeof(short)) { short shortData; if (short.TryParse(data, out shortData)) { predicate = filterField == shortData; } } else if (filterField.DataType == typeof(int)) { int intData; if (int.TryParse(data, out intData)) { predicate = filterField == intData; } } else if (filterField.DataType == typeof(long)) { long longData; if (long.TryParse(data, out longData)) { predicate = filterField == longData; } } else if (filterField.DataType == typeof(Single)) { Single singleData; if (Single.TryParse(data, out singleData)) { predicate = filterField == singleData; } } else if (filterField.DataType == typeof(double)) { double doubleData; if (double.TryParse(data, out doubleData)) { predicate = filterField == doubleData; } } else if (filterField.DataType == typeof(decimal)) { decimal decimalData; if (decimal.TryParse(data, out decimalData)) { predicate = filterField == decimalData; } } return predicate; }
/// <summary> /// Inserts in each predicate expression element the persistence info object for the field used. If there is already a fieldpersistenceinfo /// element for a given field, it is skipped. /// </summary> /// <param name="expression">IPredicateExpression object which has predicate elements whose persistence info objects have to be /// set to a value.</param> protected override void InsertPersistenceInfoObjects(IPredicateExpression expression) { if (expression == null) { return; } for (int i = 0; i < expression.Count; i++) { if (expression[i].Type != PredicateExpressionElementType.Predicate) { continue; } IPredicate currentPredicate = (IPredicate)expression[i].Contents; switch ((PredicateType)currentPredicate.InstanceType) { case PredicateType.Undefined: continue; case PredicateType.PredicateExpression: // recurse InsertPersistenceInfoObjects((IPredicateExpression)expression[i].Contents); break; case PredicateType.FieldBetweenPredicate: FieldBetweenPredicate betweenPredicate = (FieldBetweenPredicate)currentPredicate; if (betweenPredicate.PersistenceInfo == null) { betweenPredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)betweenPredicate.FieldCore); } if (betweenPredicate.BeginIsField && (betweenPredicate.PersistenceInfoBegin == null)) { betweenPredicate.PersistenceInfoBegin = GetFieldPersistenceInfo((IEntityField2)betweenPredicate.FieldBeginCore); } if (betweenPredicate.EndIsField && (betweenPredicate.PersistenceInfoEnd == null)) { betweenPredicate.PersistenceInfoEnd = GetFieldPersistenceInfo((IEntityField2)betweenPredicate.FieldEndCore); } break; case PredicateType.FieldCompareNullPredicate: FieldCompareNullPredicate compareNullPredicate = (FieldCompareNullPredicate)currentPredicate; if (compareNullPredicate.PersistenceInfo == null) { compareNullPredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)compareNullPredicate.FieldCore); } break; case PredicateType.FieldCompareValuePredicate: FieldCompareValuePredicate compareValuePredicate = (FieldCompareValuePredicate)currentPredicate; if (compareValuePredicate.PersistenceInfo == null) { compareValuePredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)compareValuePredicate.FieldCore); } break; case PredicateType.FieldLikePredicate: FieldLikePredicate likePredicate = (FieldLikePredicate)currentPredicate; if (likePredicate.PersistenceInfo == null) { likePredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)likePredicate.FieldCore); } break; case PredicateType.FieldCompareRangePredicate: FieldCompareRangePredicate compareRangePredicate = (FieldCompareRangePredicate)currentPredicate; if (compareRangePredicate.PersistenceInfo == null) { compareRangePredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)compareRangePredicate.FieldCore); } break; case PredicateType.FieldCompareExpressionPredicate: FieldCompareExpressionPredicate expressionPredicate = (FieldCompareExpressionPredicate)currentPredicate; if (expressionPredicate.PersistenceInfo == null) { expressionPredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)expressionPredicate.FieldCore); } if (expressionPredicate.ExpressionToCompareWith != null) { InsertPersistenceInfoObjects(expressionPredicate.ExpressionToCompareWith); } break; case PredicateType.FieldFullTextSearchPredicate: FieldFullTextSearchPredicate fullTextSearchPredicate = (FieldFullTextSearchPredicate)currentPredicate; if (fullTextSearchPredicate.TargetIsFieldList) { if (fullTextSearchPredicate.PersistenceInfosFieldsList == null) { ArrayList persistenceInfos = new ArrayList(); foreach (IEntityField2 field in fullTextSearchPredicate.FieldsList) { persistenceInfos.Add(GetFieldPersistenceInfo(field)); } fullTextSearchPredicate.PersistenceInfosFieldsList = persistenceInfos; } } else { if (fullTextSearchPredicate.PersistenceInfo == null) { fullTextSearchPredicate.PersistenceInfo = GetFieldPersistenceInfo((IEntityField2)fullTextSearchPredicate.FieldCore); } } break; case PredicateType.FieldCompareSetPredicate: FieldCompareSetPredicate compareSetPredicate = (FieldCompareSetPredicate)currentPredicate; if ((compareSetPredicate.PersistenceInfoField == null) && (compareSetPredicate.FieldCore != null)) { compareSetPredicate.PersistenceInfoField = GetFieldPersistenceInfo((IEntityField2)compareSetPredicate.FieldCore); } if (compareSetPredicate.PersistenceInfoSetField == null) { compareSetPredicate.PersistenceInfoSetField = GetFieldPersistenceInfo((IEntityField2)compareSetPredicate.SetFieldCore); } InsertPersistenceInfoObjects(compareSetPredicate.SetFilterAsPredicateExpression); InsertPersistenceInfoObjects(compareSetPredicate.SetRelations); InsertPersistenceInfoObjects(compareSetPredicate.SetSorter); InsertPersistenceInfoObjects(compareSetPredicate.GroupByClause); break; default: // __LLBLGENPRO_USER_CODE_REGION_START InsertPersistenceInfoObjectsPredicate // __LLBLGENPRO_USER_CODE_REGION_END OnInsertPersistenceObjects(currentPredicate); break; } } }
/* -> eq (equals any) -> neq (not equals any) -> eqc (equals any, case insensitive [on a case sensitive database]) -> neqc (not equals any, case insensitive [on a case sensitive database]) -> in (same as eq) -> nin (same as ne) -> lk (like) -> nlk (not like) -> nl (null) -> nnl (not null) -> gt (greater than) -> gte (greater than or equal to) -> lt (less than) -> lte (less than or equal to) -> ct (full text contains) -> ft (full text free text) -> bt (between) -> nbt (not between) */ private static IPredicate BuildPredicateFromClauseNode(Func<string, IEntityField2> fieldGetter, Func<string, List<IEntityRelation>, IEntityField2> relatedFieldGetter, FilterNode filterNode, List<IEntityRelation> inferredRelationsList) { // there are always at least 2 elements if (filterNode.ElementCount < 2) return null; var elements = filterNode.Elements; //TODO: may need to mess with relation aliases and join types in the future var relations = new List<IEntityRelation>(); var field = elements[0].IndexOf('.') == -1 ? fieldGetter(elements[0]) : relatedFieldGetter(elements[0], relations); if (field == null) throw new ArgumentNullException("Unable to locate field " + elements[0]); foreach (var relation in relations) inferredRelationsList.AddIfNotExists(relation); var comparisonOperatorStr = elements[1].ToLowerInvariant(); var valueElements = elements.Skip(2).ToArray(); var objElements = new object[valueElements.Length]; Action<string> throwINEEx = (s) => { throw new ArgumentException(string.Format("Invalid number of elements in '{0}' filter clause", s)); }; string objAlias; IPredicate predicate; switch (comparisonOperatorStr) { case ("bt"): //between case ("nbt"): //not between if (valueElements.Length < 2) throwINEEx(comparisonOperatorStr); objElements[0] = ConvertStringToFieldValue(field, valueElements[0]); objElements[1] = ConvertStringToFieldValue(field, valueElements[1]); objAlias = valueElements.Length == 3 ? valueElements[2] : null; predicate = new FieldBetweenPredicate(field, null, objElements[0], objElements[1], objAlias, comparisonOperatorStr == "nbt"); break; case ("in"): //same as eq case ("nin"): //same as ne case ("eq"): //equals any case ("neq"): //not equals any case ("eqc"): //equals any, case insensitive [on a case sensitive database] - only 1 element per clause with this option case ("neqc"): //not equals any, case insensitive [on a case sensitive database] - only 1 element per clause with this option if (valueElements.Length < 1) throwINEEx(comparisonOperatorStr); for (int i = 0; i < valueElements.Length; i++) objElements[i] = ConvertStringToFieldValue(field, valueElements[i]); if (objElements.Length == 1 || comparisonOperatorStr == "eqci" || comparisonOperatorStr == "neci") { predicate = new FieldCompareValuePredicate(field, null, comparisonOperatorStr.StartsWith("n") ? ComparisonOperator.NotEqual : ComparisonOperator.Equal, objElements[0]) { CaseSensitiveCollation = comparisonOperatorStr == "eqci" || comparisonOperatorStr == "neci" }; } else { if (comparisonOperatorStr.StartsWith("n")) predicate = (EntityField2)field != objElements; else predicate = (EntityField2)field == objElements; } break; case ("lk"): //like case ("nlk"): //not like if (valueElements.Length < 1) throwINEEx(comparisonOperatorStr); objElements[0] = ConvertStringToFieldValue(field, valueElements[0].Replace('*', '%')); objAlias = valueElements.Length == 2 ? valueElements[1] : null; predicate = new FieldLikePredicate(field, null, objAlias, (string)objElements[0], comparisonOperatorStr == "nlk"); break; case ("nl"): //null case ("nnl"): //not null predicate = new FieldCompareNullPredicate(field, null, null, comparisonOperatorStr == "nnl"); break; case ("gt"): //greater than) case ("gte"): //greater than or equal to case ("lt"): //less than case ("lte"): //less than or equal to if (valueElements.Length < 1) throwINEEx(comparisonOperatorStr); objElements[0] = ConvertStringToFieldValue(field, valueElements[0]); objAlias = valueElements.Length == 2 ? valueElements[1] : null; var comparisonOperator = ComparisonOperator.GreaterThan; if (comparisonOperatorStr == "gte") comparisonOperator = ComparisonOperator.GreaterEqual; else if (comparisonOperatorStr == "lt") comparisonOperator = ComparisonOperator.LesserThan; else if (comparisonOperatorStr == "lte") comparisonOperator = ComparisonOperator.LessEqual; predicate = new FieldCompareValuePredicate(field, null, comparisonOperator, objElements[0], objAlias); break; case ("ct"): //full text contains case ("ft"): //full text free text if (valueElements.Length < 1) throwINEEx(comparisonOperatorStr); objElements[0] = valueElements[0]; objAlias = valueElements.Length == 2 ? valueElements[1] : null; predicate = new FieldFullTextSearchPredicate(field, null, comparisonOperatorStr == "ct" ? FullTextSearchOperator.Contains : FullTextSearchOperator.Freetext, (string)objElements[0], objAlias); break; default: return null; } return predicate; }
/* * -> eq (equals any) * -> neq (not equals any) * -> eqc (equals any, case insensitive [on a case sensitive database]) * -> neqc (not equals any, case insensitive [on a case sensitive database]) * -> in (same as eq) * -> nin (same as ne) * -> lk (like) * -> nlk (not like) * -> nl (null) * -> nnl (not null) * -> gt (greater than) * -> gte (greater than or equal to) * -> lt (less than) * -> lte (less than or equal to) * -> ct (full text contains) * -> ft (full text free text) * -> bt (between) * -> nbt (not between) */ private static IPredicate BuildPredicateFromClauseNode(Func <string, IEntityField2> fieldGetter, Func <string, List <IEntityRelation>, IEntityField2> relatedFieldGetter, FilterNode filterNode, List <IEntityRelation> inferredRelationsList) { // there are always at least 2 elements if (filterNode.ElementCount < 2) { return(null); } var elements = filterNode.Elements; //TODO: may need to mess with relation aliases and join types in the future var relations = new List <IEntityRelation>(); var field = elements[0].IndexOf('.') == -1 ? fieldGetter(elements[0]) : relatedFieldGetter(elements[0], relations); if (field == null) { throw new ArgumentNullException("Unable to locate field " + elements[0]); } foreach (var relation in relations) { inferredRelationsList.AddIfNotExists(relation); } var comparisonOperatorStr = elements[1].ToLowerInvariant(); var valueElements = elements.Skip(2).ToArray(); var objElements = new object[valueElements.Length]; Action <string> throwINEEx = (s) => { throw new ArgumentException(string.Format("Invalid number of elements in '{0}' filter clause", s)); }; string objAlias; IPredicate predicate; switch (comparisonOperatorStr) { case ("bt"): //between case ("nbt"): //not between if (valueElements.Length < 2) { throwINEEx(comparisonOperatorStr); } objElements[0] = ConvertStringToFieldValue(field, valueElements[0]); objElements[1] = ConvertStringToFieldValue(field, valueElements[1]); objAlias = valueElements.Length == 3 ? valueElements[2] : null; predicate = new FieldBetweenPredicate(field, null, objElements[0], objElements[1], objAlias, comparisonOperatorStr == "nbt"); break; case ("in"): //same as eq case ("nin"): //same as ne case ("eq"): //equals any case ("neq"): //not equals any case ("eqc"): //equals any, case insensitive [on a case sensitive database] - only 1 element per clause with this option case ("neqc"): //not equals any, case insensitive [on a case sensitive database] - only 1 element per clause with this option if (valueElements.Length < 1) { throwINEEx(comparisonOperatorStr); } for (int i = 0; i < valueElements.Length; i++) { objElements[i] = ConvertStringToFieldValue(field, valueElements[i]); } if (objElements.Length == 1 || comparisonOperatorStr == "eqci" || comparisonOperatorStr == "neci") { predicate = new FieldCompareValuePredicate(field, null, comparisonOperatorStr.StartsWith("n") ? ComparisonOperator.NotEqual : ComparisonOperator.Equal, objElements[0]) { CaseSensitiveCollation = comparisonOperatorStr == "eqci" || comparisonOperatorStr == "neci" }; } else { if (comparisonOperatorStr.StartsWith("n")) { predicate = (EntityField2)field != objElements; } else { predicate = (EntityField2)field == objElements; } } break; case ("lk"): //like case ("nlk"): //not like if (valueElements.Length < 1) { throwINEEx(comparisonOperatorStr); } objElements[0] = ConvertStringToFieldValue(field, valueElements[0].Replace('*', '%')); objAlias = valueElements.Length == 2 ? valueElements[1] : null; predicate = new FieldLikePredicate(field, null, objAlias, (string)objElements[0], comparisonOperatorStr == "nlk"); break; case ("nl"): //null case ("nnl"): //not null predicate = new FieldCompareNullPredicate(field, null, null, comparisonOperatorStr == "nnl"); break; case ("gt"): //greater than) case ("gte"): //greater than or equal to case ("lt"): //less than case ("lte"): //less than or equal to if (valueElements.Length < 1) { throwINEEx(comparisonOperatorStr); } objElements[0] = ConvertStringToFieldValue(field, valueElements[0]); objAlias = valueElements.Length == 2 ? valueElements[1] : null; var comparisonOperator = ComparisonOperator.GreaterThan; if (comparisonOperatorStr == "gte") { comparisonOperator = ComparisonOperator.GreaterEqual; } else if (comparisonOperatorStr == "lt") { comparisonOperator = ComparisonOperator.LesserThan; } else if (comparisonOperatorStr == "lte") { comparisonOperator = ComparisonOperator.LessEqual; } predicate = new FieldCompareValuePredicate(field, null, comparisonOperator, objElements[0], objAlias); break; case ("ct"): //full text contains case ("ft"): //full text free text if (valueElements.Length < 1) { throwINEEx(comparisonOperatorStr); } objElements[0] = valueElements[0]; objAlias = valueElements.Length == 2 ? valueElements[1] : null; predicate = new FieldFullTextSearchPredicate(field, null, comparisonOperatorStr == "ct" ? FullTextSearchOperator.Contains : FullTextSearchOperator.Freetext, (string)objElements[0], objAlias); break; default: return(null); } return(predicate); }