Skip to content

Commit ed9de2a

Browse files
authored
Merge pull request #2155 from tobybain/master
实现GBase中的ExecuteInserted方法
2 parents 205da5e + 14c1657 commit ed9de2a

File tree

6 files changed

+522
-14
lines changed

6 files changed

+522
-14
lines changed

FreeSql.Tests/FreeSql.Tests.Provider.GBase/GBase/Curd/GBaseDeleteTest.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using Xunit;
67

78
namespace FreeSql.Tests.GBase
@@ -80,9 +81,28 @@ public void ExecuteAffrows()
8081
[Fact]
8182
public void ExecuteDeleted()
8283
{
83-
Assert.Throws<NotImplementedException>(() => delete.Where(a => a.Id > 0).ExecuteDeleted());
84+
g.gbase.Delete<Topic>().Where(a => a.Id > 0).ExecuteAffrows();
85+
var list = new[] { new Topic { Title = "t1" }, new Topic { Title = "t2" } };
86+
g.gbase.Insert<Topic>().ExecuteAffrows();
87+
var datas = delete.Where(a => a.Id > 0).ExecuteDeleted();
88+
foreach (var data in datas)
89+
{
90+
Assert.Contains(list, it => it.Title == data.Title);
91+
}
8492
}
85-
93+
[Fact]
94+
public async Task ExecuteDeletedAsync()
95+
{
96+
await g.gbase.Delete<Topic>().Where(a => a.Id > 0).ExecuteAffrowsAsync();
97+
var list = new[] { new Topic { Title = "t1" }, new Topic { Title = "t2" } };
98+
var cnt = await g.gbase.Insert(list).ExecuteAffrowsAsync();
99+
var datas = await delete.Where(a => a.Id > 0).ExecuteDeletedAsync();
100+
foreach (var data in datas)
101+
{
102+
Assert.Contains(list, it => it.Title == data.Title);
103+
}
104+
}
105+
86106
[Fact]
87107
public void AsTable()
88108
{

FreeSql.Tests/FreeSql.Tests.Provider.GBase/GBase/Curd/GBaseInsertTest.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using Xunit;
67

78
namespace FreeSql.Tests.GBase
@@ -249,15 +250,47 @@ public void ExecuteIdentity()
249250

250251
Assert.NotEqual(0, insert.AppendData(items.First()).ExecuteIdentity());
251252
}
253+
254+
[Table(Name = "TB_TOPIC_INSERT_T")]
255+
class Topic_T
256+
{
257+
[Column(IsIdentity = true, IsPrimary = true)]
258+
public Guid Id { get; set; } = Guid.NewGuid();
259+
public int Clicks { get; set; }
260+
public string Title { get; set; }
261+
public DateTime CreateTime { get; set; }
262+
}
263+
252264
[Fact]
253265
public void ExecuteInserted()
254266
{
255267
var items = new List<Topic>();
256268
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
269+
var items2 = insert.AppendData(items).ExecuteInserted();
270+
Assert.Equal(items.First().Title, items2.First().Title);
271+
Assert.Equal(items.Last().Title, items2.Last().Title);
257272

258-
Assert.Throws<NotImplementedException>(() => insert.AppendData(items.First()).ExecuteInserted());
273+
var items3 = new List<Topic_T>();
274+
for (var a = 0; a < 10; a++) items3.Add(new Topic_T { Title = $"newtitle{a}", Clicks = a * 100 });
275+
var items4 = g.gbase.Insert(items3).ExecuteInserted();
276+
Assert.Equal(items3.Select(u => u.Id), items4.Select(u => u.Id));
259277
}
260278

