- 增加 fsql.InsertDict/UpdateDict/DeleteDict 字典操作的扩展方法;#481

This commit is contained in:
2881099 2022-03-25 19:07:47 +08:00
parent dc688adc11
commit 0fcc5619be
22 changed files with 491 additions and 91 deletions

View File

@ -141,6 +141,11 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
fsql.Aop.AuditValue += new EventHandler<FreeSql.Aop.AuditValueEventArgs>((_, e) =>
{
});
Dictionary<string, object> dic = new Dictionary<string, object>(); Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("id", 1); dic.Add("id", 1);
dic.Add("name", "xxxx"); dic.Add("name", "xxxx");
@ -152,10 +157,31 @@ namespace base_entity
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sqss = fsql.Insert(dic).AsTable("table1").ToSql(); var sqss = fsql.InsertDict(dic).AsTable("table1").ToSql();
var sqss2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sqss2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
sqss = fsql.Insert(dic).AsTable("table1").NoneParameter(false).ToSql(); sqss = fsql.InsertDict(dic).AsTable("table1").NoneParameter(false).ToSql();
sqss2 = fsql.Insert(diclist).AsTable("table1").NoneParameter(false).ToSql(); sqss2 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter(false).ToSql();
var sqlupd1 = fsql.UpdateDict(dic).AsTable("table1").WherePrimary("id").ToSql();
var sqlupd2 = fsql.UpdateDict(diclist).AsTable("table1").WherePrimary("id").ToSql();
var sqlupd11 = fsql.UpdateDict(dic).AsTable("table1").WherePrimary("id").NoneParameter(false).ToSql();
var sqlupd22 = fsql.UpdateDict(diclist).AsTable("table1").WherePrimary("id").NoneParameter(false).ToSql();
var sqldel1 = fsql.DeleteDict(dic).AsTable("table1").ToSql();
var sqldel2 = fsql.DeleteDict(diclist).AsTable("table1").ToSql();
diclist[1]["title"] = "newtitle";
var sqldel3 = fsql.DeleteDict(diclist).AsTable("table1").ToSql();
diclist.Clear();
diclist.Add(new Dictionary<string, object>
{
["id"] = 1
});
diclist.Add(new Dictionary<string, object>
{
["id"] = 2
});
var sqldel4 = fsql.DeleteDict(diclist).AsTable("table1").ToSql();
for (var a = 0; a < 10000; a++) for (var a = 0; a < 10000; a++)
fsql.Select<User1>().First(); fsql.Select<User1>().First();

View File

@ -538,5 +538,14 @@
<param name="that"></param> <param name="that"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members> </members>
</doc> </doc>

View File

@ -44,13 +44,13 @@ namespace FreeSql.Tests.MySqlConnector
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(@id_0, @name_0)", sql1); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(@id_0, @name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx'), (2, 'yyyy')", sql4); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx'), (2, 'yyyy')", sql4);
} }

View File

@ -37,16 +37,16 @@ namespace FreeSql.Tests.Dameng
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0)", sql1); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT ALL Assert.Equal(@"INSERT ALL
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0) INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0)
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_1, :name_1) INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_1, :name_1)
SELECT 1 FROM DUAL", sql2); SELECT 1 FROM DUAL", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT ALL Assert.Equal(@"INSERT ALL
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx') INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(2, 'yyyy') INTO ""TABLE1""(""ID"", ""NAME"") VALUES(2, 'yyyy')

View File

@ -37,15 +37,15 @@ namespace FreeSql.Tests.Firebird
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(@id_0, @name_0)", sql1); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(@id_0, @name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") SELECT FIRST 1 @id_0, @name_0 FROM rdb$database Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") SELECT FIRST 1 @id_0, @name_0 FROM rdb$database
UNION ALL UNION ALL
SELECT FIRST 1 @id_1, @name_1 FROM rdb$database", sql2); SELECT FIRST 1 @id_1, @name_1 FROM rdb$database", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") SELECT FIRST 1 1, 'xxxx' FROM rdb$database Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") SELECT FIRST 1 1, 'xxxx' FROM rdb$database
UNION ALL UNION ALL
SELECT FIRST 1 2, 'yyyy' FROM rdb$database", sql4); SELECT FIRST 1 2, 'yyyy' FROM rdb$database", sql4);

View File

