コード例 #1
0
        public override void Validate(Type entityType)
        {
            if (Expr1Source == null)
            {
                throw new Exception("Unable to validate 1st expression: " + this.Expr1Source);
            }

            this._block1 = BaseBlock.CreateLHSBlock(Expr1Source, entityType);

            if (_op == Operator.In && !(Expr2Source is IList))
            {
                throw new Exception("The 'in' operator requires that its right hand argument be an array");
            }

            // Special purpose Enum handling

            var enumType = GetEnumType(this._block1);

            if (enumType != null)
            {
                if (Expr2Source != null)
                {
                    var et        = TypeFns.GetNonNullableType(enumType);
                    var expr2Enum = Enum.Parse(et, (String)Expr2Source);
                    this._block2 = BaseBlock.CreateRHSBlock(expr2Enum, entityType, null);
                }
                else
                {
                    this._block2 = BaseBlock.CreateRHSBlock(null, entityType, null);
                }
            }
            else
            {
                this._block2 = BaseBlock.CreateRHSBlock(Expr2Source, entityType, this._block1.DataType);
            }
        }
コード例 #2
0
        public static DataType FromType(Type type)
        {
            var nnType = TypeFns.GetNonNullableType(type);

            return(_typeMap[nnType]);
        }
コード例 #3
0
        public override Expression ToExpression(Expression inExpr)
        {
            var exprs = _exprs.Select(e => e.ToExpression(inExpr)).ToList();
            var expr  = exprs[0];

            // TODO: add the rest ...
            if (FnName == "toupper")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.ToUpper());
                return(Expression.Call(expr, mi));
            }
            else if (FnName == "tolower")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.ToLower());
                return(Expression.Call(expr, mi));
            }
            else if (FnName == "trim")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Trim());
                return(Expression.Call(expr, mi));
            }
            else if (FnName == "length")
            {
                return(GetPropertyExpression(expr, "Length", typeof(int)));
            }
            else if (FnName == "indexof")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.IndexOf("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else if (FnName == "concat")
            {
                // TODO: check if this works...
                var mi = TypeFns.GetMethodByExample((String s) => String.Concat(s, "xxx"));
                return(Expression.Call(mi, exprs[0], exprs[1]));
            }
            else if (FnName == "substring")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Substring(1, 5));
                return(Expression.Call(exprs[0], mi, exprs.Skip(1)));
            }
            else if (FnName == "replace")
            {
                // TODO: check if this works...
                var mi = TypeFns.GetMethodByExample((String s) => s.Replace("aaa", "bbb"));
                return(Expression.Call(exprs[0], mi, exprs[1], exprs[2]));
            }
            else if (FnName == "year")
            {
                return(GetPropertyExpression(expr, "Year", typeof(int)));
            }
            else if (FnName == "month")
            {
                return(GetPropertyExpression(expr, "Month", typeof(int)));
            }
            else if (FnName == "day")
            {
                return(GetPropertyExpression(expr, "Day", typeof(int)));
            }
            else if (FnName == "hour")
            {
                return(GetPropertyExpression(expr, "Hour", typeof(int)));
            }
            else if (FnName == "minute")
            {
                return(GetPropertyExpression(expr, "Minute", typeof(int)));
            }
            else if (FnName == "second")
            {
                return(GetPropertyExpression(expr, "Second", typeof(int)));
            }
            else if (FnName == "round")
            {
                // TODO: confirm that this works - is using static method.
                var mi = TypeFns.GetMethodByExample((Double d) => Math.Round(d));
                return(Expression.Call(mi, expr));
            }
            else if (FnName == "ceiling")
            {
                var mi = TypeFns.GetMethodByExample((Double d) => Math.Ceiling(d));
                return(Expression.Call(mi, expr));
            }
            else if (FnName == "floor")
            {
                var mi = TypeFns.GetMethodByExample((Double d) => Math.Floor(d));
                return(Expression.Call(mi, expr));
            }
            else if (FnName == "startswith")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.StartsWith("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else if (FnName == "endsWith")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.EndsWith("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else if (FnName == "substringof")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Contains("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else
            {
                throw new Exception("Unable to locate Fn: " + FnName);
            }
        }
コード例 #4
0
        private static BasePredicate PredicateFromKeyValue(String key, Object value)
        {
            Operator op = Operator.FromString(key);

            if (op != null)
            {
                if (op.OpType == OperatorType.AndOr)
                {
                    var preds2 = PredicatesFromObject(value);
                    return(new AndOrPredicate(op, preds2));
                }
                else if (op.OpType == OperatorType.Unary)
                {
                    BasePredicate pred = PredicateFromObject(value);
                    return(new UnaryPredicate(op, pred));
                }
                else
                {
                    throw new Exception("Invalid operator in context: " + key);
                }
            }

            if (value == null || TypeFns.IsPredefinedType(value.GetType()))
            {
                return(new BinaryPredicate(BinaryOperator.Equals, key, value));
            }
            else if (value is IDictionary <string, object> && ((IDictionary <string, object>)value).ContainsKey("value"))
            {
                return(new BinaryPredicate(BinaryOperator.Equals, key, value));
            }

            if (!(value is Dictionary <string, object>))
            {
                throw new Exception("Unable to resolve value associated with key:" + key);
            }

            var preds = new List <BasePredicate>();
            var map   = (Dictionary <string, object>)value;


            foreach (var subKey in map.Keys)
            {
                Operator      subOp  = Operator.FromString(subKey);
                Object        subVal = map[subKey];
                BasePredicate pred;
                if (subOp != null)
                {
                    if (subOp.OpType == OperatorType.AnyAll)
                    {
                        BasePredicate subPred = PredicateFromObject(subVal);
                        pred = new AnyAllPredicate(subOp, key, subPred);
                    }
                    else if (subOp.OpType == OperatorType.Binary)
                    {
                        pred = new BinaryPredicate(subOp, key, subVal);
                    }
                    else
                    {
                        throw new Exception("Unable to resolve OperatorType for key: " + subKey);
                    }
                    // next line old check was for null not 'ContainsKey'
                }
                else if (subVal is IDictionary <string, object> && ((IDictionary <string, object>)subVal).ContainsKey("value"))
                {
                    pred = new BinaryPredicate(BinaryOperator.Equals, key, subVal);
                }
                else
                {
                    throw new Exception("Unable to resolve BasePredicate after: " + key);
                }
                preds.Add(pred);
            }
            return(CreateCompoundPredicate(preds));
        }
コード例 #5
0
ファイル: BaseBlock.cs プロジェクト: maca88/Breeze.NHibernate
        // will return either a PropBlock or a LitBlock
        #region Modified code - Added entityMetadataProvider parameter
        public static BaseBlock CreateRHSBlock(Object exprSource,
                                               Type entityType, DataType otherExprDataType, IEntityMetadataProvider entityMetadataProvider)
        {
            if (exprSource == null)
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (exprSource is String)
            {
                String source = (String)exprSource;
                if (entityType == null)
                {
                    // if entityType is unknown then assume that the rhs is a
                    // literal
                    return(new LitBlock(source, otherExprDataType));
                }

                if (PropertySignature.IsProperty(entityType, source, entityMetadataProvider))
                {
                    return(new PropBlock(source, entityType, entityMetadataProvider));
                }
                else
                {
                    return(new LitBlock(source, otherExprDataType));
                }
            }

            if (TypeFns.IsPredefinedType(exprSource.GetType()))
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (exprSource is IDictionary <string, Object> )
            {
                var exprMap = (IDictionary <string, Object>)exprSource;
                // note that this is NOT the same a using get and checking for null
                // because null is a valid 'value'.
                if (!exprMap.ContainsKey("value"))
                {
                    throw new Exception(
                              "Unable to locate a 'value' property on: "
                              + exprMap.ToString());
                }
                Object value = exprMap["value"];

                if (exprMap.ContainsKey("isProperty"))
                {
                    return(new PropBlock((String)value, entityType, entityMetadataProvider));
                }
                else
                {
                    String   dt       = (String)exprMap["dataType"];
                    DataType dataType = (dt != null) ? DataType.FromName(dt) : otherExprDataType;
                    return(new LitBlock(value, dataType));
                }
            }

            if (exprSource is IList)
            {
                // right now this pretty much implies the values on an 'in' clause
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (TypeFns.IsEnumType(exprSource.GetType()))
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            throw new Exception(
                      "Unable to parse the right hand side of this BinaryExpression: "
                      + exprSource.ToString());
        }
コード例 #6
0
        private Expression BuildBinaryExpr(Expression expr1, Expression expr2, Operator op)
        {
            if (expr1.Type != expr2.Type)
            {
                if (TypeFns.IsNullableType(expr1.Type) && !TypeFns.IsNullableType(expr2.Type))
                {
                    if (!expr2.Type.IsEnum)
                    {
                        expr2 = Expression.Convert(expr2, expr1.Type);
                    }
                    else
                    {
                        expr1 = Expression.Convert(expr1, expr2.Type);
                    }
                }
                else if (TypeFns.IsNullableType(expr2.Type) && !TypeFns.IsNullableType(expr1.Type))
                {
                    if (!expr1.Type.IsEnum)
                    {
                        expr1 = Expression.Convert(expr1, expr2.Type);
                    }
                    else
                    {
                        expr2 = Expression.Convert(expr2, expr1.Type);
                    }
                }

                if (HasNullValue(expr2) && CannotBeNull(expr1))
                {
                    expr1 = Expression.Convert(expr1, TypeFns.GetNullableType(expr1.Type));
                }
                else if (HasNullValue(expr1) && CannotBeNull(expr2))
                {
                    expr2 = Expression.Convert(expr2, TypeFns.GetNullableType(expr2.Type));
                }
            }

            if (op == BinaryOperator.Equals)
            {
                return(Expression.Equal(expr1, expr2));
            }
            else if (op == BinaryOperator.NotEquals)
            {
                return(Expression.NotEqual(expr1, expr2));
            }
            else if (op == BinaryOperator.GreaterThan)
            {
                return(Expression.GreaterThan(expr1, expr2));
            }
            else if (op == BinaryOperator.GreaterThanOrEqual)
            {
                return(Expression.GreaterThanOrEqual(expr1, expr2));
            }
            else if (op == BinaryOperator.LessThan)
            {
                return(Expression.LessThan(expr1, expr2));
            }
            else if (op == BinaryOperator.LessThanOrEqual)
            {
                return(Expression.LessThanOrEqual(expr1, expr2));
            }
            else if (op == BinaryOperator.StartsWith)
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.StartsWith("abc"));
                return(Expression.Call(expr1, mi, expr2));
            }
            else if (op == BinaryOperator.EndsWith)
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.EndsWith("abc"));
                return(Expression.Call(expr1, mi, expr2));
            }
            else if (op == BinaryOperator.Contains)
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Contains("abc"));
                return(Expression.Call(expr1, mi, expr2));
            }
            else if (op == BinaryOperator.In)
            {
                // TODO: need to generalize this past just 'string'
                var mi = TypeFns.GetMethodByExample((List <String> list) => list.Contains("abc"), expr1.Type);
                return(Expression.Call(expr2, mi, expr1));
            }

            return(null);
        }
コード例 #7
0
        private bool CannotBeNull(Expression expr)
        {
            var t = expr.Type;

            return(TypeFns.IsPredefinedType(t) && t != typeof(String));
        }
コード例 #8
0
 /// <summary>
 /// Constructs a generic list.
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static IList MakeGenericList(Type type)
 {
     return((IList)TypeFns.ConstructGenericInstance(typeof(List <>), type));
 }