279+
[Fact]
280+
public async Task ExecuteInsertedAsync()
281+
{
282+
var items = new List<Topic>();
283+
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
284+
var items2 = await insert.AppendData(items).ExecuteInsertedAsync();
285+
Assert.Equal(items.First().Title, items2.First().Title);
286+
Assert.Equal(items.Last().Title, items2.Last().Title);
287+
288+
var items3 = new List<Topic_T>();
289+
for (var a = 0; a < 10; a++) items3.Add(new Topic_T { Title = $"newtitle{a}", Clicks = a * 100 });
290+
var items4 = await g.gbase.Insert(items3).ExecuteInsertedAsync();
291+
Assert.Equal(items3.Select(u => u.Id), items4.Select(u => u.Id));
292+
}
293+
261294
[Fact]
262295
public void AsTable()
263296
{

FreeSql.Tests/FreeSql.Tests.Provider.GBase/GBase/Curd/GBaseUpdateTest.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using Xunit;
67

78
namespace FreeSql.Tests.GBase
@@ -215,12 +216,65 @@ public void ExecuteAffrows()
215216
update.SetSource(items.First()).NoneParameter().ExecuteAffrows();
216217
update.SetSource(items).NoneParameter().ExecuteAffrows();
217218
}
219+
220+
221+
[Table(Name = "TB_TOPIC_INSERT_T")]
222+
class Topic_T
223+
{
224+
[Column(IsIdentity = true, IsPrimary = true)]
225+
public Guid Id { get; set; } = Guid.NewGuid();
226+
public int Clicks { get; set; }
227+
public string Title { get; set; }
228+
public DateTime CreateTime { get; set; }
229+
}
230+
218231
[Fact]
219232
public void ExecuteUpdated()
220233
{
221-
234+
var items = new Dictionary<Guid, Topic_T>();
235+
for (var a = 0; a < 10; a++)
236+
{
237+
var guid = Guid.NewGuid();
238+
items.Add(guid, new Topic_T { Id = guid, Title = $"newtitle{a}", Clicks = a * 100 });
239+
}
240+
g.gbase.Insert(items.Values.AsEnumerable()).ExecuteAffrows();
241+
foreach (var key in items.Keys)
242+
{
243+
items[key].Title += $"newtitle";
244+
items[key].Clicks += 100;
245+
}
246+
var items2 = g.gbase.Update<Topic_T>().SetSource(items.Values).ExecuteUpdated().ToDictionary(x => x.Id, y => y);
247+
foreach (var key in items.Keys)
248+
{
249+
Assert.Equal(items[key].Title, items2[key].Title);
250+
Assert.Equal(items[key].Clicks, items2[key].Clicks);
251+
}
222252
}
223253

