Skip to content

Commit f861c9d

Browse files
committed
- 修复 MySql8.0.30 无法使用 ExecuteInserted 的问题;#2001
1 parent e1faaa1 commit f861c9d

File tree

9 files changed

+169
-151
lines changed

9 files changed

+169
-151
lines changed

Examples/base_entity/Program.cs

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

624+
fsql.Insert(new User1()).ExecuteInserted();
625+
624626
fsql.InsertOrUpdate<AppInfoEntity>().SetSource(new AppInfoEntity { AppID = "03DN8CW8", AppName = "app_01" }).ExecuteAffrows();
625627
var repo2211 = fsql.GetRepository<AppInfoEntity>();
626628

FreeSql/FreeSql.xml

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

FreeSql/Interface/Curd/IInsert.cs

Lines changed: 6 additions & 1 deletion
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/Maridb/Firebird/DuckDB/人大金仓 有效果
159+
/// 注意:此方法只有 Postgresql/SqlServer/MySql8.0.30+/Maridb/Firebird/DuckDB/人大金仓 有效果
160160
/// </summary>
161161
/// <returns></returns>
162162
List<T1> ExecuteInserted();
@@ -174,6 +174,11 @@ public interface IInsert<T1> where T1 : class
174174
#else
175175
Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default);
176176
Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default);
177+
/// <summary>
178+
/// 执行SQL语句,返回插入后的记录<para></para>
179+
/// 注意:此方法只有 Postgresql/SqlServer/MySql8.0.30+/Maridb/Firebird/DuckDB/人大金仓 有效果
180+
/// </summary>
181+
/// <returns></returns>
177182
Task<List<T1>> ExecuteInsertedAsync(CancellationToken cancellationToken = default);
178183
#endif
179184
}

FreeSql/Internal/CommonProvider/InsertProvider.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ protected List<T1> SplitExecuteInserted(int valuesLimit, int parameterLimit)
431431
if (ss.Length == 1)
432432
{
433433
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
434-
ret = this.RawExecuteInserted();
434+
ret = this.InternalExecuteInserted();
435435
ClearData();
436436
return ret;
437437
}
@@ -452,7 +452,7 @@ protected List<T1> SplitExecuteInserted(int valuesLimit, int parameterLimit)
452452
{
453453
_source = ss[a];
454454
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
455-
ret.AddRange(this.RawExecuteInserted());
455+
ret.AddRange(this.InternalExecuteInserted());
456456
}
457457
}
458458
else
@@ -469,7 +469,7 @@ protected List<T1> SplitExecuteInserted(int valuesLimit, int parameterLimit)
469469
{
470470
_source = ss[a];
471471
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
472-
ret.AddRange(this.RawExecuteInserted());
472+
ret.AddRange(this.InternalExecuteInserted());
473473
}
474474
_transaction.Commit();
475475
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreErrorStrings.Commit, null));
@@ -525,6 +525,12 @@ protected virtual int RawExecuteAffrows()
525525

526526
protected abstract long RawExecuteIdentity();
527527
protected abstract List<T1> RawExecuteInserted();
528+
private List<T1> InternalExecuteInserted()
529+
{
530+
var ret = RawExecuteInserted();
531+
if (_table.TypeLazySetOrm != null) ret.ForEach(item => _table.TypeLazySetOrm.Invoke(item, new object[] { _orm }));
532+
return ret;
533+
}
528534

529535
public abstract int ExecuteAffrows();
530536
public abstract long ExecuteIdentity();