@ -37,9 +37,9 @@ namespace FreeSql.Tests.MsAccess
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, 'xxxx')", sql1); Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, 'xxxx')", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, 'xxxx'), (2, 'yyyy')", sql2); Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, 'xxxx'), (2, 'yyyy')", sql2);
} }

View File

@ -45,13 +45,13 @@ namespace FreeSql.Tests.MySql
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(?id_0, ?name_0)", sql1); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(?id_0, ?name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(?id_0, ?name_0), (?id_1, ?name_1)", sql2); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(?id_0, ?name_0), (?id_1, ?name_1)", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx'), (2, 'yyyy')", sql4); Assert.Equal(@"INSERT INTO `table1`(`id`, `name`) VALUES(1, 'xxxx'), (2, 'yyyy')", sql4);
} }

View File

@ -37,16 +37,16 @@ namespace FreeSql.Tests.Oracle
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0)", sql1); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT ALL Assert.Equal(@"INSERT ALL
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0) INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_0, :name_0)
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_1, :name_1) INTO ""TABLE1""(""ID"", ""NAME"") VALUES(:id_1, :name_1)
SELECT 1 FROM DUAL", sql2); SELECT 1 FROM DUAL", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT ALL Assert.Equal(@"INSERT ALL
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx') INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')
INTO ""TABLE1""(""ID"", ""NAME"") VALUES(2, 'yyyy') INTO ""TABLE1""(""ID"", ""NAME"") VALUES(2, 'yyyy')

View File

@ -37,13 +37,13 @@ namespace FreeSql.Tests.PostgreSQL
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0)", sql1); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx'), (2, 'yyyy')", sql4); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx'), (2, 'yyyy')", sql4);
} }

View File

@ -37,13 +37,13 @@ namespace FreeSql.Tests.ShenTong
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(@id_0, @name_0)", sql1); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(@id_0, @name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx'), (2, 'yyyy')", sql4); Assert.Equal(@"INSERT INTO ""TABLE1""(""ID"", ""NAME"") VALUES(1, 'xxxx'), (2, 'yyyy')", sql4);
} }

View File

@ -47,13 +47,13 @@ namespace FreeSql.Tests.SqlServer
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(@id_0, @name_0)", sql1); Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(@id_0, @name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2); Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, N'xxxx')", sql3); Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, N'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, N'xxxx'), (2, N'yyyy')", sql4); Assert.Equal(@"INSERT INTO [table1]([id], [name]) VALUES(1, N'xxxx'), (2, N'yyyy')", sql4);
} }

View File

@ -37,13 +37,13 @@ namespace FreeSql.Tests.Sqlite
["name"] = "yyyy" ["name"] = "yyyy"
}); });
var sql1 = fsql.Insert(dic).AsTable("table1").ToSql(); var sql1 = fsql.InsertDict(dic).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0)", sql1); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0)", sql1);
var sql2 = fsql.Insert(diclist).AsTable("table1").ToSql(); var sql2 = fsql.InsertDict(diclist).AsTable("table1").ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(@id_0, @name_0), (@id_1, @name_1)", sql2);
var sql3 = fsql.Insert(dic).AsTable("table1").NoneParameter().ToSql(); var sql3 = fsql.InsertDict(dic).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx')", sql3); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx')", sql3);
var sql4 = fsql.Insert(diclist).AsTable("table1").NoneParameter().ToSql(); var sql4 = fsql.InsertDict(diclist).AsTable("table1").NoneParameter().ToSql();
Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx'), (2, 'yyyy')", sql4); Assert.Equal(@"INSERT INTO ""table1""(""id"", ""name"") VALUES(1, 'xxxx'), (2, 'yyyy')", sql4);
} }

View File

