Skip to content

Commit

Permalink
代码优化:sqlserver特有的recompile相关处理 放到SqlServer包中;
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzhiyuan committed Jun 13, 2024
1 parent 74b6d94 commit 9b0d9a2
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 159 deletions.
11 changes: 7 additions & 4 deletions src/NuClear.Dapper.MySql/MySqlCriterionSqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public string ToSql(Criterion criterion)

if (oper == null && !string.IsNullOrWhiteSpace(fixedKeywordPropertyName))
{
return string.Format("{0}", fixedKeywordPropertyName);
return fixedKeywordPropertyName;
}

var aliasName = string.IsNullOrWhiteSpace(criterion.TableAliasName) ? "" : criterion.TableAliasName + ".";
Expand Down Expand Up @@ -43,17 +43,20 @@ public string ToSql(Criterion criterion)

if (criterion.Operator == CriteriaOperator.IsNotNull || criterion.Operator == CriteriaOperator.IsNull)
{
return string.Format("{0}{1} {2}", aliasName, fixedKeywordPropertyName, oper);
return $"{aliasName}{fixedKeywordPropertyName} {oper}";
}

return string.Format("{0}{1} {2} @{3}", aliasName, fixedKeywordPropertyName, oper, criterion.PropertyParameterName);
return $"{aliasName}{fixedKeywordPropertyName} {oper} @{criterion.PropertyParameterName}";
}


public string ToTableValueSql(Criterion criterion)
{
string oper = criterion.Operator.ToSqlOperator();
if (oper == null) return null;
if (oper == null)
{
return null;
}

if (criterion.Operator == CriteriaOperator.Like || criterion.Operator == CriteriaOperator.NotLike)
{
Expand Down
5 changes: 0 additions & 5 deletions src/NuClear.Dapper.MySql/MySqlSqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,5 @@ public string ReplaceFieldSql(string selectSql, string newSql)
var fieldListSql = GetFieldListSql(selectSql);
return selectSql.Replace(fieldListSql, " " + newSql + " ");
}

public string GetRecompileSql(bool recompile)
{
return "";
}
}
}
60 changes: 60 additions & 0 deletions src/NuClear.Dapper.SqlServer/IRepositoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using NuClear.Dapper.AggregateQueryObject;
using NuClear.Dapper.QueryObject;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NuClear.Dapper.SqlServer
{
public static class IRepositoryExtensions
{
private static SqlServerRepositoryBase<TKey, TEntity> CheckAndConvertToSqlServerRepositoryBase<TKey, TEntity>(IRepository<TKey, TEntity> repository)
where TEntity : IEntity<TKey>
{
if (!(repository is SqlServerRepositoryBase<TKey, TEntity> sqlServerRepository))
{
throw new NotSupportedException("不支持recompile参数!");
}

return sqlServerRepository;
}

public static int CountWithRecompile<TKey, TEntity>(this IRepository<TKey, TEntity> repository, string sql, object parameters = null)
where TEntity : IEntity<TKey>
{
var sqlServerRepository = CheckAndConvertToSqlServerRepositoryBase(repository);
return sqlServerRepository.CountWithRecompile(sql, parameters);
}



public static async Task<int> CountWithRecompileAsync<TKey, TEntity>(this IRepository<TKey, TEntity> repository, string sql, object parameters = null)
where TEntity : IEntity<TKey>
{
var sqlServerRepository = CheckAndConvertToSqlServerRepositoryBase(repository);
return await sqlServerRepository.CountWithRecompileAsync(sql, parameters);
}


public static IEnumerable<TAny> QueryWithRecompile<TKey, TEntity, TAny>(this IRepository<TKey, TEntity> repository, string selectSql, Query query, Sort sort = null)
where TEntity : IEntity<TKey>
{
var sqlServerRepository = CheckAndConvertToSqlServerRepositoryBase(repository);
return sqlServerRepository.QueryWithRecompile<TKey, TEntity, TAny>(selectSql, query, sort);
}
public static IEnumerable<TAny> QueryWithRecompilePaged<TKey, TEntity, TAny>(this IRepository<TKey, TEntity> repository, string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null)
where TEntity : IEntity<TKey>
{
var sqlServerRepository = CheckAndConvertToSqlServerRepositoryBase(repository);
return sqlServerRepository.QueryWithRecompilePaged<TKey, TEntity, TAny>(selectSql, query, pager, sort, aggrQuery);
}

public static IEnumerable<TAny> QueryWithRecompile<TKey, TEntity, TAny>(this IRepository<TKey, TEntity> repository, string query, object parameters = null)
where TEntity : IEntity<TKey>
{
var sqlServerRepository = CheckAndConvertToSqlServerRepositoryBase(repository);
return sqlServerRepository.QueryWithRecompile<TKey, TEntity, TAny>(query, parameters);
}

}
}
84 changes: 84 additions & 0 deletions src/NuClear.Dapper.SqlServer/SqlServerRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using NuClear.Dapper.AggregateQueryObject;
using NuClear.Dapper.QueryObject;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using System.Linq;

