mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IAdo.GetDbParamtersByObject 方法获取 DbParameter[];
This commit is contained in:
@ -2467,6 +2467,13 @@
|
||||
当前线程的事务
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.GetDbParamtersByObject(System.Object)">
|
||||
<summary>
|
||||
将 new { id = 1 } 或者 Dictionary<string, object> 转换为 DbParameter[]
|
||||
</summary>
|
||||
<param name="obj">new { id = 1 } 或者 Dictionary<string, object></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteReader(System.Action{FreeSql.Internal.Model.FetchCallbackArgs{System.Data.Common.DbDataReader}},System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
|
||||
|
@ -55,6 +55,13 @@ namespace FreeSql
|
||||
DbTransaction TransactionCurrentThread { get; }
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 将 new { id = 1 } 或者 Dictionary<string, object> 转换为 DbParameter[]
|
||||
/// </summary>
|
||||
/// <param name="obj">new { id = 1 } 或者 Dictionary<string, object></param>
|
||||
/// <returns></returns>
|
||||
DbParameter[] GetDbParamtersByObject(object obj);
|
||||
|
||||
/// <summary>
|
||||
/// 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
|
||||
/// </summary>
|
||||
|
@ -18,6 +18,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
protected abstract void ReturnConnection(IObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex);
|
||||
protected abstract DbCommand CreateCommand();
|
||||
protected abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
|
||||
public DbParameter[] GetDbParamtersByObject(object obj) => GetDbParamtersByObject("*", obj);
|
||||
|
||||
protected bool IsTracePerformance => _util?._orm?.Aop.CommandAfterHandler != null;
|
||||
|
||||
|
@ -1218,6 +1218,7 @@ namespace FreeSql.Internal
|
||||
public static T[] GetDbParamtersByObject<T>(string sql, object obj, string paramPrefix, Func<string, Type, object, T> constructorParamter)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql) || obj == null) return new T[0];
|
||||
var isCheckSql = sql != "*";
|
||||
var ttype = typeof(T);
|
||||
var type = obj.GetType();
|
||||
if (type == ttype) return new[] { (T)Convert.ChangeType(obj, type) };
|
||||
@ -1227,7 +1228,7 @@ namespace FreeSql.Internal
|
||||
{
|
||||
foreach (var key in dic.Keys)
|
||||
{
|
||||
if (string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{key}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
|
||||
if (isCheckSql && string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{key}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
|
||||
var val = dic[key];
|
||||
var valType = val == null ? typeof(string) : val.GetType();
|
||||
if (valType == ttype) ret.Add((T)Convert.ChangeType(val, ttype));
|
||||
@ -1239,7 +1240,7 @@ namespace FreeSql.Internal
|
||||
var ps = type.GetPropertiesDictIgnoreCase().Values;
|
||||
foreach (var p in ps)
|
||||
{
|
||||
if (string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{p.Name}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
|
||||
if (isCheckSql && string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{p.Name}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
|
||||
var pvalue = p.GetValue(obj, null);
|
||||
if (p.PropertyType == ttype) ret.Add((T)Convert.ChangeType(pvalue, ttype));
|
||||
else ret.Add(constructorParamter(p.Name, p.PropertyType, pvalue));
|
||||
|
Reference in New Issue
Block a user