1clear
This commit is contained in:
parent
9c7789ca29
commit
070a952bfd
|
@ -7,20 +7,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{490FAE47-447
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore", "src\ShardingCore\ShardingCore.csproj", "{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.SqlServer", "src\ShardingCore.SqlServer\ShardingCore.SqlServer.csproj", "{2B65466D-D4F8-48FE-A516-014404414897}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src3x", "src3x", "{62AAE0FE-4099-4A48-AA3C-F76F14C62655}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.3x", "src3x\ShardingCore.3x\ShardingCore.3x.csproj", "{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.SqlServer.3x", "src3x\ShardingCore.SqlServer.3x\ShardingCore.SqlServer.3x.csproj", "{37E11FFD-14D7-41B4-B04C-2950206C64DB}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src2x", "src2x", "{679E6084-0C45-4807-BFEE-D2FDA44B2188}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.2x", "src2x\ShardingCore.2x\ShardingCore.2x.csproj", "{FB92A4BE-A43E-4755-8132-EC38E9650B80}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.SqlServer.2x", "src2x\ShardingCore.SqlServer.2x\ShardingCore.SqlServer.2x.csproj", "{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CC2C88C0-65F2-445D-BE78-973B840FE281}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50", "test\ShardingCore.Test50\ShardingCore.Test50.csproj", "{7EE133B6-5A02-41B7-9D89-41D9EA14184E}"
|
||||
|
@ -29,12 +23,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{EDF8
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServer", "samples\Sample.SqlServer\Sample.SqlServer.csproj", "{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.MySql", "src\ShardingCore.MySql\ShardingCore.MySql.csproj", "{10308339-FF5C-43AB-9208-627B6E2FBBDB}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.MySql.3x", "src3x\ShardingCore.MySql.3x\ShardingCore.MySql.3x.csproj", "{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.MySql.2x", "src2x\ShardingCore.MySql.2x\ShardingCore.MySql.2x.csproj", "{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50.MySql", "test\ShardingCore.Test50.MySql\ShardingCore.Test50.MySql.csproj", "{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AutoByDate.SqlServer", "samples\Samples.AutoByDate.SqlServer\Samples.AutoByDate.SqlServer.csproj", "{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}"
|
||||
|
@ -57,26 +45,14 @@ Global
|
|||
{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2B65466D-D4F8-48FE-A516-014404414897}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2B65466D-D4F8-48FE-A516-014404414897}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2B65466D-D4F8-48FE-A516-014404414897}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2B65466D-D4F8-48FE-A516-014404414897}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{37E11FFD-14D7-41B4-B04C-2950206C64DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{37E11FFD-14D7-41B4-B04C-2950206C64DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{37E11FFD-14D7-41B4-B04C-2950206C64DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{37E11FFD-14D7-41B4-B04C-2950206C64DB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
@ -85,18 +61,6 @@ Global
|
|||
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{10308339-FF5C-43AB-9208-627B6E2FBBDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{10308339-FF5C-43AB-9208-627B6E2FBBDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{10308339-FF5C-43AB-9208-627B6E2FBBDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{10308339-FF5C-43AB-9208-627B6E2FBBDB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0CF88F0B-6CCB-49B5-B41D-CDC193B51581}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
@ -127,16 +91,10 @@ Global
|
|||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7} = {490FAE47-4476-4508-B216-505FC850447F}
|
||||
{2B65466D-D4F8-48FE-A516-014404414897} = {490FAE47-4476-4508-B216-505FC850447F}
|
||||
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655}
|
||||
{37E11FFD-14D7-41B4-B04C-2950206C64DB} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655}
|
||||
{FB92A4BE-A43E-4755-8132-EC38E9650B80} = {679E6084-0C45-4807-BFEE-D2FDA44B2188}
|
||||
{73B802A7-3DDE-4B02-9E3D-D6B9EE42DE5D} = {679E6084-0C45-4807-BFEE-D2FDA44B2188}
|
||||
{7EE133B6-5A02-41B7-9D89-41D9EA14184E} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
{10308339-FF5C-43AB-9208-627B6E2FBBDB} = {490FAE47-4476-4508-B216-505FC850447F}
|
||||
{954C4CA2-9CF4-4C2C-8DE6-180DD8202E38} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655}
|
||||
{0CF88F0B-6CCB-49B5-B41D-CDC193B51581} = {679E6084-0C45-4807-BFEE-D2FDA44B2188}
|
||||
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
{90675788-D5C3-415A-9C18-FF159A75B4D5} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
|
|
|
@ -5,8 +5,4 @@
|
|||
<LangVersion>9.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ShardingCore.SqlServer\ShardingCore.SqlServer.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Update;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.Core.ShardingAccessors;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.MySql.EFCores;
|
||||
using ShardingCore.TableCreator;
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Query.Sql;
|
||||
#endif
|
||||
#if !EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
#endif
|
||||
|
||||
|
||||
namespace ShardingCore.MySql
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2020年4月7日 9:30:18
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public static class DIExtension
|
||||
{
|
||||
public static IServiceCollection AddShardingMySql(this IServiceCollection services, Action<MySqlOptions> configure)
|
||||
{
|
||||
if (configure == null)
|
||||
throw new ArgumentNullException($"AddScfSqlServerProvider :{nameof(configure)}");
|
||||
|
||||
var options = new MySqlOptions();
|
||||
configure(options);
|
||||
services.AddSingleton<MySqlOptions>(options);
|
||||
services.AddSingleton<IShardingCoreOptions, MySqlOptions>(sp => options);
|
||||
services.AddShardingCore();
|
||||
|
||||
services.AddSingleton<IDbContextOptionsProvider, MySqlDbContextOptionsProvider>();
|
||||
services.AddSingleton<IShardingParallelDbContextFactory, ShardingMySqlParallelDbContextFactory>();
|
||||
|
||||
services.AddSingleton<IShardingBootstrapper,ShardingBootstrapper>();
|
||||
return services;
|
||||
}
|
||||
|
||||
internal static DbContextOptionsBuilder UseShardingMySqlQuerySqlGenerator(this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory, ShardingMySqlQuerySqlGeneratorFactory>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
internal static DbContextOptionsBuilder<TContext> UseShardingMySqlQuerySqlGenerator<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder) where TContext:DbContext
|
||||
{
|
||||
optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory, ShardingMySqlQuerySqlGeneratorFactory>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
public static DbContextOptionsBuilder UseShardingMySqlUpdateSqlGenerator(this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.ReplaceService<IUpdateSqlGenerator, ShardingMySqlUpdateSqlGenerator>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
public static DbContextOptionsBuilder<TContext> UseShardingMySqlUpdateSqlGenerator<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder) where TContext:DbContext
|
||||
{
|
||||
optionsBuilder.ReplaceService<IUpdateSqlGenerator, ShardingMySqlUpdateSqlGenerator>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,213 +0,0 @@
|
|||
#if !EFCORE2
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Query.ExpressionVisitors.Internal;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Query.Internal;
|
||||
using ShardingCore.Core.ShardingAccessors;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace ShardingCore.MySql.EFCores
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Tuesday, 22 December 2020 09:47:59
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingMySqlQuerySqlGeneratorFactory : IQuerySqlGeneratorFactory
|
||||
{
|
||||
|
||||
private readonly QuerySqlGeneratorDependencies _dependencies;
|
||||
private readonly MySqlSqlExpressionFactory _sqlExpressionFactory;
|
||||
private readonly IMySqlOptions _options;
|
||||
|
||||
public ShardingMySqlQuerySqlGeneratorFactory(
|
||||
QuerySqlGeneratorDependencies dependencies,
|
||||
ISqlExpressionFactory sqlExpressionFactory,
|
||||
IMySqlOptions options)
|
||||
{
|
||||
this._dependencies = dependencies;
|
||||
this._sqlExpressionFactory = (MySqlSqlExpressionFactory) sqlExpressionFactory;
|
||||
this._options = options;
|
||||
}
|
||||
|
||||
public virtual QuerySqlGenerator Create() => (QuerySqlGenerator) new ShardingMySqlQuerySqlGenerator(this._dependencies, this._sqlExpressionFactory, this._options);
|
||||
}
|
||||
|
||||
public class ShardingMySqlQuerySqlGenerator :MySqlQuerySqlGenerator
|
||||
{
|
||||
public ShardingMySqlQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies, MySqlSqlExpressionFactory sqlExpressionFactory, IMySqlOptions options) : base(dependencies, sqlExpressionFactory, options)
|
||||
{
|
||||
}
|
||||
protected override Expression VisitTable(TableExpression tableExpression)
|
||||
{
|
||||
return OverrideVisitTable(tableExpression);
|
||||
// this._relationalCommandBuilder.Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Name, tableExpression.Schema)).Append((object) this.AliasSeparator).Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
// return (Expression) tableExpression;
|
||||
|
||||
// typeof(TableExpression)
|
||||
// .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name.Contains(nameof(tableExpression.Name)))
|
||||
// .SetValue(tableExpression,"(select * from Log_1Message union all select * from Log_1Message)");
|
||||
|
||||
// base will append schema, table and alias
|
||||
}
|
||||
|
||||
private Expression OverrideVisitTable(TableExpression tableExpression)
|
||||
{
|
||||
var shardingAccessor = ShardingContainer.Services.GetService<IShardingAccessor>();
|
||||
if (shardingAccessor?.ShardingContext != null)
|
||||
{
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
var virtualTable = virtualTableManager.GetAllVirtualTables().FirstOrDefault(o => o.GetOriginalTableName() == tableExpression.Name);
|
||||
if(virtualTable!=null)
|
||||
{
|
||||
var tails = shardingAccessor.ShardingContext.GetContextQueryTails(virtualTable);
|
||||
var tailPrefix = virtualTable.ShardingConfig.TailPrefix;
|
||||
string newTableName = null;
|
||||
var sqlGenerationHelper = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_sqlGenerationHelper") as ISqlGenerationHelper;
|
||||
|
||||
if (tails.IsEmpty())
|
||||
{
|
||||
var firstTail = virtualTableManager.GetVirtualTable(tableExpression.Name).GetAllPhysicTables()[0].Tail;
|
||||
newTableName = $"( select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{firstTail}", tableExpression.Schema)} where 1=2 )";
|
||||
}
|
||||
else if (tails.Count == 1)
|
||||
{
|
||||
//对tableExpresion进行重写
|
||||
//typeof(TableExpression)
|
||||
// .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name==$"<{nameof(tableExpression.Name)}>k__BackingField")
|
||||
// .SetValue(tableExpression,$"{tableExpression.Name}{tailPrefix}{entry.Tails[0]}");
|
||||
newTableName = sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{tails[0]}", tableExpression.Schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{tail}", tableExpression.Schema)}")) + ")";
|
||||
}
|
||||
var relationalCommandBuilder = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder;
|
||||
relationalCommandBuilder.Append(newTableName).Append(this.AliasSeparator).Append(sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
return tableExpression;
|
||||
}
|
||||
}
|
||||
|
||||
var result = base.VisitTable(tableExpression);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Core.ShardingAccessors;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using Microsoft.EntityFrameworkCore.Query.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Query.Sql;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Query.Sql.Internal;
|
||||
|
||||
namespace ShardingCore.MySql.EFCores
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Tuesday, 22 December 2020 09:47:59
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingMySqlQuerySqlGeneratorFactory :QuerySqlGeneratorFactoryBase
|
||||
{
|
||||
|
||||
private readonly IMySqlOptions _options;
|
||||
|
||||
public ShardingMySqlQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies, IMySqlOptions options)
|
||||
: base(dependencies)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public override IQuerySqlGenerator CreateDefault(SelectExpression selectExpression)
|
||||
=> new ShardingMySqlQuerySqlGenerator(
|
||||
Dependencies,
|
||||
selectExpression, _options);
|
||||
}
|
||||
|
||||
public class ShardingMySqlQuerySqlGenerator : MySqlQuerySqlGenerator
|
||||
{
|
||||
|
||||
public ShardingMySqlQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies, SelectExpression selectExpression, IMySqlOptions options) : base(dependencies, selectExpression, options)
|
||||
{
|
||||
}
|
||||
|
||||
public override Expression VisitTable(TableExpression tableExpression)
|
||||
{
|
||||
return OverrideVisitTable(tableExpression);
|
||||
}
|
||||
// protected override Expression VisitTable(TableExpression tableExpression)
|
||||
// {
|
||||
// return OverrideVisitTable(tableExpression);
|
||||
// // this._relationalCommandBuilder.Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Name, tableExpression.Schema)).Append((object) this.AliasSeparator).Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
// // return (Expression) tableExpression;
|
||||
//
|
||||
// // typeof(TableExpression)
|
||||
// // .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name.Contains(nameof(tableExpression.Name)))
|
||||
// // .SetValue(tableExpression,"(select * from Log_1Message union all select * from Log_1Message)");
|
||||
//
|
||||
// // base will append schema, table and alias
|
||||
// }
|
||||
|
||||
private Expression OverrideVisitTable(TableExpression tableExpression)
|
||||
{
|
||||
var shardingAccessor = ShardingContainer.Services.GetService<IShardingAccessor>();
|
||||
if (shardingAccessor?.ShardingContext != null)
|
||||
{
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
var virtualTable = virtualTableManager.GetAllVirtualTables().FirstOrDefault(o => o.GetOriginalTableName() == tableExpression.Table);
|
||||
if(virtualTable!=null)
|
||||
{
|
||||
var tails = shardingAccessor.ShardingContext.GetContextQueryTails(virtualTable);
|
||||
var tailPrefix = virtualTable.ShardingConfig.TailPrefix;
|
||||
string newTableName = null;
|
||||
var sqlGenerationHelper = typeof(DefaultQuerySqlGenerator).GetTypePropertyValue(this, "SqlGenerator") as ISqlGenerationHelper;
|
||||
|
||||
if (tails.IsEmpty())
|
||||
{
|
||||
var firstTail = virtualTableManager.GetVirtualTable(tableExpression.Table).GetAllPhysicTables()[0].Tail;
|
||||
newTableName = $"( select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Table}{tailPrefix}{firstTail}", tableExpression.Schema)} where 1=2 )";
|
||||
}
|
||||
else if (tails.Count == 1)
|
||||
{
|
||||
//对tableExpresion进行重写
|
||||
//typeof(TableExpression)
|
||||
// .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name==$"<{nameof(tableExpression.Name)}>k__BackingField")
|
||||
// .SetValue(tableExpression,$"{tableExpression.Name}{tailPrefix}{entry.Tails[0]}");
|
||||
newTableName = sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Table}{tailPrefix}{tails[0]}", tableExpression.Schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Table}{tailPrefix}{tail}", tableExpression.Schema)}")) + ")";
|
||||
}
|
||||
|
||||
var relationalCommandBuilder = typeof(DefaultQuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder;
|
||||
relationalCommandBuilder.Append(newTableName).Append(this.AliasSeparator).Append(sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
return tableExpression;
|
||||
}
|
||||
}
|
||||
var result = base.VisitTable(tableExpression);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -1,133 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore.Update;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Update.Internal;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.Exceptions;
|
||||
|
||||
namespace ShardingCore.MySql.EFCores
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 23 July 2021 23:25:20
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingMySqlUpdateSqlGenerator:MySqlUpdateSqlGenerator
|
||||
{
|
||||
public ShardingMySqlUpdateSqlGenerator(UpdateSqlGeneratorDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
}
|
||||
|
||||
public override ResultSetMapping AppendInsertOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
|
||||
{
|
||||
// Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
|
||||
// Check.NotNull(command, nameof(command));
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
|
||||
var name = command.TableName;
|
||||
var schema = command.Schema;
|
||||
|
||||
var operations = command.ColumnModifications;
|
||||
var tryGetVirtualTable = virtualTableManager.TryGetVirtualTable(name);
|
||||
if (tryGetVirtualTable != null)
|
||||
{
|
||||
var shardingEntityConfig = tryGetVirtualTable.ShardingConfig;
|
||||
var shardingCol = operations.FirstOrDefault(o=>o.Property.Name.Equals(shardingEntityConfig.ShardingField));
|
||||
if (shardingCol == null)
|
||||
throw new ShardingKeyRouteNotMatchException();
|
||||
// shardingEntityConfig.ShardingField
|
||||
var physicTables = tryGetVirtualTable.RouteTo(new TableRouteConfig(null,null,shardingCol.Value));
|
||||
name = physicTables[0].FullName;
|
||||
}
|
||||
|
||||
var writeOperations = operations.Where(o => o.IsWrite).ToList();
|
||||
var readOperations = operations.Where(o => o.IsRead).ToList();
|
||||
|
||||
AppendInsertCommand(commandStringBuilder, name, schema, writeOperations);
|
||||
|
||||
if (readOperations.Count > 0)
|
||||
{
|
||||
var keyOperations = operations.Where(o => o.IsKey).ToList();
|
||||
|
||||
return AppendSelectAffectedCommand(commandStringBuilder, name, schema, readOperations, keyOperations, commandPosition);
|
||||
}
|
||||
|
||||
return ResultSetMapping.NoResultSet;
|
||||
}
|
||||
|
||||
public override ResultSetMapping AppendUpdateOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
|
||||
{
|
||||
// Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
|
||||
// Check.NotNull(command, nameof(command));
|
||||
|
||||
var name = command.TableName;
|
||||
var schema = command.Schema;
|
||||
var operations = command.ColumnModifications;
|
||||
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
var tryGetVirtualTable = virtualTableManager.TryGetVirtualTable(name);
|
||||
if (tryGetVirtualTable != null)
|
||||
{
|
||||
var shardingEntityConfig = tryGetVirtualTable.ShardingConfig;
|
||||
var shardingCol = operations.FirstOrDefault(o=>o.IsRead&&o.Property.Name.Equals(shardingEntityConfig.ShardingField));
|
||||
if (shardingCol == null)
|
||||
throw new ShardingKeyRouteNotMatchException();
|
||||
// shardingEntityConfig.ShardingField
|
||||
var physicTables = tryGetVirtualTable.RouteTo(new TableRouteConfig(null,null,shardingCol.Value));
|
||||
name = physicTables[0].FullName;
|
||||
}
|
||||
|
||||
|
||||
var writeOperations = operations.Where(o => o.IsWrite).ToList();
|
||||
var conditionOperations = operations.Where(o => o.IsCondition).ToList();
|
||||
var readOperations = operations.Where(o => o.IsRead).ToList();
|
||||
|
||||
AppendUpdateCommand(commandStringBuilder, name, schema, writeOperations, conditionOperations);
|
||||
|
||||
if (readOperations.Count > 0)
|
||||
{
|
||||
var keyOperations = operations.Where(o => o.IsKey).ToList();
|
||||
|
||||
return AppendSelectAffectedCommand(commandStringBuilder, name, schema, readOperations, keyOperations, commandPosition);
|
||||
}
|
||||
|
||||
return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
|
||||
}
|
||||
|
||||
public override ResultSetMapping AppendDeleteOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
|
||||
{
|
||||
// Check.NotNull<StringBuilder>(commandStringBuilder, nameof (commandStringBuilder));
|
||||
// Check.NotNull<ModificationCommand>(command, nameof (command));
|
||||
|
||||
var name = command.TableName;
|
||||
var schema = command.Schema;
|
||||
var conditionOperations = command.ColumnModifications.Where(o => o.IsCondition).ToList();
|
||||
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
var tryGetVirtualTable = virtualTableManager.TryGetVirtualTable(name);
|
||||
if (tryGetVirtualTable != null)
|
||||
{
|
||||
var shardingEntityConfig = tryGetVirtualTable.ShardingConfig;
|
||||
var shardingCol = command.ColumnModifications.FirstOrDefault(o=>o.IsRead&&o.Property.Name.Equals(shardingEntityConfig.ShardingField));
|
||||
if (shardingCol == null)
|
||||
throw new ShardingKeyRouteNotMatchException();
|
||||
// shardingEntityConfig.ShardingField
|
||||
var physicTables = tryGetVirtualTable.RouteTo(new TableRouteConfig(null,null,shardingCol.Value));
|
||||
name = physicTables[0].FullName;
|
||||
}
|
||||
AppendDeleteCommand(commandStringBuilder, name, schema, conditionOperations);
|
||||
|
||||
return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
|
||||
}
|
||||
|
||||
public override ResultSetMapping AppendBulkInsertOperation(StringBuilder commandStringBuilder, IReadOnlyList<ModificationCommand> modificationCommands, int commandPosition)
|
||||
{
|
||||
return base.AppendBulkInsertOperation(commandStringBuilder, modificationCommands, commandPosition);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
using System;
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
#if !EFCORE5
|
||||
using MySql.Data.MySqlClient;
|
||||
#endif
|
||||
#if EFCORE5
|
||||
using MySqlConnector;
|
||||
#endif
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace ShardingCore.MySql
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Thursday, 24 December 2020 10:33:51
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class MySqlDbContextOptionsProvider:IDbContextOptionsProvider
|
||||
{
|
||||
private readonly MySqlOptions _mySqlOptions;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly IShardingCoreOptions _shardingCoreOptions;
|
||||
|
||||
public MySqlDbContextOptionsProvider(MySqlOptions mySqlOptions,ILoggerFactory loggerFactory,IShardingCoreOptions shardingCoreOptions)
|
||||
{
|
||||
_mySqlOptions = mySqlOptions;
|
||||
_loggerFactory = loggerFactory;
|
||||
_shardingCoreOptions = shardingCoreOptions;
|
||||
}
|
||||
public DbContextOptions GetDbContextOptions(DbConnection dbConnection)
|
||||
{
|
||||
|
||||
var track = dbConnection != null;
|
||||
var connection = dbConnection ?? GetMySqlConnection();
|
||||
var dbContextOptions= CreateDbContextOptionBuilder()
|
||||
#if EFCORE5
|
||||
.UseMySql(connection, _mySqlOptions.ServerVersion, _mySqlOptions.MySqlOptionsAction)
|
||||
#endif
|
||||
#if !EFCORE5
|
||||
.UseMySql(connection, _mySqlOptions.MySqlOptionsAction)
|
||||
#endif
|
||||
.IfDo(!track, o => o.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking))
|
||||
.UseLoggerFactory(_loggerFactory)
|
||||
//.ReplaceService<IQueryCompiler, ShardingQueryCompiler>()
|
||||
.ReplaceService<IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
|
||||
.ReplaceService<IModelCustomizer, ShardingModelCustomizer>()
|
||||
//.UseShardingMySqlQuerySqlGenerator()
|
||||
.Options;
|
||||
return dbContextOptions;
|
||||
}
|
||||
private MySqlConnection GetMySqlConnection()
|
||||
{
|
||||
var connectionString = _shardingCoreOptions.GetShardingConfig().ConnectionString;
|
||||
return new MySqlConnection(connectionString);
|
||||
}
|
||||
private DbContextOptionsBuilder CreateDbContextOptionBuilder()
|
||||
{
|
||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(shardingConfigEntry.DbContextType);
|
||||
return (DbContextOptionsBuilder)Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace ShardingCore.MySql
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2020年4月7日 8:34:04
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class MySqlOptions: AbstractShardingCoreOptions
|
||||
{
|
||||
#if EFCORE5
|
||||
public MySqlServerVersion ServerVersion { get; set; }
|
||||
#endif
|
||||
|
||||
public Action<MySqlDbContextOptionsBuilder> MySqlOptionsAction { get; set; }
|
||||
|
||||
public void SetMySqlOptions(Action<MySqlDbContextOptionsBuilder> action)
|
||||
{
|
||||
MySqlOptionsAction = action;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Version>$(EFCORE5)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE5;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.MySql.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShardingCore\ShardingCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -1,66 +0,0 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Query.Internal;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace ShardingCore.MySql
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Tuesday, 29 December 2020 15:22:50
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingMySqlParallelDbContextFactory : IShardingParallelDbContextFactory
|
||||
{
|
||||
private readonly IVirtualTableManager _virtualTableManager;
|
||||
private readonly IShardingDbContextFactory _shardingDbContextFactory;
|
||||
private readonly IShardingCoreOptions _shardingCoreOptions;
|
||||
private readonly MySqlOptions _mySqlOptions;
|
||||
|
||||
public ShardingMySqlParallelDbContextFactory(IVirtualTableManager virtualTableManager,IShardingDbContextFactory shardingDbContextFactory,IShardingCoreOptions shardingCoreOptions, MySqlOptions mySqlOptions)
|
||||
{
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_shardingDbContextFactory = shardingDbContextFactory;
|
||||
_shardingCoreOptions = shardingCoreOptions;
|
||||
_mySqlOptions = mySqlOptions;
|
||||
}
|
||||
|
||||
public DbContext Create(string tail)
|
||||
{
|
||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
|
||||
var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(shardingConfigEntry.ConnectionString), tail);
|
||||
return _shardingDbContextFactory.Create(shardingDbContextOptions);
|
||||
}
|
||||
|
||||
private DbContextOptions CreateOptions(string connectionString)
|
||||
{
|
||||
return CreateDbContextOptionBuilder()
|
||||
#if EFCORE5
|
||||
.UseMySql(connectionString,_mySqlOptions.ServerVersion,_mySqlOptions.MySqlOptionsAction)
|
||||
#endif
|
||||
#if !EFCORE5
|
||||
|
||||
.UseMySql(connectionString, _mySqlOptions.MySqlOptionsAction)
|
||||
#endif
|
||||
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
|
||||
.ReplaceService<IQueryCompiler, ShardingQueryCompiler>()
|
||||
.ReplaceService<IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
|
||||
.ReplaceService<IModelCustomizer, ShardingModelCustomizer>()
|
||||
.UseShardingMySqlQuerySqlGenerator()
|
||||
.Options;
|
||||
}
|
||||
private DbContextOptionsBuilder CreateDbContextOptionBuilder()
|
||||
{
|
||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(shardingConfigEntry.DbContextType);
|
||||
return (DbContextOptionsBuilder)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.Query.Internal;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.SqlServer.EFCores;
|
||||
using System;
|
||||
|
||||
#if EFCORE2
|
||||
using Microsoft.EntityFrameworkCore.Query.Sql;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.SqlServer
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2020年4月7日 9:30:18
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public static class DIExtension
|
||||
{
|
||||
public static IServiceCollection AddShardingSqlServer(this IServiceCollection services, Action<SqlServerOptions> configure)
|
||||
{
|
||||
if (configure == null)
|
||||
throw new ArgumentNullException($"AddScfSqlServerProvider 参数不能为空:{nameof(configure)}");
|
||||
|
||||
var options = new SqlServerOptions();
|
||||
configure(options);
|
||||
services.AddSingleton<IShardingCoreOptions, SqlServerOptions>(sp=> options);
|
||||
services.AddShardingCore();
|
||||
|
||||
services.AddSingleton<IDbContextOptionsProvider, SqlServerDbContextOptionsProvider>();
|
||||
services.AddSingleton<IShardingParallelDbContextFactory, ShardingSqlServerParallelDbContextFactory>();
|
||||
|
||||
services.AddSingleton<IShardingBootstrapper,ShardingBootstrapper>();
|
||||
return services;
|
||||
}
|
||||
|
||||
internal static DbContextOptionsBuilder UseShardingSqlServerQuerySqlGenerator(this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory, ShardingSqlServerQuerySqlGeneratorFactory>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
internal static DbContextOptionsBuilder<TContext> UseShardingSqlServerQuerySqlGenerator<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder) where TContext : DbContext
|
||||
{
|
||||
optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory, ShardingSqlServerQuerySqlGeneratorFactory>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
|
||||
|
||||
public static DbContextOptionsBuilder UseSharding(this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.ReplaceService<IDbSetSource, ShardingDbSetSource>().ReplaceService<IQueryCompiler, ShardingQueryCompiler>();
|
||||
return optionsBuilder;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,214 +0,0 @@
|
|||
#if !EFCORE2
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
|
||||
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.Core.ShardingAccessors;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace ShardingCore.SqlServer.EFCores
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Tuesday, 22 December 2020 09:47:59
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingSqlServerQuerySqlGeneratorFactory : IQuerySqlGeneratorFactory
|
||||
{
|
||||
public ShardingSqlServerQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies)
|
||||
{
|
||||
Dependencies = dependencies;
|
||||
}
|
||||
|
||||
public QuerySqlGeneratorDependencies Dependencies { get; }
|
||||
public QuerySqlGenerator Create() => new ShardingSqlServerQuerySqlGenerator(Dependencies);
|
||||
}
|
||||
|
||||
public class ShardingSqlServerQuerySqlGenerator : SqlServerQuerySqlGenerator
|
||||
{
|
||||
public ShardingSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies)
|
||||
: base(dependencies)
|
||||
{
|
||||
}
|
||||
|
||||
protected override Expression VisitTable(TableExpression tableExpression)
|
||||
{
|
||||
return OverrideVisitTable(tableExpression);
|
||||
// this._relationalCommandBuilder.Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Name, tableExpression.Schema)).Append((object) this.AliasSeparator).Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
// return (Expression) tableExpression;
|
||||
|
||||
// typeof(TableExpression)
|
||||
// .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name.Contains(nameof(tableExpression.Name)))
|
||||
// .SetValue(tableExpression,"(select * from Log_1Message union all select * from Log_1Message)");
|
||||
|
||||
// base will append schema, table and alias
|
||||
}
|
||||
|
||||
private Expression OverrideVisitTable(TableExpression tableExpression)
|
||||
{
|
||||
var shardingAccessor = ShardingContainer.Services.GetService<IShardingAccessor>();
|
||||
if (shardingAccessor?.ShardingContext != null)
|
||||
{
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
var virtualTable = virtualTableManager.GetAllVirtualTables().FirstOrDefault(o => o.GetOriginalTableName() == tableExpression.Name);
|
||||
if(virtualTable!=null)
|
||||
{
|
||||
var tails = shardingAccessor.ShardingContext.GetContextQueryTails(virtualTable);
|
||||
var tailPrefix = virtualTable.ShardingConfig.TailPrefix;
|
||||
string newTableName = null;
|
||||
var sqlGenerationHelper = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_sqlGenerationHelper") as ISqlGenerationHelper;
|
||||
|
||||
if (tails.IsEmpty())
|
||||
{
|
||||
var firstTail = virtualTableManager.GetVirtualTable(tableExpression.Name).GetAllPhysicTables()[0].Tail;
|
||||
newTableName = $"( select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{firstTail}", tableExpression.Schema)} where 1=2 )";
|
||||
}
|
||||
else if (tails.Count == 1)
|
||||
{
|
||||
//对tableExpresion进行重写
|
||||
//typeof(TableExpression)
|
||||
// .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name==$"<{nameof(tableExpression.Name)}>k__BackingField")
|
||||
// .SetValue(tableExpression,$"{tableExpression.Name}{tailPrefix}{entry.Tails[0]}");
|
||||
newTableName = sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{tails[0]}", tableExpression.Schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{tail}", tableExpression.Schema)}")) + ")";
|
||||
}
|
||||
|
||||
var relationalCommandBuilder = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder;
|
||||
relationalCommandBuilder.Append(newTableName).Append(this.AliasSeparator).Append(sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
return tableExpression;
|
||||
}
|
||||
}
|
||||
|
||||
var result = base.VisitTable(tableExpression);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if EFCORE2
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Core.ShardingAccessors;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using Microsoft.EntityFrameworkCore.Query.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Query.Sql;
|
||||
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
|
||||
using Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace ShardingCore.SqlServer.EFCores
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Tuesday, 22 December 2020 09:47:59
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingSqlServerQuerySqlGeneratorFactory :QuerySqlGeneratorFactoryBase
|
||||
{
|
||||
|
||||
private readonly ISqlServerOptions _sqlServerOptions;
|
||||
|
||||
/// <summary>
|
||||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
|
||||
/// directly from your code. This API may change or be removed in future releases.
|
||||
/// </summary>
|
||||
public ShardingSqlServerQuerySqlGeneratorFactory(
|
||||
QuerySqlGeneratorDependencies dependencies,
|
||||
ISqlServerOptions sqlServerOptions)
|
||||
: base(dependencies)
|
||||
{
|
||||
_sqlServerOptions = sqlServerOptions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
|
||||
/// directly from your code. This API may change or be removed in future releases.
|
||||
/// </summary>
|
||||
public override IQuerySqlGenerator CreateDefault(SelectExpression selectExpression)
|
||||
=> new ShardingSqlServerQuerySqlGenerator(
|
||||
Dependencies,
|
||||
selectExpression,
|
||||
_sqlServerOptions.RowNumberPagingEnabled);
|
||||
}
|
||||
|
||||
public class ShardingSqlServerQuerySqlGenerator : SqlServerQuerySqlGenerator
|
||||
{
|
||||
|
||||
public ShardingSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies, SelectExpression selectExpression, bool rowNumberPagingEnabled) : base(dependencies, selectExpression, rowNumberPagingEnabled)
|
||||
{
|
||||
}
|
||||
|
||||
public override Expression VisitTable(TableExpression tableExpression)
|
||||
{
|
||||
return OverrideVisitTable(tableExpression);
|
||||
}
|
||||
// protected override Expression VisitTable(TableExpression tableExpression)
|
||||
// {
|
||||
// return OverrideVisitTable(tableExpression);
|
||||
// // this._relationalCommandBuilder.Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Name, tableExpression.Schema)).Append((object) this.AliasSeparator).Append((object) this._sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
// // return (Expression) tableExpression;
|
||||
//
|
||||
// // typeof(TableExpression)
|
||||
// // .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name.Contains(nameof(tableExpression.Name)))
|
||||
// // .SetValue(tableExpression,"(select * from Log_1Message union all select * from Log_1Message)");
|
||||
//
|
||||
// // base will append schema, table and alias
|
||||
// }
|
||||
|
||||
private Expression OverrideVisitTable(TableExpression tableExpression)
|
||||
{
|
||||
var shardingAccessor = ShardingContainer.Services.GetService<IShardingAccessor>();
|
||||
if (shardingAccessor?.ShardingContext != null)
|
||||
{
|
||||
var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
var virtualTable = virtualTableManager.GetAllVirtualTables().FirstOrDefault(o => o.GetOriginalTableName() == tableExpression.Table);
|
||||
if(virtualTable!=null)
|
||||
{
|
||||
var tails = shardingAccessor.ShardingContext.GetContextQueryTails(virtualTable);
|
||||
var tailPrefix = virtualTable.ShardingConfig.TailPrefix;
|
||||
string newTableName = null;
|
||||
var sqlGenerationHelper = typeof(DefaultQuerySqlGenerator).GetTypeFieldValue(this, "_sqlGenerationHelper") as ISqlGenerationHelper;
|
||||
|
||||
if (tails.IsEmpty())
|
||||
{
|
||||
var firstTail = virtualTableManager.GetVirtualTable(tableExpression.Table).GetAllPhysicTables()[0].Tail;
|
||||
newTableName = $"( select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Table}{tailPrefix}{firstTail}", tableExpression.Schema)} where 1=2 )";
|
||||
}
|
||||
else if (tails.Count == 1)
|
||||
{
|
||||
//对tableExpresion进行重写
|
||||
//typeof(TableExpression)
|
||||
// .GetFields( BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o=>o.Name==$"<{nameof(tableExpression.Name)}>k__BackingField")
|
||||
// .SetValue(tableExpression,$"{tableExpression.Name}{tailPrefix}{entry.Tails[0]}");
|
||||
newTableName = sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Table}{tailPrefix}{tails[0]}", tableExpression.Schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Table}{tailPrefix}{tail}", tableExpression.Schema)}")) + ")";
|
||||
}
|
||||
|
||||
var relationalCommandBuilder = typeof(DefaultQuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder;
|
||||
relationalCommandBuilder.Append(newTableName).Append(this.AliasSeparator).Append(sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
|
||||
return tableExpression;
|
||||
}
|
||||
}
|
||||
|
||||
var result = base.VisitTable(tableExpression);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -1,211 +0,0 @@
|
|||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using System.Text;
|
||||
// using Microsoft.EntityFrameworkCore;
|
||||
// using Microsoft.EntityFrameworkCore.Metadata;
|
||||
// using Microsoft.EntityFrameworkCore.SqlServer.Update.Internal;
|
||||
// using Microsoft.EntityFrameworkCore.Update;
|
||||
// using Microsoft.Extensions.DependencyInjection;
|
||||
// using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
// using ShardingCore.Core.VirtualTables;
|
||||
// using ShardingCore.Exceptions;
|
||||
//
|
||||
// namespace ShardingCore.SqlServer.EFCores
|
||||
// {
|
||||
// /*
|
||||
// * @Author: xjm
|
||||
// * @Description:
|
||||
// * @Date: Friday, 23 July 2021 23:17:19
|
||||
// * @Email: 326308290@qq.com
|
||||
// */
|
||||
// public class ShardingSqlServerUpdateSqlGenerator:SqlServerUpdateSqlGenerator
|
||||
// {
|
||||
// public ShardingSqlServerUpdateSqlGenerator(UpdateSqlGeneratorDependencies dependencies) : base(dependencies)
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// public override ResultSetMapping AppendInsertOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
|
||||
// {
|
||||
// // Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
|
||||
// // Check.NotNull(command, nameof(command));
|
||||
// var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
//
|
||||
// var name = command.TableName;
|
||||
// var schema = command.Schema;
|
||||
//
|
||||
// var operations = command.ColumnModifications;
|
||||
// var tryGetVirtualTable = virtualTableManager.TryGetVirtualTable(name);
|
||||
// if (tryGetVirtualTable != null)
|
||||
// {
|
||||
// var shardingEntityConfig = tryGetVirtualTable.ShardingConfig;
|
||||
// var shardingCol = operations.FirstOrDefault(o=>o.Property.Name.Equals(shardingEntityConfig.ShardingField));
|
||||
// if (shardingCol == null)
|
||||
// throw new ShardingKeyRouteNotMatchException();
|
||||
// // shardingEntityConfig.ShardingField
|
||||
// var physicTables = tryGetVirtualTable.RouteTo(new TableRouteConfig(null,null,shardingCol.Value));
|
||||
// name = physicTables[0].FullName;
|
||||
// }
|
||||
//
|
||||
// var writeOperations = operations.Where(o => o.IsWrite).ToList();
|
||||
// var readOperations = operations.Where(o => o.IsRead).ToList();
|
||||
//
|
||||
// AppendInsertCommand(commandStringBuilder, name, schema, writeOperations);
|
||||
//
|
||||
// if (readOperations.Count > 0)
|
||||
// {
|
||||
// var keyOperations = operations.Where(o => o.IsKey).ToList();
|
||||
//
|
||||
// return AppendSelectAffectedCommand(commandStringBuilder, name, schema, readOperations, keyOperations, commandPosition);
|
||||
// }
|
||||
//
|
||||
// return ResultSetMapping.NoResultSet;
|
||||
// }
|
||||
//
|
||||
// public override ResultSetMapping AppendUpdateOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
|
||||
// {
|
||||
// // Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
|
||||
// // Check.NotNull(command, nameof(command));
|
||||
//
|
||||
// var name = command.TableName;
|
||||
// var schema = command.Schema;
|
||||
// var operations = command.ColumnModifications;
|
||||
//
|
||||
// var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
// var tryGetVirtualTable = virtualTableManager.TryGetVirtualTable(name);
|
||||
// if (tryGetVirtualTable != null)
|
||||
// {
|
||||
// var shardingEntityConfig = tryGetVirtualTable.ShardingConfig;
|
||||
// var shardingCol = operations.FirstOrDefault(o=>o.IsRead&&o.Property.Name.Equals(shardingEntityConfig.ShardingField));
|
||||
// if (shardingCol == null)
|
||||
// throw new ShardingKeyRouteNotMatchException();
|
||||
// // shardingEntityConfig.ShardingField
|
||||
// var physicTables = tryGetVirtualTable.RouteTo(new TableRouteConfig(null,null,shardingCol.Value));
|
||||
// name = physicTables[0].FullName;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// var writeOperations = operations.Where(o => o.IsWrite).ToList();
|
||||
// var conditionOperations = operations.Where(o => o.IsCondition).ToList();
|
||||
// var readOperations = operations.Where(o => o.IsRead).ToList();
|
||||
//
|
||||
// AppendUpdateCommand(commandStringBuilder, name, schema, writeOperations, conditionOperations);
|
||||
//
|
||||
// if (readOperations.Count > 0)
|
||||
// {
|
||||
// var keyOperations = operations.Where(o => o.IsKey).ToList();
|
||||
//
|
||||
// return AppendSelectAffectedCommand(commandStringBuilder, name, schema, readOperations, keyOperations, commandPosition);
|
||||
// }
|
||||
//
|
||||
// return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
|
||||
// }
|
||||
//
|
||||
// public override ResultSetMapping AppendDeleteOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
|
||||
// {
|
||||
// // Check.NotNull<StringBuilder>(commandStringBuilder, nameof (commandStringBuilder));
|
||||
// // Check.NotNull<ModificationCommand>(command, nameof (command));
|
||||
//
|
||||
// var name = command.TableName;
|
||||
// var schema = command.Schema;
|
||||
// var conditionOperations = command.ColumnModifications.Where(o => o.IsCondition).ToList();
|
||||
//
|
||||
// var virtualTableManager = ShardingContainer.Services.GetService<IVirtualTableManager>();
|
||||
// var tryGetVirtualTable = virtualTableManager.TryGetVirtualTable(name);
|
||||
// if (tryGetVirtualTable != null)
|
||||
// {
|
||||
// var shardingEntityConfig = tryGetVirtualTable.ShardingConfig;
|
||||
// var shardingCol = command.ColumnModifications.FirstOrDefault(o=>o.IsRead&&o.Property.Name.Equals(shardingEntityConfig.ShardingField));
|
||||
// if (shardingCol == null)
|
||||
// throw new ShardingKeyRouteNotMatchException();
|
||||
// // shardingEntityConfig.ShardingField
|
||||
// var physicTables = tryGetVirtualTable.RouteTo(new TableRouteConfig(null,null,shardingCol.Value));
|
||||
// name = physicTables[0].FullName;
|
||||
// }
|
||||
// AppendDeleteCommand(commandStringBuilder, name, schema, conditionOperations);
|
||||
//
|
||||
// return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
|
||||
// }
|
||||
//
|
||||
// public override ResultSetMapping AppendBulkInsertOperation(StringBuilder commandStringBuilder, IReadOnlyList<ModificationCommand> modificationCommands, int commandPosition)
|
||||
// {
|
||||
//
|
||||
//
|
||||
// if (modificationCommands.Count == 1
|
||||
// && modificationCommands[0].ColumnModifications.All(
|
||||
// o =>
|
||||
// !o.IsKey
|
||||
// || !o.IsRead
|
||||
// || o.Property?.GetValueGenerationStrategy() == SqlServerValueGenerationStrategy.IdentityColumn))
|
||||
// {
|
||||
// return AppendInsertOperation(commandStringBuilder, modificationCommands[0], commandPosition);
|
||||
// }
|
||||
//
|
||||
// var readOperations = modificationCommands[0].ColumnModifications.Where(o => o.IsRead).ToList();
|
||||
// var writeOperations = modificationCommands[0].ColumnModifications.Where(o => o.IsWrite).ToList();
|
||||
// var keyOperations = modificationCommands[0].ColumnModifications.Where(o => o.IsKey).ToList();
|
||||
//
|
||||
// var defaultValuesOnly = writeOperations.Count == 0;
|
||||
// var nonIdentityOperations = modificationCommands[0].ColumnModifications
|
||||
// .Where(o => o.Property?.GetValueGenerationStrategy() != SqlServerValueGenerationStrategy.IdentityColumn)
|
||||
// .ToList();
|
||||
//
|
||||
// if (defaultValuesOnly)
|
||||
// {
|
||||
// if (nonIdentityOperations.Count == 0
|
||||
// || readOperations.Count == 0)
|
||||
// {
|
||||
// foreach (var modification in modificationCommands)
|
||||
// {
|
||||
// AppendInsertOperation(commandStringBuilder, modification, commandPosition);
|
||||
// }
|
||||
//
|
||||
// return readOperations.Count == 0
|
||||
// ? ResultSetMapping.NoResultSet
|
||||
// : ResultSetMapping.LastInResultSet;
|
||||
// }
|
||||
//
|
||||
// if (nonIdentityOperations.Count > 1)
|
||||
// {
|
||||
// nonIdentityOperations.RemoveRange(1, nonIdentityOperations.Count - 1);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (readOperations.Count == 0)
|
||||
// {
|
||||
// return AppendBulkInsertWithoutServerValues(commandStringBuilder, modificationCommands, writeOperations);
|
||||
// }
|
||||
//
|
||||
// if (defaultValuesOnly)
|
||||
// {
|
||||
// return AppendBulkInsertWithServerValuesOnly(
|
||||
// commandStringBuilder, modificationCommands, commandPosition, nonIdentityOperations, keyOperations, readOperations);
|
||||
// }
|
||||
//
|
||||
// if (modificationCommands[0].Entries.SelectMany(e => e.EntityType.GetAllBaseTypesInclusive())
|
||||
// .Any(e => e.IsMemoryOptimized()))
|
||||
// {
|
||||
// if (!nonIdentityOperations.Any(o => o.IsRead && o.IsKey))
|
||||
// {
|
||||
// foreach (var modification in modificationCommands)
|
||||
// {
|
||||
// AppendInsertOperation(commandStringBuilder, modification, commandPosition++);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// foreach (var modification in modificationCommands)
|
||||
// {
|
||||
// AppendInsertOperationWithServerKeys(
|
||||
// commandStringBuilder, modification, keyOperations, readOperations, commandPosition++);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return ResultSetMapping.LastInResultSet;
|
||||
// }
|
||||
//
|
||||
// return AppendBulkInsertWithServerValues(
|
||||
// commandStringBuilder, modificationCommands, commandPosition, writeOperations, keyOperations, readOperations);
|
||||
// }
|
||||
// }
|
||||
// }
|
|
@ -1,30 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Version>$(EFCORE5)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE5;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.SqlServer.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.7" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShardingCore\ShardingCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Extensions.Logging.Console">
|
||||
<HintPath>C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.Logging.Console.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -1,61 +0,0 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Query.Internal;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace ShardingCore.SqlServer
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Tuesday, 29 December 2020 15:22:50
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingSqlServerParallelDbContextFactory : IShardingParallelDbContextFactory
|
||||
{
|
||||
private readonly IVirtualTableManager _virtualTableManager;
|
||||
private readonly IShardingDbContextFactory _shardingDbContextFactory;
|
||||
private readonly IShardingCoreOptions _shardingCoreOptions;
|
||||
|
||||
public ShardingSqlServerParallelDbContextFactory(IVirtualTableManager virtualTableManager,IShardingCoreOptions shardingCoreOptions, IShardingDbContextFactory shardingDbContextFactory)
|
||||
{
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_shardingDbContextFactory = shardingDbContextFactory;
|
||||
_shardingCoreOptions = shardingCoreOptions;
|
||||
}
|
||||
|
||||
public DbContext Create(string tail)
|
||||
{
|
||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
|
||||
var shardingDbContextOptions = new ShardingDbContextOptions(CreateOptions(shardingConfigEntry.ConnectionString), tail);
|
||||
return _shardingDbContextFactory.Create(shardingDbContextOptions);
|
||||
}
|
||||
|
||||
private DbContextOptions CreateOptions(string connectString)
|
||||
{
|
||||
return CreateDbContextOptionBuilder()
|
||||
.UseSqlServer(connectString)
|
||||
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
|
||||
.ReplaceService<IQueryCompiler, ShardingQueryCompiler>()
|
||||
.ReplaceService<IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
|
||||
.ReplaceService<IModelCustomizer, ShardingModelCustomizer>()
|
||||
.UseShardingSqlServerQuerySqlGenerator()
|
||||
.Options;
|
||||
}
|
||||
private DbContextOptionsBuilder CreateDbContextOptionBuilder()
|
||||
{
|
||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(shardingConfigEntry.DbContextType);
|
||||
return (DbContextOptionsBuilder)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Query.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts.ShareDbContextOptionsProviders;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Extensions;
|
||||
#if EFCORE2
|
||||
using System.Data.SqlClient;
|
||||
#endif
|
||||
#if !EFCORE2
|
||||
using Microsoft.Data.SqlClient;
|
||||
#endif
|
||||
|
||||
namespace ShardingCore.SqlServer
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Thursday, 24 December 2020 10:33:51
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SqlServerDbContextOptionsProvider : IDbContextOptionsProvider
|
||||
{
|
||||
private readonly IShardingCoreOptions _shardingCoreOptions;
|
||||
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
||||
});
|
||||
|
||||
|
||||
public SqlServerDbContextOptionsProvider(IShardingCoreOptions shardingCoreOptions)
|
||||
{
|
||||
_shardingCoreOptions = shardingCoreOptions;
|
||||
}
|
||||
public DbContextOptions GetDbContextOptions(DbConnection dbConnection)
|
||||
{
|
||||
|
||||
var track = dbConnection != null;
|
||||
var connection = dbConnection ?? GetSqlConnection();
|
||||
var dbContextOptions = CreateDbContextOptionBuilder()
|
||||
.UseSqlServer(connection)
|
||||
.UseLoggerFactory(efLogger)
|
||||
.IfDo(!track, o => o.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking))
|
||||
//.IfDo(isQuery,o=>o.ReplaceService<IQueryCompiler, ShardingQueryCompiler>())
|
||||
.ReplaceService<IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
|
||||
.ReplaceService<IModelCustomizer, ShardingModelCustomizer>()
|
||||
//.IfDo(isQuery,o=>o.UseShardingSqlServerQuerySqlGenerator())
|
||||
.Options;
|
||||
return dbContextOptions;
|
||||
}
|
||||
private SqlConnection GetSqlConnection()
|
||||
{
|
||||
var connectionString = _shardingCoreOptions.GetShardingConfig().ConnectionString;
|
||||
return new SqlConnection(connectionString);
|
||||
}
|
||||
|
||||
|
||||
private DbContextOptionsBuilder CreateDbContextOptionBuilder()
|
||||
{
|
||||
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(shardingConfigEntry.DbContextType);
|
||||
return (DbContextOptionsBuilder)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ShardingCore.SqlServer
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2020年4月7日 8:34:04
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class SqlServerOptions: AbstractShardingCoreOptions
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,14 +1,9 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.Query.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Sharding.Enumerators;
|
||||
using ShardingCore.Sharding.StreamMergeEngines;
|
||||
using ShardingCore.Sharding.StreamMergeEngines.Abstractions;
|
||||
using ShardingCore.Sharding.StreamMergeEngines.AggregateMergeEngines;
|
||||
|
@ -29,20 +24,12 @@ namespace ShardingCore.EFCores
|
|||
**/
|
||||
public class ShardingQueryCompiler : IQueryCompiler
|
||||
{
|
||||
private readonly IQueryContextFactory _queryContextFactory;
|
||||
private readonly IDatabase _database;
|
||||
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;
|
||||
private readonly ICurrentDbContext _currentContext;
|
||||
private readonly IModel _model;
|
||||
private readonly IStreamMergeContextFactory _streamMergeContextFactory;
|
||||
|
||||
public ShardingQueryCompiler(IQueryContextFactory queryContextFactory, ICompiledQueryCache compiledQueryCache, ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator, IDatabase database, IDiagnosticsLogger<DbLoggerCategory.Query> logger, ICurrentDbContext currentContext, IEvaluatableExpressionFilter evaluatableExpressionFilter, IModel model)
|
||||
public ShardingQueryCompiler(ICurrentDbContext currentContext)
|
||||
{
|
||||
_queryContextFactory = queryContextFactory;
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
_currentContext = currentContext;
|
||||
_model = model;
|
||||
_streamMergeContextFactory = ShardingContainer.GetService<IStreamMergeContextFactory>();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>$(EFCORE2)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE2;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AssemblyName>ShardingCore.MySql</AssemblyName>
|
||||
<RootNamespace>ShardingCore.MySql</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.MySql.2x.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\ShardingCore.MySql\**\*.cs" />
|
||||
<Compile Remove="..\..\src\ShardingCore.MySql\obj\**" />
|
||||
<Compile Remove="..\..\src\ShardingCore.MySql\bin\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShardingCore.2x\ShardingCore.2x.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
|
@ -1,34 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>$(EFCORE2)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE2;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AssemblyName>ShardingCore.SqlServer</AssemblyName>
|
||||
<RootNamespace>ShardingCore.SqlServer</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.SqlServer.2x.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\ShardingCore.SqlServer\**\*.cs" />
|
||||
<Compile Remove="..\..\src\ShardingCore.SqlServer\obj\**" />
|
||||
<Compile Remove="..\..\src\ShardingCore.SqlServer\bin\**" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShardingCore.2x\ShardingCore.2x.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,32 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>$(EFCORE3)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE3;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AssemblyName>ShardingCore.MySql</AssemblyName>
|
||||
<RootNamespace>ShardingCore.MySql</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.MySql.3x.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\ShardingCore.MySql\**\*.cs" />
|
||||
<Compile Remove="..\..\src\ShardingCore.MySql\obj\**" />
|
||||
<Compile Remove="..\..\src\ShardingCore.MySql\bin\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShardingCore.3x\ShardingCore.3x.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -1,33 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>$(EFCORE3)</Version>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<DefineConstants>TRACE;DEBUG;EFCORE3;</DefineConstants>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AssemblyName>ShardingCore.SqlServer</AssemblyName>
|
||||
<RootNamespace>ShardingCore.SqlServer</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<DocumentationFile>bin\Release\ShardingCore.SqlServer.3x.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\ShardingCore.SqlServer\**\*.cs" />
|
||||
<Compile Remove="..\..\src\ShardingCore.SqlServer\obj\**" />
|
||||
<Compile Remove="..\..\src\ShardingCore.SqlServer\bin\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShardingCore.3x\ShardingCore.3x.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -15,10 +15,6 @@
|
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ShardingCore.SqlServer\ShardingCore.SqlServer.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Configs\DbConfig.json">
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
|
|
Loading…
Reference in New Issue