private static FilterSpecParamEventProp MakeParam(String eventAsName, String property) { Coercer numberCoercer = CoercerFactory.GetCoercer(typeof(int), typeof(int)); return(new FilterSpecParamEventProp(MakeLookupable("IntPrimitive"), FilterOperator.EQUAL, eventAsName, property, false, numberCoercer, typeof(int), "Test")); }
public void TestGetValueSet() { IList <FilterSpecParam> parameters = SupportFilterSpecBuilder.BuildList(_eventType, new Object[] { "IntPrimitive", FilterOperator.EQUAL, 2 }); var numberCoercer = CoercerFactory.GetCoercer(typeof(int), typeof(double)); parameters.Add(new FilterSpecParamEventProp(MakeLookupable("DoubleBoxed"), FilterOperator.EQUAL, "asName", "DoublePrimitive", false, numberCoercer, typeof(double?), "Test")); FilterSpecCompiled filterSpec = new FilterSpecCompiled(_eventType, "SupportBean", new IList <FilterSpecParam>[] { parameters }, null); SupportBean eventBean = new SupportBean(); eventBean.DoublePrimitive = 999.999; EventBean theEvent = SupportEventBeanFactory.CreateObject(eventBean); MatchedEventMap matchedEvents = new MatchedEventMapImpl(new MatchedEventMapMeta(new String[] { "asName" }, false)); matchedEvents.Add(0, theEvent); FilterValueSet valueSet = filterSpec.GetValueSet(matchedEvents, null, null); // Assert the generated filter value container Assert.AreSame(_eventType, valueSet.EventType); Assert.AreEqual(2, valueSet.Parameters[0].Length); // Assert the first param var param = valueSet.Parameters[0][0]; Assert.AreEqual("IntPrimitive", param.Lookupable.Expression); Assert.AreEqual(FilterOperator.EQUAL, param.FilterOperator); Assert.AreEqual(2, param.FilterForValue); // Assert the second param param = (FilterValueSetParam)valueSet.Parameters[0][1]; Assert.AreEqual("DoubleBoxed", param.Lookupable.Expression); Assert.AreEqual(FilterOperator.EQUAL, param.FilterOperator); Assert.AreEqual(999.999, param.FilterForValue); }
/// <summary>Ctor. </summary> /// <param name="streamNum">is the stream number of the indexed stream</param> /// <param name="eventType">is the event type of the indexed stream</param> /// <param name="propertyName">are the property names to get property values</param> /// <param name="coercionType">are the classes to coerce indexed values to</param> public PropertyIndexedEventTableSingleCoerceAddFactory( int streamNum, EventType eventType, String propertyName, Type coercionType) : base(streamNum, eventType, propertyName, false, null) { CoercionType = coercionType; Coercer = coercionType.IsNumeric() ? CoercerFactory.GetCoercer(null, coercionType) : null; }
/// <summary>Ctor. </summary> /// <param name="streamNum">is the stream number of the indexed stream</param> /// <param name="eventType">is the event type of the indexed stream</param> /// <param name="propertyNames">are the property names to get property values</param> /// <param name="coercionType">are the classes to coerce indexed values to</param> public PropertyIndexedEventTableCoerceAddFactory(int streamNum, EventType eventType, IList <string> propertyNames, IList <Type> coercionType) : base(streamNum, eventType, propertyNames, false, null) { CoercionType = coercionType.ToArray(); Coercers = new Coercer[coercionType.Count]; for (int i = 0; i < coercionType.Count; i++) { if (coercionType[i].IsNumeric()) { Coercers[i] = CoercerFactory.GetCoercer(null, coercionType[i]); } } }
/// <summary> /// Ctor. /// </summary> /// <param name="notIn">false for =, true for !=</param> /// <param name="mustCoerce">coercion required</param> /// <param name="coercionType">type to coerce to</param> /// <param name="valueExpr">LHS</param> /// <param name="selectClauseExpr">select clause or null</param> /// <param name="filterExpr">filter or null</param> public SubselectEvalStrategyEqualsIn(bool notIn, bool mustCoerce, Type coercionType, ExprEvaluator valueExpr, ExprEvaluator selectClauseExpr, ExprEvaluator filterExpr) { _isNotIn = notIn; _mustCoerce = mustCoerce; _coercer = mustCoerce ? CoercerFactory.GetCoercer(null, coercionType) : null; _valueExpr = valueExpr; _filterExpr = filterExpr; _selectClauseExpr = selectClauseExpr; }
private static Coercer GetNumberCoercer(Type leftType, Type rightType, string expression) { var numericCoercionType = leftType.GetBoxedType(); if (rightType != leftType) { if (rightType.IsNumeric()) { if (!rightType.CanCoerce(leftType)) { ThrowConversionError(rightType, leftType, expression); } return CoercerFactory.GetCoercer(rightType, numericCoercionType); } } return null; }
public static TransformDictionary <TK1, TV1, TK2, TV2> Create <TK1, TV1, TK2, TV2>(object value) { var sourceDictionary = (IDictionary <TK2, TV2>)value; var key1To2Coercer = CoercerFactory.GetCoercer(typeof(TK1), typeof(TK2)); var key2To1Coercer = CoercerFactory.GetCoercer(typeof(TK2), typeof(TK1)); var val1To2Coercer = CoercerFactory.GetCoercer(typeof(TV1), typeof(TV2)); var val2To1Coercer = CoercerFactory.GetCoercer(typeof(TV2), typeof(TV1)); return(new TransformDictionary <TK1, TV1, TK2, TV2>( sourceDictionary, k2 => (TK1)key2To1Coercer(k2), k1 => (TK2)key1To2Coercer(k1), v2 => (TV1)val2To1Coercer(v2), v1 => (TV2)val1To2Coercer(v1))); }
protected ExprNodeScriptEvalBase(String scriptName, String statementName, String[] names, ExprEvaluator[] parameters, Type returnType) { ScriptName = scriptName; StatementName = statementName; Names = names; Parameters = parameters; _returnType = returnType; if (returnType.IsNumeric()) { Coercer = CoercerFactory.GetCoercer(returnType.GetBoxedType()); } else { Coercer = null; } }
/// <summary> /// Ctor. /// </summary> /// <param name="notIn">false for =, true for !=</param> /// <param name="mustCoerce">coercion required</param> /// <param name="coercionType">type to coerce to</param> /// <param name="valueExpr">LHS</param> /// <param name="selectClauseExpr">select clause or null</param> /// <param name="filterExpr">filter or null</param> public SubselectEvalStrategyEqualsAll(bool notIn, bool mustCoerce, Type coercionType, ExprEvaluator valueExpr, ExprEvaluator selectClauseExpr, ExprEvaluator filterExpr) { _isNot = notIn; _mustCoerce = mustCoerce; if (mustCoerce) { _coercer = CoercerFactory.GetCoercer(null, coercionType); } else { _coercer = null; } _valueExpr = valueExpr; _filterExpr = filterExpr; _selectClauseExpr = selectClauseExpr; }
private void ValidateCaseTwo() { // validate we can compare result types IList <Type> comparedTypes = new List <Type>(); comparedTypes.Add(_optionalCompareExprNode.ReturnType); foreach (UniformPair <ExprEvaluator> pair in _whenThenNodeList) { comparedTypes.Add(pair.First.ReturnType); } // Determine common denominator type try { Type coercionType = TypeHelper.GetCommonCoercionType(comparedTypes); // Determine if we need to coerce numbers when one type doesn't match any other type if (coercionType.IsNumeric()) { _mustCoerce = false; foreach (Type comparedType in comparedTypes) { if (comparedType != coercionType) { _mustCoerce = true; } } if (_mustCoerce) { _coercer = CoercerFactory.GetCoercer(null, coercionType); } } } catch (CoercionException ex) { throw new ExprValidationException("Implicit conversion not allowed: " + ex.Message); } }
private static Coercer GetCoercer(Type typeOne, Type typeTwo) { // Get the common type such as Bool, string or double? and Long Type coercionType; bool mustCoerce; try { coercionType = typeOne.GetCompareToCoercionType(typeTwo); } catch (CoercionException) { throw new ExprValidationException(string.Format("Implicit conversion from datatype '{0}' to '{1}' is not allowed", Name.Of(typeTwo), Name.Of(typeOne))); } // Check if we need to coerce mustCoerce = false; if ((coercionType != typeOne.GetBoxedType()) || (coercionType != typeTwo.GetBoxedType())) { if (coercionType.IsNumeric()) { mustCoerce = true; } } return(!mustCoerce ? null : CoercerFactory.GetCoercer(null, coercionType)); }
public override ExprNode Validate(ExprValidationContext validationContext) { _length = ChildNodes.Count; _evaluators = ExprNodeUtility.GetEvaluators(ChildNodes); // Can be an empty array with no content if (ChildNodes.Count == 0) { _arrayReturnType = typeof(Object); _constantResult = new Object[0]; return(null); } var comparedTypes = new List <Type>(); for (int i = 0; i < _length; i++) { comparedTypes.Add(_evaluators[i].ReturnType); } // Determine common denominator type try { _arrayReturnType = TypeHelper.GetCommonCoercionType(comparedTypes.ToArray()); // Determine if we need to coerce numbers when one type doesn't match any other type if (_arrayReturnType.IsNumeric()) { _mustCoerce = false; foreach (var comparedType in comparedTypes) { if (comparedType != _arrayReturnType) { _mustCoerce = true; } } if (_mustCoerce) { _coercer = CoercerFactory.GetCoercer(null, _arrayReturnType); } } } catch (CoercionException) { // expected, such as mixing String and int values, or classes (not boxed) and primitives // use Object[] in such cases } if (_arrayReturnType == null) { _arrayReturnType = typeof(Object); } // Determine if we are dealing with constants only var results = new Object[_length]; int index = 0; foreach (ExprNode child in ChildNodes) { if (!child.IsConstantResult) { results = null; // not using a constant result break; } results[index] = _evaluators[index].Evaluate(new EvaluateParams(null, false, validationContext.ExprEvaluatorContext)); index++; } // Copy constants into array and coerce, if required if (results != null) { var asArray = Array.CreateInstance(_arrayReturnType, _length); _constantResult = asArray; for (int i = 0; i < _length; i++) { if (_mustCoerce) { var boxed = results[i]; if (boxed != null) { Object coercedResult = _coercer.Invoke(boxed); asArray.SetValue(coercedResult, i); } } else { asArray.SetValue(results[i], i); } } } return(null); }
public override ExprNode Validate(ExprValidationContext validationContext) { // Must have 2 child nodes if (ChildNodes.Length != 2) { throw new ExprValidationException("Invalid use of equals, expecting left-hand side and right-hand side but received " + ChildNodes.Length + " expressions"); } var evaluators = ExprNodeUtility.GetEvaluators(ChildNodes); // Must be the same boxed type returned by expressions under this var typeOne = evaluators[0].ReturnType.GetBoxedType(); var typeTwo = evaluators[1].ReturnType.GetBoxedType(); // Null constants can be compared for any type if ((typeOne == null) || (typeTwo == null)) { _evaluator = GetEvaluator(evaluators[0], evaluators[1]); return(null); } if (typeOne == typeTwo || typeOne.IsAssignableFrom(typeTwo)) { _evaluator = GetEvaluator(evaluators[0], evaluators[1]); return(null); } // Get the common type such as Bool, String or Double and Long Type coercionType; try { coercionType = typeOne.GetCompareToCoercionType(typeTwo); } catch (CoercionException) { throw new ExprValidationException(string.Format("Implicit conversion from datatype '{0}' to '{1}' is not allowed", typeTwo.FullName, typeOne.FullName)); } // Check if we need to coerce if ((coercionType == typeOne.GetBoxedType()) && (coercionType == typeTwo.GetBoxedType())) { _evaluator = GetEvaluator(evaluators[0], evaluators[1]); } else if ((typeOne.IsArray) && (typeTwo.IsArray) && (typeOne.GetElementType().GetBoxedType() == typeTwo.GetElementType().GetBoxedType())) { coercionType = typeOne.GetElementType().GetCompareToCoercionType(typeTwo.GetElementType()); _evaluator = new ExprEqualsEvaluatorCoercingArray( this, evaluators[0], evaluators[1], CoercerFactory.GetCoercer(typeOne.GetComponentType(), coercionType), CoercerFactory.GetCoercer(typeTwo.GetComponentType(), coercionType)); } else { if (!coercionType.IsNumeric()) { throw new ExprValidationException("Cannot convert datatype '" + coercionType.Name + "' to a numeric value"); } _evaluator = new ExprEqualsEvaluatorCoercing( this, evaluators[0], evaluators[1], CoercerFactory.GetCoercer(typeOne, coercionType), CoercerFactory.GetCoercer(typeTwo, coercionType)); } return(null); }
public void ValidateWithoutContext() { if (ChildNodes.Length < 2) { throw new ExprValidationException("The IN operator requires at least 2 child expressions"); } _evaluators = ExprNodeUtility.GetEvaluators(ChildNodes); // Must be the same boxed type returned by expressions under this var typeOne = _evaluators[0].ReturnType.GetBoxedType(); // collections, array or map not supported if ((typeOne.IsArray) || (typeOne.IsGenericCollection()) || (typeOne.IsGenericDictionary())) { throw new ExprValidationException("Collection or array comparison is not allowed for the IN, ANY, SOME or ALL keywords"); } _transformList = new Func <object, object> [_evaluators.Length]; var comparedTypes = new List <Type> { typeOne }; _hasCollectionOrArray = false; var length = ChildNodes.Length - 1; for (int i = 1; i <= length; i++) { var propType = _evaluators[i].ReturnType; if (propType == null) { continue; } if (propType.IsArray) { _hasCollectionOrArray = true; if (propType.GetElementType() != typeof(Object)) { comparedTypes.Add(propType.GetElementType()); } } else if (propType.IsGenericDictionary()) { var baseTransform = MagicMarker.GetDictionaryFactory(propType); _transformList[i] = o => baseTransform(o); _hasCollectionOrArray = true; } else if (propType.IsGenericCollection()) { var baseTransform = MagicMarker.GetCollectionFactory(propType); _transformList[i] = o => baseTransform(o); _hasCollectionOrArray = true; } else { comparedTypes.Add(propType); } } // Determine common denominator type Type coercionType; try { coercionType = TypeHelper.GetCommonCoercionType(comparedTypes); } catch (CoercionException ex) { throw new ExprValidationException("Implicit conversion not allowed: " + ex.Message); } // Check if we need to coerce _mustCoerce = false; if (coercionType.IsNumeric()) { foreach (Type compareType in comparedTypes) { if (coercionType != compareType.GetBoxedType()) { _mustCoerce = true; } } if (_mustCoerce) { _coercer = CoercerFactory.GetCoercer(null, coercionType.GetBoxedType()); } } }
public override ExprNode Validate(ExprValidationContext validationContext) { // Must have 2 child nodes var childNodes = ChildNodes; if (childNodes.Length < 1) { throw new IllegalStateException("Equals group node does not have 1 or more parameters"); } _evaluators = ExprNodeUtility.GetEvaluators(ChildNodes); // Must be the same boxed type returned by expressions under this Type typeOne = _evaluators[0].ReturnType.GetBoxedType(); // collections, array or map not supported if ((typeOne.IsArray) || (typeOne.IsGenericCollection()) || (typeOne.IsGenericDictionary())) { throw new ExprValidationException( "Collection or array comparison is not allowed for the IN, ANY, SOME or ALL keywords"); } _transformList = new Func <object, object> [childNodes.Length]; var comparedTypes = new List <Type>(); comparedTypes.Add(typeOne); _hasCollectionOrArray = false; for (int i = 1; i < childNodes.Length; i++) { Type propType = _evaluators[i].ReturnType; if (propType.IsArray) { _hasCollectionOrArray = true; if (propType.GetElementType() != typeof(Object)) { comparedTypes.Add(propType.GetElementType()); } } else if (propType.IsGenericDictionary()) { var baseTransform = MagicMarker.GetDictionaryFactory(propType); _transformList[i] = o => baseTransform(o); _hasCollectionOrArray = true; } else if (propType.IsGenericCollection()) { var baseTransform = MagicMarker.GetCollectionFactory(propType); _transformList[i] = o => baseTransform(o); _hasCollectionOrArray = true; } else { comparedTypes.Add(propType); } } // Determine common denominator type Type coercionType; try { coercionType = TypeHelper.GetCommonCoercionType(comparedTypes); } catch (CoercionException ex) { throw new ExprValidationException("Implicit conversion not allowed: " + ex.Message); } // Check if we need to coerce _mustCoerce = false; if (coercionType.IsNumeric()) { foreach (Type compareType in comparedTypes) { if (coercionType != compareType.GetBoxedType()) { _mustCoerce = true; } } if (_mustCoerce) { _coercer = CoercerFactory.GetCoercer(null, coercionType.GetBoxedType()); } } return(null); }