@ -1,6 +1,7 @@
using FreeSql; using FreeSql;
using FreeSql.DataAnnotations; using FreeSql.DataAnnotations;
using FreeSql.Internal.CommonProvider; using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model;
using FreeSql.Internal.ObjectPool; using FreeSql.Internal.ObjectPool;
using System; using System;
using System.Collections; using System.Collections;
@ -653,4 +654,273 @@ SELECT ");
throw new NotSupportedException($"{s0p._orm.Ado.DataType} 不支持 OrderByRandom 随机排序"); throw new NotSupportedException($"{s0p._orm.Ado.DataType} 不支持 OrderByRandom 随机排序");
} }
#endregion #endregion
#region IFreeSql Insert/Update/Delete Dictionary<string, object>
/// <summary>
/// 插入数据字典 Dictionary&lt;string, object&gt;
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static InsertDictImpl InsertDict(this IFreeSql freesql, Dictionary<string, object> source)
{
var insertDict = new InsertDictImpl(freesql);
insertDict._insertProvider.AppendData(source);
return insertDict;
}
/// <summary>
/// 插入数据字典,传入 Dictionary&lt;string, object&gt; 集合
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static InsertDictImpl InsertDict(this IFreeSql freesql, IEnumerable<Dictionary<string, object>> source)
{
var insertDict = new InsertDictImpl(freesql);
insertDict._insertProvider.AppendData(source);
return insertDict;
}
/// <summary>
/// 更新数据字典 Dictionary&lt;string, object&gt;
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static UpdateDictImpl UpdateDict(this IFreeSql freesql, Dictionary<string, object> source)
{
var updateDict = new UpdateDictImpl(freesql);
updateDict._updateProvider.SetSource(source);
return updateDict;
}
/// <summary>
/// 更新数据字典,传入 Dictionary&lt;string, object&gt; 集合
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static UpdateDictImpl UpdateDict(this IFreeSql freesql, IEnumerable<Dictionary<string, object>> source)
{
var updateDict = new UpdateDictImpl(freesql);
updateDict._updateProvider.SetSource(source);
return updateDict;
}
/// <summary>
/// 删除数据字典 Dictionary&lt;string, object&gt;
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static DeleteDictImpl DeleteDict(this IFreeSql freesql, Dictionary<string, object> source)
{
var deleteDict = new DeleteDictImpl(freesql);
UpdateProvider<Dictionary<string, object>>.GetDictionaryTableInfo(source, deleteDict._deleteProvider._orm, ref deleteDict._deleteProvider._table);
var primarys = UpdateDictImpl.GetPrimarys(deleteDict._deleteProvider._table, source.Keys.ToArray());
deleteDict._deleteProvider.Where(deleteDict._deleteProvider._commonUtils.WhereItems(primarys, "", new[] { source }));
return deleteDict;
}
/// <summary>
/// 删除数据字典,传入 Dictionary&lt;string, object&gt; 集合
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static DeleteDictImpl DeleteDict(this IFreeSql freesql, IEnumerable<Dictionary<string, object>> source)
{
DeleteDictImpl deleteDict = null;
if (source.Select(a => string.Join(",", a.Keys)).Distinct().Count() == 1)
{
deleteDict = new DeleteDictImpl(freesql);
var sourceFirst = source.FirstOrDefault();
UpdateProvider<Dictionary<string, object>>.GetDictionaryTableInfo(sourceFirst, deleteDict._deleteProvider._orm, ref deleteDict._deleteProvider._table);
var primarys = UpdateDictImpl.GetPrimarys(deleteDict._deleteProvider._table, sourceFirst.Keys.ToArray());
deleteDict._deleteProvider.Where(deleteDict._deleteProvider._commonUtils.WhereItems(primarys, "", source));
return deleteDict;
}
foreach (var item in source)
{
var tmpDelteDict = DeleteDict(freesql, item);
if (deleteDict == null) deleteDict = tmpDelteDict;
else deleteDict._deleteProvider._where.Append(" OR ").Append(tmpDelteDict._deleteProvider._where);
}
return deleteDict ?? new DeleteDictImpl(freesql);
}
public class InsertDictImpl
{
internal readonly InsertProvider<Dictionary<string, object>> _insertProvider;
internal InsertDictImpl(IFreeSql orm)
{
_insertProvider = (orm as BaseDbProvider ?? throw new Exception("IFreeSql 无法转换成 BaseDbProvider"))
.CreateInsertProvider<Dictionary<string, object>>() as InsertProvider<Dictionary<string, object>>;
}
public InsertDictImpl AsTable(string tableName)
{
_insertProvider.AsTable(tableName);
return this;
}
public InsertDictImpl BatchOptions(int valuesLimit, int parameterLimit, bool autoTransaction = true)
{
_insertProvider.BatchOptions(valuesLimit, parameterLimit, autoTransaction);
return this;
}
public InsertDictImpl BatchProgress(Action<BatchProgressStatus<Dictionary<string, object>>> callback)
{
_insertProvider.BatchProgress(callback);
return this;
}
public InsertDictImpl CommandTimeout(int timeout)
{
_insertProvider.CommandTimeout(timeout);
return this;
}
public int ExecuteAffrows() => _insertProvider.ExecuteAffrows();
public long ExecuteIdentity() => _insertProvider.ExecuteAffrows();
public List<Dictionary<string, object>> ExecuteInserted() => _insertProvider.ExecuteInserted();
#if net40
#else
public Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) => _insertProvider.ExecuteAffrowsAsync(cancellationToken);
public Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default) => _insertProvider.ExecuteIdentityAsync(cancellationToken);
public Task<List<Dictionary<string, object>>> ExecuteInsertedAsync(CancellationToken cancellationToken = default) => _insertProvider.ExecuteInsertedAsync(cancellationToken);
#endif
public InsertDictImpl NoneParameter(bool isNotCommandParameter = true)
{
_insertProvider.NoneParameter(isNotCommandParameter);
return this;
}
public DataTable ToDataTable() => _insertProvider.ToDataTable();
public string ToSql() => _insertProvider.ToSql();
public InsertDictImpl WithConnection(DbConnection connection)
{
_insertProvider.WithConnection(connection);
return this;
}
public InsertDictImpl WithTransaction(DbTransaction transaction)
{
_insertProvider.WithTransaction(transaction);
return this;
}
}
public class UpdateDictImpl
{
internal readonly UpdateProvider<Dictionary<string, object>> _updateProvider;
internal UpdateDictImpl(IFreeSql orm)
{
_updateProvider = (orm as BaseDbProvider ?? throw new Exception("IFreeSql 无法转换成 BaseDbProvider"))
.CreateUpdateProvider<Dictionary<string, object>>(null) as UpdateProvider<Dictionary<string, object>>;
}
public UpdateDictImpl WherePrimary(params string[] primarys)
{
_updateProvider._tempPrimarys = GetPrimarys(_updateProvider._table, primarys);
return this;
}
public static ColumnInfo[] GetPrimarys(TableInfo table, params string[] primarys)
{
if (primarys?.Any() != true) throw new ArgumentException(nameof(primarys));
var pks = new List<ColumnInfo>();
foreach (var primary in primarys)
{
if (table.ColumnsByCs.TryGetValue(string.Concat(primary), out var col)) pks.Add(col);
else throw new Exception($"GetPrimarys 传递的参数 \"{primary}\" 不正确,它不属于字典数据的键名");
}
return pks.ToArray();
}
public UpdateDictImpl AsTable(string tableName)
{
_updateProvider.AsTable(tableName);
return this;
}
public UpdateDictImpl BatchOptions(int rowsLimit, int parameterLimit, bool autoTransaction = true)
{
_updateProvider.BatchOptions(rowsLimit, parameterLimit, autoTransaction);
return this;
}
public UpdateDictImpl BatchProgress(Action<BatchProgressStatus<Dictionary<string, object>>> callback)
{
_updateProvider.BatchProgress(callback);
return this;
}
public UpdateDictImpl CommandTimeout(int timeout)
{
_updateProvider.CommandTimeout(timeout);
return this;
}
public int ExecuteAffrows() => _updateProvider.ExecuteAffrows();
public List<Dictionary<string, object>> ExecuteUpdated() => _updateProvider.ExecuteUpdated();
#if net40
#else
public Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) => _updateProvider.ExecuteAffrowsAsync(cancellationToken);
public Task<List<Dictionary<string, object>>> ExecuteUpdatedAsync(CancellationToken cancellationToken = default) => _updateProvider.ExecuteUpdatedAsync(cancellationToken);
#endif
public UpdateDictImpl NoneParameter(bool isNotCommandParameter = true)
{
_updateProvider.NoneParameter(isNotCommandParameter);
return this;
}
public string ToSql() => _updateProvider.ToSql();
public UpdateDictImpl WithConnection(DbConnection connection)
{
_updateProvider.WithConnection(connection);
return this;
}
public UpdateDictImpl WithTransaction(DbTransaction transaction)
{
_updateProvider.WithTransaction(transaction);
return this;
}
}
public class DeleteDictImpl
{
internal readonly DeleteProvider<Dictionary<string, object>> _deleteProvider;
internal DeleteDictImpl(IFreeSql orm)
{
_deleteProvider = (orm as BaseDbProvider ?? throw new Exception("IFreeSql 无法转换成 BaseDbProvider"))
.CreateDeleteProvider<Dictionary<string, object>>(null) as DeleteProvider<Dictionary<string, object>>;
}
public DeleteDictImpl AsTable(string tableName)
{
_deleteProvider.AsTable(tableName);
return this;
}
public DeleteDictImpl CommandTimeout(int timeout)
{
_deleteProvider.CommandTimeout(timeout);
return this;
}
public int ExecuteAffrows() => _deleteProvider.ExecuteAffrows();
public List<Dictionary<string, object>> ExecuteDeleted() => _deleteProvider.ExecuteDeleted();
#if net40
#else
public Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default) => _deleteProvider.ExecuteAffrowsAsync(cancellationToken);
public Task<List<Dictionary<string, object>>> ExecuteDeletedAsync(CancellationToken cancellationToken = default) => _deleteProvider.ExecuteDeletedAsync(cancellationToken);
#endif
public string ToSql() => _deleteProvider.ToSql();
public DeleteDictImpl WithConnection(DbConnection connection)
{
_deleteProvider.WithConnection(connection);
return this;
}
public DeleteDictImpl WithTransaction(DbTransaction transaction)
{
_deleteProvider.WithTransaction(transaction);
return this;
}
}
#endregion
} }

