Skip to content

Commit

Permalink
v2.0.10 更新内容:1. 更新数据时,支持设置只更新某些字段,支持设置不更新某些字段。2. ISqlString接口移除之前版本标记…
Browse files Browse the repository at this point in the history
…为Obsolete的方法。
  • Loading branch information
0611163 committed Jan 3, 2024
1 parent 4ef81e3 commit 475a192
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 175 deletions.
2 changes: 1 addition & 1 deletion Dapper.Lite/DAL/BsOrderDal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ from bs_order t
sql.Append(" order by t.order_time desc, t.id asc ");

pageModel.TotalRows = sql.QueryCount();
List<BsOrder> result = sql.QueryPage<BsOrder>(null, pageModel.PageSize, pageModel.CurrentPage);
List<BsOrder> result = sql.ToPageList<BsOrder>(null, pageModel.PageSize, pageModel.CurrentPage);
return result;
}
#endregion
Expand Down
10 changes: 6 additions & 4 deletions Dapper.Lite/Dapper.Lite/Dapper.Lite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>disable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>Dapper.Lite</Title>
<AssemblyVersion>2.0.9</AssemblyVersion>
<FileVersion>2.0.9</FileVersion>
<Version>2.0.9</Version>
<AssemblyVersion>2.0.10</AssemblyVersion>
<FileVersion>2.0.10</FileVersion>
<Version>2.0.10</Version>
<PackageId>Dapper.Lite</PackageId>
<PackageProjectUrl>https://github.com/0611163/Dapper.Lite</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -20,7 +20,9 @@
</Description>
<PackageReleaseNotes>
更新内容:
1. 修复bug:防注入过滤会把查询语句中的字段FDelete替换成F
1. 更新数据时,支持设置只更新某些字段,支持设置不更新某些字段。
2. ISqlString接口移除之前版本标记为Obsolete的方法。替代方法:Query的替代方法是First,QueryAsync的替代方法是FirstAsync,QueryList的替代方法是ToList,QueryListAsync的替代方法是ToListAsync
QueryPage的替代方法是ToPageList,QueryPageAsync的替代方法是ToPageListAsync,DeleteByCondition的替代方法是Delete,DeleteByConditionAsync的替代方法是DeleteAsync。
</PackageReleaseNotes>
</PropertyGroup>

Expand Down
69 changes: 63 additions & 6 deletions Dapper.Lite/Dapper.Lite/Session/DbSessionUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Linq.Expressions;

namespace Dapper.Lite
{
Expand All @@ -16,7 +17,17 @@ public partial class DbSession : IDbSession
/// <summary>
/// 新旧数据集合 key:新数据 value:旧数据
/// </summary>
private ConcurrentDictionary<object, object> _oldObjs = new ConcurrentDictionary<object, object>();
private readonly ConcurrentDictionary<object, object> _oldObjs = new ConcurrentDictionary<object, object>();

/// <summary>
/// 只更新这些字段
/// </summary>
private readonly ConcurrentDictionary<Type, HashSet<string>> _updateColumns = new ConcurrentDictionary<Type, HashSet<string>>();

/// <summary>
/// 不更新这些字段
/// </summary>
private readonly ConcurrentDictionary<Type, HashSet<string>> _ignoreColumns = new ConcurrentDictionary<Type, HashSet<string>>();
#endregion

#region AttachOld 附加更新前的旧实体
Expand All @@ -27,14 +38,12 @@ public void AttachOld<T>(T obj)
{
if (_oldObjs.ContainsKey(obj))
{
object temp;
_oldObjs.TryRemove(obj, out temp);
_oldObjs.TryRemove(obj, out object _);
}

if (!_oldObjs.ContainsKey(obj))
{
object cloneObj;
cloneObj = ModelMapper<T>.Map(obj);
object cloneObj = ModelMapper<T>.Map(obj);
_oldObjs.TryAdd(obj, cloneObj);
}
}
Expand All @@ -51,6 +60,44 @@ public void AttachOld<T>(List<T> objList)
}
#endregion

#region UpdateColumns 设置只更新某些字段
/// <summary>
/// 设置只更新某些字段
/// </summary>
public IDbSession UpdateColumns<T>(Expression<Func<T, object>> expression)
{
Type type = expression.Body.Type;
PropertyInfo[] props = type.GetProperties();
HashSet<string> updateColumnsSet = new HashSet<string>();
_updateColumns.TryAdd(typeof(T), updateColumnsSet);
foreach (PropertyInfo propertyInfo in props)
{
updateColumnsSet.Add(propertyInfo.Name);
}

return this;
}
#endregion

