コード例 #1
0
        static string ProcessCallExpression(ISqlDialect dialect, Expression rootExpression, MethodCallExpression e, IDictionary<string, object> parameters, Func<string> getParameterName)
        {
            var method = e.Method;
            string memberName = null;

            if (e.Object is ParameterExpression)
            {
                // This is used by DynamicSQLinqLambdaExpression
                memberName = "{FieldName}";
            }
            else if (e.Object is ConstantExpression)
            {
                throw new Exception("SqlExpressionCompiler.ProcessCallExpresion: ConstantExpression Unsupported");
            }
            else if (e.Object != null)
            {
                // Get Column Name to Use
                var member = (MemberInfo)((dynamic)e.Object).Member;
                memberName = GetMemberColumnName(member, dialect);
            }

            if (method.DeclaringType == typeof(Guid))
            {
                var parameterName = getParameterName();

                switch (method.Name.ToLower())
                {
                    case "newguid":
                        parameters.Add(parameterName, Guid.NewGuid());
                        return string.Format("{0}{1}", memberName, parameterName);

                    default:
                        throw new Exception("Unsupported Method Name (" + method.Name + ") on Guid object");
                }
            }
            else if (method.DeclaringType == typeof(string))
            {
                string parameterName = null;
                string secondParameterName = null;
                if (e.Arguments.Count > 0)
                {
                    parameterName = GetExpressionValue(dialect, rootExpression, e.Arguments[0], parameters, getParameterName);

                    if (e.Arguments.Count > 1)
                    {
                        secondParameterName = GetExpressionValue(dialect, rootExpression, e.Arguments[1], parameters, getParameterName);
                    }
                }

                switch (method.Name.ToLower())
                {
                    case "startswith":
                        parameters[parameterName] = parameters[parameterName].ToString() + "%";
                        return string.Format("{0} LIKE {1}", memberName, parameterName);

                    case "endswith":
                        parameters[parameterName] = "%" + parameters[parameterName].ToString();
                        return string.Format("{0} LIKE {1}", memberName, parameterName);

                    case "contains":
                        parameters[parameterName] = "%" + parameters[parameterName].ToString() + "%";
                        return string.Format("{0} LIKE {1}", memberName, parameterName);

                    case "toupper":
                        return dialect.ToUpper(memberName);

                    case "tolower":
                        return dialect.ToLower(memberName);

                    case "replace":
                        return string.Format("REPLACE({0}, {1}, {2})", memberName, parameterName, secondParameterName);

                    case "substring":
                        if (secondParameterName != null)
                        {
                            return string.Format("SUBSTR({0}, {1}, {2})", memberName, parameterName, secondParameterName);
                        }
                        else
                        {
                            return string.Format("SUBSTR({0}, {1})", memberName, parameterName);
                        }

                    case "indexof":
                        return dialect.IndexOf(memberName, parameterName);

                    case "trim":
                        return dialect.Trim(memberName);

                    default:
                        throw new Exception("Unsupported Method Name (" + method.Name + ") on String object");
                }
            }
            else
                throw new Exception("Unsupported Method Declaring Type (" + method.DeclaringType.Name + ")");
        }