View File

@ -1490,6 +1490,13 @@
<param name="tableRule"></param> <param name="tableRule"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.IDelete`1.AsTable(System.String)">
<summary>
设置表名
</summary>
<param name="tableName"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IDelete`1.AsType(System.Type)"> <member name="M:FreeSql.IDelete`1.AsType(System.Type)">
<summary> <summary>
动态Type在使用 Delete&lt;object&gt; 后使用本方法,指定实体类型 动态Type在使用 Delete&lt;object&gt; 后使用本方法,指定实体类型
@ -2953,6 +2960,13 @@
<param name="tableRule"></param> <param name="tableRule"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.IUpdate`1.AsTable(System.String)">
<summary>
设置表名
</summary>
<param name="tableName"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IUpdate`1.AsType(System.Type)"> <member name="M:FreeSql.IUpdate`1.AsType(System.Type)">
<summary> <summary>
动态Type在使用 Update&lt;object&gt; 后使用本方法,指定实体类型 动态Type在使用 Update&lt;object&gt; 后使用本方法,指定实体类型
@ -4639,6 +4653,48 @@
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSqlGlobalExtensions.InsertDict(IFreeSql,System.Collections.Generic.Dictionary{System.String,System.Object})">
<summary>
插入数据字典 Dictionary&lt;string, object&gt;
</summary>
<param name="source"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.InsertDict(IFreeSql,System.Collections.Generic.IEnumerable{System.Collections.Generic.Dictionary{System.String,System.Object}})">
<summary>
插入数据字典,传入 Dictionary&lt;string, object&gt; 集合
</summary>
<param name="source"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.UpdateDict(IFreeSql,System.Collections.Generic.Dictionary{System.String,System.Object})">
<summary>
更新数据字典 Dictionary&lt;string, object&gt;
</summary>
<param name="source"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.UpdateDict(IFreeSql,System.Collections.Generic.IEnumerable{System.Collections.Generic.Dictionary{System.String,System.Object}})">
<summary>
更新数据字典,传入 Dictionary&lt;string, object&gt; 集合
</summary>
<param name="source"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.DeleteDict(IFreeSql,System.Collections.Generic.Dictionary{System.String,System.Object})">
<summary>
删除数据字典 Dictionary&lt;string, object&gt;
</summary>
<param name="source"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.DeleteDict(IFreeSql,System.Collections.Generic.IEnumerable{System.Collections.Generic.Dictionary{System.String,System.Object}})">
<summary>
删除数据字典,传入 Dictionary&lt;string, object&gt; 集合
</summary>
<param name="source"></param>
<returns></returns>
</member>
<member name="M:System.Linq.Expressions.LambadaExpressionExtensions.And``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})"> <member name="M:System.Linq.Expressions.LambadaExpressionExtensions.And``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary> <summary>
使用 and 拼接两个 lambda 表达式 使用 and 拼接两个 lambda 表达式

