forked from bing-framework/Bing.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7391cb1
commit 2bbc5f9
Showing
12 changed files
with
391 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
src/Bing.Datas.Dapper.MySql/Bing.Datas.Dapper.MySql.csproj
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/Bing.Datas.Dapper.PgSql/Bing.Datas.Dapper.PgSql.csproj
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/Bing.Datas.Dapper.SqlServer/Bing.Datas.Dapper.SqlServer.csproj
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System; | ||
using Bing.Datas.Configs; | ||
using Bing.Datas.Dapper.Handlers; | ||
using Bing.Datas.Dapper.MySql; | ||
using Bing.Datas.Dapper.PgSql; | ||
using Bing.Datas.Dapper.SqlServer; | ||
using Bing.Datas.Matedatas; | ||
using Bing.Datas.Sql; | ||
using Bing.Datas.Sql.Queries; | ||
using Bing.Datas.Sql.Queries.Builders.Abstractions; | ||
using Bing.Utils.Extensions; | ||
using Dapper; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Bing.Datas.Dapper | ||
{ | ||
/// <summary> | ||
/// 服务扩展 | ||
/// </summary> | ||
public static partial class Extensions | ||
{ | ||
/// <summary> | ||
/// 注册Sql查询服务 | ||
/// </summary> | ||
/// <param name="services">服务集合</param> | ||
/// <param name="action">Sql查询配置</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSqlQuery(this IServiceCollection services, | ||
Action<SqlQueryConfig> action = null) | ||
{ | ||
return AddSqlQuery(services, action, null, null); | ||
} | ||
|
||
/// <summary> | ||
/// 注册Sql查询服务 | ||
/// </summary> | ||
/// <typeparam name="TDatabase">IDatabase实现类型,提供数据库连接</typeparam> | ||
/// <param name="services">服务集合</param> | ||
/// <param name="action">Sql查询配置</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSqlQuery<TDatabase>(this IServiceCollection services, | ||
Action<SqlQueryConfig> action = null) where TDatabase : class, IDatabase | ||
{ | ||
return AddSqlQuery(services, action, typeof(TDatabase), null); | ||
} | ||
|
||
/// <summary> | ||
/// 注册Sql查询服务 | ||
/// </summary> | ||
/// <typeparam name="TDatabase">IDatabase实现类型,提供数据库连接</typeparam> | ||
/// <typeparam name="TEntityMatedata">IEntityMatedata实现类型,提供实体元数据解析</typeparam> | ||
/// <param name="services">服务集合</param> | ||
/// <param name="action">Sql查询配置</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSqlQuery<TDatabase, TEntityMatedata>(this IServiceCollection services, | ||
Action<SqlQueryConfig> action = null) | ||
where TDatabase : class, IDatabase | ||
where TEntityMatedata : class, IEntityMatedata | ||
{ | ||
return AddSqlQuery(services, action, typeof(TDatabase), typeof(TEntityMatedata)); | ||
} | ||
|
||
/// <summary> | ||
/// 注册Sql查询服务 | ||
/// </summary> | ||
/// <param name="services">服务集合</param> | ||
/// <param name="action">Sql查询配置</param> | ||
/// <param name="database">数据库类型</param> | ||
/// <param name="entityMatedata">实体元数据解析器类型</param> | ||
/// <returns></returns> | ||
private static IServiceCollection AddSqlQuery(IServiceCollection services, Action<SqlQueryConfig> action, | ||
Type database, Type entityMatedata) | ||
{ | ||
if (database != null) | ||
{ | ||
services.TryAddScoped(database); | ||
services.TryAddScoped(typeof(IDatabase),t=>t.GetService(database)); | ||
} | ||
services.TryAddScoped<ISqlQuery,SqlQuery>(); | ||
if (entityMatedata != null) | ||
{ | ||
services.TryAddScoped(typeof(IEntityMatedata),t=>t.GetService(entityMatedata)); | ||
} | ||
var config=new SqlQueryConfig(); | ||
action?.Invoke(config); | ||
AddSqlBuilder(services,config); | ||
RegisterTypeHandlers(); | ||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// 配置Sql生成器 | ||
/// </summary> | ||
/// <param name="services">服务集合</param> | ||
/// <param name="config">Sql查询配置</param> | ||
private static void AddSqlBuilder(IServiceCollection services, SqlQueryConfig config) | ||
{ | ||
switch (config.DatabaseType) | ||
{ | ||
case DatabaseType.SqlServer: | ||
services.TryAddScoped<ISqlBuilder, SqlServerBuilder>(); | ||
return; | ||
case DatabaseType.MySql: | ||
services.TryAddScoped<ISqlBuilder,MySqlBuilder>(); | ||
return; | ||
case DatabaseType.PgSql: | ||
services.TryAddScoped<ISqlBuilder,PgSqlBuilder>(); | ||
return; | ||
default: | ||
throw new NotImplementedException($"Sql生成器未实现 {config.DatabaseType.Description()} 数据库"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 注册类型处理器 | ||
/// </summary> | ||
private static void RegisterTypeHandlers() | ||
{ | ||
SqlMapper.AddTypeHandler(typeof(string), new StringTypeHandler()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text; | ||
using Bing.Datas.Matedatas; | ||
using Bing.Datas.Sql.Queries.Builders.Abstractions; | ||
using Bing.Datas.Sql.Queries.Builders.Core; | ||
|
||
namespace Bing.Datas.Dapper.MySql | ||
{ | ||
/// <summary> | ||
/// MySql Sql生成器 | ||
/// </summary> | ||
public class MySqlBuilder:SqlBuilderBase | ||
{ | ||
/// <summary> | ||
/// 初始化一个<see cref="MySqlBuilder"/>类型的实例 | ||
/// </summary> | ||
/// <param name="matedata">实体元数据解析器</param> | ||
/// <param name="parameterManager">参数管理器</param> | ||
public MySqlBuilder(IEntityMatedata matedata=null,IParameterManager parameterManager = null) : base(matedata, parameterManager) { } | ||
|
||
/// <summary> | ||
/// 创建Sql生成器 | ||
/// </summary> | ||
/// <returns></returns> | ||
public override ISqlBuilder New() | ||
{ | ||
return new MySqlBuilder(EntityMatedata, ParameterManager) | ||
{ | ||
Tag = $"{Tag}{++ChildBuilderCount}" | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// 创建分页Sql | ||
/// </summary> | ||
/// <param name="result">Sql拼接</param> | ||
protected override void CreatePagerSql(StringBuilder result) | ||
{ | ||
AppendSql(result, GetSelect()); | ||
AppendSql(result, GetFrom()); | ||
AppendSql(result, GetJoin()); | ||
AppendSql(result, GetWhere()); | ||
AppendSql(result, GetOrderBy()); | ||
result.Append($"Limit {GetPager().GetSkipCount()}, {GetPager().PageSize}"); | ||
} | ||
|
||
/// <summary> | ||
/// 获取Sql方言 | ||
/// </summary> | ||
/// <returns></returns> | ||
protected override IDialect GetDialect() | ||
{ | ||
return new MySqlDialect(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Bing.Datas.Sql.Queries.Builders.Core; | ||
|
||
namespace Bing.Datas.Dapper.MySql | ||
{ | ||
/// <summary> | ||
/// MySql方言 | ||
/// </summary> | ||
public class MySqlDialect:DialectBase | ||
{ | ||
/// <summary> | ||
/// 获取参数前缀 | ||
/// </summary> | ||
/// <returns></returns> | ||
public override string GetPrefix() | ||
{ | ||
return "@"; | ||
} | ||
|
||
/// <summary> | ||
/// 闭合字符-开 | ||
/// </summary> | ||
public override char OpenQuote => '`'; | ||
|
||
/// <summary> | ||
/// 闭合字符-闭 | ||
/// </summary> | ||
public override char CloseQuote => '`'; | ||
|
||
/// <summary> | ||
/// 批量操作分隔符 | ||
/// </summary> | ||
public override char BatchSeperator => ';'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text; | ||
using Bing.Datas.Matedatas; | ||
using Bing.Datas.Sql.Queries.Builders.Abstractions; | ||
using Bing.Datas.Sql.Queries.Builders.Core; | ||
|
||
namespace Bing.Datas.Dapper.PgSql | ||
{ | ||
/// <summary> | ||
/// PgSql Sql生成器 | ||
/// </summary> | ||
public class PgSqlBuilder:SqlBuilderBase | ||
{ | ||
/// <summary> | ||
/// 初始化一个<see cref="PgSqlBuilder"/>类型的实例 | ||
/// </summary> | ||
/// <param name="matedata">实体元数据解析器</param> | ||
/// <param name="parameterManager">参数管理器</param> | ||
public PgSqlBuilder(IEntityMatedata matedata = null, IParameterManager parameterManager = null) : base(matedata, parameterManager) { } | ||
|
||
/// <summary> | ||
/// 创建Sql生成器 | ||
/// </summary> | ||
/// <returns></returns> | ||
public override ISqlBuilder New() | ||
{ | ||
return new PgSqlBuilder(EntityMatedata, ParameterManager) | ||
{ | ||
Tag = $"{Tag}{++ChildBuilderCount}" | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// 创建分页Sql | ||
/// </summary> | ||
/// <param name="result">Sql拼接</param> | ||
protected override void CreatePagerSql(StringBuilder result) | ||
{ | ||
AppendSql(result, GetSelect()); | ||
AppendSql(result, GetFrom()); | ||
AppendSql(result, GetJoin()); | ||
AppendSql(result, GetWhere()); | ||
AppendSql(result, GetOrderBy()); | ||
result.Append($"Limit {GetPager().PageSize} OFFSET {GetPager().GetSkipCount()}"); | ||
} | ||
|
||
/// <summary> | ||
/// 获取Sql方言 | ||
/// </summary> | ||
/// <returns></returns> | ||
protected override IDialect GetDialect() | ||
{ | ||
return new PgSqlDialect(); | ||
} | ||
} | ||
} |
Oops, something went wrong.