Parcourir la source

added limited stored proc support

tags/v0.1.0
Robin Thoni il y a 8 ans
Parent
révision
68b554f446

+ 0
- 2
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs Voir le fichier

@@ -224,8 +224,6 @@ namespace Luticate2.Utils.DataAccess
224 224
             return Execute(() =>
225 225
             {
226 226
                 var queryable = GetFilterQuery(filter);
227
-                var v = new ExpressionVisitorDebugger();
228
-                v.Visit(queryable.Expression);
229 227
                 var count = queryable.Count();
230 228
                 IOrderedQueryable<TModel> ordered = null;
231 229
                 foreach (var field in orderBy.Fields)

+ 0
- 17
Luticate2.Utils/DataAccess/Npgsql/ExpressionVisitorDebugger.cs Voir le fichier

@@ -1,17 +0,0 @@
1
-using System.Linq.Expressions;
2
-
3
-namespace Luticate2.Utils.DataAccess.Npgsql
4
-{
5
-    public class ExpressionVisitorDebugger : ExpressionVisitor
6
-    {
7
-        protected override Expression VisitUnary(UnaryExpression node)
8
-        {
9
-            return base.VisitUnary(node);
10
-        }
11
-
12
-        protected override Expression VisitBinary(BinaryExpression node)
13
-        {
14
-            return base.VisitBinary(node);
15
-        }
16
-    }
17
-}

+ 2
- 1
Luticate2.Utils/DataAccess/Npgsql/NpgsqlCompositeMethodCallTranslatorLuticate.cs Voir le fichier

@@ -8,7 +8,8 @@ namespace Luticate2.Utils.DataAccess.Npgsql
8 8
     {
9 9
         private static readonly IMethodCallTranslator[] _methodCallTranslators2 =
10 10
         {
11
-            new NpgsqlIntToStringTranslator()
11
+            new NpgsqlIntToStringTranslator(),
12
+            new NpgsqlStoredProcTranslator()
12 13
         };
13 14
 
14 15
         public NpgsqlCompositeMethodCallTranslatorLuticate(ILogger<NpgsqlCompositeMethodCallTranslator> logger) : base(logger)

+ 3
- 2
Luticate2.Utils/DataAccess/Npgsql/NpgsqlIntToStringTranslator.cs Voir le fichier

@@ -11,7 +11,7 @@ namespace Luticate2.Utils.DataAccess.Npgsql
11 11
     {
12 12
         private readonly Type[] _declaringTypes = {typeof(int), typeof(int?)};
13 13
 
14
-        private readonly string _clrMethodName = "ToString";
14
+        private readonly string _clrMethodName = nameof(ToString);
15 15
 
16 16
         public Expression Translate(MethodCallExpression methodCallExpression)
17 17
         {
@@ -19,7 +19,8 @@ namespace Luticate2.Utils.DataAccess.Npgsql
19 19
             {
20 20
                 var methodInfo = declaringType.GetTypeInfo()
21 21
                     .GetDeclaredMethods(_clrMethodName).SingleOrDefault(m => !m.GetParameters().Any());
22
-                if (methodInfo != null && declaringType.IsAssignableFrom(methodCallExpression.Object.Type))
22
+                if (methodInfo != null && methodCallExpression.Method.Name == _clrMethodName
23
+                    && declaringType.IsAssignableFrom(methodCallExpression.Object.Type))
23 24
                 {
24 25
                     return new ExplicitCastExpression(methodCallExpression.Object, typeof(string));
25 26
                 }

+ 23
- 0
Luticate2.Utils/DataAccess/Npgsql/NpgsqlStoredProcTranslator.cs Voir le fichier

@@ -0,0 +1,23 @@
1
+using System;
2
+using System.Linq.Expressions;
3
+using System.Reflection;
4
+using Microsoft.EntityFrameworkCore;
5
+using Microsoft.EntityFrameworkCore.Query.Expressions;
6
+using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
7
+
8
+namespace Luticate2.Utils.DataAccess.Npgsql
9
+{
10
+    public class NpgsqlStoredProcTranslator : IMethodCallTranslator
11
+    {
12
+        private readonly Type _declaringType = typeof(DbContext);
13
+
14
+        public Expression Translate(MethodCallExpression methodCallExpression)
15
+        {
16
+            if (_declaringType.IsAssignableFrom(methodCallExpression.Method.DeclaringType))
17
+            {
18
+                return new SqlFunctionExpression(methodCallExpression.Method.Name, methodCallExpression.Type, methodCallExpression.Arguments);
19
+            }
20
+            return null;
21
+        }
22
+    }
23
+}

+ 10
- 3
Test/Utils/DataAccess/LuUtilsPartialContext.cs Voir le fichier

@@ -1,11 +1,18 @@
1 1
 using System;
2
-using Microsoft.EntityFrameworkCore;
3
-using Luticate2.Auth.DataAccess.Models;
4
-using Test.Utils.DataAccess.Models;
2
+using System.Linq;
5 3
 
6 4
 namespace Test.Utils.DataAccess
7 5
 {
8 6
     public partial class LuUtilsDbContext
9 7
     {
8
+        public static string unaccent(string str)
9
+        {
10
+            throw new NotImplementedException();
11
+        }
12
+
13
+        public static bool lu_texts_match(string query, string data)
14
+        {
15
+            throw new NotImplementedException();
16
+        }
10 17
     }
11 18
 }

+ 2
- 2
Test/Utils/DataAccess/LuUtilsPkGuidDataAccess.cs Voir le fichier

@@ -2,6 +2,7 @@
2 2
 using System.Linq.Expressions;
3 3
 using Luticate2.Utils.DataAccess;
4 4
 using Luticate2.Utils.Dbo.Filter;
5
+using Luticate2.Utils.Utils;
5 6
 using Test.Utils.DataAccess.Models;
6 7
 using Test.Utils.Dbo.PkGuid;
7 8
 
@@ -38,8 +39,7 @@ namespace Test.Utils.DataAccess
38 39
 
39 40
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
40 41
         {
41
-            var query = filter.Query.ToLower();
42
-            return model => model.some_text.ToLower().Contains(query) || model.some_int.ToString().Contains(query);
42
+            return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
43 43
         }
44 44
     }
45 45
 }

Chargement…
Annuler
Enregistrer