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); }
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); }