Example #1
0
        public static string GetQueryFromExpression(ITranslator translator, Expression clause, Parameters parameters)
        {
            var members = GetMemberExpressions(translator, clause);

            if (!members.Any())
            {
                return(string.Empty);
            }

            var enumerator = members.GetEnumerator();

            return(GetString(translator, enumerator, parameters));
        }
Example #2
0
        public static string GetString(ITranslator translator, IEnumerator <Expression> iterator, Parameters parameters)
        {
            if (!iterator.MoveNext())
            {
                return(string.Empty);
            }

            var expression = iterator.Current;

            var binaryExpression = expression as BinaryExpression;

            if (binaryExpression != null)
            {
                return(BinaryExpressionString(translator, iterator, binaryExpression, parameters));
            }

            var memberExpression = expression as MemberExpression;

            if (memberExpression != null)
            {
                return(MemberExpressionString(translator, memberExpression, parameters));
            }

            var constantExpression = expression as ConstantExpression;

            if (constantExpression != null)
            {
                return(ConstantExpressionString(translator, constantExpression, parameters));
            }

            var methodCallExpression = expression as MethodCallExpression;

            if (methodCallExpression != null)
            {
                string constantValue;
                if (GetSqlExtensionMethodCallConstant(translator, methodCallExpression, parameters, out constantValue))
                {
                    return(constantValue);
                }
            }

            return(string.Empty);
        }
Example #3
0
        public static bool GetSqlExtensionMethodCallConstant(ITranslator translator, MethodCallExpression methodExpression, Parameters parameters, out string concat)
        {
            if (methodExpression.Method.Name == "Min" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("MIN(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");
                return(true);
            }

            if (methodExpression.Method.Name == "Max" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("MAX(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");
                return(true);
            }

            if (methodExpression.Method.Name == "Sum" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("SUM(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");
                return(true);
            }

            if (methodExpression.Method.Name == "As" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                var aliasFormat = translator.GetAliasFormat();
                var alias       = string.Format(aliasFormat,
                                                Convert.ToString(methodExpression.Arguments[1]).Replace("\"", "").Replace("'", ""));

                concat = string.Concat(GetStringForExpression(translator, methodExpression.Arguments[0], parameters), " AS ", alias);
                return(true);
            }

            if (methodExpression.Method.Name == "Between" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), " BETWEEN ",
                                       GetStringForExpression(translator, methodExpression.Arguments[1], parameters), " AND ",
                                       GetStringForExpression(translator, methodExpression.Arguments[2], parameters), ")");
                return(true);
            }

            if (methodExpression.Method.Name == "In" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), " IN (",
                                       GetStringForExpression(translator, methodExpression.Arguments[1], parameters), "))");

                return(true);
            }

            if (methodExpression.Method.Name == "Like" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), " LIKE ",
                                       GetStringForExpression(translator, methodExpression.Arguments[1], parameters), ")");

                return(true);
            }

            if (methodExpression.Method.Name == "TrimSql" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("LTRIM(RTRIM(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), "))");

                return(true);
            }

            if (methodExpression.Method.Name == "Length" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                var lengthName = translator.GetLengthFunctionName();

                concat = string.Concat(lengthName, "(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");

                return(true);
            }

            if (methodExpression.Method.Name == "Upper" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("UPPER(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");

                return(true);
            }

            if (methodExpression.Method.Name == "Lower" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("LOWER(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");

                return(true);
            }

            if (methodExpression.Method.Name == "IsNull" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                var isNullName = translator.GetIsNullFunctionName();

                concat = string.Concat(isNullName, "(",
                                       GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ",",
                                       GetStringForExpression(translator, methodExpression.Arguments[1], parameters), ")");

                return(true);
            }

            if (methodExpression.Method.Name == "Cast" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                var returnDbType = TableDefinition.GetTypeForProperty(methodExpression.Method.ReturnType);

                concat = string.Concat("CAST(",
                                       GetStringForExpression(translator, methodExpression.Arguments[0], parameters), " AS ",
                                       translator.GetTextFor(new FieldDefinition {
                    Type = returnDbType
                }, true), ")");

                return(true);
            }

            if (methodExpression.Method.Name == "Average" && methodExpression.Method.ReflectedType.Name == "SqlExtensions")
            {
                concat = string.Concat("AVG(", GetStringForExpression(translator, methodExpression.Arguments[0], parameters), ")");

                return(true);
            }

            concat = "";
            return(false);
        }
