|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Data.Entity.Core.Metadata.Edm; |
| 3 | +using System.Linq; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Data.Common; |
| 7 | +using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; |
| 8 | +using System.Diagnostics; |
| 9 | +#if ENTITIES6 |
| 10 | +using System.Globalization; |
| 11 | +using System.Data.Entity.Core.Common.CommandTrees; |
| 12 | +using System.Data.Entity.Core.Metadata.Edm; |
| 13 | +#else |
| 14 | +using System.Data.Common.CommandTrees; |
| 15 | +using System.Data.Metadata.Edm; |
| 16 | +#endif |
| 17 | +using JetBrains.Annotations; |
| 18 | + |
| 19 | + |
| 20 | +namespace Npgsql.SqlGenerators |
| 21 | +{ |
| 22 | + public class CaseIsNullToCoalesceReducer |
| 23 | + { |
| 24 | + public static DbFunctionExpression InvokeCoalesceExpression(params DbExpression[] argumentExpressions) |
| 25 | + { |
| 26 | + var fromClrType = PrimitiveType |
| 27 | + .GetEdmPrimitiveTypes() |
| 28 | + .FirstOrDefault(t => t.ClrEquivalentType == typeof(string)); |
| 29 | + |
| 30 | + int i=0; |
| 31 | + var func = EdmFunction.Create( |
| 32 | + "coalesce", |
| 33 | + "Npgsql", |
| 34 | + DataSpace.SSpace, |
| 35 | + new EdmFunctionPayload |
| 36 | + { |
| 37 | + ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion, |
| 38 | + Schema = string.Empty, |
| 39 | + IsBuiltIn = true, |
| 40 | + IsAggregate = false, |
| 41 | + IsFromProviderManifest = true, |
| 42 | + StoreFunctionName = "coalesce", |
| 43 | + IsComposable = true, |
| 44 | + ReturnParameters = new[] |
| 45 | + { |
| 46 | + FunctionParameter.Create("ReturnType", fromClrType,ParameterMode.ReturnValue) |
| 47 | + }, |
| 48 | + Parameters = argumentExpressions.Select( |
| 49 | + x => FunctionParameter.Create( |
| 50 | + "p" + (i++).ToString(),fromClrType,ParameterMode.In)).ToList() |
| 51 | + }, |
| 52 | + new List<MetadataProperty>()); |
| 53 | + |
| 54 | + return func.Invoke(argumentExpressions); |
| 55 | + } |
| 56 | + |
| 57 | + public static DbFunctionExpression UnnestCoalesceInvocations(DbFunctionExpression dbFunctionExpression) |
| 58 | + { |
| 59 | + var args = new List<DbExpression>(); |
| 60 | + foreach (var arg in dbFunctionExpression.Arguments) |
| 61 | + { |
| 62 | + if(arg is DbFunctionExpression funcCall |
| 63 | + && funcCall.Function.NamespaceName=="Npgsql" |
| 64 | + && funcCall.Function.Name=="coalesce") |
| 65 | + { |
| 66 | + args.AddRange(funcCall.Arguments); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + args.Add(arg); |
| 71 | + } |
| 72 | + } |
| 73 | + return InvokeCoalesceExpression(args.ToArray()); |
| 74 | + } |
| 75 | + |
| 76 | + public static DbExpression TransformCoalesce(DbExpression expression) |
| 77 | + { |
| 78 | + if (expression is DbCaseExpression case2) |
| 79 | + { |
| 80 | + return TransformCoalesce(case2); |
| 81 | + } |
| 82 | + |
| 83 | + if (expression is DbIsNullExpression nullExp) |
| 84 | + { |
| 85 | + return TransformCoalesce(nullExp.Argument).IsNull(); |
| 86 | + } |
| 87 | + return expression; |
| 88 | + } |
| 89 | + |
| 90 | + public static DbExpression TransformCoalesce(DbCaseExpression expression) |
| 91 | + { |
| 92 | + expression = DbExpressionBuilder.Case( |
| 93 | + expression.When.Select(TransformCoalesce), |
| 94 | + expression.Then.Select(TransformCoalesce), |
| 95 | + expression.Else); |
| 96 | + |
| 97 | + var lastWhen = expression.When.Count-1; |
| 98 | + if (expression.When[lastWhen].ExpressionKind == DbExpressionKind.IsNull) |
| 99 | + { |
| 100 | + var is_null = expression.When[lastWhen] as DbIsNullExpression; |
| 101 | + if (DbExpressionDeepEqual.DeepEqual(is_null.Argument,expression.Else)) |
| 102 | + { |
| 103 | + var coalesceInvocation = InvokeCoalesceExpression(is_null.Argument, expression.Then[lastWhen]); |
| 104 | + coalesceInvocation = UnnestCoalesceInvocations(coalesceInvocation); |
| 105 | + |
| 106 | + if (expression.When.Count == 1) |
| 107 | + { |
| 108 | + return coalesceInvocation; |
| 109 | + } |
| 110 | + |
| 111 | + var simplifiendCase = DbExpressionBuilder.Case( |
| 112 | + expression.When.Take(lastWhen), |
| 113 | + expression.Then.Take(lastWhen), |
| 114 | + coalesceInvocation); |
| 115 | + |
| 116 | + return TransformCoalesce(simplifiendCase); |
| 117 | + } |
| 118 | + return expression; |
| 119 | + } |
| 120 | + return expression; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments