Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 60 additions & 40 deletions Kogel.Dapper.Extension/Core/Interfaces/IDapperSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,76 @@

namespace Kogel.Dapper.Extension
{
public abstract class AbstractSet
{
/// <summary>
/// 数据解析提供方
/// </summary>
public SqlProvider SqlProvider;
public abstract class AbstractSet
{
/// <summary>
/// 数据解析提供方
/// </summary>
public SqlProvider SqlProvider;

/// <summary>
/// 表类型
/// </summary>
public Type TableType { get; set; }
/// <summary>
/// 表类型
/// </summary>
public Type TableType { get; set; }

/// <summary>
/// [已弃用]只用来生成对象
/// </summary>
internal LambdaExpression WhereExpression { get; set; }
/// <summary>
/// [已弃用]只用来生成对象
/// </summary>
internal LambdaExpression WhereExpression { get; set; }

<<<<<<< HEAD
/// <summary>
/// 条件表达式对象
/// </summary>
internal List<LambdaExpression> WhereExpressionList { get; set; }
public List<LambdaExpression> WhereExpressionList { get; set; }

/// <summary>
/// 表达式排序集合
/// </summary>
internal Dictionary<LambdaExpression, EOrderBy> OrderbyExpressionList { get; set; }
public Dictionary<LambdaExpression, EOrderBy> OrderbyExpressionList { get; set; }
=======
/// <summary>
/// 条件表达式对象
/// </summary>
public List<LambdaExpression> WhereExpressionList { get; set; }

/// <summary>
/// 字符串排序
/// </summary>
internal StringBuilder OrderbyBuilder { get; set; }
/// <summary>
/// 表达式排序集合
/// </summary>
public Dictionary<LambdaExpression, EOrderBy> OrderbyExpressionList { get; set; }
>>>>>>> 0f7f297adff43ead4861f5f52d9d2b740d15ad2e

/// <summary>
/// 字段查询对象
/// </summary>
public LambdaExpression SelectExpression { get; set; }
/// <summary>
/// 字符串排序
/// </summary>
internal StringBuilder OrderbyBuilder { get; set; }

/// <summary>
/// 是否锁表(with(nolock))
/// </summary>
public bool NoLock { get; set; }
/// <summary>
/// 字段查询对象
/// </summary>
public LambdaExpression SelectExpression { get; set; }

/// <summary>
/// sql字符串对象
/// </summary>
internal StringBuilder WhereBuilder { get; set; }
/// <summary>
/// 是否锁表(with(nolock))
/// </summary>
public bool NoLock { get; set; }

/// <summary>
/// sql参数对象
/// </summary>
internal DynamicParameters Params { get => SqlProvider.Params; set => SqlProvider.Params.AddDynamicParams(value); }
/// <summary>
/// sql字符串对象
/// </summary>
internal StringBuilder WhereBuilder { get; set; }

/// <summary>
/// 分组表达式对象
/// </summary>
internal List<LambdaExpression> GroupExpressionList { get; set; }
/// <summary>
/// sql参数对象
/// </summary>
internal DynamicParameters Params { get => SqlProvider.Params; set => SqlProvider.Params.AddDynamicParams(value); }

/// <summary>
/// 分组表达式对象
/// </summary>
internal List<LambdaExpression> GroupExpressionList { get; set; }

<<<<<<< HEAD
/// <summary>
/// 分组聚合条件
/// </summary>
Expand All @@ -74,4 +87,11 @@ public abstract class AbstractSet
/// </summary>
public bool IsDistinct { get; set; } = false;
}
=======
/// <summary>
/// 分组聚合条件
/// </summary>
internal List<LambdaExpression> HavingExpressionList { get; set; }
}
>>>>>>> 0f7f297adff43ead4861f5f52d9d2b740d15ad2e
}
22 changes: 11 additions & 11 deletions Kogel.Dapper.Extension/Core/Interfaces/IResolveExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
Expand Down Expand Up @@ -71,8 +72,7 @@ public virtual string ResolveWhereList(string prefix = null)
StringBuilder builder = new StringBuilder("WHERE 1=1 ");
for (int i = 0; i < lambdaExpressionList.Count; i++)
{
prefix = $"{prefix}{i}";
var whereParam = new WhereExpression(lambdaExpressionList[i],prefix, provider);
var whereParam = new WhereExpression(lambdaExpressionList[i], $"{prefix}_{i}", provider);
builder.Append(whereParam.SqlCmd);
//参数
foreach (var paramKey in whereParam.Param.ParameterNames)
Expand Down Expand Up @@ -229,9 +229,8 @@ public virtual UpdateExpression ResolveUpdate<T>(Expression<Func<T, T>> updateEx
/// <param name="t"></param>
/// <param name="Param"></param>
/// <param name="excludeFields"></param>
/// <param name="isBatch">是否批量修改</param>
/// <returns></returns>
public virtual string ResolveUpdates<T>(T t, DynamicParameters param, string[] excludeFields, bool isBatch = false)
public virtual string ResolveUpdates<T>(T t, DynamicParameters Param, string[] excludeFields)
{
var entity = EntityCache.QueryEntity(t.GetType());
var properties = entity.Properties;
Expand Down Expand Up @@ -260,19 +259,20 @@ public virtual string ResolveUpdates<T>(T t, DynamicParameters param, string[] e
continue;
}
}
var typeAttribute2 = customAttributes.FirstOrDefault(x => x.GetType().Equals(typeof(KeyAttribute)));
if (typeAttribute2 != null)
{
continue;

}
object value = propertiy.GetValue(t);
string name = entity.FieldPairs[propertiy.Name];
if (builder.Length != 0)
{
builder.Append(",");
}
string paramName = string.Empty;
if (!isBatch)
paramName = $"{providerOption.ParameterPrefix}Update_{param.ParameterNames.Count()}";
else
paramName = $"{providerOption.ParameterPrefix}Update_{name}";
builder.Append($"{providerOption.CombineFieldName(name)}={paramName}");
param.Add($"{paramName}", value);
builder.Append($"{providerOption.CombineFieldName(name)}={providerOption.ParameterPrefix}Update_{name}");
Param.Add($"{providerOption.ParameterPrefix}Update_{name}", value);
}
builder.Insert(0, " SET ");
return builder.ToString();
Expand Down
61 changes: 61 additions & 0 deletions Kogel.Dapper.Extension/Core/SetQ/QuerySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public QuerySet(IDbConnection conn, SqlProvider sqlProvider) : base(conn, sqlPro
HavingExpressionList = new List<LambdaExpression>();
}

<<<<<<< HEAD
public QuerySet(IDbConnection conn, SqlProvider sqlProvider, IDbTransaction dbTransaction) : base(conn, sqlProvider, dbTransaction)
{
TableType = typeof(T);
Expand Down Expand Up @@ -89,6 +90,66 @@ public QuerySet<T> Where<TWhere>(Expression<Func<TWhere, bool>> predicate)
WhereExpressionList.Add(predicate);
return this;
}
=======
public QuerySet(IDbConnection conn, SqlProvider sqlProvider, IDbTransaction dbTransaction) : base(conn, sqlProvider, dbTransaction)
{
TableType = typeof(T);
SetContext = new DataBaseContext<T>
{
Set = this,
OperateType = EOperateType.Query
};
sqlProvider.Context = SetContext;
WhereExpressionList = new List<LambdaExpression>();
WhereBuilder = new StringBuilder();
Params = new DynamicParameters();
GroupExpressionList = new List<LambdaExpression>();
HavingExpressionList = new List<LambdaExpression>();
}
#region 基础函数
public QuerySet<T> AsTableName(Type type, string tableName)
{
SqlProvider.AsTableNameDic.Add(type, tableName);
return this;
}
/// <summary>
/// 不锁表查询(此方法只支持Mssql)
/// </summary>
/// <returns></returns>
public QuerySet<T> WithNoLock()
{
NoLock = true;
return this;
}
/// <summary>
/// 字段匹配[已弃用]
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <returns></returns>
public QuerySet<T> FieldMatch<TSource>()
{
return this;
}
#endregion
#region 条件
public QuerySet<T> Where(Expression<Func<T, bool>> predicate)
{
if (predicate != null)
{
WhereExpressionList.Add(predicate);
}

return this;
}
public QuerySet<T> Where<TWhere>(Expression<Func<TWhere, bool>> predicate)
{
if(predicate != null)
{
WhereExpressionList.Add(predicate);
}
return this;
}
>>>>>>> 0f7f297adff43ead4861f5f52d9d2b740d15ad2e


public QuerySet<T> Where(T model)
Expand Down
43 changes: 43 additions & 0 deletions Kogel.Dapper.Extension/Expressions/WhereExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ public WhereExpression(LambdaExpression expression, string prefix, SqlProvider p
this._sqlCmd = new StringBuilder(100);
this.Param = new DynamicParameters();
this.providerOption = provider.ProviderOption;
<<<<<<< HEAD
base.Prefix = prefix;
=======
>>>>>>> 0f7f297adff43ead4861f5f52d9d2b740d15ad2e
//开始解析对象
Visit(expression);
//开始拼接成条件
this._sqlCmd.Append(base.SpliceField);
<<<<<<< HEAD
this.SqlCmd = " AND " + this._sqlCmd.ToString();
this.Param.AddDynamicParams(base.Param);
}
Expand All @@ -56,6 +60,45 @@ protected override Expression VisitBinary(BinaryExpression node)
}

/// <summary>
=======
string sql = this._sqlCmd.ToString();
if (!string.IsNullOrWhiteSpace(sql))
{
this.SqlCmd = " AND " + sql;
}

if (string.IsNullOrEmpty(prefix))
{
this.Param.AddDynamicParams(base.Param);
}
else
{
//加上参数标记
foreach (var paramName in base.Param.ParameterNames)
{
string newName = (paramName + prefix).Replace("_", "xx");
object value = base.Param.Get<object>(paramName);
this.SqlCmd = this.SqlCmd.Replace(paramName, newName);
this.Param.Add(newName, value);
}
}
}

/// <summary>
/// 解析二元表达式
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
protected override Expression VisitBinary(BinaryExpression node)
{
var binaryWhere = new BinaryExpressionVisitor(node, base.Provider);
this._sqlCmd.Append(binaryWhere.SpliceField);
base.Param.AddDynamicParams(binaryWhere.Param);
return node;
}

/// <summary>
>>>>>>> 0f7f297adff43ead4861f5f52d9d2b740d15ad2e
/// 解析!不等于(!里只能包含一个条件)
/// </summary>
/// <param name="node"></param>
Expand Down
5 changes: 3 additions & 2 deletions Kogel.Dapper.Extension/Extension/ReflectExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Linq;
using System.Reflection;
using Kogel.Dapper.Extension;
using System.ComponentModel.DataAnnotations;

namespace Kogel.Dapper.Extension.Extension
{
public static class ReflectExtension
{
public static PropertyInfo GetKeyPropertity(this object obj)
{
var properties = EntityCache.QueryEntity(obj.GetType()).Properties.Where(a => a.GetCustomAttribute<Identity>() != null).ToArray();
var properties = EntityCache.QueryEntity(obj.GetType()).Properties.Where(a => a.GetCustomAttribute<Identity>() != null || a.GetCustomAttribute<System.ComponentModel.DataAnnotations.KeyAttribute>() != null).ToArray();

if (!properties.Any())
throw new DapperExtensionException($"the {nameof(obj)} entity with no KeyAttribute Propertity");
Expand All @@ -22,7 +23,7 @@ public static PropertyInfo GetKeyPropertity(this object obj)
}
public static PropertyInfo GetKeyPropertity(this Type typeInfo)
{
var properties = EntityCache.QueryEntity(typeInfo).Properties.Where(a => a.GetCustomAttribute<Identity>() != null).ToArray();
var properties = EntityCache.QueryEntity(typeInfo).Properties.Where(a => a.GetCustomAttribute<Identity>() != null || a.GetCustomAttribute<System.ComponentModel.DataAnnotations.KeyAttribute>() != null).ToArray();

if (!properties.Any())
throw new DapperExtensionException($"the type {nameof(typeInfo.FullName)} entity with no KeyAttribute Propertity");
Expand Down
1 change: 1 addition & 0 deletions Kogel.Dapper.Extension/Kogel.Dapper.Extension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ https://www.cnblogs.com/kogel/p/10805696.html</Description>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.6.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.6.0" />
Expand Down
9 changes: 9 additions & 0 deletions Kogel.Dapper.Extension/Model/EntityObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Kogel.Dapper.Extension.Core.Interfaces;
using System.Linq.Expressions;
using System.Data;
using System.ComponentModel.DataAnnotations;

namespace Kogel.Dapper.Extension.Model
{
Expand Down Expand Up @@ -142,6 +143,14 @@ public EntityObject(Type type)
EntityFieldList[EntityFieldList.Count - 1].IsIdentity = true;
EntityFieldList[EntityFieldList.Count - 1].IsIncrease = (identityAttribute as Identity).IsIncrease;
}

var identityAttribute2 = item.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().Equals(typeof(KeyAttribute)));
if (identityAttribute2 != null)
{
this.Identitys = this.FieldPairs[item.Name];
EntityFieldList[EntityFieldList.Count - 1].IsIdentity = true;
EntityFieldList[EntityFieldList.Count - 1].IsIncrease = true;
}
}
}
this.Properties = PropertyInfoList.ToArray();
Expand Down
Loading