Преглед на файлове

support all ExpressionTree NodeType

Lith преди 10 месеца
родител
ревизия
89561e1781

+ 4 - 4
src/Vitorm/Sql/SqlTranslate/SqlTranslateService.cs

@@ -93,12 +93,12 @@ namespace Vitorm.Sql.SqlTranslate
         {
             switch (data.nodeType)
             {
-                case NodeType.And:
-                    ExpressionNode_And and = data;
+                case NodeType.AndAlso:
+                    ExpressionNode_AndAlso and = data;
                     return $"({EvalExpression(arg, and.left)}) and ({EvalExpression(arg, and.right)})";
 
-                case NodeType.Or:
-                    ExpressionNode_Or or = data;
+                case NodeType.OrElse:
+                    ExpressionNode_OrElse or = data;
                     return $"({EvalExpression(arg, or.left)}) or ({EvalExpression(arg, or.right)})";
 
                 case NodeType.Not:

+ 1 - 1
src/Vitorm/StreamQuery/StreamReader.Join.cs

@@ -85,7 +85,7 @@ namespace Vitorm.StreamQuery
                         var curWhere = ExpressionNode.Binary(NodeType.Equal, leftKey.value, rightKey.value);
 
                         if (on == null) on = curWhere;
-                        else on = ExpressionNode.Binary(NodeType.And, on, curWhere);
+                        else on = ExpressionNode.Binary(NodeType.AndAlso, on, curWhere);
                     });
                 }
                 else

+ 2 - 2
src/Vitorm/StreamQuery/StreamReader.cs

@@ -167,7 +167,7 @@ namespace Vitorm.StreamQuery
                         }
                         else
                         {
-                            groupedStream.having = ExpressionNode.And(groupedStream.having, having);
+                            groupedStream.having = ExpressionNode.AndAlso(groupedStream.having, having);
                         }
                         return groupedStream;
                     }
@@ -187,7 +187,7 @@ namespace Vitorm.StreamQuery
                         }
                         else
                         {
-                            combinedStream.where = ExpressionNode.And(combinedStream.where, where);
+                            combinedStream.where = ExpressionNode.AndAlso(combinedStream.where, where);
                         }
                         return combinedStream;
                     }

+ 2 - 2
test/Vitorm.MySql.MsTest/DataSource.cs

@@ -42,11 +42,11 @@ namespace Vitorm.MsTest
 
             dbContext.BeginTransaction();
 
-            var userSet = dbContext.DbSet<User>();
+            var dbSet = dbContext.DbSet<User>();
 
             dbContext.Execute(sql: "DROP TABLE  if exists `User`;");
 
