diff --git a/src/SmartSql.Test.Unit/ConfigBuilder/XmlConfigLoaderTest.cs b/src/SmartSql.Test.Unit/ConfigBuilder/XmlConfigLoaderTest.cs
index 8059b757..0221b5bb 100644
--- a/src/SmartSql.Test.Unit/ConfigBuilder/XmlConfigLoaderTest.cs
+++ b/src/SmartSql.Test.Unit/ConfigBuilder/XmlConfigLoaderTest.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using SmartSql.ConfigBuilder;
+using SmartSql.Exceptions;
using Xunit;
namespace SmartSql.Test.Unit.ConfigBuilder
@@ -14,5 +15,18 @@ public void Load()
var configLoader = new XmlConfigBuilder(ResourceType.File, "SmartSqlMapConfig.xml");
var config = configLoader.Build();
}
+ [Fact]
+ public void LoadFailTest()
+ {
+ try
+ {
+ var configLoader = new XmlConfigBuilder(ResourceType.File, "SmartSqlMapConfig2.xml");
+ var config = configLoader.Build();
+ }
+ catch (SmartSqlException ex)
+ {
+ Assert.StartsWith( "Read Nodes must have Weight attribute",ex.Message);
+ }
+ }
}
}
diff --git a/src/SmartSql.Test.Unit/SmartSql.Test.Unit.csproj b/src/SmartSql.Test.Unit/SmartSql.Test.Unit.csproj
index e6ad8ed5..4b44f819 100644
--- a/src/SmartSql.Test.Unit/SmartSql.Test.Unit.csproj
+++ b/src/SmartSql.Test.Unit/SmartSql.Test.Unit.csproj
@@ -131,6 +131,9 @@
Always
+
+ Always
+
Always
diff --git a/src/SmartSql.Test.Unit/SmartSqlMapConfig.xml b/src/SmartSql.Test.Unit/SmartSqlMapConfig.xml
index bb0f6cad..80881bff 100644
--- a/src/SmartSql.Test.Unit/SmartSqlMapConfig.xml
+++ b/src/SmartSql.Test.Unit/SmartSqlMapConfig.xml
@@ -53,7 +53,7 @@
-
+
diff --git a/src/SmartSql.Test.Unit/SmartSqlMapConfig2.xml b/src/SmartSql.Test.Unit/SmartSqlMapConfig2.xml
new file mode 100644
index 00000000..558de765
--- /dev/null
+++ b/src/SmartSql.Test.Unit/SmartSqlMapConfig2.xml
@@ -0,0 +1,99 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/SmartSql/ConfigBuilder/XmlConfigBuilder.cs b/src/SmartSql/ConfigBuilder/XmlConfigBuilder.cs
index 605c0fc5..156bffe2 100644
--- a/src/SmartSql/ConfigBuilder/XmlConfigBuilder.cs
+++ b/src/SmartSql/ConfigBuilder/XmlConfigBuilder.cs
@@ -28,7 +28,7 @@ public class XmlConfigBuilder : AbstractConfigBuilder
private readonly IWordsConverterBuilder _wordsConverterBuilder = new WordsConverterBuilder();
private readonly ITokenizerBuilder _tokenizerBuilder = new TokenizerBuilder();
-
+
public XmlConfigBuilder(ResourceType resourceType, string resourcePath, ILoggerFactory loggerFactory = null)
{
_resourceType = resourceType;
@@ -153,12 +153,8 @@ protected override void BuildDatabase()
database.Write.DbProvider = database.DbProvider;
var readDataSourceNodes = databaseNode.SelectNodes($"{CONFIG_PREFIX}:Read", XmlNsManager);
if (readDataSourceNodes == null) return;
- foreach (XmlNode readNode in readDataSourceNodes)
- {
- var readDb = ParseReadDataSource(readNode);
- readDb.DbProvider = database.DbProvider;
- database.Reads.Add(readDb.Name, readDb);
- }
+ var readDataSources = this.ParseReadDataSource(readDataSourceNodes, database.DbProvider);
+ database.Reads.AddRange(readDataSources);
}
private DbProvider ParseDbProvider(XmlNode dbProviderNode)
@@ -226,34 +222,46 @@ private WriteDataSource ParseWriteDataSource(XmlNode writeDataSourceNode)
{
writeDataSource.ConnectionString = connectionString;
}
-
+
return writeDataSource;
}
- private ReadDataSource ParseReadDataSource(XmlNode readDataSourceNode)
+ private IDictionary ParseReadDataSource(XmlNodeList readDataSourceNodes, DbProvider dbProvider)
{
- if (readDataSourceNode == null)
- {
- throw new SmartSqlException("ReadDataSource can not be null.");
- }
- var readDataSource = new ReadDataSource();
- if (readDataSourceNode.Attributes.TryGetValueAsString(nameof(ReadDataSource.Name), out string name, SmartSqlConfig.Properties)
- )
- {
- readDataSource.Name = name;
- }
- if (readDataSourceNode.Attributes.TryGetValueAsString(nameof(ReadDataSource.ConnectionString), out string connectionString, SmartSqlConfig.Properties)
- )
- {
- readDataSource.ConnectionString = connectionString;
- }
- if (readDataSourceNode.Attributes.TryGetValueAsInt32(nameof(ReadDataSource.Weight), out int weight, SmartSqlConfig.Properties)
- )
- {
- readDataSource.Weight = weight;
+ var readDataSources = new Dictionary();
+ var s = new List();
+ foreach (XmlNode readDataSourceNode in readDataSourceNodes)
+ {
+
+ var readDataSource = new ReadDataSource();
+ if (readDataSourceNode.Attributes.TryGetValueAsString(nameof(ReadDataSource.Name), out string name, SmartSqlConfig.Properties) )
+ {
+ readDataSource.Name = name;
+ }
+ else
+ {
+ throw new SmartSqlException($"Read Nodes must have Name attribute");
+ }
+ if (readDataSourceNode.Attributes.TryGetValueAsString(nameof(ReadDataSource.ConnectionString), out string connectionString, SmartSqlConfig.Properties) )
+ {
+ readDataSource.ConnectionString = connectionString;
+ }
+ if (readDataSourceNode.Attributes.TryGetValueAsInt32(nameof(ReadDataSource.Weight), out int weight, SmartSqlConfig.Properties) )
+ {
+ readDataSource.Weight = weight;
+ }
+ else
+ {
+ if (readDataSourceNodes.Count > 1)
+ {
+ throw new SmartSqlException($"Read Nodes must have Weight attribute,Name is {readDataSource.Name}");
+ }
+ }
+ readDataSource.DbProvider = dbProvider;
+ readDataSources.Add(readDataSource.Name, readDataSource);
}
-
- return readDataSource;
+ return readDataSources;
}
+
#endregion
#region 3. TypeHandlers
protected override void BuildTypeHandlers()
@@ -414,7 +422,7 @@ private void BuildAutoConverter(XmlNode autoConverterNode)
var wordsConverter = BuildWordsConverter(wordsConverterNode);
var autoConverter = new AutoConverter.AutoConverter(converterName, tokenizer, wordsConverter);
-
+
SmartSqlConfig.AutoConverters.Add(autoConverter.Name, autoConverter);
if (isDefault)
{
@@ -431,7 +439,7 @@ private ITokenizer BuildTokenizer(XmlNode tokenizerNode)
var properties = ParseProperties(tokenizerNode);
return _tokenizerBuilder.Build(tokenizerName, properties);
}
-
+
private IWordsConverter BuildWordsConverter(XmlNode wordsConverterNode)
{
if (!wordsConverterNode.Attributes.TryGetValueAsString("Name", out String wordsConverterName, SmartSqlConfig.Properties))
@@ -441,10 +449,10 @@ private IWordsConverter BuildWordsConverter(XmlNode wordsConverterNode)
var properties = ParseProperties(wordsConverterNode);
return _wordsConverterBuilder.Build(wordsConverterName, properties);
}
-
+
#endregion
-
-
+
+
private IDictionary ParseProperties(XmlNode parentNode)
{
var parametersNode = parentNode.SelectSingleNode($"{CONFIG_PREFIX}:Properties", XmlNsManager);
diff --git a/src/SmartSql/Utils/DictionaryExtensions.cs b/src/SmartSql/Utils/DictionaryExtensions.cs
index db33da1e..3409930e 100644
--- a/src/SmartSql/Utils/DictionaryExtensions.cs
+++ b/src/SmartSql/Utils/DictionaryExtensions.cs
@@ -39,6 +39,13 @@ public static void EnsureValue(this IDictionary(this IDictionary dic, IDictionary collection)
+ {
+ foreach(var item in collection)
+ {
+ dic.Add(item);
+ }
+ }
}
}
diff --git a/src/SmartSql/Utils/WeightFilter.cs b/src/SmartSql/Utils/WeightFilter.cs
index 94d0e048..ad543ac9 100644
--- a/src/SmartSql/Utils/WeightFilter.cs
+++ b/src/SmartSql/Utils/WeightFilter.cs
@@ -11,6 +11,7 @@ namespace SmartSql.Utils
///
public class WeightFilter
{
+
///
/// 选举权重源
///
@@ -20,7 +21,16 @@ public WeightSource Elect(IEnumerable inWeightSources)
{
var random = new Random((int)Stopwatch.GetTimestamp());
var weightSources = inWeightSources.ToList();
- int totalWeight = weightSources.Sum(source => source.Weight);
+ int totalWeight = 0;
+ if (weightSources.Count ==1)
+ {
+ return weightSources[0];
+ }
+ else
+ {
+ totalWeight=weightSources.Sum(source => source.Weight);
+ }
+
int position = random.Next(1, totalWeight);
return FindByPosition(weightSources, position);
}