#region IgnoreColumns 设置不更新某些字段
/// <summary>
/// 设置不更新某些字段
/// </summary>
public IDbSession IgnoreColumns<T>(Expression<Func<T, object>> expression)
{
Type type = expression.Body.Type;
PropertyInfo[] props = type.GetProperties();
HashSet<string> ignoreColumnsSet = new HashSet<string>();
_ignoreColumns.TryAdd(typeof(T), ignoreColumnsSet);
foreach (PropertyInfo propertyInfo in props)
{
ignoreColumnsSet.Add(propertyInfo.Name);
}

return this;
}
#endregion


#region Update 修改
/// <summary>
Expand Down Expand Up @@ -198,10 +245,15 @@ private void PrepareUpdateSql(object obj, object oldObj, ref StringBuilder strSq
List<DbParameter> paramList = new List<DbParameter>();
PropertyInfoEx[] propertyInfoList = GetEntityProperties(type);
StringBuilder sbPros = new StringBuilder();
_updateColumns.TryGetValue(type, out HashSet<string> updateColumnsSet);
_ignoreColumns.TryGetValue(type, out HashSet<string> ignoreColumnsSet);
foreach (PropertyInfoEx propertyInfoEx in propertyInfoList)
{
PropertyInfo propertyInfo = propertyInfoEx.PropertyInfo;

if (updateColumnsSet != null && updateColumnsSet.Count > 0 && !updateColumnsSet.Contains(propertyInfo.Name)) continue;
if (ignoreColumnsSet != null && ignoreColumnsSet.Count > 0 && ignoreColumnsSet.Contains(propertyInfo.Name)) continue;

if (propertyInfoEx.IsReadOnly) continue;

if (propertyInfoEx.IsDBField && !propertyInfoEx.IsDBKey)
Expand Down Expand Up @@ -242,19 +294,24 @@ private void PrepareUpdateSql<T>(List<T> list, List<T> oldList, ref StringBuilde

Tuple<string, string, string> updateTmpl = _provider.CreateUpdateSqlTempldate();
List<DbParameter> paramList = new List<DbParameter>();
_updateColumns.TryGetValue(type, out HashSet<string> updateColumnsSet);
_ignoreColumns.TryGetValue(type, out HashSet<string> ignoreColumnsSet);
PropertyInfoEx[] propertyInfoList = GetEntityProperties(type);
for (int n = 0; n < list.Count; n++)
{
T obj = list[n];
T oldObj = oldList[n];

int subSavedCount = 0;

PropertyInfoEx[] propertyInfoList = GetEntityProperties(type);
StringBuilder sbPros = new StringBuilder();
foreach (PropertyInfoEx propertyInfoEx in propertyInfoList)
{
PropertyInfo propertyInfo = propertyInfoEx.PropertyInfo;

if (updateColumnsSet != null && updateColumnsSet.Count > 0 && !updateColumnsSet.Contains(propertyInfo.Name)) continue;
if (ignoreColumnsSet != null && ignoreColumnsSet.Count > 0 && ignoreColumnsSet.Contains(propertyInfo.Name)) continue;

if (propertyInfoEx.IsReadOnly) continue;

if (propertyInfoEx.IsDBField && !propertyInfoEx.IsDBKey)
Expand Down
11 changes: 11 additions & 0 deletions Dapper.Lite/Dapper.Lite/Session/IDbSessionUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -47,5 +48,15 @@ public partial interface IDbSession
/// 附加更新前的旧数据,只更新数据发生变化的字段
/// </summary>
void AttachOld<T>(List<T> objList);

/// <summary>
/// 设置只更新某些字段
/// </summary>
IDbSession UpdateColumns<T>(Expression<Func<T, object>> expression);

/// <summary>
/// 设置不更新某些字段
/// </summary>
IDbSession IgnoreColumns<T>(Expression<Func<T, object>> expression);
}
}
62 changes: 0 additions & 62 deletions Dapper.Lite/Dapper.Lite/SqlString/ISqlString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,67 +277,5 @@ public interface ISqlString
Task<CountResult> QueryCountAsync(int pageSize);
#endregion

#region Obsolete 增删改查接口
/// <summary>
/// 查询实体
/// </summary>
[Obsolete]
T Query<T>();

/// <summary>
/// 查询实体
/// </summary>
[Obsolete]
Task<T> QueryAsync<T>();

/// <summary>
/// 查询列表
/// </summary>
[Obsolete]
List<T> QueryList<T>();

/// <summary>
/// 查询列表
/// </summary>
[Obsolete]
Task<List<T>> QueryListAsync<T>();

/// <summary>
/// 分页查询
/// </summary>
[Obsolete]
List<T> QueryPage<T>(string orderby, int pageSize, int currentPage);

/// <summary>
/// 分页查询
/// </summary>
[Obsolete]
Task<List<T>> QueryPageAsync<T>(string orderby, int pageSize, int currentPage);

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
int DeleteByCondition<T>();

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
Task<int> DeleteByConditionAsync<T>();

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
int DeleteByCondition(Type type);

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
Task<int> DeleteByConditionAsync(Type type);
#endregion

}
}
92 changes: 0 additions & 92 deletions Dapper.Lite/Dapper.Lite/SqlString/SqlString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,97 +812,5 @@ public Task<CountResult> QueryCountAsync(int pageSize)

#endregion

#region Obsolete 增删改查接口
/// <summary>
/// 查询实体
/// </summary>
[Obsolete]
public T Query<T>()
{
return _session.Query<T>(this);
}

/// <summary>
/// 查询实体
/// </summary>
[Obsolete]
public Task<T> QueryAsync<T>()
{
return _session.QueryAsync<T>(this);
}

/// <summary>
/// 查询列表
/// </summary>
[Obsolete]
public List<T> QueryList<T>()
{
return _session.QueryList<T>(this);
}

/// <summary>
/// 查询列表
/// </summary>
[Obsolete]
public Task<List<T>> QueryListAsync<T>()
{
return _session.QueryListAsync<T>(this);
}

/// <summary>
/// 分页查询
/// </summary>
[Obsolete]
public List<T> QueryPage<T>(string orderby, int pageSize, int currentPage)
{
return _session.QueryPage<T>(this, orderby, pageSize, currentPage);
}

/// <summary>
/// 分页查询
/// </summary>
[Obsolete]
public Task<List<T>> QueryPageAsync<T>(string orderby, int pageSize, int currentPage)
{
return _session.QueryPageAsync<T>(this, orderby, pageSize, currentPage);
}

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
public int DeleteByCondition<T>()
{
return _session.DeleteByCondition<T>(this);
}

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
public Task<int> DeleteByConditionAsync<T>()
{
return _session.DeleteByConditionAsync<T>(this);
}

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
public int DeleteByCondition(Type type)
{
return _session.DeleteByCondition(type, this);
}

/// <summary>
/// 条件删除
/// </summary>
[Obsolete]
public Task<int> DeleteByConditionAsync(Type type)
{
return _session.DeleteByConditionAsync(type, this);
}
#endregion

}
}
4 changes: 2 additions & 2 deletions Dapper.Lite/Dapper.LiteTest/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ and t.password like @Password

