From 6c5aaeeac222601eebe8af62148b95a4cd4aef90 Mon Sep 17 00:00:00 2001 From: xiangxiren Date: Wed, 4 Jan 2023 11:26:20 +0800 Subject: [PATCH] =?UTF-8?q?'=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8StatementA?= =?UTF-8?q?ttribute=E6=97=B6=EF=BC=8C=E5=8F=82=E6=95=B0=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E6=9C=AA=E8=A2=AB=E6=AD=A3=E7=A1=AE=E6=9B=BF=E6=8D=A2'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EmitRepositoryBuilder.cs | 132 +++++++++--------- .../Deserializer/EntityDeserializer.cs | 22 +-- 2 files changed, 78 insertions(+), 76 deletions(-) diff --git a/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs b/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs index 5471066a..cb092fe8 100644 --- a/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs +++ b/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs @@ -53,7 +53,7 @@ private void InitAssembly() private void EmitBuildCtor(string scope, TypeBuilder typeBuilder, FieldBuilder sqlMapperField, FieldBuilder scopeField) { - var paramTypes = new Type[] {ISqlMapperType.Type}; + var paramTypes = new Type[] { ISqlMapperType.Type }; var ctorBuilder = typeBuilder.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, paramTypes); var ilGen = ctorBuilder.GetILGenerator(); @@ -321,25 +321,27 @@ private Statement PreStatement(Type interfaceType, SqlMap sqlMap, MethodInfo met StatementType = _statementAnalyzer.Analyse(statementAttr.Sql), SqlTags = new List { - new SqlText(statementAttr.Sql, sqlMap.SmartSqlConfig.Database.DbProvider.ParameterPrefix) + new SqlText( + statementAttr.Sql.Replace(sqlMap.SmartSqlConfig.Settings.ParameterPrefix, + sqlMap.SmartSqlConfig.Database.DbProvider.ParameterPrefix), + sqlMap.SmartSqlConfig.Database.DbProvider.ParameterPrefix) }, CommandType = statementAttr.CommandType, EnablePropertyChangedTrack = statementAttr.EnablePropertyChangedTrack, ReadDb = statementAttr.ReadDb }; + if (statementAttr.CommandTimeout > 0) { statement.CommandTimeout = statementAttr.CommandTimeout; } - if (statementAttr.SourceChoice != DataSourceChoice.Unknow) { statement.SourceChoice = statementAttr.SourceChoice; } sqlMap.Statements.Add(statement.FullSqlId, statement); - } var resultCacheAttr = methodInfo.GetCustomAttribute(); @@ -349,7 +351,6 @@ private Statement PreStatement(Type interfaceType, SqlMap sqlMap, MethodInfo met statement.Cache = sqlMap.GetCache(statement.CacheId); } - returnType = isTaskReturnType ? returnType.GetGenericArguments().FirstOrDefault() : returnType; if (returnType == typeof(DataTable)) { @@ -406,47 +407,47 @@ private MethodInfo PreExecuteMethod(ExecuteBehavior executeBehavior, Type return switch (executeBehavior) { case ExecuteBehavior.Execute: - { - executeMethod = ISqlMapperType.Method.ExecuteAsync; - break; - } + { + executeMethod = ISqlMapperType.Method.ExecuteAsync; + break; + } case ExecuteBehavior.ExecuteScalar: - { - executeMethod = ISqlMapperType.Method.ExecuteScalarAsync.MakeGenericMethod(realReturnType); - break; - } + { + executeMethod = ISqlMapperType.Method.ExecuteScalarAsync.MakeGenericMethod(realReturnType); + break; + } case ExecuteBehavior.QuerySingle: - { - executeMethod = ISqlMapperType.Method.QuerySingleAsync.MakeGenericMethod(realReturnType); - break; - } + { + executeMethod = ISqlMapperType.Method.QuerySingleAsync.MakeGenericMethod(realReturnType); + break; + } case ExecuteBehavior.Query: - { - var method = ISqlMapperType.Method.QueryAsync; - var enumerableType = realReturnType.GenericTypeArguments[0]; - executeMethod = method.MakeGenericMethod(enumerableType); - break; - } + { + var method = ISqlMapperType.Method.QueryAsync; + var enumerableType = realReturnType.GenericTypeArguments[0]; + executeMethod = method.MakeGenericMethod(enumerableType); + break; + } case ExecuteBehavior.GetDataTable: - { - executeMethod = ISqlMapperType.Method.GetDataTableAsync; - break; - } + { + executeMethod = ISqlMapperType.Method.GetDataTableAsync; + break; + } case ExecuteBehavior.GetDataSet: - { - executeMethod = ISqlMapperType.Method.GetDataSetAsync; - break; - } + { + executeMethod = ISqlMapperType.Method.GetDataSetAsync; + break; + } default: - { - throw new ArgumentException(); - } + { + throw new ArgumentException(); + } } } else @@ -454,47 +455,47 @@ private MethodInfo PreExecuteMethod(ExecuteBehavior executeBehavior, Type return switch (executeBehavior) { case ExecuteBehavior.Execute: - { - executeMethod = ISqlMapperType.Method.Execute; - break; - } + { + executeMethod = ISqlMapperType.Method.Execute; + break; + } case ExecuteBehavior.ExecuteScalar: - { - executeMethod = ISqlMapperType.Method.ExecuteScalar.MakeGenericMethod(returnType); - break; - } + { + executeMethod = ISqlMapperType.Method.ExecuteScalar.MakeGenericMethod(returnType); + break; + } case ExecuteBehavior.QuerySingle: - { - executeMethod = ISqlMapperType.Method.QuerySingle.MakeGenericMethod(returnType); - break; - } + { + executeMethod = ISqlMapperType.Method.QuerySingle.MakeGenericMethod(returnType); + break; + } case ExecuteBehavior.Query: - { - var method = ISqlMapperType.Method.Query; - var enumerableType = returnType.GenericTypeArguments[0]; - executeMethod = method.MakeGenericMethod(new Type[] {enumerableType}); - break; - } + { + var method = ISqlMapperType.Method.Query; + var enumerableType = returnType.GenericTypeArguments[0]; + executeMethod = method.MakeGenericMethod(new Type[] { enumerableType }); + break; + } case ExecuteBehavior.GetDataTable: - { - executeMethod = ISqlMapperType.Method.GetDataTable; - break; - } + { + executeMethod = ISqlMapperType.Method.GetDataTable; + break; + } case ExecuteBehavior.GetDataSet: - { - executeMethod = ISqlMapperType.Method.GetDataSet; - break; - } + { + executeMethod = ISqlMapperType.Method.GetDataSet; + break; + } default: - { - throw new ArgumentException(); - } + { + throw new ArgumentException(); + } } } @@ -547,7 +548,7 @@ private void EmitSetTransaction(ILGenerator ilGen, MethodInfo methodInfo) var transactionLevel = attrType.GetProperty("Level")?.GetValue(transactionAttribute); if (transactionLevel != null) { - isolationLevel = (IsolationLevel) transactionLevel; + isolationLevel = (IsolationLevel)transactionLevel; } break; @@ -654,7 +655,8 @@ private void BuildCache(SqlMap sqlMap, Type interfaceType) { Configuration.Cache cache = new Configuration.Cache { - FlushInterval = new FlushInterval(), Id = ParseCacheFullId(sqlMap.Scope, cacheAttribute.Id) + FlushInterval = new FlushInterval(), + Id = ParseCacheFullId(sqlMap.Scope, cacheAttribute.Id) }; if (sqlMap.Caches.ContainsKey(cache.Id)) { diff --git a/src/SmartSql/Deserializer/EntityDeserializer.cs b/src/SmartSql/Deserializer/EntityDeserializer.cs index 9ad8f151..f06b3407 100644 --- a/src/SmartSql/Deserializer/EntityDeserializer.cs +++ b/src/SmartSql/Deserializer/EntityDeserializer.cs @@ -1,7 +1,4 @@ -using SmartSql.Data; -using SmartSql.Exceptions; -using SmartSql.Reflection.TypeConstants; -using System; +using System; using System.Collections.Generic; using System.Data; using System.Linq; @@ -9,11 +6,13 @@ using System.Reflection.Emit; using System.Threading.Tasks; using Microsoft.Extensions.Logging; -using SmartSql.Annotations; using SmartSql.Configuration; using SmartSql.CUD; +using SmartSql.Data; +using SmartSql.Exceptions; using SmartSql.Reflection; using SmartSql.Reflection.EntityProxy; +using SmartSql.Reflection.TypeConstants; using SmartSql.TypeHandlers; using SmartSql.Utils; @@ -124,7 +123,7 @@ private Delegate CreateDeserialize(ExecutionContext executionContext) .ToDictionary(col => col.ColumnName); var deserFunc = new DynamicMethod("Deserialize" + Guid.NewGuid().ToString("N"), resultType, - new[] {DataType.DataReaderWrapper, RequestContextType.AbstractType}, resultType, true); + new[] { DataType.DataReaderWrapper, RequestContextType.AbstractType }, resultType, true); var ilGen = deserFunc.GetILGenerator(); ilGen.DeclareLocal(resultType); // return value ilGen.DeclareLocal(CommonType.Int32); // current column index @@ -133,7 +132,7 @@ private Delegate CreateDeserialize(ExecutionContext executionContext) #region New - ConstructorInfo resultCtor = null; + ConstructorInfo resultCtor; if (constructorMap == null) { resultCtor = resultType.GetConstructor( @@ -171,8 +170,7 @@ private Delegate CreateDeserialize(ExecutionContext executionContext) { #region Ensure Property & TypeHanlder - if (!ResolveProperty(resultMap, resultType, col.Value, out var propertyHolder) - ) + if (!ResolveProperty(resultMap, resultType, col.Value, out var propertyHolder)) { continue; } @@ -287,9 +285,11 @@ ColumnDescriptor columnDescriptor { if (resultMap.Properties.TryGetValue(columnDescriptor.ColumnName, out var resultProperty)) { + var property = resultType.GetProperty(resultProperty.Name) ?? + throw new SmartSqlException($"ResultMap:[{resultMap.Id}], can not find property:[{resultProperty.Name}] in class:[{resultType.Name}]"); propertyHolder = new PropertyHolder { - Property = resultType.GetProperty(resultProperty.Name), + Property = property, TypeHandler = resultProperty.TypeHandler }; return true; @@ -337,7 +337,7 @@ private void LoadPropertyValue(ILGenerator ilGen, ExecutionContext executionCont #endregion - MethodInfo getValMethod = null; + MethodInfo getValMethod; if (String.IsNullOrEmpty(typeHandler)) { LoadTypeHandlerInvokeArgs(ilGen, propertyType);