- 增加 fsql.Insert(Dictionary<string, object>) 无实体类插入方法;#481

This commit is contained in:
2881099
2022-03-24 18:06:54 +08:00
parent 56ce675b65
commit dc688adc11
47 changed files with 416 additions and 80 deletions

View File

@ -5,7 +5,7 @@
<Version>3.2.100</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Firebird, Odbc, 达梦, 人大金仓, 神舟通用, 翰高, And Access</Description>
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Firebird, Odbc, 达梦, 人大金仓, 神舟通用, 南大通用, 翰高, And Access</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
<RepositoryType>git</RepositoryType>

View File

@ -1629,6 +1629,13 @@
<param name="tableRule"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IInsert`1.AsTable(System.String)">
<summary>
设置表名
</summary>
<param name="tableName"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IInsert`1.AsType(System.Type)">
<summary>
动态Type在使用 Insert&lt;object&gt; 后使用本方法,指定实体类型

View File

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

View File

@ -94,7 +94,7 @@ namespace FreeSql.Internal.CommonProvider
protected void SyncStructure(params TypeAndName[] objects)
{
if (objects == null) return;
var syncObjects = objects.Where(a => a.entityType != typeof(object) && _dicSycedGetOrAdd(a.entityType).ContainsKey(GetTableNameLowerOrUpper(a.tableName)) == false && GetTableByEntity(a.entityType)?.DisableSyncStructure == false)
var syncObjects = objects.Where(a => a.entityType != null && a.entityType != typeof(object) && _dicSycedGetOrAdd(a.entityType).ContainsKey(GetTableNameLowerOrUpper(a.tableName)) == false && GetTableByEntity(a.entityType)?.DisableSyncStructure == false)
.Select(a => new TypeAndName(a.entityType, GetTableNameLowerOrUpper(a.tableName))).ToArray();
if (syncObjects.Any() == false) return;
var before = new Aop.SyncStructureBeforeEventArgs(syncObjects.Select(a => a.entityType).ToArray());

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace FreeSql.Internal.CommonProvider
{
@ -115,6 +116,7 @@ namespace FreeSql.Internal.CommonProvider
{
if (source != null)
{
GetDictionaryTableInfo(source, _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.Add(source);
}
@ -124,6 +126,7 @@ namespace FreeSql.Internal.CommonProvider
{
if (source != null)
{
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source);
}
@ -134,12 +137,49 @@ namespace FreeSql.Internal.CommonProvider
if (source != null)
{
source = source.Where(a => a != null).ToList();
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source);
}
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)
{
if (data?.Any() != true) return;
@ -148,7 +188,7 @@ namespace FreeSql.Internal.CommonProvider
}
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)
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
foreach (var col in table.Columns.Values)
@ -505,13 +545,14 @@ namespace FreeSql.Internal.CommonProvider
protected string TableRuleInvoke()
{
if (_tableRule == null) return _table.DbName;
var newname = _tableRule(_table.DbName);
if (newname == _table.DbName) return _table.DbName;
if (string.IsNullOrEmpty(newname)) return _table.DbName;
var tbname = _table?.DbName ?? "";
if (_tableRule == null) return tbname;
var newname = _tableRule(tbname);
if (newname == tbname) return tbname;
if (string.IsNullOrEmpty(newname)) return tbname;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table?.Type, newname);
return newname;
}
public IInsert<T1> AsTable(Func<string, string> tableRule)
@ -519,6 +560,11 @@ namespace FreeSql.Internal.CommonProvider
_tableRule = tableRule;
return this;
}
public IInsert<T1> AsTable(string tableName)
{
_tableRule = (oldname) => tableName;
return this;
}
public IInsert<T1> AsType(Type entityType)
{
if (entityType == typeof(object)) throw new Exception("IInsert.AsType 参数不支持指定为 object");

View File

@ -31,6 +31,7 @@ namespace FreeSql.Internal.Model
/// <returns></returns>
public object GetDbValue(object obj)
{
if (Table.IsDictionaryType) return (obj as Dictionary<string, object>)?.TryGetValue(CsName, out var tryval) == true ? tryval : null;
var dbval = Table.GetPropertyValue(obj, CsName);
//if (ConversionCsToDb != null) dbval = ConversionCsToDb(dbval);
if (Attribute.MapType != CsType) dbval = Utils.GetDataReaderValue(Attribute.MapType, dbval);
@ -40,7 +41,11 @@ namespace FreeSql.Internal.Model
/// 获取 obj.CsName 属性原始值(不经过 MapType
/// </summary>
/// <param name="obj"></param>
public object GetValue(object obj) => Table.GetPropertyValue(obj, CsName);
public object GetValue(object obj)
{
if (Table.IsDictionaryType) return (obj as Dictionary<string, object>)?.TryGetValue(CsName, out var tryval) == true ? tryval : null;
return Table.GetPropertyValue(obj, CsName);
}
/// <summary>
/// 设置 obj.CsName 属性值
/// </summary>

View File

@ -25,6 +25,7 @@ namespace FreeSql.Internal.Model
public bool DisableSyncStructure { get; set; }
public string Comment { get; internal set; }
public bool IsRereadSql { get; internal set; }
public bool IsDictionaryType { get; internal set; }
public ColumnInfo VersionColumn { get; set; }