.Append(" and t.create_time <= @EndTime ", new { EndTime = new DateTime(2022, 8, 1) })

.QueryList<SysUser>();
.ToList<SysUser>();

long id = session.Sql("select id from sys_user where id=@Id", new { Id = 1 })
.QuerySingle<long>();
Expand Down Expand Up @@ -231,7 +231,7 @@ public void TestIDapperLiteClient2()
long id = db.Sql("select id from sys_user where id=@Id", new { Id = 1 }).QuerySingle<long>();
Assert.IsTrue(id == 1);

List<SysUser> list = sql.QueryList<SysUser>();
List<SysUser> list = sql.ToList<SysUser>();
foreach (SysUser item in list)
{
Console.WriteLine(ModelToStringUtil.ToString(item));
Expand Down
4 changes: 2 additions & 2 deletions Dapper.Lite/Dapper.LiteTest/SplitTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public long Test1InsertInternal(long start, long index)

SysUser userInfo = session.Sql(
"select * from sys_user_202208 where id = @Id", new { user.Id })
.Query<SysUser>();
.First<SysUser>();
Assert.IsTrue(userInfo != null);

Assert.IsTrue(userInfo.Remark == (start + index).ToString());
Expand Down Expand Up @@ -125,7 +125,7 @@ public void Test2Update()

SysUser userInfo = session.Sql(
"select * from sys_user_202208 where Remark like @Remark", new { Remark = "测试修改分表数据%" })
.Query<SysUser>();
.First<SysUser>();
Assert.IsTrue(userInfo.Remark == user.Remark);

Console.WriteLine("用户 ID=" + user.Id + " 已修改");
Expand Down
Loading

0 comments on commit 475a192

Please sign in to comment.