Skip to content

Commit

Permalink
1. fix [Scope Or SqlId] repeated conflicts throw explicit error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Oct 26, 2018
1 parent 9b2d006 commit cf004db
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/SmartSql.DIExtension/SmartSql.DIExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>3.7.15</Version>
<Version>3.7.16</Version>
<Authors>Ahoo Wang</Authors>
<Company>Ahoo Wang</Company>
<Copyright>Ahoo Wang</Copyright>
Expand All @@ -16,7 +16,7 @@
<PackageIconUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/SmartSql.png</PackageIconUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/LICENSE</PackageLicenseUrl>
<PackageReleaseNotes>
1. adapt SmartSql V3.7.15
1. adapt SmartSql V3.7.16
</PackageReleaseNotes>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/SmartSql.DyRepository/SmartSql.DyRepository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<Company>Ahoo Wang</Company>
<Title>SmartSql.DyRepository</Title>
<Description>SmartSql 动态代理仓储</Description>
<Version>3.7.15</Version>
<Version>3.7.16</Version>
<PackageProjectUrl>https://github.com/Ahoo-Wang/SmartSql</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl>
<PackageReleaseNotes>
1. adapt SmartSql V3.7.15
1. adapt SmartSql V3.7.16
</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/SmartSql.png</PackageIconUrl>
<Copyright>Ahoo Wang</Copyright>
Expand Down
9 changes: 5 additions & 4 deletions src/SmartSql.Options/OptionConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override SmartSqlMapConfig Load()
{
SqlMapConfig = new SmartSqlMapConfig()
{
SmartSqlMaps = new List<SmartSqlMap>(),
SmartSqlMaps = new Dictionary<String, SmartSqlMap>(),
SmartSqlMapSources = _options.SmartSqlMaps,
Database = new Configuration.Database()
{
Expand Down Expand Up @@ -123,7 +123,7 @@ private SmartSqlMap LoadSmartSqlMap(SmartSqlMapConfig config, string sqlMapPath)
}
var sqlmapStream = LoadConfigStream(sqlMapPath);
var sqlmap = LoadSmartSqlMap(sqlmapStream);
config.SmartSqlMaps.Add(sqlmap);
config.SmartSqlMaps.Add(sqlmap.Scope, sqlmap);

return sqlmap;
}
Expand All @@ -145,8 +145,9 @@ private void WatchConfig(SmartSqlMapConfig config)
{
#region SmartSqlMaps File Watch

foreach (var sqlmap in SqlMapConfig.SmartSqlMaps)
foreach (var sqlmapKV in SqlMapConfig.SmartSqlMaps)
{
var sqlmap = sqlmapKV.Value;
#region SqlMap File Watch

if (_logger.IsEnabled(LogLevel.Debug))
Expand All @@ -166,7 +167,7 @@ private void WatchConfig(SmartSqlMapConfig config)
_logger.LogDebug($"LocalFileConfigLoader Changed Reload SmartSqlMap: {sqlmap.Path} Starting");
}
var newSqlMap = LoadSmartSqlMap(config, sqlmap.Path);
var oldSqlMap = SqlMapConfig.SmartSqlMaps.First(s => s.Path == sqlmap.Path);
var oldSqlMap = SqlMapConfig.SmartSqlMaps.Values.First(s => s.Path == sqlmap.Path);
oldSqlMap.Caches = newSqlMap.Caches;
oldSqlMap.Scope = newSqlMap.Scope;
oldSqlMap.Statements = newSqlMap.Statements;
Expand Down
4 changes: 2 additions & 2 deletions src/SmartSql.Options/SmartSql.Options.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<Product>SmartSq</Product>
<Copyright>SmartSq</Copyright>
<Title>SmartSql.Options</Title>
<Version>1.4.1</Version>
<Version>3.7.16</Version>
<PackageLicenseUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/Ahoo-Wang/SmartSql</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/SmartSql.png</PackageIconUrl>
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl>
<PackageTags>orm sql read-write-separation cache redis dotnet-core cross-platform high-performance distributed-computing zookeeper</PackageTags>
<PackageReleaseNotes>
1. fix https://github.com/Ahoo-Wang/SmartSql/issues/42
1. adapt SmartSql V3.7.16
</PackageReleaseNotes>

</PropertyGroup>
Expand Down
9 changes: 5 additions & 4 deletions src/SmartSql.ZooKeeperConfig/SmartSql.ZooKeeperConfig.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<RepositoryUrl>https://github.com/Ahoo-Wang/SmartSql</RepositoryUrl>
<PackageTags>SmartSql Dapper MyBatis ORM ZooKeeper Config</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
<Version>3.0.0</Version>
<Version>3.7.16</Version>
<Authors>Ahoo Wang</Authors>
<Company>Ahoo Wang</Company>
<Product>SmartSql.ZooKeeperConfig</Product>
<PackageIconUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/SmartSql.png</PackageIconUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/Ahoo-Wang/SmartSql/master/LICENSE</PackageLicenseUrl>
<PackageReleaseNotes>
1. adapt SmartSql V3.7.16
</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
Expand All @@ -27,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ZooKeeperNetEx" Version="3.4.10.1" />
<PackageReference Include="ZooKeeperNetEx" Version="3.4.12" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/SmartSql.ZooKeeperConfig/ZooKeeperConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<SmartSqlMapConfig> LoadAsync()
case SmartSqlMapSource.ResourceType.File:
{
var sqlmap = await LoadSmartSqlMapAsync(sqlmapSource.Path, config);
config.SmartSqlMaps.Add(sqlmap);
config.SmartSqlMaps.Add(sqlmap.Scope, sqlmap);
break;
}
case SmartSqlMapSource.ResourceType.Directory:
Expand All @@ -96,7 +96,7 @@ public async Task<SmartSqlMapConfig> LoadAsync()
{
var sqlmapPath = $"{sqlmapSource.Path}/{sqlmapChild}";
var sqlmap = await LoadSmartSqlMapAsync(sqlmapPath, config);
config.SmartSqlMaps.Add(sqlmap);
config.SmartSqlMaps.Add(sqlmap.Scope, sqlmap);
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql.ZooKeeperConfig/ZooKeeperWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override async Task process(WatchedEvent @event)
else
{
_logger.LogDebug($"ZooKeeperConfigLoader.SmartSqlMapWatcher Changed Reload SmartSqlMap: {path} Starting");
var sqlmap = SmartSqlMapConfig.SmartSqlMaps.FirstOrDefault(m => m.Path == path);
var sqlmap = SmartSqlMapConfig.SmartSqlMaps.Values.FirstOrDefault(m => m.Path == path);
var newSqlmap = await ConfigLoader.LoadSmartSqlMapAsync(path, SmartSqlMapConfig);
sqlmap.Scope = newSqlmap.Scope;
sqlmap.Statements = newSqlmap.Statements;
Expand Down
99 changes: 60 additions & 39 deletions src/SmartSql/Abstractions/ConfigLoader/ConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public SmartSqlMapConfig LoadConfig(ConfigStream configStream)
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SmartSqlMapConfig));
SqlMapConfig = xmlSerializer.Deserialize(configStream.Stream) as SmartSqlMapConfig;
SqlMapConfig.Path = configStream.Path;
SqlMapConfig.SmartSqlMaps = new List<SmartSqlMap> { };
SqlMapConfig.SmartSqlMaps = new Dictionary<String, SmartSqlMap> { };
if (SqlMapConfig.TypeHandlers != null)
{
foreach (var typeHandler in SqlMapConfig.TypeHandlers)
Expand All @@ -51,11 +51,11 @@ public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream)
{
SqlMapConfig = SqlMapConfig,
Path = configStream.Path,
Statements = new List<Statement> { },
Caches = new List<Configuration.Cache> { },
ResultMaps = new List<ResultMap> { },
MultipleResultMaps = new List<MultipleResultMap> { },
ParameterMaps = new List<ParameterMap> { }
Statements = new Dictionary<String, Statement> { },
Caches = new Dictionary<String, Configuration.Cache> { },
ResultMaps = new Dictionary<String, ResultMap> { },
MultipleResultMaps = new Dictionary<String, MultipleResultMap> { },
ParameterMaps = new Dictionary<String, ParameterMap> { }
};
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configStream.Stream);
Expand All @@ -69,7 +69,7 @@ public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream)
foreach (XmlElement cacheNode in cacheNodes)
{
var cache = CacheFactory.Load(cacheNode, sqlMap);
sqlMap.Caches.Add(cache);
sqlMap.Caches.Add(cache.Id, cache);
}
#endregion