Example #4
0
        private static string GetStringForExpression(ITranslator translator, Expression expression, Parameters parameters)
        {
            var memberExpression = expression as MemberExpression;

            if (memberExpression != null)
            {
                var constantExpression = memberExpression.Expression as ConstantExpression;
                if (constantExpression != null)
                {
                    var container = constantExpression.Value;
                    var member    = memberExpression.Member;

                    var fieldInfo = member as FieldInfo;
                    if (fieldInfo != null)
                    {
                        var value = fieldInfo.GetValue(container);
                        return(GetStringForExpression(translator, Expression.Constant(value), parameters));
                    }

                    var propertyInfo = member as PropertyInfo;
                    if (propertyInfo != null)
                    {
                        var value = propertyInfo.GetValue(container, null);
                        return(GetStringForExpression(translator, Expression.Constant(value), parameters));
                    }
                }

                var tableDefinition = new TableDefinition(memberExpression.Member.DeclaringType);

                return(string.Concat(tableDefinition.Name, ".", memberExpression.Member.Name));
            }

            var constExpr = expression as ConstantExpression;

            if (constExpr != null)
            {
                var value = GetValueFrom(translator, constExpr.Type, constExpr.Value);
                var key   = parameters.Add(translator, value);
                return(key);
            }

            var methodExpression = expression as MethodCallExpression;

            if (methodExpression != null)
            {
                string concat;
                if (GetSqlExtensionMethodCallConstant(translator, methodExpression, parameters, out concat))
                {
                    return(concat);
                }
            }

            var newArrayExpression = expression as NewArrayExpression;

            if (newArrayExpression != null)
            {
                return(string.Join(",", newArrayExpression.Expressions.Select(ex => GetStringForExpression(translator, ex, parameters))));
            }

            return(string.Empty);
        }
Example #5
0
        public static string ConstantExpressionString(ITranslator translator, ConstantExpression constantExpression, Parameters parameters)
        {
            var value = GetValueFrom(translator, constantExpression.Type, constantExpression.Value);

            return(parameters.Add(translator, value));
        }
Example #6
0
        public static string BinaryExpressionString(ITranslator translator, IEnumerator <Expression> iterator, BinaryExpression binaryExpression, Parameters parameters)
        {
            var format = translator.GetFormatFor(binaryExpression.NodeType);

            var left  = string.Empty;
            var right = string.Empty;

            if (iterator.MoveNext())
            {
                left = GetQueryFromExpression(translator, iterator.Current, parameters);
            }

            if (iterator.MoveNext())
            {
                right = GetQueryFromExpression(translator, iterator.Current, parameters);
            }

            return(string.Format(format, left, right));
        }
Example #7
0
        public static string MemberExpressionString(ITranslator translator, MemberExpression memberExpression, Parameters parameters)
        {
            if (memberExpression.Expression is ConstantExpression)
            {
                return(GetStringForExpression(translator, memberExpression, parameters));
            }

            var tableDefinition = new TableDefinition(memberExpression.Member.DeclaringType);

            var member = (PropertyInfo)memberExpression.Member;
            var field  = tableDefinition.Fields.FirstOrDefault(f => f.PropertyInfo.Name == member.Name);

            if (field == null)
            {
                return(string.Concat(tableDefinition.Name, ".", member.Name));
            }

            return(string.Concat(tableDefinition.Name, ".", field));
        }