namespace NuClear.Dapper.SqlServer
{
Expand All @@ -9,6 +13,86 @@ public abstract class SqlServerRepositoryBase<TKey, TEntity> : RepositoryBase<TK
protected override IAggrQuerySqlParser AggrQuerySqlParser => new SqlServerAggrQuerySqlParser();
protected override ICriterionSqlParser CriterionSqlParser => new SqlServerCriterionSqlParser();
protected override ISqlParser SqlParser => new SqlServerSqlParser();


protected string GetRecompileSql()
{
return " OPTION(RECOMPILE)";
}

protected void SetAggrWithRecompile(string sql, AggrQuery aggrQuery, object param)
{
aggrQuery.SetResult(InternalQuery(GetAggrSelectSql(sql, aggrQuery) + GetRecompileSql(), param).FirstOrDefault());
}

public virtual int CountWithRecompile(string sql, object parameters = null)
{
return InternalExecuteScalar<int>(sql + GetRecompileSql(), parameters);
}

public virtual async Task<int> CountWithRecompileAsync(string sql, object parameters = null)
{
return await InternalExecuteScalarAsync<int>(sql + GetRecompileSql(), parameters);
}

public virtual IEnumerable<TAny> QueryWithRecompile<TAny>(string selectSql, Query query, Sort sort = null)
{
string whereSql = GetWhereSql(query, out object param);
return InternalQuery<TAny>(CombineSql(selectSql, whereSql, sort?.Translate()) + GetRecompileSql(), param);
}
public virtual IEnumerable<TAny> QueryPagedWithRecompile<TAny>(string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null)
{
if (pager == null)
throw new ArgumentNullException(nameof(pager));

string whereSql = GetWhereSql(query, out object param);
param = AddPagedParams(param, pager);
if (pager.IsGetTotalCount)
{
pager.TotalCount = this.CountWithRecompile(CombineCountSql(selectSql, whereSql), param);
}
if (aggrQuery != null)
{
var sql = CombineSql(selectSql, whereSql);
SetAggrWithRecompile(sql, aggrQuery, param);
}
return InternalQuery<TAny>(CombinePagedSql(selectSql, whereSql, sort?.Translate(), pager) + GetRecompileSql(), param);
}

public virtual IEnumerable<TAny> QueryWithRecompile<TAny>(string query, object parameters = null)
{
return InternalQuery<TAny>(query + GetRecompileSql(), parameters);
}

public virtual async Task<IEnumerable<TAny>> QueryWithRecompileAsync<TAny>(string selectSql, Query query, Sort sort = null)
{
string whereSql = GetWhereSql(query, out object param);
return await InternalQueryAsync<TAny>(CombineSql(selectSql, whereSql, sort?.Translate()) + GetRecompileSql(), param);
}

public virtual async Task<IEnumerable<TAny>> QueryPagedWithRecompileAsync<TAny>(string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null)
{
if (pager == null)
throw new ArgumentNullException(nameof(pager));

string whereSql = GetWhereSql(query, out object param);
param = AddPagedParams(param, pager);
if (pager.IsGetTotalCount)
{
pager.TotalCount = this.CountWithRecompile(CombineCountSql(selectSql, whereSql), param);
}
if (aggrQuery != null)
{
var sql = CombineSql(selectSql, whereSql);
SetAggrWithRecompile(sql, aggrQuery, param);
}
return await InternalQueryAsync<TAny>(CombinePagedSql(selectSql, whereSql, sort?.Translate(), pager) + GetRecompileSql(), param);
}

public virtual async Task<IEnumerable<TAny>> QueryWithRecompileAsync<TAny>(string query, object parameters = null)
{
return await InternalQueryAsync<TAny>(query + GetRecompileSql(), parameters);
}
}
public abstract class SqlServerRepositoryBaseWithLongKey<TEntity> : SqlServerRepositoryBase<long, TEntity>
where TEntity : IEntityWithLongKey
Expand Down
5 changes: 0 additions & 5 deletions src/NuClear.Dapper.SqlServer/SqlServerSqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,5 @@ public string ReplaceFieldSql(string selectSql, string newSql)
var fieldListSql = GetFieldListSql(selectSql);
return selectSql.Replace(fieldListSql, " " + newSql + " ");
}

public string GetRecompileSql(bool recompile)
{
return recompile ? " OPTION(RECOMPILE)" : "";
}
}
}
3 changes: 0 additions & 3 deletions src/NuClear.Dapper.UnitTest/SqlSourceCacheTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace NuClear.Dapper.UnitTest
{
Expand Down
2 changes: 1 addition & 1 deletion src/NuClear.Dapper/BaseEntityWithLongKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NuClear.Dapper
{
public class BaseEntityWithLongKey : BaseEntity<long>, IEntity
public class BaseEntityWithLongKey : BaseEntity<long>, IEntityWithLongKey, IEntity
{
internal static readonly IdWorker idWorker = IdWorker.Create();
public BaseEntityWithLongKey()
Expand Down
20 changes: 8 additions & 12 deletions src/NuClear.Dapper/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IRepository<TKey, TEntity> : IDisposable

TEntity FirstOrDefault(Query query, Sort sort = null);

int Count(string sql, object parameters = null, bool recompile = false);
int Count(string sql, object parameters = null);

/// <summary>
/// TEntity count
Expand All @@ -29,11 +29,9 @@ public interface IRepository<TKey, TEntity> : IDisposable
IEnumerable<TEntity> QueryPaged(Query query, Pager pager, Sort sort);
IEnumerable<TEntity> QueryPaged(Query query, Pager pager, Sort sort, AggrQuery aggrQuery);
IEnumerable<TEntity> QueryPaged(string countSql, object countParameters, string query, object parameters, out int totalCount);
IEnumerable<TAny> Query<TAny>(string selectSql, Query query, Sort sort = null, bool recompile = false);
IEnumerable<TAny> Query<TAny>(string selectSql, Query query, string andWhereSql, Dictionary<string, object> parameters, Sort sort = null);
IEnumerable<TAny> QueryPaged<TAny>(string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null, bool recompile = false);
IEnumerable<TAny> QueryPaged<TAny>(string selectSql, Query query, Pager pager, Sort sort, string andWhereSql, Dictionary<string, object> parameters, AggrQuery aggrQuery = null);
IEnumerable<TAny> Query<TAny>(string query, object parameters = null, bool recompile = false);
IEnumerable<TAny> Query<TAny>(string selectSql, Query query, Sort sort = null);
IEnumerable<TAny> QueryPaged<TAny>(string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null);
IEnumerable<TAny> Query<TAny>(string query, object parameters = null);
IEnumerable<TAny> QueryPaged<TAny>(string countSql, object countParameters, string query, object parameters, out int totalCount) where TAny : class;

IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, object param = null, string split = null) where TReturn : class;
Expand All @@ -53,7 +51,7 @@ public interface IRepository<TKey, TEntity> : IDisposable

Task<TEntity> FirstOrDefaultAsync(Query query, Sort sort = null);

Task<int> CountAsync(string sql, object parameters = null, bool recompile = false);
Task<int> CountAsync(string sql, object parameters = null);

/// <summary>
/// TEntity count
Expand All @@ -68,11 +66,9 @@ public interface IRepository<TKey, TEntity> : IDisposable
Task<IEnumerable<TEntity>> QueryPagedAsync(Query query, Pager pager, Sort sort);
Task<IEnumerable<TEntity>> QueryPagedAsync(Query query, Pager pager, Sort sort, AggrQuery aggrQuery);
Task<(IEnumerable<TEntity>, int)> QueryPagedAsync(string countSql, object countParameters, string query, object parameters);
Task<IEnumerable<TAny>> QueryAsync<TAny>(string selectSql, Query query, Sort sort = null, bool recompile = false);
Task<IEnumerable<TAny>> QueryAsync<TAny>(string selectSql, Query query, string andWhereSql, Dictionary<string, object> parameters, Sort sort = null);
Task<IEnumerable<TAny>> QueryPagedAsync<TAny>(string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null, bool recompile = false);
Task<IEnumerable<TAny>> QueryPagedAsync<TAny>(string selectSql, Query query, Pager pager, Sort sort, string andWhereSql, Dictionary<string, object> parameters, AggrQuery aggrQuery = null, bool recompile = false);
Task<IEnumerable<TAny>> QueryAsync<TAny>(string query, object parameters = null, bool recompile = false);
Task<IEnumerable<TAny>> QueryAsync<TAny>(string selectSql, Query query, Sort sort = null);
Task<IEnumerable<TAny>> QueryPagedAsync<TAny>(string selectSql, Query query, Pager pager, Sort sort, AggrQuery aggrQuery = null);
Task<IEnumerable<TAny>> QueryAsync<TAny>(string query, object parameters = null);
Task<(IEnumerable<TAny>, int)> QueryPagedAsync<TAny>(string countSql, object countParameters, string query, object parameters) where TAny : class;
Task InsertAsync(TEntity entity);
Task<int> ExecuteAsync(string sql, object parameters = null, CommandType? commandType = null);
Expand Down
6 changes: 0 additions & 6 deletions src/NuClear.Dapper/ISqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,5 @@ public interface ISqlParser
/// <param name="newSql"></param>
/// <returns></returns>
string ReplaceFieldSql(string selectSql, string newSql);
/// <summary>
/// 获取重新编译SQL
/// </summary>
/// <param name="recompile"></param>
/// <returns></returns>
string GetRecompileSql(bool recompile);
}
}
4 changes: 3 additions & 1 deletion src/NuClear.Dapper/QueryObject/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ public static TranslateResult Translate(this Query query, ICriterionSqlParser cr
if (subResult == null)
continue;

tempSQLs.Add(string.Format("({0})", subResult.Sql));
tempSQLs.Add($"({subResult.Sql})");
result.SqlParameters.AddRange(subResult.SqlParameters);
}
}
if (tempSQLs.Count == 0)
{
return null;
}

result.Sql = string.Join(query.Operator == QueryOperator.And ? " and " : " or ", tempSQLs);
return result;
Expand Down
Loading

0 comments on commit 9b0d9a2

Please sign in to comment.