View File

@ -95,6 +95,12 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
IDelete<T1> AsTable(Func<string, string> tableRule); IDelete<T1> AsTable(Func<string, string> tableRule);
/// <summary> /// <summary>
/// 设置表名
/// </summary>
/// <param name="tableName"></param>
/// <returns></returns>
IDelete<T1> AsTable(string tableName);
/// <summary>
/// 动态Type在使用 Delete&lt;object&gt; 后使用本方法,指定实体类型 /// 动态Type在使用 Delete&lt;object&gt; 后使用本方法,指定实体类型
/// </summary> /// </summary>
/// <param name="entityType"></param> /// <param name="entityType"></param>

View File

@ -227,6 +227,12 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
IUpdate<T1> AsTable(Func<string, string> tableRule); IUpdate<T1> AsTable(Func<string, string> tableRule);
/// <summary> /// <summary>
/// 设置表名
/// </summary>
/// <param name="tableName"></param>
/// <returns></returns>
IUpdate<T1> AsTable(string tableName);
/// <summary>
/// 动态Type在使用 Update&lt;object&gt; 后使用本方法,指定实体类型 /// 动态Type在使用 Update&lt;object&gt; 后使用本方法,指定实体类型
/// </summary> /// </summary>
/// <param name="entityType"></param> /// <param name="entityType"></param>

