Explorar o código

- [Vitorm] IEntityDescriptor, rename properties/allProperties to propertiesWithoutKey/properties
- [Vitorm] DbContext, change method Event_OnExecuting to virtual instance from static extension, and support Laze<executeString>

Lith hai 5 meses
pai
achega
5c3b480759

+ 6 - 0
doc/ReleaseNotes.md

@@ -1,5 +1,11 @@
 # Vitorm ReleaseNotes
 
+-----------------------
+# 2.3.0
+
+- [Vitorm] support nested entity and rename Column to Property
+- [Vitorm] IEntityDescriptor, rename properties/allProperties to propertiesWithoutKey/properties
+- [Vitorm] DbContext, change method Event_OnExecuting to virtual instance from static extension, and support Laze<executeString>
 
 -----------------------
 # 2.2.0

+ 1 - 1
doc/TODO.md

@@ -2,7 +2,7 @@
 
 
 # group then orderBy aggregate column
-> [QueryTranslator] not suported MethodCall: Sum
+> [QueryTranslator] not supported MethodCall: Sum
 ``` csharp
 using var dbContext = DataSource.CreateDbContext();
 var userQuery = dbContext.Query<User>();

+ 3 - 3
src/Vitorm.MySql/SqlTranslateService.cs

@@ -189,7 +189,7 @@ CREATE TABLE IF NOT EXISTS `User` (
                 sqlFields.Add(GetColumnSql(entityDescriptor.key));
 
             // #2 columns
-            entityDescriptor.properties?.ForEach(column => sqlFields.Add(GetColumnSql(column)));
+            entityDescriptor.propertiesWithoutKey?.ForEach(column => sqlFields.Add(GetColumnSql(column)));
 
             return $@"
 CREATE TABLE IF NOT EXISTS {DelimitTableName(entityDescriptor)} (
@@ -280,7 +280,7 @@ CREATE TABLE IF NOT EXISTS {DelimitTableName(entityDescriptor)} (
                 // insert into user(name,fatherId,motherId) values('',0,0); select last_insert_id();
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.propertiesWithoutKey);
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) values({string.Join(",", sqlColumnParams)});";
 
                 // get generated id
@@ -292,7 +292,7 @@ CREATE TABLE IF NOT EXISTS {DelimitTableName(entityDescriptor)} (
                 // insert into user(name,fatherId,motherId) values('',0,0);
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.allProperties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) values({string.Join(",", sqlColumnParams)});";
                 return (sql, GetSqlParams);
             }

+ 4 - 4
src/Vitorm.SqlServer/SqlTranslateService.cs

@@ -229,7 +229,7 @@ if object_id(N'[dbo].[User]', N'U') is null
                 sqlFields.Add(GetColumnSql(entityDescriptor.key));
 
             // #2 columns
-            entityDescriptor.properties?.ForEach(column => sqlFields.Add(GetColumnSql(column)));
+            entityDescriptor.propertiesWithoutKey?.ForEach(column => sqlFields.Add(GetColumnSql(column)));
 
             return $@"
 if object_id(N'{DelimitTableName(entityDescriptor)}', N'U') is null
@@ -320,7 +320,7 @@ create table {DelimitTableName(entityDescriptor)} (
             if (key == null) return EAddType.noKeyColumn;
 
             var keyValue = key.GetValue(entity);
-            var keyIsEmpty = keyValue is null || keyValue.Equals(TypeUtil.DefaultValue(arg.entityDescriptor.key.type));
+            var keyIsEmpty = keyValue is null || keyValue.Equals(TypeUtil.GetDefaultValue(arg.entityDescriptor.key.type));
 
             if (key.isIdentity)
             {
@@ -338,7 +338,7 @@ create table {DelimitTableName(entityDescriptor)} (
                 // insert into UserInfo(name) output inserted.guid values('dd');
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.propertiesWithoutKey);
                 var sqlOutput = "output inserted." + DelimitIdentifier(entityDescriptor.key.columnName);
 
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) {sqlOutput} values({string.Join(",", sqlColumnParams)});";
@@ -349,7 +349,7 @@ create table {DelimitTableName(entityDescriptor)} (
                 // insert into user(name,fatherId,motherId) values('',0,0);
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.allProperties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) values({string.Join(",", sqlColumnParams)});";
                 return (sql, GetSqlParams);
             }

+ 3 - 3
src/Vitorm.Sqlite/SqlTranslateService.cs

@@ -172,7 +172,7 @@ CREATE TABLE IF NOT EXISTS "User" (
                 sqlFields.Add(GetColumnSql(entityDescriptor.key));
 
             // #2 columns
-            entityDescriptor.properties?.ForEach(column => sqlFields.Add(GetColumnSql(column)));
+            entityDescriptor.propertiesWithoutKey?.ForEach(column => sqlFields.Add(GetColumnSql(column)));
 
             return $@"
 CREATE TABLE IF NOT EXISTS {DelimitTableName(entityDescriptor)} (
@@ -262,7 +262,7 @@ CREATE TABLE IF NOT EXISTS {DelimitTableName(entityDescriptor)} (
                 // insert into "user"(name,fatherId,motherId) values('lith',1,1); select last_insert_rowid();
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.propertiesWithoutKey);
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) values({string.Join(",", sqlColumnParams)});";
 
                 // get generated id
@@ -274,7 +274,7 @@ CREATE TABLE IF NOT EXISTS {DelimitTableName(entityDescriptor)} (
                 // insert into "user"(name,fatherId,motherId) values('lith',1,1);
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.allProperties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) values({string.Join(",", sqlColumnParams)});";
                 return (sql, GetSqlParams);
             }

+ 45 - 15
src/Vitorm/DbContext.Event.cs

@@ -7,19 +7,20 @@ namespace Vitorm
     {
         public static Action<ExecuteEventArgument> event_DefaultOnExecuting;
         public Action<ExecuteEventArgument> event_OnExecuting = event_DefaultOnExecuting;
-    }
 
-    public static class DbContext_Extensions_Event
-    {
-        public static void Event_OnExecuting(this DbContext dbContext, string executeString = null, object param = null)
+        public virtual void Event_OnExecuting(string executeString = null, object param = null)
         {
-            if (dbContext.event_OnExecuting != null)
-                dbContext.event_OnExecuting(new(dbContext, executeString, param));
+            event_OnExecuting?.Invoke(new(this, executeString, param));
         }
-        public static void Event_OnExecuting(this DbContext dbContext, ExecuteEventArgument arg)
+
+        public virtual void Event_OnExecuting(ExecuteEventArgument arg)
         {
-            if (dbContext.event_OnExecuting != null)
-                dbContext.event_OnExecuting(arg);
+            event_OnExecuting?.Invoke(arg);
+        }
+
+        public virtual void Event_OnExecuting(Lazy<ExecuteEventArgument> arg)
+        {
+            event_OnExecuting?.Invoke(new ExecuteEventArgument_Lazy(arg));
         }
     }
 
@@ -31,14 +32,17 @@ namespace Vitorm
             this.dbContext = dbContext;
             this.executeString = executeString;
             this.param = param;
-            this.extraParam = extraParam;
+            this._extraParam = extraParam;
         }
 
-        public DbContext dbContext;
-        public string executeString;
-        public object param;
-        public Dictionary<string, object> extraParam;
-        public object GetExtraParam(string key) => extraParam?.TryGetValue(key, out var value) == true ? value : null;
+
+        public virtual DbContext dbContext { get; protected set; }
+        public virtual string executeString { get; protected set; }
+        public virtual object param { get; protected set; }
+
+        protected Dictionary<string, object> _extraParam;
+        public virtual Dictionary<string, object> extraParam { get => _extraParam; set => _extraParam = value; }
+        public virtual object GetExtraParam(string key) => extraParam?.TryGetValue(key, out var value) == true ? value : null;
 
         public ExecuteEventArgument SetExtraParam(string key, object param)
         {
@@ -48,4 +52,30 @@ namespace Vitorm
         }
     }
 
+    public class ExecuteEventArgument_Lazy : ExecuteEventArgument
+    {
+        protected Lazy<ExecuteEventArgument> _lazyArg;
+
+        public ExecuteEventArgument_Lazy(Lazy<ExecuteEventArgument> lazyArg)
+        {
+            this._lazyArg = lazyArg;
+        }
+
+        public override DbContext dbContext => _lazyArg.Value?.dbContext;
+        public override string executeString => _lazyArg.Value?.executeString;
+        public override object param => _lazyArg.Value?.dbContext;
+        public override Dictionary<string, object> extraParam
+        {
+            get
+            {
+                return _lazyArg.Value?.extraParam;
+            }
+            set
+            {
+                _lazyArg.Value.extraParam = value;
+            }
+        }
+
+    }
+
 }

+ 5 - 3
src/Vitorm/Entity/EntityDescriptorWithAlias.cs

@@ -1,5 +1,7 @@
 using System;
 
+using Vitorm.Entity.PropertyType;
+
 namespace Vitorm.Entity
 {
     public partial class EntityDescriptorWithAlias : IEntityDescriptor
@@ -11,7 +13,7 @@ namespace Vitorm.Entity
             this.tableName = tableName;
         }
 
-
+        public IPropertyObjectType propertyType => originEntityDescriptor.propertyType;
         public Type entityType => originEntityDescriptor?.entityType;
         public string tableName { get; protected set; }
         public string schema => originEntityDescriptor?.schema;
@@ -29,10 +31,10 @@ namespace Vitorm.Entity
         /// <summary>
         /// not include primary key
         /// </summary>
-        public IPropertyDescriptor[] properties => originEntityDescriptor?.properties;
+        public IPropertyDescriptor[] propertiesWithoutKey => originEntityDescriptor?.propertiesWithoutKey;
 
 
-        public IPropertyDescriptor[] allProperties => originEntityDescriptor?.allProperties;
+        public IPropertyDescriptor[] properties => originEntityDescriptor?.properties;
 
 
     }

+ 1 - 1
src/Vitorm/Entity/Extensions/IEntityDescriptor_Extensions.Column.cs

@@ -14,7 +14,7 @@ namespace Vitorm
         /// <returns></returns>
         public static string GetColumnNameByPropertyName(this IEntityDescriptor data, string propertyName)
         {
-            return data?.allProperties.FirstOrDefault(m => m.propertyName == propertyName)?.columnName;
+            return data?.properties.FirstOrDefault(m => m.propertyName == propertyName)?.columnName;
         }
     }
 }

+ 5 - 2
src/Vitorm/Entity/IEntityDescriptor.cs

@@ -1,9 +1,12 @@
 using System;
 
+using Vitorm.Entity.PropertyType;
+
 namespace Vitorm.Entity
 {
     public interface IEntityDescriptor
     {
+        IPropertyObjectType propertyType { get; }
         Type entityType { get; }
         string schema { get; }
         string tableName { get; }
@@ -16,11 +19,11 @@ namespace Vitorm.Entity
         /// <summary>
         /// columns except primary key
         /// </summary>
-        public IPropertyDescriptor[] properties { get; }
+        public IPropertyDescriptor[] propertiesWithoutKey { get; }
 
         /// <summary>
         /// columns including primary key
         /// </summary>
-        public IPropertyDescriptor[] allProperties { get; }
+        public IPropertyDescriptor[] properties { get; }
     }
 }

+ 4 - 4
src/Vitorm/Entity/Loader/DataAnnotations/EntityDescriptor.cs

@@ -16,8 +16,8 @@ namespace Vitorm.Entity.Loader.DataAnnotations
             var allProperties = propertyType.properties;
 
             this.key = allProperties.FirstOrDefault(m => m.isKey);
-            this.properties = allProperties.Where(m => !m.isKey).OrderBy(col => col.columnOrder ?? int.MaxValue).ToArray();
-            this.allProperties = allProperties.OrderBy(col => col.columnOrder ?? int.MaxValue).ToArray();
+            this.propertiesWithoutKey = allProperties.Where(m => !m.isKey).OrderBy(col => col.columnOrder ?? int.MaxValue).ToArray();
+            this.properties = allProperties.OrderBy(col => col.columnOrder ?? int.MaxValue).ToArray();
         }
 
         public IPropertyObjectType propertyType { get; protected set; }
@@ -39,10 +39,10 @@ namespace Vitorm.Entity.Loader.DataAnnotations
         /// <summary>
         /// not include primary key
         /// </summary>
-        public IPropertyDescriptor[] properties { get; protected set; }
+        public IPropertyDescriptor[] propertiesWithoutKey { get; protected set; }
 
 
-        public IPropertyDescriptor[] allProperties { get; protected set; }
+        public IPropertyDescriptor[] properties { get; protected set; }
 
 
     }

+ 1 - 1
src/Vitorm/Entity/Loader/DataAnnotations/EntityLoader.cs

@@ -92,7 +92,7 @@ namespace Vitorm.Entity.Loader.DataAnnotations
                 typeCache[propertyClrType] = propertyType;
 
                 propertyType.elementPropertyType = ConvertToPropertyType(arrayElementType, typeCache);
-
+                return propertyType;
             }
 
             // object

+ 1 - 1
src/Vitorm/Sql/DataReader/EntityReader/CompiledLambda/ModelReader.cs

@@ -24,7 +24,7 @@ namespace Vitorm.Sql.DataReader.EntityReader.CompiledLambda
 
             this.entityType = entityDescriptor.entityType;
 
-            foreach (var column in entityDescriptor.allProperties)
+            foreach (var column in entityDescriptor.properties)
             {
                 var sqlColumnIndex = sqlColumns.AddSqlColumnAndGetIndex(sqlTranslateService, tableName, columnDescriptor: column);
 

+ 1 - 1
src/Vitorm/Sql/DataReader/EntityReader/EntityConstructor/EntityReader_.cs

@@ -16,7 +16,7 @@ namespace Vitorm.Sql.DataReader.EntityReader.EntityConstructor
         {
             this.entityType = entityDescriptor.entityType;
 
-            foreach (var column in entityDescriptor.allProperties)
+            foreach (var column in entityDescriptor.properties)
             {
                 var sqlColumnIndex = config.sqlColumns.AddSqlColumnAndGetIndex(config.sqlTranslateService, tableName, columnDescriptor: column);
 

+ 1 - 1
src/Vitorm/Sql/SqlDbSet.Async.cs

@@ -127,7 +127,7 @@ namespace Vitorm.Sql
             if (reader is DbDataReader dataReader ? await dataReader.ReadAsync() : reader.Read())
             {
                 var entity = (Entity)Activator.CreateInstance(entityDescriptor.entityType);
-                foreach (var column in entityDescriptor.allProperties)
+                foreach (var column in entityDescriptor.properties)
                 {
                     var value = TypeUtil.ConvertToType(reader[column.columnName], column.type);
                     if (value != null)

+ 1 - 1
src/Vitorm/Sql/SqlDbSet.cs

@@ -163,7 +163,7 @@ namespace Vitorm.Sql
             if (reader.Read())
             {
                 var entity = (Entity)Activator.CreateInstance(entityDescriptor.entityType);
-                foreach (var column in entityDescriptor.allProperties)
+                foreach (var column in entityDescriptor.properties)
                 {
                     var value = TypeUtil.ConvertToType(reader[column.columnName], column.type);
                     if (value != null)

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

@@ -314,7 +314,7 @@ namespace Vitorm.Sql.SqlTranslate
                                 }
 
                         }
-                        throw new NotSupportedException("[QueryTranslator] not suported MethodCall: " + methodCall.methodName);
+                        throw new NotSupportedException("[QueryTranslator] not supported MethodCall: " + methodCall.methodName);
                     }
 
                 #region Read Value
@@ -361,7 +361,7 @@ namespace Vitorm.Sql.SqlTranslate
                     }
                     #endregion
             }
-            throw new NotSupportedException("[QueryTranslator] not suported nodeType: " + node.nodeType);
+            throw new NotSupportedException("[QueryTranslator] not supported nodeType: " + node.nodeType);
         }
 
 
@@ -400,7 +400,7 @@ namespace Vitorm.Sql.SqlTranslate
         public static bool Entity_HasKeyValue(SqlTranslateArgument arg, object entity)
         {
             var keyValue = arg.entityDescriptor?.key.GetValue(entity);
-            return keyValue is not null && !keyValue.Equals(TypeUtil.DefaultValue(arg.entityDescriptor.key.type));
+            return keyValue is not null && !keyValue.Equals(TypeUtil.GetDefaultValue(arg.entityDescriptor.key.type));
         }
         public virtual bool HasKeyValue(SqlTranslateArgument arg, object entity) => hasKeyValue?.Invoke(arg, entity) == true;
         public virtual EAddType Entity_GetAddType(SqlTranslateArgument arg, object entity)
@@ -452,7 +452,7 @@ namespace Vitorm.Sql.SqlTranslate
                 // insert into user(name,fatherId,motherId) values('',0,0);
 
                 var entityDescriptor = arg.entityDescriptor;
-                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.allProperties);
+                var (columnNames, sqlColumnParams, GetSqlParams) = PrepareAdd_Columns(arg, entityDescriptor.properties);
                 string sql = $@"insert into {DelimitTableName(entityDescriptor)}({string.Join(",", columnNames)}) values({string.Join(",", sqlColumnParams)});";
                 return (sql, GetSqlParams);
             }
@@ -501,7 +501,7 @@ namespace Vitorm.Sql.SqlTranslate
             Dictionary<string, object> GetSqlParams(object entity)
             {
                 var sqlParam = new Dictionary<string, object>();
-                foreach (var column in entityDescriptor.allProperties)
+                foreach (var column in entityDescriptor.properties)
                 {
                     var columnName = column.columnName;
                     var value = column.GetValue(entity);
@@ -515,7 +515,7 @@ namespace Vitorm.Sql.SqlTranslate
             // #2 columns
             List<string> columnsToUpdate = new List<string>();
             string columnName;
-            foreach (var column in entityDescriptor.properties)
+            foreach (var column in entityDescriptor.propertiesWithoutKey)
             {
                 columnName = column.columnName;
                 columnsToUpdate.Add($"{DelimitIdentifier(columnName)}={GenerateParameterName(columnName)}");

+ 13 - 1
src/Vitorm/TypeUtil.cs

@@ -29,6 +29,18 @@ namespace Vitorm
         }
 
 
+        /// <summary>
+        /// is Array or List(SortedSet...)
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
+        public static bool IsArrayType(Type type)
+        {
+            if (type.IsArray) return true;
+            if (type.IsGenericType && typeof(ICollection).IsAssignableFrom(type)) return true;
+            return false;
+        }
+
         public static Type GetElementTypeFromArray(Type type)
         {
             if (type.IsArray) return type.GetElementType();
@@ -56,7 +68,7 @@ namespace Vitorm
         }
 
 
-        public static object DefaultValue(Type type)
+        public static object GetDefaultValue(Type type)
         {
             if (null == type || !type.IsValueType) return null;
             return Activator.CreateInstance(type);

+ 44 - 0
test/Vitorm.Sqlite.MsTest/CommonTest/Query_Where_In_Test.cs

@@ -0,0 +1,44 @@
+using System.Data;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using Vit.Linq;
+
+namespace Vitorm.MsTest.CommonTest
+{
+
+    [TestClass]
+    public class Query_Where_In_Test
+    {
+
+        [TestMethod]
+        public void Test_In()
+        {
+            using var dbContext = DataSource.CreateDbContext();
+            var userQuery = dbContext.Query<User>();
+
+            {
+                var ids = new[] { 1, 2 };
+                var userList = userQuery.Where(m => ids.Contains(m.id)).OrderBy(m => m.id).ToList();
+
+                var strIds = String.Join(',', userList.Select(m => m.id));
+                Assert.AreEqual(2, userList.Count);
+                Assert.AreEqual("1,2", strIds);
+            }
+
+
+            {
+                var ids = new List<int> { 1, 2 };
+                var userList = userQuery.Where(m => ids.Contains(m.id)).OrderBy(m => m.id).ToList();
+
+                var strIds = String.Join(',', userList.Select(m => m.id));
+                Assert.AreEqual(2, userList.Count);
+                Assert.AreEqual("1,2", strIds);
+            }
+
+        }
+
+
+
+    }
+}