|
3 | 3 |
|
4 | 4 | namespace Stravaig.Configuration.SqlServer; |
5 | 5 |
|
6 | | -public interface IDataLoader |
7 | | -{ |
8 | | - IEnumerable<KeyValuePair<string, string>> RetrieveData(SqlServerConfigurationSource source); |
9 | | -} |
10 | 6 | public class DataLoader : IDataLoader |
11 | 7 | { |
12 | 8 | private const string RetrieveSqlTemplate = "SELECT [ConfigKey], [ConfigValue] FROM [{0}].[{1}]"; |
13 | 9 | private const int KeyColumnPosition = 0; |
14 | 10 | private const int ValueColumnPosition = 1; |
15 | 11 |
|
16 | | - public IEnumerable<KeyValuePair<string, string>> RetrieveData(SqlServerConfigurationSource source) |
| 12 | + public IDictionary<string, string> RetrieveData(SqlServerConfigurationSource source) |
17 | 13 | { |
18 | 14 | var sql = RetrieveSql(source.SchemaName, source.TableName); |
19 | 15 | using SqlConnection connection = new SqlConnection(source.ConnectionString); |
20 | 16 | connection.Open(); |
21 | 17 | var cmd = new SqlCommand(sql, connection); |
22 | 18 | using var reader = cmd.ExecuteReader(); |
| 19 | + return MaterialiseData(reader); |
| 20 | + } |
| 21 | + |
| 22 | + private static IDictionary<string, string> MaterialiseData(SqlDataReader reader) |
| 23 | + { |
| 24 | + Dictionary<string, string> results = new (); |
23 | 25 | while (reader.Read()) |
24 | 26 | { |
25 | 27 | var key = reader.GetString(KeyColumnPosition); |
26 | 28 | var value = reader.GetString(ValueColumnPosition); |
27 | | - yield return new KeyValuePair<string, string>(key, value); |
| 29 | + results.Add(key,value); |
28 | 30 | } |
| 31 | + |
| 32 | + return results; |
29 | 33 | } |
30 | 34 |
|
31 | 35 | private string RetrieveSql(string schemaName, string tableName) => |
|
0 commit comments