Skip to content

Commit

Permalink
Merge pull request #228 from xiangxiren/Issue219
Browse files Browse the repository at this point in the history
'修复使用StatementAttribute时,参数前缀未被正确替换'
  • Loading branch information
xiangxiren authored Apr 21, 2023
2 parents 1dce744 + 6c5aaee commit 5620943
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 76 deletions.
132 changes: 67 additions & 65 deletions src/SmartSql.DyRepository/EmitRepositoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -321,25 +321,27 @@ private Statement PreStatement(Type interfaceType, SqlMap sqlMap, MethodInfo met
StatementType = _statementAnalyzer.Analyse(statementAttr.Sql),
SqlTags = new List<ITag>
{
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<ResultCacheAttribute>();
Expand All @@ -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))
{
Expand Down Expand Up @@ -406,95 +407,95 @@ 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
{
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();
}
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
{
Expand Down
22 changes: 11 additions & 11 deletions src/SmartSql/Deserializer/EntityDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using SmartSql.Data;
using SmartSql.Exceptions;
using SmartSql.Reflection.TypeConstants;
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
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;

Expand Down Expand Up @@ -124,7 +123,7 @@ private Delegate CreateDeserialize<TResult>(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
Expand All @@ -133,7 +132,7 @@ private Delegate CreateDeserialize<TResult>(ExecutionContext executionContext)

#region New

ConstructorInfo resultCtor = null;
ConstructorInfo resultCtor;
if (constructorMap == null)
{
resultCtor = resultType.GetConstructor(
Expand Down Expand Up @@ -171,8 +170,7 @@ private Delegate CreateDeserialize<TResult>(ExecutionContext executionContext)
{
#region Ensure Property & TypeHanlder

if (!ResolveProperty<TResult>(resultMap, resultType, col.Value, out var propertyHolder)
)
if (!ResolveProperty<TResult>(resultMap, resultType, col.Value, out var propertyHolder))
{
continue;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -337,7 +337,7 @@ private void LoadPropertyValue(ILGenerator ilGen, ExecutionContext executionCont

#endregion

MethodInfo getValMethod = null;
MethodInfo getValMethod;
if (String.IsNullOrEmpty(typeHandler))
{
LoadTypeHandlerInvokeArgs(ilGen, propertyType);
Expand Down

0 comments on commit 5620943

Please sign in to comment.