Skip to content

Commit a692537

Browse files
committed
Merge branch 'master' of https://github.com/2881099/FreeSql
2 parents a23bd97 + 66d94d0 commit a692537

File tree

17 files changed

+258
-305
lines changed

17 files changed

+258
-305
lines changed

Examples/base_entity/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,16 @@ static void Main(string[] args)
621621
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
622622
#endregion
623623

624+
fsql.Delete<User1>().Where("1=1").ExecuteAffrows();
625+
fsql.Insert(new List<User1>
626+
{
627+
new User1 { Nickname = "nickname11", Username = "username11", Description = "desc11" },
628+
new User1 { Nickname = "n2", Username = "u2", Description = "d2" },
629+
new User1 { Nickname = "n3", Username = "u3", Description = "d3" },
630+
}).ExecuteAffrows();
631+
624632
fsql.Insert(new User1()).ExecuteInserted();
633+
fsql.Update<User1>().SetSource(new User1()).ExecuteUpdated();
625634

626635
fsql.InsertOrUpdate<AppInfoEntity>().SetSource(new AppInfoEntity { AppID = "03DN8CW8", AppName = "app_01" }).ExecuteAffrows();
627636
var repo2211 = fsql.GetRepository<AppInfoEntity>();

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Interface/Curd/IDelete.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public interface IDelete<T1>
133133
#if net40
134134
#else
135135
Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default);
136+
/// <summary>
137+
/// 执行SQL语句,返回被删除的记录<para></para>
138+
/// 注意:此方法只有 Postgresql/SqlServer/Maridb/Firebird/人大金仓 有效果
139+
/// </summary>
140+
/// <returns></returns>
136141
Task<List<T1>> ExecuteDeletedAsync(CancellationToken cancellationToken = default);
137142
#endif
138143
}

FreeSql/Interface/Curd/IInsert.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public interface IInsert<T1> where T1 : class
156156
long ExecuteIdentity();
157157
/// <summary>
158158
/// 执行SQL语句,返回插入后的记录<para></para>
159-
/// 注意:此方法只有 Postgresql/SqlServer/MySql8.0.30+/Maridb/Firebird/DuckDB/人大金仓 有效果
159+
/// 注意:此方法只有 Postgresql/SqlServer/Maridb/Firebird/DuckDB/人大金仓 有效果
160160
/// </summary>
161161
/// <returns></returns>
162162
List<T1> ExecuteInserted();
@@ -176,7 +176,7 @@ public interface IInsert<T1> where T1 : class
176176
Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default);
177177
/// <summary>
178178
/// 执行SQL语句,返回插入后的记录<para></para>
179-
/// 注意:此方法只有 Postgresql/SqlServer/MySql8.0.30+/Maridb/Firebird/DuckDB/人大金仓 有效果
179+
/// 注意:此方法只有 Postgresql/SqlServer/Maridb/Firebird/DuckDB/人大金仓 有效果
180180
/// </summary>
181181
/// <returns></returns>
182182
Task<List<T1>> ExecuteInsertedAsync(CancellationToken cancellationToken = default);

FreeSql/Interface/Curd/IUpdate.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ public interface IUpdate<T1>
303303
#if net40
304304
#else
305305
Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default);
306-
/// <summary>
307-
/// 执行SQL语句,返回更新后的记录<para></para>
308-
/// 注意:此方法只有 Postgresql/SqlServer 有效果
309-
/// </summary>
310-
/// <returns></returns>
306+
/// <summary>
307+
/// 执行SQL语句,返回更新后的记录<para></para>
308+
/// 注意:此方法只有 Postgresql/SqlServer/Maridb/Firebird/人大金仓 有效果
309+
/// </summary>
310+
/// <returns></returns>
311311
Task<List<T1>> ExecuteUpdatedAsync(CancellationToken cancellationToken = default);
312312
Task<List<TReturn>> ExecuteUpdatedAsync<TReturn>(Expression<Func<T1, TReturn>> returnColumns, CancellationToken cancellationToken = default);
313313
#endif

FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void LoggerException(IObjectPool<DbConnection> pool, PrepareCommandResult pc, Ex
9292
}
9393
}
9494

