Example #1
0
        private static WherePart MemberExpressionExtract(ref int i, Expression expression, bool isUnary, string prefix, string postfix, bool left)
        {
            var member = (MemberExpression)expression;

            if (isUnary && member.Type == typeof(bool))
            {
                return(WherePart.Concat(Recurse(ref i, expression), "=", WherePart.IsSql("1")));
            }

            if (member.Member is PropertyInfo && left)
            {
                var property  = (PropertyInfo)member.Member;
                var colName   = GetName <ColumnName>(property);
                var tableName = GetName <TableName>(property.DeclaringType.IsAbstract ? ((ParameterExpression)member.Expression).Type : property.DeclaringType);
                return(WherePart.IsSql($"[{tableName}].[{ colName }]"));
            }
            if (member.Member is FieldInfo || left == false)
            {
                var value = GetValue(member);
                if (value is string)
                {
                    value = prefix + (string)value + postfix;
                }
                return(WherePart.IsParameter(i++, value));
            }
            throw new Exception($"Expression does not refer to a property or field: {expression}");
        }
Example #2
0
        private static WherePart ConstantExpressionExtract(ref int i, Expression expression, bool isUnary, string prefix, string postfix, bool left)
        {
            var constant = (ConstantExpression)expression;
            var value    = constant.Value;

            if (value is null)
            {
                return(WherePart.IsSql("NULL"));
            }
            if (value is int)
            {
                return(WherePart.IsSql(value.ToString()));
            }
            if (value is string)
            {
                value = prefix + (string)value + postfix;
            }
            if (value is bool && !isUnary)
            {
                var result = ((bool)value) ? "1" : "0";
                if (left)
                {
                    result = result.Equals("1") ? "1=1" : "0=0";
                }
                return(WherePart.IsSql(result));
            }
            return(WherePart.IsParameter(i++, value));
        }
        private static WherePart ConstantExpressionExtract(ref int i, ConstantExpression expression, bool isUnary,
                                                           string prefix, string postfix, bool left)
        {
            var value = expression.Value;

            switch (value)
            {
            case null:
                return(WherePart.IsSql("NULL"));

            case int _:
                return(WherePart.IsSql(value.ToString()));

            case string text:
                value = prefix + text + postfix;
                break;
            }

            if (!(value is bool) || isUnary)
            {
                return(WherePart.IsParameter(i++, value));
            }

            var result = ((bool)value) ? "1" : "0";

            if (left)
            {
                result = result.Equals("1") ? "1=1" : "0=0";
            }
            return(WherePart.IsSql(result));
        }
Example #4
0
 public static WherePart Concat(WherePart left, string @operator, WherePart right)
 {
     return(new WherePart()
     {
         Parameters = left.Parameters.Union(right.Parameters).ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
         Sql = $"({left.Sql} {@operator} {right.Sql})"
     });
 }
Example #5
0
 public static WherePart Concat(string @operator, WherePart operand)
 {
     return(new WherePart()
     {
         Parameters = operand.Parameters,
         Sql = $"({@operator} {operand.Sql})"
     });
 }