254+
[Fact]
255+
public async Task ExecuteUpdatedAsnyc()
256+
{
257+
var items = new Dictionary<Guid, Topic_T>();
258+
for (var a = 0; a < 10; a++)
259+
{
260+
var guid = Guid.NewGuid();
261+
items.Add(guid, new Topic_T { Id = guid, Title = $"newtitle{a}", Clicks = a * 100 });
262+
}
263+
await g.gbase.Insert(items.Values.AsEnumerable()).ExecuteAffrowsAsync();
264+
foreach (var key in items.Keys)
265+
{
266+
items[key].Title += $"newtitle";
267+
items[key].Clicks += 100;
268+
}
269+
var list = await g.gbase.Update<Topic_T>().SetSource(items.Values).ExecuteUpdatedAsync();
270+
var items2 = list.ToDictionary(x => x.Id, y => y);
271+
foreach (var key in items.Keys)
272+
{
273+
Assert.Equal(items[key].Title, items2[key].Title);
274+
Assert.Equal(items[key].Clicks, items2[key].Clicks);
275+
}
276+
}
277+
224278
[Fact]
225279
public void AsTable()
226280
{

Providers/FreeSql.Provider.GBase/Curd/GBaseDelete.cs

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Data;
5+
using System.Data.Common;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -16,11 +17,111 @@ public GBaseDelete(IFreeSql orm, CommonUtils commonUtils, CommonExpression commo
1617
{
1718
}
1819

19-
public override List<T1> ExecuteDeleted() => throw new NotImplementedException($"FreeSql.Provider.GBase {CoreErrorStrings.S_Not_Implemented_Feature}");
20+
public override List<T1> ExecuteDeleted()
21+
{
22+
var ret = new List<T1>();
23+
DbParameter[] dbParms = null;
24+
StringBuilder sbret = null;
25+
ToSqlFetch(sb =>
26+
{
27+
if (dbParms == null)
28+
{
29+
dbParms = _params.ToArray();
30+
sbret = new StringBuilder();
31+
32+
var colidx = 0;
33+
foreach (var col in _table.Columns.Values)
34+
{
35+
if (colidx > 0) sbret.Append(", ");
36+
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
37+
++colidx;
38+
}
39+
}
40+
var delSql = sb.ToString();
41+
var validx = delSql.IndexOf(" WHERE ");
42+
if (validx == -1) throw new ArgumentException(CoreErrorStrings.S_NotFound_Name("WHERE"));
43+
var wherePart = delSql.Substring(validx);
44+
var selectSql = new StringBuilder()
45+
.Append("SELECT ").Append(sbret)
46+
.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
47+
.Append(wherePart);
48+
49+
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, string.Concat(selectSql.ToString(), "; ", delSql, ";"), dbParms);
50+
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
51+
52+
Exception exception = null;
53+
try
54+
{
55+
ret.AddRange(_orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, selectSql.ToString(), _commandTimeout, dbParms));
56+
_orm.Ado.ExecuteNonQuery(_connection, _transaction, CommandType.Text, delSql, _commandTimeout, dbParms);
57+
}
58+
catch (Exception ex)
59+
{
60+
exception = ex;
61+
throw;
62+
}
63+
finally
64+
{
65+
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
66+
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
67+
}
68+
});
69+
return ret;
70+
}
2071

2172
#if net40
2273
#else
23-
public override Task<List<T1>> ExecuteDeletedAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException($"FreeSql.Provider.GBase {CoreErrorStrings.S_Not_Implemented_Feature}");
74+
async public override Task<List<T1>> ExecuteDeletedAsync(CancellationToken cancellationToken = default)
75+
{
76+
var ret = new List<T1>();
77+
DbParameter[] dbParms = null;
78+
StringBuilder sbret = null;
79+
await ToSqlFetchAsync(async sb =>
80+
{
81+
if (dbParms == null)
82+
{
83+
dbParms = _params.ToArray();
84+
sbret = new StringBuilder();
85+
86+
var colidx = 0;
87+
foreach (var col in _table.Columns.Values)
88+
{
89+
if (colidx > 0) sbret.Append(", ");
90+
sbret.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
91+
++colidx;
92+
}
93+
}
94+
var delSql = sb.ToString();
95+
var validx = delSql.IndexOf(" WHERE ");
96+
if (validx == -1) throw new ArgumentException(CoreErrorStrings.S_NotFound_Name("WHERE"));
97+
var wherePart = delSql.Substring(validx);
98+
var selectSql = new StringBuilder()
99+
.Append("SELECT ").Append(sbret)
100+
.Append(" FROM ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke()))
101+
.Append(wherePart);
102+
103+
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Delete, string.Concat(selectSql.ToString(), "; ", delSql, ";"), dbParms);
104+
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
105+
106+
Exception exception = null;
107+
try
108+
{
109+
ret.AddRange(await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, selectSql.ToString(), _commandTimeout, dbParms, cancellationToken));
110+
await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, delSql, _commandTimeout, dbParms, cancellationToken);
111+
}
112+
catch (Exception ex)
113+
{
114+
exception = ex;
115+
throw;
116+
}
117+
finally
118+
{
119+
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
120+
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
121+
}
122+
});
123+
return ret;
124+
}
24125
#endif
25126
}
26127
}

0 commit comments

Comments
 (0)