public string GetSQLCondition(SQLFieldMapping mapping)
 {
     return SQLText
         .Replace("%fullsqlname", mapping.FullSQLName)
         .Replace("%table", mapping.Table)
         .Replace("%field", mapping.Field)
         .Replace("%property", mapping.PropertyName);
 }
Exemplo n.º 2
0
 public string GetSQLText(SQLFieldMapping mapping)
 {
     return(SQLText
            .Replace("%fullsqlname", mapping.FullSQLName)
            .Replace("%table", mapping.Table)
            .Replace("%field", mapping.Field)
            .Replace("%property", mapping.PropertyName));
 }
Exemplo n.º 3
0
        private IQueryable <T> SmartWhereBinaryExpression(Expression <Func <T, bool> > predicate)
        {
            // parse the body
            BinaryExpression ex = (BinaryExpression)predicate.Body;

            if (ex.NodeType != ExpressionType.Equal)
            {
                return(null);
            }

            // get left value
            MemberExpression left = (MemberExpression)ex.Left;

            if (left.Expression.NodeType != ExpressionType.Parameter)
            {
                return(null);
            }
            string leftName = left.Member.Name;

            // check right value
            if (ex.Right.NodeType == ExpressionType.MemberAccess && ((MemberExpression)ex.Right).Expression.NodeType == ExpressionType.Parameter)
            {
                return(null);
            }
            object value = Expression.Lambda(ex.Right).Compile().DynamicInvoke();

            if (value is Boolean)
            {
                value = (bool)value ? 1 : 0;
            }
            if (!(value is Int32) && !(value is String))
            {
                return(null);
            }

            // check if mapping supports SQL compare
            var mappingList = mapping.Where(x => x.PropertyName == leftName);

            if (mappingList.Count() == 0)
            {
                return(null);
            }
            SQLFieldMapping thisMapping = mappingList.First();

            if (!Attribute.IsDefined(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute)))
            {
                return(null);
            }

            // build SQL
            AllowSQLCompareAttribute attr = (AllowSQLCompareAttribute)Attribute.GetCustomAttribute(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute));

            // create smart where
            whereItems.Add(new Tuple <string, object>(attr.GetSQLCondition(thisMapping), value));
            return(this);
        }
Exemplo n.º 4
0
        private IOrderedQueryable <T> SmartAddOrder <TKey>(bool desc, Expression <Func <T, TKey> > keySelector)
        {
            // don't execute query twice
            if (result != null)
            {
                return(null);
            }

            // validate the parameter
            if (keySelector.Parameters.Count != 1)
            {
                return(null);
            }
            if (!keySelector.Parameters[0].Type.IsAssignableFrom(typeof(T)))
            {
                return(null);
            }

            // parse the body
            if (!(keySelector.Body is MemberExpression))
            {
                return(null);
            }

            // we expect a parameter or a cast to an interface here
            MemberExpression ex = (MemberExpression)keySelector.Body;

            if (ex.Expression.NodeType != ExpressionType.Convert && ex.Expression.NodeType != ExpressionType.Parameter)
            {
                return(null);
            }

            // we got the fieldname, map it to an SQL name
            string fieldName = ex.Member.Name;
            var    list      = mapping.Where(x => x.PropertyName == fieldName);

            if (list.Count() == 0)
            {
                return(null);
            }
            SQLFieldMapping thisMapping = list.First();

            // check if supported on reader
            if (!Attribute.IsDefined(thisMapping.Reader.Method, typeof(AllowSQLSortAttribute)))
            {
                return(null);
            }

            // add to the order clausule
            AllowSQLSortAttribute attr = (AllowSQLSortAttribute)Attribute.GetCustomAttribute(thisMapping.Reader.Method, typeof(AllowSQLSortAttribute));

            orderItems.Add(new Tuple <string, string, bool>(fieldName, attr.GetSQLText(thisMapping).Replace("%order", desc ? "DESC" : "ASC"), desc));
            return(this);
        }
Exemplo n.º 5
0
        private IQueryable <T> SmartWhereMethodCall(Expression <Func <T, bool> > predicate)
        {
            MethodCallExpression mce = predicate.Body as MethodCallExpression;

            // verify some preconditions
            if (mce.Method.Name != "Contains")
            {
                return(null);
            }
            if (mce.Arguments.Count != 1 || mce.Arguments[0].NodeType != ExpressionType.MemberAccess)
            {
                return(null);
            }
            if (mce.Object.NodeType != ExpressionType.MemberAccess)
            {
                return(null);
            }

            // get value
            object value = Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke();

            if (!(value is Int32) && !(value is String))
            {
                return(null);
            }

            // check mapping
            string field       = (mce.Object as MemberExpression).Member.Name;
            var    mappingList = mapping.Where(x => x.PropertyName == field);

            if (mappingList.Count() == 0)
            {
                return(null);
            }
            SQLFieldMapping thisMapping = mappingList.First();

            if (!Attribute.IsDefined(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute)))
            {
                return(null);
            }

            // build SQL
            AllowSQLCompareAttribute attr = (AllowSQLCompareAttribute)Attribute.GetCustomAttribute(thisMapping.Reader.Method, typeof(AllowSQLCompareAttribute));

            whereItems.Add(new Tuple <string, object>(attr.GetSQLCondition(thisMapping), value));
            return(this);
        }