Skip to content

Commit

Permalink
1. fixed Dynamic child nest tag ignore prepend bug.
Browse files Browse the repository at this point in the history
2. fixed RequestContext.ReadDb Default bug.
3. add SmartSql.Extensions.IgnoreDataSourceChoiceFilter
4. add SmartSql.Extensions.IgnoreDataSourceChoiceFilter_Tests
  • Loading branch information
Ahoo-Wang committed Sep 30, 2018
1 parent 194e86f commit 884a7e0
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 3 deletions.
6 changes: 6 additions & 0 deletions SmartSql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartSql.Options", "src\Sma
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartSql.AOP", "src\SmartSql.AOP\SmartSql.AOP.csproj", "{351F5809-0CD0-4453-964C-FF5EF68D318B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartSql.Extensions", "src\SmartSql.Extensions\SmartSql.Extensions.csproj", "{70465DA9-354E-4864-99D6-4BF53ECB077B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -102,6 +104,10 @@ Global
{351F5809-0CD0-4453-964C-FF5EF68D318B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{351F5809-0CD0-4453-964C-FF5EF68D318B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{351F5809-0CD0-4453-964C-FF5EF68D318B}.Release|Any CPU.Build.0 = Release|Any CPU
{70465DA9-354E-4864-99D6-4BF53ECB077B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70465DA9-354E-4864-99D6-4BF53ECB077B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70465DA9-354E-4864-99D6-4BF53ECB077B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70465DA9-354E-4864-99D6-4BF53ECB077B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
87 changes: 87 additions & 0 deletions src/SmartSql.Extensions/IgnoreDataSourceChoiceFilter.cs
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;
}
}
}
29 changes: 29 additions & 0 deletions src/SmartSql.Extensions/SmartSql.Extensions.csproj
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 src/SmartSql.UTests/Extensions/IgnoreDataSourceChoiceFilter_Tests.cs
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();
}
}
}
1 change: 1 addition & 0 deletions src/SmartSql.UTests/SmartSql.UTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SmartSql.Extensions\SmartSql.Extensions.csproj" />
<ProjectReference Include="..\SmartSql.DapperDeserializer\SmartSql.DapperDeserializer.csproj" />
<ProjectReference Include="..\SmartSql.DIExtension\SmartSql.DIExtension.csproj" />
<ProjectReference Include="..\SmartSql.DyRepository\SmartSql.DyRepository.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Abstractions/RequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal void SetupStatement()
{
CommandType = Statement.CommandType.Value;
}
if (!String.IsNullOrEmpty(ReadDb))
if (String.IsNullOrEmpty(ReadDb))
{
ReadDb = Statement.ReadDb;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/DataSourceFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private IDataSource GetDataSource(RequestContext context)
}
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug($"DataSourceManager GetDataSource Choice: {choiceDataSource.Name} .");
_logger.LogDebug($"DataSourceFilter GetDataSource Choice: {choiceDataSource.Name} .");
}
return choiceDataSource;
}
Expand Down
3 changes: 2 additions & 1 deletion src/SmartSql/SmartSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
<PackageProjectUrl>https://github.com/Ahoo-Wang/SmartSql</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<Version>3.7.7</Version>
<Version>3.7.8</Version>
<PackageTags>orm sql read-write-separation cache redis dotnet-core cross-platform high-performance distributed-computing zookeeper</PackageTags>
<PackageReleaseNotes>
1. fixed Dynamic child nest tag ignore prepend bug.
2. fixed RequestContext.ReadDb Default bug.
</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/SmartSql.png</PackageIconUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/LICENSE</PackageLicenseUrl>
Expand Down

0 comments on commit 884a7e0

Please sign in to comment.