FreeSql/Internal/CommonProvider/InsertProviderAsync.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ async protected virtual Task<List<T1>> SplitExecuteInsertedAsync(int valuesLimit
189189
if (ss.Length == 1)
190190
{
191191
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
192-
ret = await this.RawExecuteInsertedAsync(cancellationToken);
192+
ret = await this.InternalExecuteInsertedAsync(cancellationToken);
193193
ClearData();
194194
return ret;
195195
}
@@ -210,7 +210,7 @@ async protected virtual Task<List<T1>> SplitExecuteInsertedAsync(int valuesLimit
210210
{
211211
_source = ss[a];
212212
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
213-
ret.AddRange(await this.RawExecuteInsertedAsync(cancellationToken));
213+
ret.AddRange(await this.InternalExecuteInsertedAsync(cancellationToken));
214214
}
215215
}
216216
else
@@ -227,7 +227,7 @@ async protected virtual Task<List<T1>> SplitExecuteInsertedAsync(int valuesLimit
227227
{
228228
_source = ss[a];
229229
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
230-
ret.AddRange(await this.RawExecuteInsertedAsync(cancellationToken));
230+
ret.AddRange(await this.InternalExecuteInsertedAsync(cancellationToken));
231231
}
232232
_transaction.Commit();
233233
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreErrorStrings.Commit, null));
@@ -282,6 +282,12 @@ async protected virtual Task<int> RawExecuteAffrowsAsync(CancellationToken cance
282282

283283
protected abstract Task<long> RawExecuteIdentityAsync(CancellationToken cancellationToken = default);
284284
protected abstract Task<List<T1>> RawExecuteInsertedAsync(CancellationToken cancellationToken = default);
285+
async private Task<List<T1>> InternalExecuteInsertedAsync(CancellationToken cancellationToken = default)
286+
{
287+
var ret = await RawExecuteInsertedAsync(cancellationToken);
288+
if (_table.TypeLazySetOrm != null) ret.ForEach(item => _table.TypeLazySetOrm.Invoke(item, new object[] { _orm }));
289+
return ret;
290+
}
285291

286292
public abstract Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default);
287293
public abstract Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default);

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,8 @@ internal static object InternalDataReaderGetValue(CommonUtils commonUtil, DbData
17521752
}
17531753
return dr.GetValue(index);
17541754
}
1755+
public static object ExecuteReaderToClass(string flagStr, Type typeOrg, int[] indexes, DbDataReader row, int dataIndex, CommonUtils _commonUtils) =>
1756+
ExecuteArrayRowReadClassOrTuple(flagStr, typeOrg, indexes, row, dataIndex, _commonUtils)?.Value;
17551757
internal static RowInfo ExecuteArrayRowReadClassOrTuple(string flagStr, Type typeOrg, int[] indexes, DbDataReader row, int dataIndex, CommonUtils _commonUtils)
17561758
{
17571759
if (string.IsNullOrEmpty(flagStr)) flagStr = "all";

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,41 @@ protected override List<T1> RawExecuteInserted()
8989
sb.Append(sql).Append(" RETURNING ");
9090

9191
var colidx = 0;
92-
foreach (var col in _table.Columns.Values)
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)
9397
{
94-
if (colidx > 0) sb.Append(", ");
95-
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
96-
++colidx;
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;
97109
}
110+
var flag = sbflag.ToString();
98111
sql = sb.ToString();
99112
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params);
100113
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
101114
var ret = new List<T1>();
102115
Exception exception = null;
103116
try
104117
{
105-
ret = _orm.Ado.Query<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
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);
106122
}
107123
catch (Exception ex)
108124
{
109125
exception = ex;
110-
throw ex;
126+
throw;
111127
}
112128
finally
113129
{
@@ -169,25 +185,42 @@ async protected override Task<List<T1>> RawExecuteInsertedAsync(CancellationToke
169185
sb.Append(sql).Append(" RETURNING ");
170186

171187
var colidx = 0;
172-
foreach (var col in _table.Columns.Values)
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)
173193
{
174-
if (colidx > 0) sb.Append(", ");
175-
sb.Append(_commonUtils.RereadColumn(col, _commonUtils.QuoteSqlName(col.Attribute.Name))).Append(" as ").Append(_commonUtils.QuoteSqlName(col.CsName));
176-
++colidx;
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;
177205
}
206+
var flag = sbflag.ToString();
178207
sql = sb.ToString();
179208
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params);
180209
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
181210
var ret = new List<T1>();
182211
Exception exception = null;
183212
try
184213
{
185-
ret = await _orm.Ado.QueryAsync<T1>(_table.TypeLazy ?? _table.Type, _connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
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);
186219
}
187220
catch (Exception ex)
188221
{
189222
exception = ex;
190-
throw ex;
223+
throw;
191224
}
192225
finally
193226
{

0 commit comments

Comments
 (0)