-            userSet.Create();
+            dbSet.Create();
 
             var users = new List<User> {
                     new User {   name="u146", fatherId=4, motherId=6 },

+ 2 - 2
test/Vitorm.SqlServer.MsTest/DataSource.cs

@@ -47,11 +47,11 @@ namespace Vitorm.MsTest
 
             dbContext.BeginTransaction();
 
-            var userSet = dbContext.DbSet<User>();
+            var dbSet = dbContext.DbSet<User>();
 
             dbContext.Execute(sql: "IF OBJECT_ID(N'User', N'U') IS  NOT  NULL \r\nDROP TABLE [User];");
 
-            userSet.Create();
+            dbSet.Create();
 
             var users = new List<User> {
                     new User {   name="u146", fatherId=4, motherId=6 },

+ 34 - 0
test/Vitorm.Sqlite.MsTest/CustomTest/ExpressionTreeTest/ExpressionTester.Model.cs

@@ -0,0 +1,34 @@
+namespace Vit.Linq.ExpressionTree.ExpressionTreeTest
+{
+
+    public abstract partial class ExpressionTester
+    {
+        [System.ComponentModel.DataAnnotations.Schema.Table("User2")]
+        public class User : Vitorm.MsTest.User
+        {
+        }
+
+
+        public static List<User> GetSourceData()
+        {
+            int count = 1000;
+
+            var Now = DateTime.Now;
+            var list = new List<User>(count);
+            for (int i = 1; i < count; i++)
+            {
+                list.Add(new User
+                {
+                    id = i,
+                    name = "name" + i,
+                    birth = Now.AddSeconds(i),
+                    fatherId = i >= 2 ? i >> 1 : null,
+                    motherId = i >= 2 ? (i >> 1) + 1 : null,
+                });
+            }
+            return list;
+        }
+
+
+    }
+}

+ 188 - 0
test/Vitorm.Sqlite.MsTest/CustomTest/ExpressionTreeTest/ExpressionTester.cs

@@ -0,0 +1,188 @@
+using System.Linq.Expressions;
+using Vit.Extensions.Linq_Extensions;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Vit.Linq.ExpressionTree.ExpressionTreeTest
+{
+
+    public abstract partial class ExpressionTester
+    {
+
+        public static List<User> Test(IQueryable<User> query, Expression<Func<User, bool>> predicate)
+        {
+            var expected = GetSourceData().AsQueryable().Where(predicate).ToList();
+            {
+                var actual = query.Where(predicate).ToList();
+                Check(expected, actual);
+                return actual;
+            }
+
+            void Check(List<User> expected, List<User> actual)
+            {
+                Assert.AreEqual(expected.Count, actual.Count);
+                for (var t = 0; t < expected.Count; t++)
+                {
+                    Assert.AreEqual(expected[t].id, actual[t].id);
+                }
+            }
+        }
+
+
+
+        public static void TestQueryable(IQueryable<User> query)
+        {
+            Expression<Func<User, bool>> predicate;
+
+            #region #0 Add, An addition operation, such as a + b, without overflow checking, for numeric operands.
+            {
+                predicate = u => u.id + 1 == 6;
+                var rows = Test(query, predicate);
+                Assert.AreEqual(5, rows[0].id);
+            }
+            #endregion
+
+            #region #3 AndAlso, A conditional AND operation that evaluates the second operand only if the first operand evaluates to true. It corresponds to (a && b)
+            {
+                predicate = u => u.id > 5 && u.id < 7;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #7 Coalesce, A node that represents a null coalescing operation, such as (a ?? b)
+            {
+                predicate = u => (u.fatherId ?? u.id) == 5;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #8 Conditional, A conditional operation, such as a > b ? a : b 
+            {
+                predicate = u => (u.id == 2 ? 1 : 0) == 1;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #9 Constant, A constant value.
+            {
+                predicate = u => u.id == 2;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #10 Convert, A cast or conversion operation, such as (SampleType)obj
+            {
+                predicate = u => ((double)u.id) <= 2.0;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #12 Divide, A division operation, such as (a / b), for numeric operands.
+            {
+                predicate = u => u.id / 10 == 10;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #13 Equal, A node that represents an equality comparison, such as (a == b) 
+            {
+                predicate = u => u.id == 2;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #15 GreaterThan, A "greater than" comparison, such as (a > b).
+            {
+                predicate = u => u.id > 5;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #16 GreaterThanOrEqual, A "greater than or equal to" comparison, such as (a >= b).
+            {
+                predicate = u => u.id >= 5;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #20 LessThan, A "less than" comparison, such as (a < b).
+            {
+                predicate = u => u.id < 5;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #21 LessThanOrEqual, A "less than or equal to" comparison, such as (a <= b).
+            {
+                predicate = u => u.id <= 5;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #23 MemberAccess, An operation that reads from a field or property, such as obj.SampleProperty.
+            {
+                predicate = u => u.id == 5;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #25 Modulo, An arithmetic remainder operation, such as (a % b)
+            {
+                predicate = u => (u.id % 10) == 0;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #26 Multiply, A multiplication operation, such as (a * b), without overflow checking, for numeric operands
+            {
+                predicate = u => u.id * 10 == 20;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #28 Negate, An arithmetic negation operation, such as (-a). The object a should not be modified in place.
+            {
+                predicate = u => -u.id == -2;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #34 Not, A bitwise complement or logical negation operation. It is equivalent to  (~a) for integral types and to (!a) for Boolean values.
+            {
+                predicate = u => !(u.id == 2);
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #35 NotEqual,  An inequality comparison, such as (a != b)
+            {
+                predicate = u => u.id != 2;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #37 OrElse,  A short-circuiting conditional OR operation, such as (a || b)
+            {
+                predicate = u => u.id == 2 || u.id == 3;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #39 Power,  A mathematical operation that raises a number to a power, such as (a ^ b)
+            {
+                predicate = u => (u.id ^ 2) == 9;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+            #region #42 Subtract,  A subtraction operation, such as (a - b), without overflow checking
+            {
+                predicate = u => u.id - 2 == 9;
+                var rows = Test(query, predicate);
+            }
+            #endregion
+
+        }
+
+
+    }
+}

+ 25 - 0
test/Vitorm.Sqlite.MsTest/CustomTest/ExpressionTreeTest/Query_Test.cs

@@ -0,0 +1,25 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using Vit.Linq.ExpressionTree.ExpressionTreeTest;
+
+namespace Vitorm.MsTest.CustomTest
+{
+    [TestClass]
+    public class Query_Test
+    {
+        [TestMethod]
+        public void TestQueryable()
+        {
+            var initUsers = ExpressionTester.GetSourceData();
+
+            using var dbContext = DataSource.CreateDbContext();
+            var dbSet = dbContext.DbSet<ExpressionTester.User>();
+
+            dbSet.Create();
+            dbSet.AddRange(initUsers);
+
+            var query = dbSet.Query();
+            ExpressionTester.TestQueryable(query);
+        }
+    }
+}

+ 2 - 2
test/Vitorm.Sqlite.MsTest/DataSource.cs

@@ -45,8 +45,8 @@ namespace Vitorm.MsTest
 
             dbContext.BeginTransaction();
 
-            var userSet = dbContext.DbSet<User>();
-            userSet.Create();
+            var dbSet = dbContext.DbSet<User>();
+            dbSet.Create();
 
             var users = new List<User> {
                     new User { id=1, name="u146", fatherId=4, motherId=6 },