View File

@ -15,7 +15,11 @@ namespace FreeSql.Internal.CommonProvider
public ISelect<T1> Select<T1>() where T1 : class => CreateSelectProvider<T1>(null); public ISelect<T1> Select<T1>() where T1 : class => CreateSelectProvider<T1>(null);
public ISelect<T1> Select<T1>(object dywhere) where T1 : class => CreateSelectProvider<T1>(dywhere); public ISelect<T1> Select<T1>(object dywhere) where T1 : class => CreateSelectProvider<T1>(dywhere);
public IInsert<T1> Insert<T1>() where T1 : class => CreateInsertProvider<T1>(); public IInsert<T1> Insert<T1>() where T1 : class
{
if (typeof(T1) == typeof(Dictionary<string, object>)) throw new Exception("请使用 fsql.InsertDict(dict) 方法插入字典数据");
return CreateInsertProvider<T1>();
}
public IInsert<T1> Insert<T1>(T1 source) where T1 : class => this.Insert<T1>().AppendData(source); public IInsert<T1> Insert<T1>(T1 source) where T1 : class => this.Insert<T1>().AppendData(source);
public IInsert<T1> Insert<T1>(T1[] source) where T1 : class => this.Insert<T1>().AppendData(source); public IInsert<T1> Insert<T1>(T1[] source) where T1 : class => this.Insert<T1>().AppendData(source);
public IInsert<T1> Insert<T1>(List<T1> source) where T1 : class => this.Insert<T1>().AppendData(source); public IInsert<T1> Insert<T1>(List<T1> source) where T1 : class => this.Insert<T1>().AppendData(source);

View File

