-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. fixed Dynamic child nest tag ignore prepend bug.
2. fixed RequestContext.ReadDb Default bug. 3. add SmartSql.Extensions.IgnoreDataSourceChoiceFilter 4. add SmartSql.Extensions.IgnoreDataSourceChoiceFilter_Tests
- Loading branch information
Showing
8 changed files
with
254 additions
and
3 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
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,87 @@ | ||
using Microsoft.Extensions.Logging; | ||
using SmartSql.Abstractions; | ||
using SmartSql.Abstractions.DataSource; | ||
using SmartSql.Exceptions; | ||
using SmartSql.Utils; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SmartSql.Extensions | ||
{ | ||
/// <summary> | ||
/// Ignore DataSourceChoiceFilter | ||
/// Warning: It is not recommended to use!!! | ||
/// </summary> | ||
public class IgnoreDataSourceChoiceFilter : IDataSourceFilter | ||
{ | ||
private readonly ILogger<IgnoreDataSourceChoiceFilter> _logger; | ||
/// <summary> | ||
/// 权重筛选器 | ||
/// </summary> | ||
private WeightFilter<IReadDataSource> _weightFilter = new WeightFilter<IReadDataSource>(); | ||
public IgnoreDataSourceChoiceFilter(ILogger<IgnoreDataSourceChoiceFilter> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
public IDataSource Elect(RequestContext context) | ||
{ | ||
IDataSource dataSource; | ||
if (!String.IsNullOrEmpty(context.ReadDb)) | ||
{ | ||
dataSource = FindByDbName(context); | ||
} | ||
else | ||
{ | ||
dataSource = FindDefault(context); | ||
} | ||
if (dataSource == null) | ||
{ | ||
throw new SmartSqlException($"Statement.Key:{context.StatementKey},can not find ReadDb:{context.ReadDb} ."); | ||
} | ||
if (_logger.IsEnabled(LogLevel.Debug)) | ||
{ | ||
_logger.LogDebug($"IgnoreDataSourceChoiceFilter GetDataSource Choice: {dataSource.Name} ."); | ||
} | ||
return dataSource; | ||
} | ||
|
||
private IDataSource FindDefault(RequestContext context) | ||
{ | ||
var database = context.SmartSqlContext.Database; | ||
IDataSource dataSource = default; | ||
if (context.DataSourceChoice == DataSourceChoice.Write) | ||
{ | ||
dataSource = database.WriteDataSource; | ||
} | ||
else if (database.ReadDataSources != null) | ||
{ | ||
var seekList = database.ReadDataSources.Select(readDataSource => new WeightFilter<IReadDataSource>.WeightSource | ||
{ | ||
Source = readDataSource, | ||
Weight = readDataSource.Weight | ||
}); | ||
dataSource = _weightFilter.Elect(seekList).Source; | ||
} | ||
return dataSource; | ||
} | ||
|
||
private static IDataSource FindByDbName(RequestContext context) | ||
{ | ||
var database = context.SmartSqlContext.Database; | ||
IDataSource dataSource; | ||
if (database.WriteDataSource.Name == context.ReadDb) | ||
{ | ||
dataSource = database.WriteDataSource; | ||
} | ||
else | ||
{ | ||
dataSource = database.ReadDataSources.FirstOrDefault(m => m.Name == context.ReadDb); | ||
} | ||
if (dataSource == null) | ||
{ | ||
throw new SmartSqlException($"Statement.Key:{context.StatementKey},can not find DbName:{context.ReadDb} ."); | ||
} | ||
return dataSource; | ||
} | ||
} | ||
} |
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,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<TargetFrameworks>netstandard2.0;net46;</TargetFrameworks> | ||
<Authors>Ahoo Wang</Authors> | ||
<Company>Ahoo Wang</Company> | ||
<Title>SmartSql.Extensions</Title> | ||
<Description>SmartSql = MyBatis + Cache(Memory | Redis) + ZooKeeper + R/W Splitting +Dynamic Repository ....</Description> | ||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> | ||
<Copyright>Ahoo Wang</Copyright> | ||
<PackageProjectUrl>https://github.com/Ahoo-Wang/SmartSql</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl> | ||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance> | ||
<Version>1.0.0</Version> | ||
<PackageTags>orm sql read-write-separation cache redis dotnet-core cross-platform high-performance distributed-computing zookeeper</PackageTags> | ||
<PackageReleaseNotes> | ||
|
||
</PackageReleaseNotes> | ||
<PackageIconUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/SmartSql.png</PackageIconUrl> | ||
<PackageLicenseUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/LICENSE</PackageLicenseUrl> | ||
<LangVersion>7.1</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SmartSql\SmartSql.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
127 changes: 127 additions & 0 deletions
127
src/SmartSql.UTests/Extensions/IgnoreDataSourceChoiceFilter_Tests.cs
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,127 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using SmartSql.Abstractions; | ||
using SmartSql.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using SmartSql.UTests.Entity; | ||
|
||
namespace SmartSql.UTests.Extensions | ||
{ | ||
public class IgnoreDataSourceChoiceFilter_Tests : TestBase, IDisposable | ||
{ | ||
ISmartSqlMapper _sqlMapper; | ||
public IgnoreDataSourceChoiceFilter_Tests() | ||
{ | ||
var logger = Logging.NoneLoggerFactory.Instance.CreateLogger<IgnoreDataSourceChoiceFilter>(); | ||
_sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions | ||
{ | ||
ConfigPath = Consts.DEFAULT_SMARTSQL_CONFIG_PATH, | ||
DataSourceFilter = new IgnoreDataSourceChoiceFilter(logger) | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Query_WriteDB_Elect() | ||
{ | ||
var entity = _sqlMapper.QuerySingle<T_Entity>(new RequestContext | ||
{ | ||
Scope = Scope, | ||
SqlId = "GetEntity", | ||
ReadDb = "WriteDB", | ||
Request = new { Id = 8 } | ||
}); | ||
} | ||
[Fact] | ||
public void Query_ReadDb_Elect() | ||
{ | ||
var entity = _sqlMapper.QuerySingle<T_Entity>(new RequestContext | ||
{ | ||
Scope = Scope, | ||
SqlId = "GetEntity", | ||
ReadDb = "ReadDb-1", | ||
Request = new { Id = 8 } | ||
}); | ||
} | ||
[Fact] | ||
public void Write_ReadDb_Elect() | ||
{ | ||
_sqlMapper.Execute(new RequestContext | ||
{ | ||
Scope = Scope, | ||
SqlId = "Insert", | ||
ReadDb = "ReadDb-2", | ||
Request = new T_Entity | ||
{ | ||
CreationTime = DateTime.Now, | ||
FBool = true, | ||
FDecimal = 1, | ||
FLong = 1, | ||
FNullBool = false, | ||
FString = Guid.NewGuid().ToString("N"), | ||
FNullDecimal = 1.1M, | ||
LastUpdateTime = DateTime.Now, | ||
Status = EntityStatus.Ok | ||
} | ||
}); | ||
} | ||
[Fact] | ||
public void Write_Write_Elect() | ||
{ | ||
_sqlMapper.Execute(new RequestContext | ||
{ | ||
Scope = Scope, | ||
SqlId = "Insert", | ||
ReadDb = "WriteDB", | ||
Request = new T_Entity | ||
{ | ||
CreationTime = DateTime.Now, | ||
FBool = true, | ||
FDecimal = 1, | ||
FLong = 1, | ||
FNullBool = false, | ||
FString = Guid.NewGuid().ToString("N"), | ||
FNullDecimal = 1.1M, | ||
LastUpdateTime = DateTime.Now, | ||
Status = EntityStatus.Ok | ||
} | ||
}); | ||
} | ||
[Fact] | ||
public void Query_Default_Elect() | ||
{ | ||
var entity = _sqlMapper.QuerySingle<T_Entity>(new RequestContext | ||
{ | ||
Scope = Scope, | ||
SqlId = "GetEntity", | ||
Request = new { Id = 8 } | ||
}); | ||
} | ||
[Fact] | ||
public void Write_Default_Elect() | ||
{ | ||
_sqlMapper.Execute(new RequestContext | ||
{ | ||
Scope = Scope, | ||
SqlId = "Insert", | ||
Request = new T_Entity | ||
{ | ||
CreationTime = DateTime.Now, | ||
FBool = true, | ||
FDecimal = 1, | ||
FLong = 1, | ||
FNullBool = false, | ||
FString = Guid.NewGuid().ToString("N"), | ||
FNullDecimal = 1.1M, | ||
LastUpdateTime = DateTime.Now, | ||
Status = EntityStatus.Ok | ||
} | ||
}); | ||
} | ||
public void Dispose() | ||
{ | ||
_sqlMapper.Dispose(); | ||
} | ||
} | ||
} |
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
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
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
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