Expand All @@ -78,23 +78,23 @@ public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream)
foreach (XmlElement xmlNode in resultMapsNodes)
{
var resultMap = MapFactory.LoadResultMap(xmlNode, sqlMap, xmlNsM);
sqlMap.ResultMaps.Add(resultMap);
sqlMap.ResultMaps.Add(resultMap.Id, resultMap);
}
#endregion
#region Init MultipleResultMaps
var multipleResultMapsNode = xmlDoc.SelectNodes("//ns:MultipleResultMaps/ns:MultipleResultMap", xmlNsM);
foreach (XmlElement xmlNode in multipleResultMapsNode)
{
var multipleResultMap = MapFactory.LoadMultipleResultMap(xmlNode, sqlMap);
sqlMap.MultipleResultMaps.Add(multipleResultMap);
sqlMap.MultipleResultMaps.Add(multipleResultMap.Id, multipleResultMap);
}
#endregion
#region Init ParameterMaps
var parameterMaps = xmlDoc.SelectNodes("//ns:ParameterMaps/ns:ParameterMap", xmlNsM);
foreach (XmlElement xmlNode in parameterMaps)
{
var parameterMap = MapFactory.LoadParameterMap(xmlNode, sqlMap);
sqlMap.ParameterMaps.Add(parameterMap);
sqlMap.ParameterMaps.Add(parameterMap.Id, parameterMap);
}
#endregion