@ -147,6 +147,11 @@ namespace FreeSql.Internal.CommonProvider
_tableRule = tableRule; _tableRule = tableRule;
return this; return this;
} }
public IDelete<T1> AsTable(string tableName)
{
_tableRule = (oldname) => tableName;
return this;
}
public IDelete<T1> AsType(Type entityType) public IDelete<T1> AsType(Type entityType)
{ {
if (entityType == typeof(object)) throw new Exception("IDelete.AsType 参数不支持指定为 object"); if (entityType == typeof(object)) throw new Exception("IDelete.AsType 参数不支持指定为 object");

View File

@ -88,14 +88,14 @@ namespace FreeSql.Internal.CommonProvider
} }
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict) public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (data == null) return; if (data == null || table == null) return;
if (typeof(T1) == typeof(object) && new[] { table.Type, table.TypeLazy }.Contains(data.GetType()) == false) if (typeof(T1) == typeof(object) && new[] { table.Type, table.TypeLazy }.Contains(data.GetType()) == false)
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。"); throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
if (orm.Aop.AuditValueHandler == null) return; if (orm.Aop.AuditValueHandler == null) return;
foreach (var col in table.Columns.Values) foreach (var col in table.Columns.Values)
{ {
object val = col.GetValue(data); object val = col.GetValue(data);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.InsertOrUpdate, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.InsertOrUpdate, col, table.Properties.TryGetValue(col.CsName, out var tryprop) ? tryprop : null, val);
orm.Aop.AuditValueHandler(sender, auditArgs); orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.ValueIsChanged) if (auditArgs.ValueIsChanged)
{ {

View File

@ -116,7 +116,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (source != null) if (source != null)
{ {
GetDictionaryTableInfo(source, _orm, ref _table); UpdateProvider<T1>.GetDictionaryTableInfo(source, _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.Add(source); _source.Add(source);
} }
@ -126,7 +126,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (source != null) if (source != null)
{ {
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); UpdateProvider<T1>.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source); _source.AddRange(source);
} }
@ -137,49 +137,13 @@ namespace FreeSql.Internal.CommonProvider
if (source != null) if (source != null)
{ {
source = source.Where(a => a != null).ToList(); source = source.Where(a => a != null).ToList();
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table); UpdateProvider<T1>.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source); _source.AddRange(source);
} }
return this; return this;
} }
public static void GetDictionaryTableInfo(T1 source, IFreeSql orm, ref TableInfo table)
{
if (table == null && typeof(T1) == typeof(Dictionary<string, object>))
{
var dic = source as Dictionary<string, object>;
table = new TableInfo();
table.Type = typeof(Dictionary<string, object>);
table.CsName = dic.TryGetValue("", out var tryval) ? string.Concat(tryval) : "";
table.DbName = table.CsName;
table.DisableSyncStructure = true;
table.IsDictionaryType = true;
var colpos = new List<ColumnInfo>();
foreach (var kv in dic)
{
var colName = kv.Key;
if (orm.CodeFirst.IsSyncStructureToLower) colName = colName.ToLower();
if (orm.CodeFirst.IsSyncStructureToUpper) colName = colName.ToUpper();
var col = new ColumnInfo
{
CsName = kv.Key,
Table = table,
Attribute = new DataAnnotations.ColumnAttribute
{
Name = colName,
MapType = typeof(object)
},
CsType = typeof(object)
};
table.Columns.Add(colName, col);
table.ColumnsByCs.Add(kv.Key, col);
colpos.Add(col);
}
table.ColumnsByPosition = colpos.ToArray();
colpos.Clear();
}
}
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict) public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (data?.Any() != true) return; if (data?.Any() != true) return;
@ -196,7 +160,7 @@ namespace FreeSql.Internal.CommonProvider
object val = col.GetValue(data); object val = col.GetValue(data);
if (orm.Aop.AuditValueHandler != null) if (orm.Aop.AuditValueHandler != null)
{ {
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties.TryGetValue(col.CsName, out var tryprop) ? tryprop : null, val);
orm.Aop.AuditValueHandler(sender, auditArgs); orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.ValueIsChanged) if (auditArgs.ValueIsChanged)
{ {

View File

@ -46,7 +46,7 @@ namespace FreeSql.Internal.CommonProvider
_commonUtils = commonUtils; _commonUtils = commonUtils;
_commonExpression = commonExpression; _commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1)); _table = _commonUtils.GetTableByEntity(typeof(T1));
_tempPrimarys = _table.Primarys; _tempPrimarys = _table?.Primarys ?? new ColumnInfo[0];
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter; _noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
this.Where(_commonUtils.WhereObject(_table, "", dywhere)); this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>(); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
@ -60,7 +60,7 @@ namespace FreeSql.Internal.CommonProvider
protected void IgnoreCanUpdate() protected void IgnoreCanUpdate()
{ {
if (_table == null || _table.Type == typeof(object)) return; if (_table == null || _table.Type == typeof(object)) return;
foreach (var col in _table.Columns.Values) foreach (var col in _table?.Columns.Values)
if (col.Attribute.CanUpdate == false && _ignore.ContainsKey(col.Attribute.Name) == false) if (col.Attribute.CanUpdate == false && _ignore.ContainsKey(col.Attribute.Name) == false)
_ignore.Add(col.Attribute.Name, true); _ignore.Add(col.Attribute.Name, true);
} }
@ -377,7 +377,7 @@ namespace FreeSql.Internal.CommonProvider
foreach (var col in table.Columns.Values) foreach (var col in table.Columns.Values)
{ {
object val = col.GetValue(d); object val = col.GetValue(d);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties.TryGetValue(col.CsName, out var tryprop) ? tryprop : null, val);
orm.Aop.AuditValueHandler(sender, auditArgs); orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.ValueIsChanged) if (auditArgs.ValueIsChanged)
{ {
@ -393,13 +393,13 @@ namespace FreeSql.Internal.CommonProvider
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict) public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (orm.Aop.AuditValueHandler == null) return; if (orm.Aop.AuditValueHandler == null) return;
if (data == null) return; if (data == null || table == null) return;
if (typeof(T1) == typeof(object) && new[] { table.Type, table.TypeLazy }.Contains(data.GetType()) == false) if (typeof(T1) == typeof(object) && new[] { table.Type, table.TypeLazy }.Contains(data.GetType()) == false)
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。"); throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
foreach (var col in table.Columns.Values) foreach (var col in table.Columns.Values)
{ {
object val = col.GetValue(data); object val = col.GetValue(data);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties.TryGetValue(col.CsName, out var tryprop) ? tryprop : null, val);
orm.Aop.AuditValueHandler(sender, auditArgs); orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.ValueIsChanged) if (auditArgs.ValueIsChanged)
{ {
@ -412,10 +412,49 @@ namespace FreeSql.Internal.CommonProvider
} }
} }
public static void GetDictionaryTableInfo(T1 source, IFreeSql orm, ref TableInfo table)
{
if (table == null && typeof(T1) == typeof(Dictionary<string, object>))
{
if (source == null) throw new ArgumentNullException(nameof(source));
var dic = source as Dictionary<string, object>;
table = new TableInfo();
table.Type = typeof(Dictionary<string, object>);
table.CsName = dic.TryGetValue("", out var tryval) ? string.Concat(tryval) : "";
table.DbName = table.CsName;
table.DisableSyncStructure = true;
table.IsDictionaryType = true;
var colpos = new List<ColumnInfo>();
foreach (var kv in dic)
{
var colName = kv.Key;
if (orm.CodeFirst.IsSyncStructureToLower) colName = colName.ToLower();
if (orm.CodeFirst.IsSyncStructureToUpper) colName = colName.ToUpper();
var col = new ColumnInfo
{
CsName = kv.Key,
Table = table,
Attribute = new DataAnnotations.ColumnAttribute
{
Name = colName,
MapType = typeof(object)
},
CsType = typeof(object)
};
table.Columns.Add(colName, col);
table.ColumnsByCs.Add(kv.Key, col);
colpos.Add(col);
}
table.ColumnsByPosition = colpos.ToArray();
colpos.Clear();
}
}
public IUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source }); public IUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
public IUpdate<T1> SetSource(IEnumerable<T1> source, Expression<Func<T1, object>> tempPrimarys = null) public IUpdate<T1> SetSource(IEnumerable<T1> source, Expression<Func<T1, object>> tempPrimarys = null)
{ {
if (source == null || source.Any() == false) return this; if (source == null || source.Any() == false) return this;
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source.Where(a => a != null)); _source.AddRange(source.Where(a => a != null));
@ -665,6 +704,11 @@ namespace FreeSql.Internal.CommonProvider
_tableRule = tableRule; _tableRule = tableRule;
return this; return this;
} }
public IUpdate<T1> AsTable(string tableName)
{
_tableRule = (oldname) => tableName;
return this;
}
public IUpdate<T1> AsType(Type entityType) public IUpdate<T1> AsType(Type entityType)
{ {
if (entityType == typeof(object)) throw new Exception("IUpdate.AsType 参数不支持指定为 object"); if (entityType == typeof(object)) throw new Exception("IUpdate.AsType 参数不支持指定为 object");

View File

@ -51,7 +51,17 @@ namespace FreeSql.Internal.Model
/// </summary> /// </summary>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <param name="val"></param> /// <param name="val"></param>
public void SetValue(object obj, object val) => Table.SetPropertyValue(obj, CsName, Utils.GetDataReaderValue(CsType, val)); public void SetValue(object obj, object val)
{
if (Table.IsDictionaryType)
{
var dic = obj as Dictionary<string, object>;
if (dic.ContainsKey(CsName)) dic[CsName] = val;
else dic.Add(CsName, val);
return;
}
Table.SetPropertyValue(obj, CsName, Utils.GetDataReaderValue(CsType, val));
}