Example #6
0
        public static WherePart Concat(WherePart left, string @operator, WherePart right)
        {
            if (right.Sql.Equals("NULL", StringComparison.InvariantCultureIgnoreCase))
            {
                @operator = @operator == "=" ? "IS" : "IS NOT";
            }

            return(new WherePart($"({left.Sql} {@operator} {right.Sql})", left.Parameters.Union(right.Parameters)));
        }
        private static WherePart MethodCallExpressionExtract <T>(ref int i, MethodCallExpression expression)
        {
            // LIKE queries:
            if (expression.Method == typeof(string).GetMethod("Contains", new[] { typeof(string) }))
            {
                return(WherePart.Concat(Recurse <T>(ref i, expression.Object), "LIKE",
                                        Recurse <T>(ref i, expression.Arguments[0], prefix: "%", postfix: "%")));
            }

            if (expression.Method == typeof(string).GetMethod("StartsWith", new[] { typeof(string) }))
            {
                return(WherePart.Concat(Recurse <T>(ref i, expression.Object), "LIKE",
                                        Recurse <T>(ref i, expression.Arguments[0], postfix: "%")));
            }

            if (expression.Method == typeof(string).GetMethod("EndsWith", new[] { typeof(string) }))
            {
                return(WherePart.Concat(Recurse <T>(ref i, expression.Object), "LIKE",
                                        Recurse <T>(ref i, expression.Arguments[0], prefix: "%")));
            }

            if (expression.Method == typeof(string).GetMethod("Equals", new[] { typeof(string) }))
            {
                return(WherePart.Concat(Recurse <T>(ref i, expression.Object), "=",
                                        Recurse <T>(ref i, expression.Arguments[0], left: false)));
            }

            // IN queries:
            if (expression.Method.Name == "Contains")
            {
                Expression collection;
                Expression property;
                if (expression.Method.IsDefined(typeof(ExtensionAttribute)) && expression.Arguments.Count == 2)
                {
                    collection = expression.Arguments[0];
                    property   = expression.Arguments[1];
                }
                else if (!expression.Method.IsDefined(typeof(ExtensionAttribute)) && expression.Arguments.Count == 1)
                {
                    collection = expression.Object;
                    property   = expression.Arguments[0];
                }
                else
                {
                    throw new Exception("Unsupported method call: " + expression.Method.Name);
                }

                var values = (IEnumerable)GetValue(collection);
                return(WherePart.Concat(Recurse <T>(ref i, property), "IN", WherePart.IsCollection(ref i, values)));
            }

            throw new Exception("Unsupported method call: " + expression.Method.Name);
        }
Example #8
0
        public static WherePart Concat(WherePart left, string @operator, WherePart right)
        {
            if (right.Sql.Equals("NULL", StringComparison.InvariantCultureIgnoreCase))
            {
                @operator = @operator == "=" ? "IS" : "IS NOT";
            }

            return(new WherePart()
            {
                Parameters = left.Parameters.Union(right.Parameters).ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                Sql = $"({left.Sql} {@operator} {right.Sql})"
            });
        }
        private static WherePart ConstantExpressionExtract(ref int i, Expression expression, bool isUnary, string prefix, string postfix)
        {
            var constant = (ConstantExpression)expression;
            var value    = constant.Value;

            if (value is int)
            {
                return(WherePart.IsSql(value.ToString()));
            }
            if (value is string)
            {
                value = prefix + (string)value + postfix;
            }
            if (value is bool && isUnary)
            {
                return(WherePart.Concat(WherePart.IsParameter(i++, value), "=", WherePart.IsSql("1")));
            }
            return(WherePart.IsParameter(i++, value));
        }
Example #10
0
        private static WherePart UnaryExpressionExtract(ref int i, Expression expression)
        {
            var unary = (UnaryExpression)expression;

            return(WherePart.Concat(NodeTypeToString(unary.NodeType), Recurse(ref i, unary.Operand, true)));
        }
Example #11
0
        private static WherePart BinaryExpressionExtract(ref int i, Expression expression)
        {
            var body = (BinaryExpression)expression;

            return(WherePart.Concat(Recurse(ref i, body.Left, left: true), NodeTypeToString(body.NodeType), Recurse(ref i, body.Right, left: false)));
        }
Example #12
0
 public static WherePart Concat(string @operator, WherePart operand)
 {
     return(new WherePart($"({@operator} {operand.Sql})", operand.Parameters));
 }
 private static WherePart UnaryExpressionExtract <T>(ref int i, UnaryExpression expression)
 {
     return(WherePart.Concat(NodeTypeToString(expression.NodeType), Recurse <T>(ref i, expression.Operand, true)));
 }
 private static WherePart BinaryExpressionExtract <T>(ref int i, BinaryExpression expression)
 {
     return(WherePart.Concat(Recurse <T>(ref i, expression.Left), NodeTypeToString(expression.NodeType),
                             Recurse <T>(ref i, expression.Right, left: false)));
 }