Skip to content

Commit

Permalink
[fix] 统一约定,updateColumns 外部传入Name,内部再根据columns转为专用字段名
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 28, 2024
1 parent 1c00c17 commit 5a90283
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 57 deletions.
12 changes: 9 additions & 3 deletions XCode/DataAccessLayer/Common/DbSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,8 @@ public virtual Task<T> ExecuteAsync<T>(DbCommand cmd, Boolean query, Func<DbComm
/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public virtual Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list) => throw new NotSupportedException();
Expand Down Expand Up @@ -835,6 +835,12 @@ protected virtual void BuildBatchValues(StringBuilder sb, DbBase db, String acti
sb.Length--;
}

/// <summary>生成重复键更新语句</summary>
/// <param name="sb">SQL拼接器</param>
/// <param name="db">数据库</param>
/// <param name="columns">所有字段</param>
/// <param name="updateColumns">需要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">需要累加的字段。属性名,不是字段名</param>
protected virtual void BuildDuplicateKey(StringBuilder sb, DbBase db, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns)
{
// 重复键执行update
Expand Down Expand Up @@ -1024,7 +1030,7 @@ public void WriteSQL(String sql)
sb.AppendFormat("{0}={1}", ps[i].ParameterName, sv);
}
sb.Append(']');
sql = sb.Put(true);
sql = sb.Return(true);
}

// 截断超长字符串
Expand Down
8 changes: 4 additions & 4 deletions XCode/DataAccessLayer/Common/IDbSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ public interface IDbSession : IDisposable2
/// <summary>批量更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要更新的字段,默认所有字段</param>
/// <param name="updateColumns">要更新的字段,默认脏数据</param>
/// <param name="addColumns">要累加更新的字段,默认累加</param>
/// <param name="updateColumns">要更新的字段,默认脏数据。属性名,不是字段名</param>
/// <param name="addColumns">要累加更新的字段,默认累加。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
Int32 Update(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list);

/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list);
Expand Down
13 changes: 10 additions & 3 deletions XCode/DataAccessLayer/Database/DB2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection
sb.Length--;
sb.Append(')');

return sb.Put(true);
return sb.Return(true);
}

private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection<String> ps, IEnumerable<IModel> list)
Expand Down Expand Up @@ -478,6 +478,13 @@ private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection<String
return dps.ToArray();
}

/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var ps = new HashSet<String>();
Expand All @@ -502,7 +509,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio
}
sb.AppendLine("END;");

var sql = sb.Put(true);
var sql = sb.Return(true);
DefaultSpan.Current?.AppendTag(sql);

var dps = GetParameters(columns, ps, list);
Expand Down Expand Up @@ -552,7 +559,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio
}
sb.Length -= " And ".Length;

return sb.Put(true);
return sb.Return(true);
}

public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
Expand Down
13 changes: 10 additions & 3 deletions XCode/DataAccessLayer/Database/DaMeng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection
sb.Length--;
sb.Append(')');

return sb.Put(true);
return sb.Return(true);
}

private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection<String> ps, IEnumerable<IModel> list)
Expand Down Expand Up @@ -363,6 +363,13 @@ private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection<String
return dps.ToArray();
}

/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var ps = new HashSet<String>();
Expand All @@ -387,7 +394,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio
}
sb.AppendLine("END;");

var sql = sb.Put(true);
var sql = sb.Return(true);
DefaultSpan.Current?.AppendTag(sql);

var dps = GetParameters(columns, ps, list);
Expand Down Expand Up @@ -437,7 +444,7 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio
}
sb.Length -= " And ".Length;

return sb.Put(true);
return sb.Return(true);
}

public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
Expand Down
15 changes: 11 additions & 4 deletions XCode/DataAccessLayer/Database/Hana.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ on duplicate key update
updatetime=values(updatetime);
*/

private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)
private String GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sb = Pool.StringBuilder.Get();
var db = Database as DbBase;
Expand All @@ -284,7 +284,7 @@ private String GetBatchSql(String action, IDataTable table, IDataColumn[] column
// 重复键执行update
BuildDuplicateKey(sb, db, columns, updateColumns, addColumns);

return sb.Put(true);
return sb.Return(true);
}

public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable<IModel> list)
Expand All @@ -305,7 +305,14 @@ public override Int32 Replace(IDataTable table, IDataColumn[] columns, IEnumerab
return Execute(sql);
}

public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)
/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list);
return Execute(sql);
Expand Down Expand Up @@ -586,7 +593,7 @@ public override String CreateTableSQL(IDataTable table)
sb.Append(" DEFAULT CHARSET=utf8mb4");
sb.Append(';');

return sb.Put(true);
return sb.Return(true);
}

public override String? AddTableDescriptionSQL(IDataTable table)
Expand Down
16 changes: 12 additions & 4 deletions XCode/DataAccessLayer/Database/HighGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override Task<Int64> QueryCountFastAsync(String tableName)
#endregion

#region 批量操作
string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)
string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sb = Pool.StringBuilder.Get();
var db = Database as DbBase;
Expand Down Expand Up @@ -154,14 +154,22 @@ string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, IColl
sb.Length--;
}
}
return sb.Put(true);
return sb.Return(true);
}
public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable<IModel> list)
{
var sql = GetBatchSql("Insert Into", table, columns, null, null, list);
return Execute(sql);
}
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)