95-
internal Dictionary<string, PropertyInfo> GetQueryTypeProperties(Type type)
95+
public static Dictionary<string, PropertyInfo> GetQueryTypeProperties(Type type)
9696
{
9797
return type.GetPropertiesDictIgnoreCase(); //与 ExecuteArrayRowReadClassOrTuple 顺序同步,防止【延时属性】获取到位置不对的问题
9898
//var tb = _util.GetTableByEntity(type);

FreeSql/Internal/CommonProvider/UpdateProvider.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Linq.Expressions;
1010
using System.Text;
11+
using System.Threading;
1112
using System.Threading.Tasks;
1213

1314
namespace FreeSql.Internal.CommonProvider
@@ -476,13 +477,20 @@ protected int RawExecuteAffrows()
476477
public abstract int ExecuteAffrows();
477478
protected abstract List<TReturn> ExecuteUpdated<TReturn>(IEnumerable<ColumnInfo> columns);
478479

479-
public List<T1> ExecuteUpdated() => ExecuteUpdated<T1>(_table.ColumnsByPosition);
480+
public List<T1> ExecuteUpdated()
481+
{
482+
var ret = ExecuteUpdated<T1>(_table.Columns.Values);
483+
if (_table.TypeLazySetOrm != null) ret.ForEach(item => _table.TypeLazySetOrm.Invoke(item, new object[] { _orm }));
484+
return ret;
485+
}
480486
public List<TReturn> ExecuteUpdated<TReturn>(Expression<Func<T1, TReturn>> returnColumns)
481487
{
482488
var cols = _commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, returnColumns?.Body, false, null)
483489
.Distinct().Select(a => _table.ColumnsByCs.TryGetValue(a, out var c) ? c : null).Where(a => a != null).ToArray();
484-
return ExecuteUpdated<TReturn>(cols);
485-
}
490+
var ret = ExecuteUpdated<TReturn>(cols);
491+
if (_table.TypeLazySetOrm != null) ret.ForEach(item => _table.TypeLazySetOrm.Invoke(item, new object[] { _orm }));
492+
return ret;
493+
}
486494

487495
public IUpdate<T1> IgnoreColumns(Expression<Func<T1, object>> columns) => IgnoreColumns(_commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, columns?.Body, false, null));
488496
public IUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns) => UpdateColumns(_commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, columns?.Body, false, null));

FreeSql/Internal/CommonProvider/UpdateProviderAsync.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,20 @@ await ToSqlFetchAsync(async sb =>
143143
public abstract Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default);
144144
protected abstract Task<List<TReturn>> ExecuteUpdatedAsync<TReturn>(IEnumerable<ColumnInfo> columns, CancellationToken cancellationToken = default);
145145

146-
public Task<List<T1>> ExecuteUpdatedAsync(CancellationToken cancellationToken = default) => ExecuteUpdatedAsync<T1>(_table.ColumnsByPosition, cancellationToken);
147-
public Task<List<TReturn>> ExecuteUpdatedAsync<TReturn>(Expression<Func<T1, TReturn>> returnColumns, CancellationToken cancellationToken = default)
146+
async public Task<List<T1>> ExecuteUpdatedAsync(CancellationToken cancellationToken = default)
147+
{
148+
var ret = await ExecuteUpdatedAsync<T1>(_table.ColumnsByPosition, cancellationToken);
149+
if (_table.TypeLazySetOrm != null) ret.ForEach(item => _table.TypeLazySetOrm.Invoke(item, new object[] { _orm }));
150+
return ret;
151+
}
152+
async public Task<List<TReturn>> ExecuteUpdatedAsync<TReturn>(Expression<Func<T1, TReturn>> returnColumns, CancellationToken cancellationToken = default)
148153
{
149154
var cols = _commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, returnColumns?.Body, false, null)
150155
.Distinct().Select(a => _table.ColumnsByCs.TryGetValue(a, out var c) ? c : null).Where(a => a != null).ToArray();
151-
return ExecuteUpdatedAsync<TReturn>(cols, cancellationToken);
152-
}
156+
var ret = await ExecuteUpdatedAsync<TReturn>(cols, cancellationToken);
157+
if (_table.TypeLazySetOrm != null) ret.ForEach(item => _table.TypeLazySetOrm.Invoke(item, new object[] { _orm }));
158+
return ret;
159+
}
153160
#endif
154161
}
155162
}

