mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-18 14:20:55 +08:00
Merge pull request #2155 from tobybain/master
实现GBase中的ExecuteInserted方法
This commit is contained in:
@@ -2,6 +2,7 @@ using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.GBase
|
||||
@@ -80,9 +81,28 @@ namespace FreeSql.Tests.GBase
|
||||
[Fact]
|
||||
public void ExecuteDeleted()
|
||||
{
|
||||
Assert.Throws<NotImplementedException>(() => delete.Where(a => a.Id > 0).ExecuteDeleted());
|
||||
g.gbase.Delete<Topic>().Where(a => a.Id > 0).ExecuteAffrows();
|
||||
var list = new[] { new Topic { Title = "t1" }, new Topic { Title = "t2" } };
|
||||
g.gbase.Insert<Topic>().ExecuteAffrows();
|
||||
var datas = delete.Where(a => a.Id > 0).ExecuteDeleted();
|
||||
foreach (var data in datas)
|
||||
{
|
||||
Assert.Contains(list, it => it.Title == data.Title);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteDeletedAsync()
|
||||
{
|
||||
await g.gbase.Delete<Topic>().Where(a => a.Id > 0).ExecuteAffrowsAsync();
|
||||
var list = new[] { new Topic { Title = "t1" }, new Topic { Title = "t2" } };
|
||||
var cnt = await g.gbase.Insert(list).ExecuteAffrowsAsync();
|
||||
var datas = await delete.Where(a => a.Id > 0).ExecuteDeletedAsync();
|
||||
foreach (var data in datas)
|
||||
{
|
||||
Assert.Contains(list, it => it.Title == data.Title);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AsTable()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@ using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.GBase
|
||||
@@ -249,15 +250,47 @@ UNION ALL
|
||||
|
||||
Assert.NotEqual(0, insert.AppendData(items.First()).ExecuteIdentity());
|
||||
}
|
||||
|
||||
[Table(Name = "TB_TOPIC_INSERT_T")]
|
||||
class Topic_T
|
||||
{
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
public int Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExecuteInserted()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
var items2 = insert.AppendData(items).ExecuteInserted();
|
||||
Assert.Equal(items.First().Title, items2.First().Title);
|
||||
Assert.Equal(items.Last().Title, items2.Last().Title);
|
||||
|
||||
Assert.Throws<NotImplementedException>(() => insert.AppendData(items.First()).ExecuteInserted());
|
||||
var items3 = new List<Topic_T>();
|
||||
for (var a = 0; a < 10; a++) items3.Add(new Topic_T { Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
var items4 = g.gbase.Insert(items3).ExecuteInserted();
|
||||
Assert.Equal(items3.Select(u => u.Id), items4.Select(u => u.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteInsertedAsync()
|
||||
{
|
||||
var items = new List<Topic>();
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
var items2 = await insert.AppendData(items).ExecuteInsertedAsync();
|
||||
Assert.Equal(items.First().Title, items2.First().Title);
|
||||
Assert.Equal(items.Last().Title, items2.Last().Title);
|
||||
|
||||
var items3 = new List<Topic_T>();
|
||||
for (var a = 0; a < 10; a++) items3.Add(new Topic_T { Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
var items4 = await g.gbase.Insert(items3).ExecuteInsertedAsync();
|
||||
Assert.Equal(items3.Select(u => u.Id), items4.Select(u => u.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AsTable()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@ using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.GBase
|
||||
@@ -215,12 +216,65 @@ namespace FreeSql.Tests.GBase
|
||||
update.SetSource(items.First()).NoneParameter().ExecuteAffrows();
|
||||
update.SetSource(items).NoneParameter().ExecuteAffrows();
|
||||
}
|
||||
|
||||
|
||||
[Table(Name = "TB_TOPIC_INSERT_T")]
|
||||
class Topic_T
|
||||
{
|
||||
[Column(IsIdentity = true, IsPrimary = true)]
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
public int Clicks { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExecuteUpdated()
|
||||
{
|
||||
|
||||
var items = new Dictionary<Guid, Topic_T>();
|
||||
for (var a = 0; a < 10; a++)
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
items.Add(guid, new Topic_T { Id = guid, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
}
|
||||
g.gbase.Insert(items.Values.AsEnumerable()).ExecuteAffrows();
|
||||
foreach (var key in items.Keys)
|
||||
{
|
||||
items[key].Title += $"newtitle";
|
||||
items[key].Clicks += 100;
|
||||
}
|
||||
var items2 = g.gbase.Update<Topic_T>().SetSource(items.Values).ExecuteUpdated().ToDictionary(x => x.Id, y => y);
|
||||
foreach (var key in items.Keys)
|
||||
{
|
||||
Assert.Equal(items[key].Title, items2[key].Title);
|
||||
Assert.Equal(items[key].Clicks, items2[key].Clicks);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteUpdatedAsnyc()
|
||||
{
|
||||
var items = new Dictionary<Guid, Topic_T>();
|
||||
for (var a = 0; a < 10; a++)
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
items.Add(guid, new Topic_T { Id = guid, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
}
|
||||
await g.gbase.Insert(items.Values.AsEnumerable()).ExecuteAffrowsAsync();
|
||||
foreach (var key in items.Keys)
|
||||
{
|
||||
items[key].Title += $"newtitle";
|
||||
items[key].Clicks += 100;
|
||||
}
|
||||
var list = await g.gbase.Update<Topic_T>().SetSource(items.Values).ExecuteUpdatedAsync();
|
||||
var items2 = list.ToDictionary(x => x.Id, y => y);
|
||||
foreach (var key in items.Keys)
|
||||
{
|
||||
Assert.Equal(items[key].Title, items2[key].Title);
|
||||
Assert.Equal(items[key].Clicks, items2[key].Clicks);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AsTable()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -16,11 +17,111 @@ namespace FreeSql.GBase.Curd
|
||||
{
|
||||
}
|
||||
|
||||
public override List<T1> ExecuteDeleted() => throw new NotImplementedException($"FreeSql.Provider.GBase {CoreErrorStrings.S_Not_Implemented_Feature}");
|
||||
public override List<T1> ExecuteDeleted()
|
||||
{
|
||||
var ret = new List<T1>();
|
||||
DbParameter[] dbParms = null;
|
||||
StringBuilder sbret = null;
|
||||
ToSqlFetch(sb =>
|
||||
{
|
||||
if (dbParms == null)
|
||||
{
|
||||
dbParms = _params.ToArray();
|
||||
sbret = new StringBuilder();
|
||||
|
||||
var colidx = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx > 0) sbret.Append(", ");
|
||||
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
var delSql = sb.ToString();
|
||||
var validx = delSql.IndexOf(" WHERE ");
|
||||
if (validx == -1) throw new ArgumentException(CoreErrorStrings.S_NotFound_Name("WHERE"));
|
||||
var wherePart = delSql.Substring(validx);
|
||||
var selectSql = new StringBuilder()
|
||||
.Append("SELECT ").Append(sbret)
|
||||
.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
|
||||
.Append(wherePart);
|
||||
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, string.Concat(selectSql.ToString(), "; ", delSql, ";"), dbParms);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
ret.AddRange(_orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, selectSql.ToString(), _commandTimeout, dbParms));
|
||||
_orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, delSql, _commandTimeout, dbParms);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if net40
|
||||
#else
|
||||
public override Task<List<T1>> ExecuteDeletedAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException($"FreeSql.Provider.GBase {CoreErrorStrings.S_Not_Implemented_Feature}");
|
||||
async public override Task<List<T1>> ExecuteDeletedAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ret = new List<T1>();
|
||||
DbParameter[] dbParms = null;
|
||||
StringBuilder sbret = null;
|
||||
await ToSqlFetchAsync(async sb =>
|
||||
{
|
||||
if (dbParms == null)
|
||||
{
|
||||
dbParms = _params.ToArray();
|
||||
sbret = new StringBuilder();
|
||||
|
||||
var colidx = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx > 0) sbret.Append(", ");
|
||||
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
var delSql = sb.ToString();
|
||||
var validx = delSql.IndexOf(" WHERE ");
|
||||
if (validx == -1) throw new ArgumentException(CoreErrorStrings.S_NotFound_Name("WHERE"));
|
||||
var wherePart = delSql.Substring(validx);
|
||||
var selectSql = new StringBuilder()
|
||||
.Append("SELECT ").Append(sbret)
|
||||
.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
|
||||
.Append(wherePart);
|
||||
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, string.Concat(selectSql.ToString(), "; ", delSql, ";"), dbParms);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
ret.AddRange(await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, selectSql.ToString(), _commandTimeout, dbParms, cancellationToken));
|
||||
await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, delSql, _commandTimeout, dbParms, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace FreeSql.GBase.Curd
|
||||
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(_batchValuesLimit > 0 ? _batchValuesLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999);
|
||||
public override long ExecuteIdentity() => base.SplitExecuteIdentity(_batchValuesLimit > 0 ? _batchValuesLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999);
|
||||
public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(1, 999);
|
||||
public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(_batchValuesLimit > 0 ? _batchValuesLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999);
|
||||
|
||||
public override string ToSql()
|
||||
{
|
||||
@@ -91,14 +91,110 @@ namespace FreeSql.GBase.Curd
|
||||
}
|
||||
protected override List<T1> RawExecuteInserted()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
var identityCol = _table.Primarys.Where(a => a.Attribute.IsIdentity).FirstOrDefault();
|
||||
var identityType = identityCol?.Attribute.MapType.NullableTypeOrThis();
|
||||
var identitySql = "";
|
||||
if (identityType != null)
|
||||
{
|
||||
if (identityType == typeof(int) || identityType == typeof(uint)) identitySql = "SELECT dbinfo('sqlca.sqlerrd1') FROM dual";
|
||||
else if (identityType == typeof(long) || identityType == typeof(ulong)) identitySql =
|
||||
identityCol.Attribute.DbType.IndexOf("bigserial", StringComparison.OrdinalIgnoreCase) != -1 ?
|
||||
"SELECT dbinfo('bigserial')::INT8 FROM dual" : "SELECT dbinfo('serial8') FROM dual";
|
||||
}
|
||||
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, string.Concat(sql, string.IsNullOrWhiteSpace(identitySql) ? "" : $"; {identitySql};"), _params);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
var ret = new List<T1>();
|
||||
Exception exception = null;
|
||||
var isUseConnection = _connection != null;
|
||||
try
|
||||
{
|
||||
if (isUseConnection == false)
|
||||
{
|
||||
using (var conn = _orm.Ado.MasterPool.Get())
|
||||
{
|
||||
_connection = conn.Value;
|
||||
_orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
|
||||
|
||||
if (identityCol != null && string.IsNullOrWhiteSpace(identitySql) == false)
|
||||
{
|
||||
long lastId = 0;
|
||||
long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_connection, _transaction, CommandType.Text, identitySql, _commandTimeout, _params)), out lastId);
|
||||
var cnt = _source.Count;
|
||||
var startId = lastId - cnt + 1;
|
||||
var sb = new StringBuilder();
|
||||
var colidx = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
sb.Insert(0, "SELECT ");
|
||||
sb.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
|
||||
.Append(" WHERE ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name))
|
||||
.Append(" BETWEEN ").Append(startId).Append(" AND ").Append(lastId)
|
||||
.Append(" ORDER BY ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name));
|
||||
ret = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sb.ToString(), _commandTimeout, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = _source.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
|
||||
if (identityCol != null && string.IsNullOrWhiteSpace(identitySql) == false)
|
||||
{
|
||||
long lastId = 0;
|
||||
long.TryParse(string.Concat(_orm.Ado.ExecuteScalar(_connection, _transaction, CommandType.Text, identitySql, _commandTimeout, _params)), out lastId);
|
||||
var cnt = _source.Count;
|
||||
var startId = lastId - cnt + 1;
|
||||
var sb = new StringBuilder();
|
||||
var colidx = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
sb.Insert(0, "SELECT ");
|
||||
sb.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
|
||||
.Append(" WHERE ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name))
|
||||
.Append(" BETWEEN ").Append(startId).Append(" AND ").Append(lastId)
|
||||
.Append(" ORDER BY ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name));
|
||||
ret = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sb.ToString(), _commandTimeout, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = _source.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (isUseConnection == false) _connection = null;
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if net40
|
||||
#else
|
||||
public override Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) => base.SplitExecuteAffrowsAsync(_batchValuesLimit > 0 ? _batchValuesLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999, cancellationToken);
|
||||
public override Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default) => base.SplitExecuteIdentityAsync(_batchValuesLimit > 0 ? _batchValuesLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999, cancellationToken);
|
||||
public override Task<List<T1>> ExecuteInsertedAsync(CancellationToken cancellationToken = default) => base.SplitExecuteInsertedAsync(1, 1000, cancellationToken);
|
||||
public override Task<List<T1>> ExecuteInsertedAsync(CancellationToken cancellationToken = default) => base.SplitExecuteInsertedAsync(_batchValuesLimit > 0 ? _batchValuesLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999, cancellationToken);
|
||||
|
||||
async protected override Task<long> RawExecuteIdentityAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -152,9 +248,105 @@ namespace FreeSql.GBase.Curd
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
protected override Task<List<T1>> RawExecuteInsertedAsync(CancellationToken cancellationToken = default)
|
||||
async protected override Task<List<T1>> RawExecuteInsertedAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var sql = this.ToSql();
|
||||
if (string.IsNullOrEmpty(sql)) return new List<T1>();
|
||||
|
||||
var identityCol = _table.Primarys.Where(a => a.Attribute.IsIdentity).FirstOrDefault();
|
||||
var identityType = identityCol?.Attribute.MapType.NullableTypeOrThis();
|
||||
var identitySql = "";
|
||||
if (identityType != null)
|
||||
{
|
||||
if (identityType == typeof(int) || identityType == typeof(uint)) identitySql = "SELECT dbinfo('sqlca.sqlerrd1') FROM dual";
|
||||
else if (identityType == typeof(long) || identityType == typeof(ulong)) identitySql =
|
||||
identityCol.Attribute.DbType.IndexOf("bigserial", StringComparison.OrdinalIgnoreCase) != -1 ?
|
||||
"SELECT dbinfo('bigserial') FROM dual" : "SELECT dbinfo('serial8') FROM dual";
|
||||
}
|
||||
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, string.Concat(sql, string.IsNullOrWhiteSpace(identitySql) ? "" : $"; {identitySql};"), _params);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
var ret = new List<T1>();
|
||||
Exception exception = null;
|
||||
var isUseConnection = _connection != null;
|
||||
try
|
||||
{
|
||||
if (isUseConnection == false)
|
||||
{
|
||||
using (var conn = await _orm.Ado.MasterPool.GetAsync())
|
||||
{
|
||||
_connection = conn.Value;
|
||||
await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
|
||||
|
||||
if (identityCol != null && string.IsNullOrWhiteSpace(identitySql) == false)
|
||||
{
|
||||
long lastId = 0;
|
||||
long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_connection, _transaction, CommandType.Text, identitySql, _commandTimeout, _params, cancellationToken)), out lastId);
|
||||
var cnt = _source.Count;
|
||||
var startId = lastId - cnt + 1;
|
||||
var sb = new StringBuilder();
|
||||
var colidx = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
sb.Insert(0, "SELECT ");
|
||||
sb.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
|
||||
.Append(" WHERE ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name))
|
||||
.Append(" BETWEEN ").Append(startId).Append(" AND ").Append(lastId)
|
||||
.Append(" ORDER BY ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name));
|
||||
ret = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sb.ToString(), _commandTimeout, null, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = _source.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
|
||||
if (identityCol != null && string.IsNullOrWhiteSpace(identitySql) == false)
|
||||
{
|
||||
long lastId = 0;
|
||||
long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_connection, _transaction, CommandType.Text, identitySql, _commandTimeout, _params, cancellationToken)), out lastId);
|
||||
var cnt = _source.Count;
|
||||
var startId = lastId - cnt + 1;
|
||||
var sb = new StringBuilder();
|
||||
var colidx = 0;
|
||||
foreach (var col in _table.Columns.Values)
|
||||
{
|
||||
if (colidx > 0) sb.Append(", ");
|
||||
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
sb.Insert(0, "SELECT ");
|
||||
sb.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
|
||||
.Append(" WHERE ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name))
|
||||
.Append(" BETWEEN ").Append(startId).Append(" AND ").Append(lastId)
|
||||
.Append(" ORDER BY ").Append(_commonUtils.QuoteSqlName(identityCol.Attribute.Name));
|
||||
ret = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sb.ToString(), _commandTimeout, null, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = _source.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (isUseConnection == false) _connection = null;
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using FreeSql.Internal;
|
||||
using FreeSql.Internal;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Data.Common;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -22,7 +23,61 @@ namespace FreeSql.GBase.Curd
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(_batchRowsLimit > 0 ? _batchRowsLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999);
|
||||
protected override List<TReturn> ExecuteUpdated<TReturn>(IEnumerable<ColumnInfo> columns) => base.SplitExecuteUpdated<TReturn>(_batchRowsLimit > 0 ? _batchRowsLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999, columns);
|
||||
|
||||
protected override List<TReturn> RawExecuteUpdated<TReturn>(IEnumerable<ColumnInfo> columns) => throw new NotImplementedException($"FreeSql.Provider.GBase {CoreErrorStrings.S_Not_Implemented_Feature}");
|
||||
protected override List<TReturn> RawExecuteUpdated<TReturn>(IEnumerable<ColumnInfo> columns)
|
||||
{
|
||||
var ret = new List<TReturn>();
|
||||
DbParameter[] dbParms = null;
|
||||
StringBuilder sbret = null;
|
||||
ToSqlFetch(sb =>
|
||||
{
|
||||
if (dbParms == null)
|
||||
{
|
||||
dbParms = _params.Concat(_paramsSource).ToArray();
|
||||
sbret = new StringBuilder();
|
||||
var colidx = 0;
|
||||
foreach (var col in columns)
|
||||
{
|
||||
if (colidx > 0) sbret.Append(", ");
|
||||
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
var sql = sb.ToString();
|
||||
var validx = sql.IndexOf(" \r\nWHERE ");
|
||||
if (validx == -1) throw new ArgumentException(CoreErrorStrings.S_NotFound_Name("WHERE"));
|
||||
var wherePart = sql.Substring(validx);
|
||||
var selectSql = new StringBuilder()
|
||||
.Append("SELECT ").Append(sbret)
|
||||
.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()));
|
||||
if (string.IsNullOrWhiteSpace(_tableAlias) == false) selectSql.Append(" ").Append(_tableAlias);
|
||||
selectSql.Append(wherePart);
|
||||
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, string.Concat(sql, "; ", selectSql.ToString(), ";"), dbParms);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
var affrowstmp = _orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms);
|
||||
ValidateVersionAndThrow(affrowstmp, sql, dbParms);
|
||||
var queryType = typeof(TReturn) == typeof(T1) ? (_table.TypeLazy ?? _table.Type) : null;
|
||||
var rettmp = _orm.Ado.Query<TReturn>(queryType, _connection, _transaction, CommandType.Text, selectSql.ToString(), _commandTimeout, dbParms);
|
||||
ret.AddRange(rettmp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
});
|
||||
sbret?.Clear();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected override void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys)
|
||||
{
|
||||
@@ -66,7 +121,60 @@ namespace FreeSql.GBase.Curd
|
||||
public override Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) => base.SplitExecuteAffrowsAsync(_batchRowsLimit > 0 ? _batchRowsLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999, cancellationToken);
|
||||
protected override Task<List<TReturn>> ExecuteUpdatedAsync<TReturn>(IEnumerable<ColumnInfo> columns, CancellationToken cancellationToken = default) => base.SplitExecuteUpdatedAsync<TReturn>(_batchRowsLimit > 0 ? _batchRowsLimit : 200, _batchParameterLimit > 0 ? _batchParameterLimit : 999, columns, cancellationToken);
|
||||
|
||||
protected override Task<List<TReturn>> RawExecuteUpdatedAsync<TReturn>(IEnumerable<ColumnInfo> columns, CancellationToken cancellationToken = default) => throw new NotImplementedException($"FreeSql.Provider.GBase {CoreErrorStrings.S_Not_Implemented_Feature}");
|
||||
async protected override Task<List<TReturn>> RawExecuteUpdatedAsync<TReturn>(IEnumerable<ColumnInfo> columns, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ret = new List<TReturn>();
|
||||
DbParameter[] dbParms = null;
|
||||
StringBuilder sbret = null;
|
||||
await ToSqlFetchAsync(async sb =>
|
||||
{
|
||||
if (dbParms == null)
|
||||
{
|
||||
dbParms = _params.Concat(_paramsSource).ToArray();
|
||||
sbret = new StringBuilder();
|
||||
var colidx = 0;
|
||||
foreach (var col in columns)
|
||||
{
|
||||
if (colidx > 0) sbret.Append(", ");
|
||||
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
|
||||
++colidx;
|
||||
}
|
||||
}
|
||||
var sql = sb.ToString();
|
||||
var validx = sql.IndexOf(" \r\nWHERE ");
|
||||
if (validx == -1) throw new ArgumentException(CoreErrorStrings.S_NotFound_Name("WHERE"));
|
||||
var wherePart = sql.Substring(validx);
|
||||
var selectSql = new StringBuilder()
|
||||
.Append("SELECT ").Append(sbret)
|
||||
.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()));
|
||||
if (string.IsNullOrWhiteSpace(_tableAlias) == false) selectSql.Append(" ").Append(_tableAlias);
|
||||
selectSql.Append(wherePart);
|
||||
|
||||
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Update, string.Concat(sql, "; ", selectSql.ToString(), ";"), dbParms);
|
||||
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
|
||||
|
||||
Exception exception = null;
|
||||
try
|
||||
{
|
||||
var affrowstmp = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken);
|
||||
ValidateVersionAndThrow(affrowstmp, sql, dbParms);
|
||||
var queryType = typeof(TReturn) == typeof(T1) ? (_table.TypeLazy ?? _table.Type) : null;
|
||||
var rettmp = await _orm.Ado.QueryAsync<TReturn>(queryType, _connection, _transaction, CommandType.Text, selectSql.ToString(), _commandTimeout, dbParms, cancellationToken);
|
||||
ret.AddRange(rettmp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
|
||||
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user