mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 fsql.InsertDict/UpdateDict/DeleteDict 字典操作的扩展方法;#481
This commit is contained in:
@ -15,7 +15,11 @@ namespace FreeSql.Internal.CommonProvider
|
||||
|
||||
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 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>(List<T1> source) where T1 : class => this.Insert<T1>().AppendData(source);
|
||||
|
@ -147,6 +147,11 @@ namespace FreeSql.Internal.CommonProvider
|
||||
_tableRule = tableRule;
|
||||
return this;
|
||||
}
|
||||
public IDelete<T1> AsTable(string tableName)
|
||||
{
|
||||
_tableRule = (oldname) => tableName;
|
||||
return this;
|
||||
}
|
||||
public IDelete<T1> AsType(Type entityType)
|
||||
{
|
||||
if (entityType == typeof(object)) throw new Exception("IDelete.AsType 参数不支持指定为 object");
|
||||
|
@ -88,14 +88,14 @@ 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()}) 不一致,请检查。");
|
||||
if (orm.Aop.AuditValueHandler == null) return;
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
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);
|
||||
if (auditArgs.ValueIsChanged)
|
||||
{
|
||||
|
@ -116,7 +116,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (source != null)
|
||||
{
|
||||
GetDictionaryTableInfo(source, _orm, ref _table);
|
||||
UpdateProvider<T1>.GetDictionaryTableInfo(source, _orm, ref _table);
|
||||
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
|
||||
_source.Add(source);
|
||||
}
|
||||
@ -126,7 +126,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (source != null)
|
||||
{
|
||||
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
|
||||
UpdateProvider<T1>.GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
|
||||
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
|
||||
_source.AddRange(source);
|
||||
}
|
||||
@ -137,49 +137,13 @@ namespace FreeSql.Internal.CommonProvider
|
||||
if (source != null)
|
||||
{
|
||||
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);
|
||||
_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;
|
||||
@ -196,7 +160,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
object val = col.GetValue(data);
|
||||
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);
|
||||
if (auditArgs.ValueIsChanged)
|
||||
{
|
||||
|
@ -46,7 +46,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
_commonUtils = commonUtils;
|
||||
_commonExpression = commonExpression;
|
||||
_table = _commonUtils.GetTableByEntity(typeof(T1));
|
||||
_tempPrimarys = _table.Primarys;
|
||||
_tempPrimarys = _table?.Primarys ?? new ColumnInfo[0];
|
||||
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
|
||||
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
||||
@ -60,7 +60,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
protected void IgnoreCanUpdate()
|
||||
{
|
||||
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)
|
||||
_ignore.Add(col.Attribute.Name, true);
|
||||
}
|
||||
@ -377,7 +377,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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)
|
||||
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
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);
|
||||
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(IEnumerable<T1> source, Expression<Func<T1, object>> tempPrimarys = null)
|
||||
{
|
||||
if (source == null || source.Any() == false) return this;
|
||||
GetDictionaryTableInfo(source.FirstOrDefault(), _orm, ref _table);
|
||||
AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
|
||||
_source.AddRange(source.Where(a => a != null));
|
||||
|
||||
@ -665,6 +704,11 @@ namespace FreeSql.Internal.CommonProvider
|
||||
_tableRule = tableRule;
|
||||
return this;
|
||||
}
|
||||
public IUpdate<T1> AsTable(string tableName)
|
||||
{
|
||||
_tableRule = (oldname) => tableName;
|
||||
return this;
|
||||
}
|
||||
public IUpdate<T1> AsType(Type entityType)
|
||||
{
|
||||
if (entityType == typeof(object)) throw new Exception("IUpdate.AsType 参数不支持指定为 object");
|
||||
|
@ -51,7 +51,17 @@ namespace FreeSql.Internal.Model
|
||||
/// </summary>
|
||||
/// <param name="obj"></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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user