Providers/FreeSql.Provider.Custom/MySql/Curd/CustomMySqlDelete.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using FreeSql.Internal;
2+
using FreeSql.Internal.CommonProvider;
23
using System;
34
using System.Collections.Generic;
45
using System.Data;
56
using System.Data.Common;
7+
using System.Linq;
68
using System.Text;
79
using System.Threading;
810
using System.Threading.Tasks;
@@ -50,7 +52,7 @@ public override List<T1> ExecuteDeleted()
5052
catch (Exception ex)
5153
{
5254
exception = ex;
53-
throw ex;
55+
throw;
5456
}
5557
finally
5658
{
@@ -101,7 +103,7 @@ await ToSqlFetchAsync(async sb =>
101103
catch (Exception ex)
102104
{
103105
exception = ex;
104-
throw ex;
106+
throw;
105107
}
106108
finally
107109
{

Providers/FreeSql.Provider.Custom/MySql/Curd/CustomMySqlInsert.cs

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using FreeSql.Internal;
2+
using FreeSql.Internal.CommonProvider;
23
using FreeSql.Internal.Model;
34
using FreeSql.Internal.ObjectPool;
45
using System;
56
using System.Collections.Generic;
67
using System.Data;
78
using System.Data.Common;
9+
using System.Linq;
810
using System.Text;
911
using System.Threading;
1012
using System.Threading.Tasks;
@@ -89,36 +91,20 @@ protected override List<T1> RawExecuteInserted()
8991
sb.Append(sql).Append(" RETURNING ");
9092

9193
var colidx = 0;
92-
var propidx = 0;
93-
var props = _table.Type.GetPropertiesDictIgnoreCase();
94-
var indexes = new int[props.Count];
95-
var sbflag = new StringBuilder().Append("insertedQuery");
96-
foreach (var prop in props)
94+
foreach (var col in _table.Columns.Values)
9795
{
98-
if (_table.ColumnsByCs.TryGetValue(prop.Key, out var col))
99-
{
100-
if (colidx > 0) sb.Append(", ");
101-
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name)));
102-
sbflag.Append(col.Attribute.Name).Append(":").Append(colidx).Append(",");
103-
indexes[propidx] = colidx;
104-
++colidx;
105-
}
106-
else
107-
indexes[propidx] = -1;
108-
++propidx;
96+
if (colidx > 0) sb.Append(", ");
97+
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
98+
++colidx;
10999
}
110-
var flag = sbflag.ToString();
111100
sql = sb.ToString();
112101
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params);
113102
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
114103
var ret = new List<T1>();
115104
Exception exception = null;
116105
try
117106
{
118-
_orm.Ado.ExecuteReader(_connection, _transaction, fetch =>
119-
{
120-
ret.Add((T1)Utils.ExecuteReaderToClass(flag, _table.TypeLazy ?? _table.Type, indexes, fetch.Object, 0, _commonUtils));
121-
}, CommandType.Text, sql, _commandTimeout, _params);
107+
ret = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
122108
}
123109
catch (Exception ex)
124110
{
@@ -185,37 +171,20 @@ async protected override Task<List<T1>> RawExecuteInsertedAsync(CancellationToke
185171
sb.Append(sql).Append(" RETURNING ");
186172

187173
var colidx = 0;
188-
var propidx = 0;
189-
var props = _table.Type.GetPropertiesDictIgnoreCase();
190-
var indexes = new int[props.Count];
191-
var sbflag = new StringBuilder().Append("insertedQuery");
192-
foreach (var prop in props)
174+
foreach (var col in _table.Columns.Values)
193175
{
194-
if (_table.ColumnsByCs.TryGetValue(prop.Key, out var col))
195-
{
196-
if (colidx > 0) sb.Append(", ");
197-
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name)));
198-
sbflag.Append(col.Attribute.Name).Append(":").Append(colidx).Append(",");
199-
indexes[propidx] = colidx;
200-
++colidx;
201-
}
202-
else
203-
indexes[propidx] = -1;
204-
++propidx;
176+
if (colidx > 0) sb.Append(", ");
177+
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
178+
++colidx;
205179
}
206-
var flag = sbflag.ToString();
207180
sql = sb.ToString();
208181
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params);
209182
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
210183
var ret = new List<T1>();
211184
Exception exception = null;
212185
try
213186
{
214-
await _orm.Ado.ExecuteReaderAsync(_connection, _transaction, fetch =>
215-
{
216-
ret.Add((T1)Utils.ExecuteReaderToClass(flag, _table.TypeLazy ?? _table.Type, indexes, fetch.Object, 0, _commonUtils));
217-
return Task.FromResult(false);
218-
}, CommandType.Text, sql, _commandTimeout, _params);
187+
ret = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
219188
}
220189
catch (Exception ex)
221190
{

0 commit comments

Comments
 (0)