Expand All @@ -109,50 +109,61 @@ public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream)
private ResultMap GetResultMap(string fullMapId)
{
var scope = fullMapId.Split('.')[0];
var sqlMap = SqlMapConfig.SmartSqlMaps.FirstOrDefault(m => m.Scope == scope);
if (sqlMap == null) { throw new SmartSqlException($"ConfigLoader can not find SqlMap.Scoe:{scope}"); }
var resultMap = sqlMap.ResultMaps.FirstOrDefault(m => m.Id == fullMapId);
if (resultMap == null) { throw new SmartSqlException($"ConfigLoader can not find ResultMap.Id:{fullMapId}"); }
var sqlMap = GetSmartSqlMap(scope);
if (!sqlMap.ResultMaps.TryGetValue(fullMapId, out ResultMap resultMap))
{
throw new SmartSqlException($"ConfigLoader can not find ResultMap.Id:{fullMapId}");
}
return resultMap;
}
private SmartSqlMap GetSmartSqlMap(string scope)
{
return SqlMapConfig.SmartSqlMaps.FirstOrDefault(m => m.Scope == scope);
if (!SqlMapConfig.SmartSqlMaps.TryGetValue(scope, out SmartSqlMap sqlMap))
{
throw new SmartSqlException($"ConfigLoader can not find SqlMap.Scoe:{scope}");
}
return sqlMap;
}
public void InitDependency()
{
#region Init Statement.Include
foreach (var sqlMap in SqlMapConfig.SmartSqlMaps)
{
foreach (var statement in sqlMap.Statements)
foreach (var statementKV in sqlMap.Value.Statements)
{
foreach (var include in statement.IncludeDependencies)
foreach (var include in statementKV.Value.IncludeDependencies)
{
if (include.RefId == include.Statement.FullSqlId)
{
throw new SmartSqlException($"Include.RefId can not be self statement.id:{include.RefId}");
}
var scope = include.RefId.Split('.')[0];
var refStatement = GetSmartSqlMap(scope)?.Statements?.FirstOrDefault(m => m.FullSqlId == include.RefId);
include.Ref = refStatement ?? throw new SmartSqlException($"Include can not find statement.id:{include.RefId}");
var refSqlMap = GetSmartSqlMap(scope);
if (!refSqlMap.Statements.TryGetValue(include.RefId, out Statement refStatement))
{
throw new SmartSqlException($"Include can not find statement.id:{include.RefId}");
}
include.Ref = refStatement;
}
}
}
#endregion
#region Check Statement.Include Cyclic Dependency
foreach (var sqlMap in SqlMapConfig.SmartSqlMaps)
{
foreach (var statement in sqlMap.Statements)
foreach (var statementKV in sqlMap.Value.Statements)
{
CheckIncludeCyclicDependency(statement, statement.IncludeDependencies);
CheckIncludeCyclicDependency(statementKV.Value, statementKV.Value.IncludeDependencies);
}
}
#endregion
foreach (var sqlMap in SqlMapConfig.SmartSqlMaps)
foreach (var sqlMapKV in SqlMapConfig.SmartSqlMaps)
{
var sqlMap = sqlMapKV.Value;
#region Init MultipleResultMaps
foreach (var mResult in sqlMap.MultipleResultMaps)
foreach (var mResultKV in sqlMap.MultipleResultMaps)
{
var mResult = mResultKV.Value;
if (!String.IsNullOrEmpty(mResult.Root?.MapId))
{
mResult.Root.Map = GetResultMap(mResult.Root.MapId);
Expand All @@ -168,47 +179,57 @@ public void InitDependency()
}
#endregion
#region Init Statement Attribute For Cache & ResultMap & ParameterMap & MultipleResultMap
foreach (var statement in sqlMap.Statements)
foreach (var statementKV in sqlMap.Statements)
{
var statement = statementKV.Value;
if (!String.IsNullOrEmpty(statement.CacheId))
{
var scope = statement.CacheId.Split('.')[0];
var cache = GetSmartSqlMap(scope)?
.Caches?.FirstOrDefault(m => m.Id == statement.CacheId);
statement.Cache = cache ?? throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find Cache.Id:{statement.CacheId}");
if (!GetSmartSqlMap(scope).Caches.TryGetValue(statement.CacheId, out Configuration.Cache cache))
{
throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find Cache.Id:{statement.CacheId}");
}
statement.Cache = cache;
}

if (!String.IsNullOrEmpty(statement.ResultMapId))
{
var scope = statement.ResultMapId.Split('.')[0];
var resultMap = GetSmartSqlMap(scope)?
.ResultMaps?.FirstOrDefault(m => m.Id == statement.ResultMapId);
statement.ResultMap = resultMap ?? throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find ResultMap.Id:{statement.ResultMapId}");
if (!GetSmartSqlMap(scope).ResultMaps.TryGetValue(statement.ResultMapId, out ResultMap resultMap))
{
throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find ResultMap.Id:{statement.ResultMapId}");
}
statement.ResultMap = resultMap;
}

if (!String.IsNullOrEmpty(statement.ParameterMapId))
{
var scope = statement.ParameterMapId.Split('.')[0];
var parameterMap = GetSmartSqlMap(scope)?
.ParameterMaps?.FirstOrDefault(m => m.Id == statement.ParameterMapId);
statement.ParameterMap = parameterMap ?? throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find ParameterMap.Id:{statement.ParameterMapId}");
if (!GetSmartSqlMap(scope).ParameterMaps.TryGetValue(statement.ParameterMapId, out ParameterMap parameterMap))
{
throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find ParameterMap.Id:{statement.ParameterMapId}");
}
statement.ParameterMap = parameterMap;
}

if (!String.IsNullOrEmpty(statement.MultipleResultMapId))
{
var scope = statement.MultipleResultMapId.Split('.')[0];
var multipleResultMap = GetSmartSqlMap(scope)?
.MultipleResultMaps?.FirstOrDefault(m => m.Id == statement.MultipleResultMapId);
statement.MultipleResultMap = multipleResultMap ?? throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find MultipleResultMap.Id:{statement.MultipleResultMapId}");
if (!GetSmartSqlMap(scope).MultipleResultMaps.TryGetValue(statement.MultipleResultMapId, out MultipleResultMap multipleResultMap))
{
throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find MultipleResultMap.Id:{statement.MultipleResultMapId}");
}
statement.MultipleResultMap = multipleResultMap;
}
}
#endregion
}
#region Init Statement.SqlCommandType
foreach (var sqlMap in SqlMapConfig.SmartSqlMaps)
foreach (var sqlMapKV in SqlMapConfig.SmartSqlMaps)
{
foreach (var statement in sqlMap.Statements)
foreach (var statementKV in sqlMapKV.Value.Statements)
{
var statement = statementKV.Value;
var fullSqlTextBuilder = new StringBuilder();
BuildStatementFullSql(statement, fullSqlTextBuilder);
var fullSqlText = fullSqlTextBuilder.ToString();
Expand Down Expand Up @@ -266,7 +287,7 @@ private void LoadStatementInSqlMap(SmartSqlMap sqlMap, XmlNodeList statementNode
foreach (XmlElement statementNode in statementNodes)
{
var statement = _statementFactory.Load(statementNode, sqlMap);
sqlMap.Statements.Add(statement);
sqlMap.Statements.Add(statement.FullSqlId, statement);
}
}
}
Expand Down

0 comments on commit cf004db

Please sign in to comment.