///<summary> /// Based on the class definition the given <see cref="Criteria"/> object is set up with the correct entity /// names and field names, in preparation for using it as part of a <see cref="SelectQuery"/> that has been built using /// the <see cref="QueryBuilder"/> and the same <see cref="ClassDef"/> ///</summary> ///<param name="classDef">The class definition to use for preparing the <see cref="Criteria"/>.</param> ///<param name="criteria">The <see cref="Criteria"/> to prepare for use with a <see cref="SelectQuery"/>.</param> public static void PrepareCriteria(IClassDef classDef, Criteria criteria) { if (classDef == null) { throw new ArgumentNullException("classDef"); } if (criteria == null) { return; } if (criteria.IsComposite()) { PrepareCriteria(classDef, criteria.LeftCriteria); PrepareCriteria(classDef, criteria.RightCriteria); } else { var field = criteria.Field; var fieldPropDef = PrepareField(field.Source, classDef, field); if (fieldPropDef != null) { field.FieldName = fieldPropDef.DatabaseFieldName; if (null == field.Source) { throw new NullReferenceException("the field.Source is null"); } if (null == field.Source.RelatedClassDef) { throw new NullReferenceException("the field.Source.RelatedClassDef is null"); } if (null == field.Source.ChildSourceLeaf) { throw new NullReferenceException("the field.Source.ChildSourceLeaf is null"); } field.Source.ChildSourceLeaf.EntityName = field.Source.RelatedClassDef.GetTableName(fieldPropDef); if (criteria.CanBeParametrised() && (criteria.ComparisonOperator != Criteria.ComparisonOp.In && criteria.ComparisonOperator != Criteria.ComparisonOp.NotIn)) { object returnedValue; fieldPropDef.TryParsePropValue(criteria.FieldValue, out returnedValue); criteria.FieldValue = returnedValue; } } } }
public void TestNotIn() { //-------------Setup Test Pack ------------------ //-------------Test Pre-conditions -------------- //-------------Execute test --------------------- object[] values = new object[] { "100", "200", "300" }; Criteria.CriteriaValues criteriaValues = new Criteria.CriteriaValues(values); Criteria criteria = new Criteria("MyField", Criteria.ComparisonOp.NotIn, criteriaValues); //-------------Test Result ---------------------- Assert.IsNotNull(criteria.Field); Assert.AreEqual("MyField", criteria.Field.PropertyName); Assert.IsNull(criteria.Field.Source); Assert.AreEqual("MyField", criteria.Field.FieldName); Assert.AreEqual(Criteria.ComparisonOp.NotIn, criteria.ComparisonOperator); Assert.IsTrue(criteria.CanBeParametrised()); }
///<summary> /// Based on the class definition the given <see cref="Criteria"/> object is set up with the correct entity /// names and field names, in preparation for using it as part of a <see cref="SelectQuery"/> that has been built using /// the <see cref="QueryBuilder"/> and the same <see cref="ClassDef"/> ///</summary> ///<param name="classDef">The class definition to use for preparing the <see cref="Criteria"/>.</param> ///<param name="criteria">The <see cref="Criteria"/> to prepare for use with a <see cref="SelectQuery"/>.</param> public static void PrepareCriteria(IClassDef classDef, Criteria criteria) { if (classDef == null) throw new ArgumentNullException("classDef"); if (criteria == null) return; if (criteria.IsComposite()) { PrepareCriteria(classDef, criteria.LeftCriteria); PrepareCriteria(classDef, criteria.RightCriteria); } else { var field = criteria.Field; var fieldPropDef = PrepareField(field.Source, classDef, field); if (fieldPropDef != null) { field.FieldName = fieldPropDef.DatabaseFieldName; if (null == field.Source) throw new NullReferenceException("the field.Source is null"); if (null == field.Source.RelatedClassDef) throw new NullReferenceException("the field.Source.RelatedClassDef is null"); if (null == field.Source.ChildSourceLeaf) throw new NullReferenceException("the field.Source.ChildSourceLeaf is null"); field.Source.ChildSourceLeaf.EntityName = field.Source.RelatedClassDef.GetTableName(fieldPropDef); if (criteria.CanBeParametrised() && (criteria.ComparisonOperator != Criteria.ComparisonOp.In && criteria.ComparisonOperator != Criteria.ComparisonOp.NotIn)) { object returnedValue; fieldPropDef.TryParsePropValue(criteria.FieldValue, out returnedValue); criteria.FieldValue = returnedValue; } } } }