/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list);
return Execute(sql);
Expand Down Expand Up @@ -430,7 +438,7 @@ public override String CreateIndexSQL(IDataIndex index)
var dcs = index.Table.GetColumns(index.Columns);
sb.AppendFormat(" On {0} USING btree ({1})", FormatName(index.Table), dcs.Join(",", FormatName));

return sb.Put(true);
return sb.Return(true);
}
#endregion
}
19 changes: 14 additions & 5 deletions XCode/DataAccessLayer/Database/KingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override Task<Int64> InsertAndGetIdentityAsync(String sql, CommandType ty
}

#region 批量操作
string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)
string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sb = Pool.StringBuilder.Get();
var db = Database as DbBase;
Expand All @@ -133,14 +133,23 @@ string GetBatchSql(String action, IDataTable table, IDataColumn[] columns, IColl
// 重复键执行update
BuildDuplicateKey(sb, db, columns, updateColumns, addColumns);

return sb.Put(true);
return sb.Return(true);
}

public override Int32 Insert(IDataTable table, IDataColumn[] columns, IEnumerable<IModel> list)
{
var sql = GetBatchSql("Insert Into", table, columns, null, null, list);
return Execute(sql);
}
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)

/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list);
return Execute(sql);
Expand Down Expand Up @@ -488,7 +497,7 @@ public override String CreateTableSQL(IDataTable table)
sb.AppendLine();
sb.Append(");");
//if (!string.IsNullOrWhiteSpace(table.Description)) { sb.Append($" COMMENT '{table.Description}'"); } 非Mysql兼容模式下,不支持,所以直接注释掉
return sb.Put(true);
return sb.Return(true);
}
public override String DropTableSQL(IDataTable table) => $"Drop Table If Exists {FormatName(table)}";
public override String? AddTableDescriptionSQL(IDataTable table)
Expand Down Expand Up @@ -516,7 +525,7 @@ public override String CreateIndexSQL(IDataIndex index)
var dcs = index.Table.GetColumns(index.Columns);
sb.AppendFormat(" On {0} USING btree ({1})", FormatName(index.Table), dcs.Join(",", FormatName));

return sb.Put(true);
return sb.Return(true);
}
public override String DropIndexSQL(IDataIndex index) => $"DROP INDEX IF EXISTS \"{index.Name}\"";
//public virtual String AlertSequenceSQL(IDataTable table)
Expand Down
9 changes: 8 additions & 1 deletion XCode/DataAccessLayer/Database/MySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ public override Int32 Replace(IDataTable table, IDataColumn[] columns, IEnumerab
return Execute(sql);
}

/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var sql = GetBatchSql("Insert Into", table, columns, updateColumns, addColumns, list);
Expand Down Expand Up @@ -723,7 +730,7 @@ public override String CreateTableSQL(IDataTable table)
sb.Append(" DEFAULT CHARSET=utf8mb4");
sb.Append(';');

return sb.Put(true);
return sb.Return(true);
}

public override String AddTableDescriptionSQL(IDataTable table)
Expand Down
19 changes: 13 additions & 6 deletions XCode/DataAccessLayer/Database/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ private String GetInsertSql(IDataTable table, IDataColumn[] columns, ICollection
sb.Length--;
sb.Append(')');

return sb.Put(true);
return sb.Return(true);
}

private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection<String> ps, IEnumerable<IModel> list)
Expand Down Expand Up @@ -615,7 +615,14 @@ private IDataParameter[] GetParameters(IDataColumn[] columns, ICollection<String
return dps.ToArray();
}

public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)
/// <summary>批量插入或更新</summary>
/// <param name="table">数据表</param>
/// <param name="columns">要插入的字段,默认所有字段</param>
/// <param name="updateColumns">主键已存在时,要更新的字段。属性名,不是字段名</param>
/// <param name="addColumns">主键已存在时,要累加更新的字段。属性名,不是字段名</param>
/// <param name="list">实体列表</param>
/// <returns></returns>
public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var ps = new HashSet<String>();
var insert = GetInsertSql(table, columns, ps);
Expand All @@ -639,15 +646,15 @@ public override Int32 Upsert(IDataTable table, IDataColumn[] columns, ICollectio
}
sb.AppendLine("END;");

var sql = sb.Put(true);
var sql = sb.Return(true);
DefaultSpan.Current?.AppendTag(sql);

var dps = GetParameters(columns, ps, list);

return Execute(sql, CommandType.Text, dps);
}

private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, ICollection<String> ps)
private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, ICollection<String> ps)
{
if ((updateColumns == null || updateColumns.Count == 0)
&& (addColumns == null || addColumns.Count == 0)) return null;
Expand Down Expand Up @@ -689,10 +696,10 @@ private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection
}
sb.Length -= " And ".Length;

return sb.Put(true);
return sb.Return(true);
}

public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection<String> updateColumns, ICollection<String> addColumns, IEnumerable<IModel> list)
public override Int32 Update(IDataTable table, IDataColumn[] columns, ICollection<String>? updateColumns, ICollection<String>? addColumns, IEnumerable<IModel> list)
{
var ps = new HashSet<String>();
var sql = GetUpdateSql(table, columns, updateColumns, addColumns, ps);
Expand Down
Loading

0 comments on commit 5a90283

Please sign in to comment.