Skip to content

Commit 99decc4

Browse files
committed
String homogenization improved.
1 parent eef48a7 commit 99decc4

23 files changed

+523
-50
lines changed

Simple.Data.Ado/AdoAdapterFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static IEnumerable<IDictionary<string, object>> TryExecuteQuery(IDbComma
6262
{
6363
try
6464
{
65-
return command.ToAsyncEnumerable();
65+
return command.ToBufferedEnumerable();
6666
}
6767
catch (DbException ex)
6868
{

Simple.Data.Ado/DataReaderExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace Simple.Data.Ado
77
{
88
internal static class DataReaderExtensions
99
{
10+
public static IDictionary<string,object> ToDictionary(this IDataReader dataReader)
11+
{
12+
return dataReader.ToDictionary(dataReader.CreateDictionaryIndex());
13+
}
14+
1015
public static IEnumerable<IDictionary<string, object>> ToDictionaries(this IDataReader reader)
1116
{
1217
using (reader)
@@ -34,7 +39,7 @@ private static IEnumerable<IEnumerable<IDictionary<string,object>>> ToMultipleDi
3439

3540
private static IEnumerable<IDictionary<string,object>> ToDictionariesImpl(IDataReader reader)
3641
{
37-
var index = HomogenizedDictionaryIndex.CreateIndex(reader.GetFieldNames());
42+
var index = reader.CreateDictionaryIndex();
3843
var values = new object[reader.FieldCount];
3944
while (reader.Read())
4045
{

Simple.Data.Ado/DataRecordExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static dynamic ToDynamicRecord(this IDataRecord dataRecord)
1212
return ToDynamicRecord(dataRecord, null, null);
1313
}
1414

15-
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, OptimizedDictionaryIndex<string> index)
15+
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, IDictionary<string,int> index)
1616
{
1717
return ToDynamicRecord(dataRecord, index, null, null);
1818
}
@@ -22,7 +22,7 @@ public static dynamic ToDynamicRecord(this IDataRecord dataRecord, string tableN
2222
return new SimpleRecord(dataRecord.ToDictionary(), tableName, database);
2323
}
2424

25-
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, OptimizedDictionaryIndex<string> index, string tableName, Database database)
25+
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, IDictionary<string,int> index, string tableName, Database database)
2626
{
2727
return new SimpleRecord(dataRecord.ToDictionary(index), tableName, database);
2828
}
@@ -32,7 +32,7 @@ public static Dictionary<string, object> ToDictionary(this IDataRecord dataRecor
3232
return dataRecord.GetFieldNames().ToDictionary(fieldName => fieldName.Homogenize(), fieldName => dataRecord[fieldName]);
3333
}
3434

35-
public static IDictionary<string, object> ToDictionary(this IDataRecord dataRecord, OptimizedDictionaryIndex<string> index)
35+
public static IDictionary<string, object> ToDictionary(this IDataRecord dataRecord, IDictionary<string,int> index)
3636
{
3737
return OptimizedDictionary.Create(index,dataRecord.GetValues());
3838
}

Simple.Data.Ado/DbCommandExtensions.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,37 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Text;
8+
using Simple.Data.Extensions;
89

910
namespace Simple.Data.Ado
1011
{
1112
static class DbCommandExtensions
1213
{
14+
public static IEnumerable<IDictionary<string,object>> ToBufferedEnumerable(this IDbCommand command)
15+
{
16+
try
17+
{
18+
command.Connection.Open();
19+
}
20+
catch (DbException ex)
21+
{
22+
throw new AdoAdapterException(ex.Message, ex);
23+
}
24+
var reader = command.ExecuteReaderWithExceptionWrap();
25+
var index = reader.CreateDictionaryIndex();
26+
return BufferedEnumerable.Create(() => reader.Read()
27+
? Maybe.Some(reader.ToDictionary(index))
28+
: Maybe<IDictionary<string, object>>.None,
29+
() => { using (command.Connection)using(command)using(reader) {} });
30+
}
31+
32+
public static Dictionary<string, int> CreateDictionaryIndex(this IDataReader reader)
33+
{
34+
var keys =
35+
reader.GetFieldNames().Select((s, i) => new KeyValuePair<string, int>(s.Homogenize(), i)).ToDictionary();
36+
return new Dictionary<string, int>(keys, HomogenizedEqualityComparer.DefaultInstance);
37+
}
38+
1339
public static IEnumerable<IDictionary<string, object>> ToAsyncEnumerable(this IDbCommand command)
1440
{
1541
if (command.Connection == null) throw new InvalidOperationException("Command has no connection.");
@@ -29,5 +55,19 @@ public static IDbDataParameter AddParameter(this IDbCommand command, string name
2955
command.Parameters.Add(parameter);
3056
return parameter;
3157
}
58+
59+
public static IDataReader ExecuteReaderWithExceptionWrap(this IDbCommand command)
60+
{
61+
try
62+
{
63+
return command.ExecuteReader();
64+
}
65+
catch (DbException ex)
66+
{
67+
throw new AdoAdapterException(ex.Message, command.CommandText,
68+
command.Parameters.Cast<IDbDataParameter>()
69+
.ToDictionary(p => p.ParameterName, p => p.Value));
70+
}
71+
}
3272
}
3373
}

Simple.Data/HomogenizedDictionaryIndex.cs renamed to Simple.Data.Ado/HomogenizedDictionaryIndex.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
53
using Simple.Data.Extensions;
64

7-
namespace Simple.Data
5+
namespace Simple.Data.Ado
86
{
97
public class HomogenizedDictionaryIndex : OptimizedDictionaryIndex<string>
108
{

Simple.Data/HomogenizedKeyDictionary.cs renamed to Simple.Data.Ado/HomogenizedKeyDictionary.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65
using Simple.Data.Extensions;
6+
using StringExtensions = Simple.Data.Extensions.StringExtensions;
77

8-
namespace Simple.Data
8+
namespace Simple.Data.Ado
99
{
1010
/// <summary>
1111
/// Stores data internally for <see cref="SimpleRecord"/>.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33

4-
namespace Simple.Data
4+
namespace Simple.Data.Ado
55
{
66
public static class OptimizedDictionary
77
{
@@ -12,7 +12,7 @@ public static OptimizedDictionaryIndex<T> CreateIndex<T>(IEnumerable<T> keys)
1212
return new OptimizedDictionaryIndex<T>(index);
1313
}
1414

15-
public static OptimizedDictionary<TKey,TValue> Create<TKey,TValue>(OptimizedDictionaryIndex<TKey> index, IEnumerable<TValue> values)
15+
public static OptimizedDictionary<TKey,TValue> Create<TKey,TValue>(IDictionary<TKey,int> index, IEnumerable<TValue> values)
1616
{
1717
return new OptimizedDictionary<TKey, TValue>(index, values);
1818
}

Simple.Data/OptimizedDictionaryIndex.cs renamed to Simple.Data.Ado/OptimizedDictionaryIndex.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace Simple.Data
3+
namespace Simple.Data.Ado
44
{
55
public class OptimizedDictionaryIndex<T>
66
{
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
using System.Collections.Generic;
44
using System.Linq;
55

6-
namespace Simple.Data
6+
namespace Simple.Data.Ado
77
{
88
public class OptimizedDictionary<TKey,TValue> : IDictionary<TKey,TValue>
99
{
10-
private readonly OptimizedDictionaryIndex<TKey> _index;
10+
private readonly IDictionary<TKey,int> _index;
1111
private readonly List<TValue> _values;
1212

13-
public OptimizedDictionary(OptimizedDictionaryIndex<TKey> index, IEnumerable<TValue> values)
13+
public OptimizedDictionary(IDictionary<TKey, int> index, IEnumerable<TValue> values)
1414
{
1515
_index = index;
1616
_values = values.ToList();
@@ -81,7 +81,7 @@ public bool Contains(KeyValuePair<TKey, TValue> item)
8181
/// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException"><paramref name="array"/> is multidimensional.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
8282
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
8383
{
84-
foreach (var key in _index.GetKeys())
84+
foreach (var key in _index.Keys)
8585
{
8686
int index = arrayIndex + _index[key];
8787
array[index] = new KeyValuePair<TKey, TValue>(key, _values[index]);
@@ -165,7 +165,7 @@ public bool Remove(TKey key)
165165
public bool TryGetValue(TKey key, out TValue value)
166166
{
167167
int index;
168-
if (!_index.TryGetIndex(key, out index))
168+
if (!_index.TryGetValue(key, out index))
169169
{
170170
value = default(TValue);
171171
return false;
@@ -210,7 +210,7 @@ public TValue this[TKey key]
210210
/// </returns>
211211
public ICollection<TKey> Keys
212212
{
213-
get { return _index.GetKeys().ToArray(); }
213+
get { return _index.Keys.ToArray(); }
214214
}
215215

216216
/// <summary>

Simple.Data.Ado/Simple.Data.Ado.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@
6868
<Compile Include="DeleteHelper.cs" />
6969
<Compile Include="ExpressionFormatter.cs" />
7070
<Compile Include="FindHelper.cs" />
71+
<Compile Include="HomogenizedDictionaryIndex.cs" />
7172
<Compile Include="ICommandBuilder.cs" />
7273
<Compile Include="IConnectionProvider.cs" />
7374
<Compile Include="IExpressionFormatter.cs" />
7475
<Compile Include="Joiner.cs" />
7576
<Compile Include="JoinType.cs" />
7677
<Compile Include="ObservableDataReader.cs" />
78+
<Compile Include="OptimizedDictionary.cs" />
79+
<Compile Include="OptimizedDictionaryIndex.cs" />
80+
<Compile Include="OptimizedDictionary`2.cs" />
7781
<Compile Include="ProcedureExecutor.cs" />
7882
<Compile Include="Properties\AssemblyInfo.cs" />
7983
<Compile Include="Properties\Settings.Designer.cs">

0 commit comments

Comments
 (0)