mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IFreeSql.InsertOrUpdate 方法 #316
This commit is contained in:
@ -1011,6 +1011,60 @@
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.WithTransaction(System.Data.Common.DbTransaction)">
|
||||
<summary>
|
||||
指定事务对象
|
||||
</summary>
|
||||
<param name="transaction"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.WithConnection(System.Data.Common.DbConnection)">
|
||||
<summary>
|
||||
指定事务对象
|
||||
</summary>
|
||||
<param name="connection"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.SetSource(`0)">
|
||||
<summary>
|
||||
添加或更新,设置实体
|
||||
</summary>
|
||||
<param name="source">实体</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.SetSource(System.Collections.Generic.IEnumerable{`0})">
|
||||
<summary>
|
||||
添加或更新,设置实体集合
|
||||
</summary>
|
||||
<param name="source">实体集合</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.AsTable(System.Func{System.String,System.String})">
|
||||
<summary>
|
||||
设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
|
||||
</summary>
|
||||
<param name="tableRule"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.AsType(System.Type)">
|
||||
<summary>
|
||||
动态Type,在使用 Update<object> 后使用本方法,指定实体类型
|
||||
</summary>
|
||||
<param name="entityType"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.ToSql">
|
||||
<summary>
|
||||
返回即将执行的SQL语句
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.ExecuteAffrows">
|
||||
<summary>
|
||||
执行SQL语句,返回影响的行数
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="F:FreeSql.FieldAliasOptions.AsIndex">
|
||||
<summary>
|
||||
自动产生 as1, as2, as3 .... 字段别名<para></para>
|
||||
@ -2337,137 +2391,6 @@
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteReaderAsync(System.Func{System.Data.Common.DbDataReader,System.Threading.Tasks.Task},System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
|
||||
</summary>
|
||||
<param name="readerHander"></param>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteReaderAsync(System.Func{System.Data.Common.DbDataReader,System.Threading.Tasks.Task},System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteReaderAsync(dr => {}, "select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteArrayAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteArrayAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteArrayAsync("select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataSetAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataSetAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteDataSetAsync("select * from user where age > ?age; select 2", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataTableAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataTableAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteDataTableAsync("select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteNonQueryAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
在【主库】执行
|
||||
</summary>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteNonQueryAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
在【主库】执行,ExecuteNonQueryAsync("delete from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteScalarAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
在【主库】执行
|
||||
</summary>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteScalarAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
在【主库】执行,ExecuteScalarAsync("select 1 from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``1(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||
</summary>
|
||||
<typeparam name="T"></typeparam>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``1(System.String,System.Object)">
|
||||
<summary>
|
||||
执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<typeparam name="T"></typeparam>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``2(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
执行SQL返回对象集合,Query<User>("select * from user where age > ?age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``2(System.String,System.Object)">
|
||||
<summary>
|
||||
执行SQL返回对象集合,Query<User>("select * from user where age > ?age; select * from address", new { age = 25 })
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="E:FreeSql.IAop.ParseExpression">
|
||||
<summary>
|
||||
可自定义解析表达式
|
||||
@ -3063,12 +2986,6 @@
|
||||
<param name="timeout">超时</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IObjectPool`1.GetAsync">
|
||||
<summary>
|
||||
获取资源
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IObjectPool`1.Return(FreeSql.Internal.ObjectPool.Object{`0},System.Boolean)">
|
||||
<summary>
|
||||
使用完毕后,归还资源
|
||||
@ -3139,12 +3056,6 @@
|
||||
</summary>
|
||||
<param name="obj">资源对象</param>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnGetAsync(FreeSql.Internal.ObjectPool.Object{`0})">
|
||||
<summary>
|
||||
从对象池获取对象成功的时候触发,通过该方法统计或初始化对象
|
||||
</summary>
|
||||
<param name="obj">资源对象</param>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnReturn(FreeSql.Internal.ObjectPool.Object{`0})">
|
||||
<summary>
|
||||
归还对象给对象池的时候触发
|
||||
@ -3685,6 +3596,19 @@
|
||||
<param name="source"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:IFreeSql.InsertOrUpdate``1">
|
||||
<summary>
|
||||
插入或更新数据<para></para>
|
||||
MySql: on duplicate key update<para></para>
|
||||
PostgreSQL: on conflict do update<para></para>
|
||||
SqlServer: merge into<para></para>
|
||||
Oracle: merge into<para></para>
|
||||
Sqlite: replace into<para></para>
|
||||
Dameng: merge into<para></para>
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:IFreeSql.Update``1">
|
||||
<summary>
|
||||
修改数据
|
||||
|
66
FreeSql/Interface/Curd/IInsertOrUpdate.cs
Normal file
66
FreeSql/Interface/Curd/IInsertOrUpdate.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
public interface IInsertOrUpdate<T1> where T1 : class
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 指定事务对象
|
||||
/// </summary>
|
||||
/// <param name="transaction"></param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> WithTransaction(DbTransaction transaction);
|
||||
/// <summary>
|
||||
/// 指定事务对象
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> WithConnection(DbConnection connection);
|
||||
|
||||
/// <summary>
|
||||
/// 添加或更新,设置实体
|
||||
/// </summary>
|
||||
/// <param name="source">实体</param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> SetSource(T1 source);
|
||||
/// <summary>
|
||||
/// 添加或更新,设置实体集合
|
||||
/// </summary>
|
||||
/// <param name="source">实体集合</param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> SetSource(IEnumerable<T1> source);
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
|
||||
/// </summary>
|
||||
/// <param name="tableRule"></param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> AsTable(Func<string, string> tableRule);
|
||||
/// <summary>
|
||||
/// 动态Type,在使用 Update<object> 后使用本方法,指定实体类型
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> AsType(Type entityType);
|
||||
/// <summary>
|
||||
/// 返回即将执行的SQL语句
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string ToSql();
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的行数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
int ExecuteAffrows();
|
||||
|
||||
#if net40
|
||||
#else
|
||||
Task<int> ExecuteAffrowsAsync();
|
||||
#endif
|
||||
}
|
||||
}
|
@ -201,7 +201,7 @@ namespace FreeSql.Aop
|
||||
/// </summary>
|
||||
public DbParameter[] DbParms { get; }
|
||||
}
|
||||
public enum CurdType { Select, Delete, Update, Insert }
|
||||
public enum CurdType { Select, Delete, Update, Insert, InsertOrUpdate }
|
||||
public class CurdAfterEventArgs : CurdBeforeEventArgs
|
||||
{
|
||||
public CurdAfterEventArgs(CurdBeforeEventArgs before, Exception exception, object executeResult) :
|
||||
@ -324,7 +324,7 @@ namespace FreeSql.Aop
|
||||
private object _value;
|
||||
public bool IsChanged { get; private set; }
|
||||
}
|
||||
public enum AuditValueType { Update, Insert }
|
||||
public enum AuditValueType { Update, Insert, InsertOrUpdate }
|
||||
#endregion
|
||||
|
||||
#region CommandBefore/After
|
||||
|
@ -44,6 +44,19 @@ public interface IFreeSql : IDisposable
|
||||
/// <returns></returns>
|
||||
IInsert<T1> Insert<T1>(IEnumerable<T1> source) where T1 : class;
|
||||
|
||||
/// <summary>
|
||||
/// 插入或更新数据<para></para>
|
||||
/// MySql: on duplicate key update<para></para>
|
||||
/// PostgreSQL: on conflict do update<para></para>
|
||||
/// SqlServer: merge into<para></para>
|
||||
/// Oracle: merge into<para></para>
|
||||
/// Sqlite: replace into<para></para>
|
||||
/// Dameng: merge into<para></para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T1"></typeparam>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> InsertOrUpdate<T1>() where T1 : class;
|
||||
|
||||
/// <summary>
|
||||
/// 修改数据
|
||||
/// </summary>
|
||||
|
222
FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs
Normal file
222
FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs
Normal file
@ -0,0 +1,222 @@
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
|
||||
public abstract partial class InsertOrUpdateProvider<T1> : IInsertOrUpdate<T1> where T1 : class
|
||||
{
|
||||
protected IFreeSql _orm;
|
||||
protected CommonUtils _commonUtils;
|
||||
protected CommonExpression _commonExpression;
|
||||
protected List<T1> _source = new List<T1>();
|
||||
protected Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
protected TableInfo _table;
|
||||
protected Func<string, string> _tableRule;
|
||||
protected DbParameter[] _params;
|
||||
protected DbTransaction _transaction;
|
||||
protected DbConnection _connection;
|
||||
|
||||
public InsertOrUpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
||||
{
|
||||
_orm = orm;
|
||||
_commonUtils = commonUtils;
|
||||
_commonExpression = commonExpression;
|
||||
_table = _commonUtils.GetTableByEntity(typeof(T1));
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
||||
}
|
||||
|
||||
protected void ClearData()
|
||||
{
|
||||
_source.Clear();
|
||||
_auditValueChangedDict.Clear();
|
||||
}
|
||||
|
||||
public IInsertOrUpdate<T1> WithTransaction(DbTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
_connection = _transaction?.Connection;
|
||||
return this;
|
||||
}
|
||||
public IInsertOrUpdate<T1> WithConnection(DbConnection connection)
|
||||
{
|
||||
if (_transaction?.Connection != connection) _transaction = null;
|
||||
_connection = connection;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
|
||||
{
|
||||
if (data?.Any() != true) return;
|
||||
if (orm.Aop.AuditValueHandler == null) return;
|
||||
foreach (var d in data)
|
||||
{
|
||||
if (d == null) continue;
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
object val = col.GetMapValue(d);
|
||||
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.InsertOrUpdate, col, table.Properties[col.CsName], val);
|
||||
orm.Aop.AuditValueHandler(sender, auditArgs);
|
||||
if (auditArgs.IsChanged)
|
||||
{
|
||||
col.SetMapValue(d, val = auditArgs.Value);
|
||||
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
|
||||
changedDict.Add(col.Attribute.Name, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
|
||||
{
|
||||
if (orm.Aop.AuditValueHandler == null) return;
|
||||
if (data == null) return;
|
||||
if (typeof(T1) == typeof(object) && new[] { table.Type, table.TypeLazy }.Contains(data.GetType()) == false)
|
||||
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
object val = col.GetMapValue(data);
|
||||
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.InsertOrUpdate, col, table.Properties[col.CsName], val);
|
||||
orm.Aop.AuditValueHandler(sender, auditArgs);
|
||||
if (auditArgs.IsChanged)
|
||||
{
|
||||
col.SetMapValue(data, val = auditArgs.Value);
|
||||
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
|
||||
changedDict.Add(col.Attribute.Name, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IInsertOrUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
|
||||
public IInsertOrUpdate<T1> SetSource(IEnumerable<T1> source)
|
||||
{
|
||||
if (source == null || source.Any() == false) return this;
|
||||
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
|
||||
_source.AddRange(source.Where(a => a != null));
|
||||
return this;
|
||||
}
|
||||
|
||||
protected string TableRuleInvoke()
|
||||
{
|
||||
if (_tableRule == null) return _table.DbName;
|
||||
var newname = _tableRule(_table.DbName);
|
||||
if (newname == _table.DbName) return _table.DbName;
|
||||
if (string.IsNullOrEmpty(newname)) return _table.DbName;
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
|
||||
return newname;
|
||||
}
|
||||
public IInsertOrUpdate<T1> AsTable(Func<string, string> tableRule)
|
||||
{
|
||||
_tableRule = tableRule;
|
||||
return this;
|
||||
}
|
||||
public IInsertOrUpdate<T1> AsType(Type entityType)
|
||||
{
|
||||
if (entityType == typeof(object)) throw new Exception("IInsertOrUpdate.AsType 参数不支持指定为 object");
|
||||
if (entityType == _table.Type) return this;
|
||||
var newtb = _commonUtils.GetTableByEntity(entityType);
|
||||
_table = newtb ?? throw new Exception("IInsertOrUpdate.AsType 参数错误,请传入正确的实体类型");
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void WriteSourceSelectUnionAll(StringBuilder sb)
|
||||
{
|
||||
var specialParams = new List<DbParameter>();
|
||||
var didx = 0;
|
||||
foreach (var d in _source)
|
||||
{
|
||||
if (didx > 0) sb.Append(" \r\nUNION ALL\r\n ");
|
||||
sb.Append("SELECT ");
|
||||
var colidx2 = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx2 > 0) sb.Append(", ");
|
||||
if (string.IsNullOrEmpty(col.DbInsertValue) == false)
|
||||
sb.Append(col.DbInsertValue);
|
||||
else
|
||||
{
|
||||
object val = col.GetMapValue(d);
|
||||
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
|
||||
}
|
||||
if (didx == 0) sb.Append(" as ").Append(col.Attribute.Name);
|
||||
++colidx2;
|
||||
}
|
||||
switch (_orm.Ado.DataType)
|
||||
{
|
||||
case DataType.OdbcOracle:
|
||||
case DataType.Oracle:
|
||||
case DataType.OdbcDameng:
|
||||
case DataType.Dameng:
|
||||
sb.Append(" FROM dual");
|
||||
break;
|
||||
}
|
||||
++didx;
|
||||
}
|
||||
if (specialParams.Any()) _params = specialParams.ToArray();
|
||||
}
|
||||
|
||||
public abstract string ToSql();
|
||||
public int ExecuteAffrows()
|
||||
{
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.InsertOrUpdate, sql, _params);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
var affrows = 0;
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
affrows = _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _params);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, affrows);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
return affrows;
|
||||
}
|
||||
#if net40
|
||||
#else
|
||||
async public Task<int> ExecuteAffrowsAsync()
|
||||
{
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return 0;
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.InsertOrUpdate, sql, _params);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
var affrows = 0;
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
affrows = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _params);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, affrows);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
return affrows;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
@ -15,20 +15,20 @@ namespace FreeSql.Internal.CommonProvider
|
||||
|
||||
public abstract partial class InsertProvider<T1> : IInsert<T1> where T1 : class
|
||||
{
|
||||
protected IFreeSql _orm;
|
||||
protected CommonUtils _commonUtils;
|
||||
protected CommonExpression _commonExpression;
|
||||
protected List<T1> _source = new List<T1>();
|
||||
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
protected Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
protected TableInfo _table;
|
||||
protected Func<string, string> _tableRule;
|
||||
protected bool _noneParameter, _insertIdentity;
|
||||
protected int _batchValuesLimit, _batchParameterLimit;
|
||||
protected bool _batchAutoTransaction = true;
|
||||
protected DbParameter[] _params;
|
||||
protected DbTransaction _transaction;
|
||||
protected DbConnection _connection;
|
||||
public IFreeSql _orm;
|
||||
public CommonUtils _commonUtils;
|
||||
public CommonExpression _commonExpression;
|
||||
public List<T1> _source = new List<T1>();
|
||||
public Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
public Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
public TableInfo _table;
|
||||
public Func<string, string> _tableRule;
|
||||
public bool _noneParameter, _insertIdentity;
|
||||
public int _batchValuesLimit, _batchParameterLimit;
|
||||
public bool _batchAutoTransaction = true;
|
||||
public DbParameter[] _params;
|
||||
public DbTransaction _transaction;
|
||||
public DbConnection _connection;
|
||||
|
||||
public InsertProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
||||
{
|
||||
@ -509,7 +509,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
++colidx;
|
||||
}
|
||||
sb.Append(") ");
|
||||
if (isValues) sb.Append(isValues ? "VALUES" : "SELECT ");
|
||||
if (isValues) sb.Append("VALUES");
|
||||
_params = _noneParameter ? new DbParameter[0] : new DbParameter[colidx * _source.Count];
|
||||
var specialParams = new List<DbParameter>();
|
||||
var didx = 0;
|
||||
|
@ -15,25 +15,25 @@ namespace FreeSql.Internal.CommonProvider
|
||||
|
||||
public abstract partial class UpdateProvider<T1> : IUpdate<T1> where T1 : class
|
||||
{
|
||||
protected IFreeSql _orm;
|
||||
protected CommonUtils _commonUtils;
|
||||
protected CommonExpression _commonExpression;
|
||||
protected List<T1> _source = new List<T1>();
|
||||
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
protected Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
protected TableInfo _table;
|
||||
protected Func<string, string> _tableRule;
|
||||
protected StringBuilder _where = new StringBuilder();
|
||||
protected List<GlobalFilter.Item> _whereGlobalFilter;
|
||||
protected StringBuilder _set = new StringBuilder();
|
||||
protected StringBuilder _setIncr = new StringBuilder();
|
||||
protected List<DbParameter> _params = new List<DbParameter>();
|
||||
protected List<DbParameter> _paramsSource = new List<DbParameter>();
|
||||
protected bool _noneParameter;
|
||||
protected int _batchRowsLimit, _batchParameterLimit;
|
||||
protected bool _batchAutoTransaction = true;
|
||||
protected DbTransaction _transaction;
|
||||
protected DbConnection _connection;
|
||||
public IFreeSql _orm;
|
||||
public CommonUtils _commonUtils;
|
||||
public CommonExpression _commonExpression;
|
||||
public List<T1> _source = new List<T1>();
|
||||
public Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
public Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
public TableInfo _table;
|
||||
public Func<string, string> _tableRule;
|
||||
public StringBuilder _where = new StringBuilder();
|
||||
public List<GlobalFilter.Item> _whereGlobalFilter;
|
||||
public StringBuilder _set = new StringBuilder();
|
||||
public StringBuilder _setIncr = new StringBuilder();
|
||||
public List<DbParameter> _params = new List<DbParameter>();
|
||||
public List<DbParameter> _paramsSource = new List<DbParameter>();
|
||||
public bool _noneParameter;
|
||||
public int _batchRowsLimit, _batchParameterLimit;
|
||||
public bool _batchAutoTransaction = true;
|
||||
public DbTransaction _transaction;
|
||||
public DbConnection _connection;
|
||||
|
||||
public UpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere)
|
||||
{
|
||||
|
Reference in New Issue
Block a user