Skip to content

Commit 712aab4

Browse files
committed
Initial SQL tests passing
1 parent 4c68c3a commit 712aab4

5 files changed

Lines changed: 47 additions & 7 deletions

File tree

Simple.Data.Ado/AdoAdapterQueryRunner.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,31 @@ public IEnumerable<IDictionary<string, object>> RunQuery(SimpleQuery query,
1818
out IEnumerable<SimpleQueryClauseBase>
1919
unhandledClauses)
2020
{
21+
IEnumerable<IDictionary<string, object>> result;
22+
2123
if (query.Clauses.OfType<WithCountClause>().Any()) return RunQueryWithCount(query, out unhandledClauses);
2224

2325
ICommandBuilder[] commandBuilders = GetQueryCommandBuilders(query, out unhandledClauses);
2426
IDbConnection connection = _adapter.CreateConnection();
2527
if (_adapter.ProviderSupportsCompoundStatements || commandBuilders.Length == 1)
2628
{
27-
return
29+
result =
2830
CommandBuilder.CreateCommand(
2931
_adapter.ProviderHelper.GetCustomProvider<IDbParameterFactory>(_adapter.SchemaProvider),
3032
commandBuilders,
3133
connection).ToEnumerable(_adapter.CreateConnection);
3234
}
33-
return commandBuilders.SelectMany(cb => cb.GetCommand(connection).ToEnumerable(_adapter.CreateConnection));
35+
else
36+
{
37+
result = commandBuilders.SelectMany(cb => cb.GetCommand(connection).ToEnumerable(_adapter.CreateConnection));
38+
}
39+
40+
if (query.Clauses.OfType<WithClause>().Any())
41+
{
42+
result = new EagerLoadingEnumerable(result);
43+
}
44+
45+
return result;
3446
}
3547

3648
public IObservable<IDictionary<string, object>> RunQueryAsObservable(SimpleQuery query,

Simple.Data.Ado/EagerLoadingEnumerable.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ private IEnumerable<IDictionary<string,object>> CreateObjectGraphs()
6464
}
6565
}
6666

67+
IDictionary<string,int> index = null;
6768
foreach (var kvp in load)
6869
{
70+
if (index == null)
71+
{
72+
index = kvp.Key.Keys.Select((k, i) => new KeyValuePair<string, int>(k, i)).ToDictionary(HomogenizedEqualityComparer.DefaultInstance);
73+
}
74+
var row = new OptimizedDictionary<string, object>(index, kvp.Key.Values);
6975
foreach (var sub in kvp.Value)
7076
{
7177
if (sub.Value.Count == 1)

Simple.Data.Ado/OptimizedDictionary2.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OptimizedDictionary<TKey,TValue> : IDictionary<TKey,TValue>, IClone
1414
public OptimizedDictionary(IDictionary<TKey, int> index, IEnumerable<TValue> values)
1515
{
1616
_index = index;
17-
_values = values.ToList();
17+
_values = new List<TValue>(values);
1818
}
1919

2020
/// <summary>
@@ -66,6 +66,10 @@ private void AddKeyToIndex(TKey key)
6666
if (!_index.ContainsKey(key))
6767
{
6868
_index.Add(key, _index.Count);
69+
while (_values.Count < _index.Count)
70+
{
71+
_values.Add(default(TValue));
72+
}
6973
}
7074
}
7175
}
@@ -217,6 +221,10 @@ public TValue this[TKey key]
217221
}
218222
set
219223
{
224+
if (!_index.ContainsKey(key))
225+
{
226+
AddKeyToIndex(key);
227+
}
220228
try
221229
{
222230
_values[_index[key]] = value;
@@ -225,10 +233,6 @@ public TValue this[TKey key]
225233
{
226234
throw new ArgumentNullException("key");
227235
}
228-
catch (KeyNotFoundException)
229-
{
230-
throw new KeyNotFoundException();
231-
}
232236
}
233237
}
234238

Simple.Data.SqlTest/QueryTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Simple.Data.SqlTest
88
{
9+
using System.Collections;
10+
911
[TestFixture]
1012
public class QueryTest
1113
{
@@ -316,5 +318,14 @@ public void QueryWithSchemaQualifiedTableNameAndAliases()
316318
Assert.AreEqual(2, result.This);
317319
Assert.AreEqual("Pass", result.That);
318320
}
321+
322+
[Test]
323+
public void WithClauseShouldPreselectSubTable()
324+
{
325+
var db = DatabaseHelper.Open();
326+
var result = db.Customers.FindAllByCustomerId(1).WithOrders().FirstOrDefault() as IDictionary<string,object>;
327+
Assert.IsNotNull(result);
328+
Assert.Contains("Orders", (ICollection)result.Keys);
329+
}
319330
}
320331
}

Simple.Data/Extensions/EnumerableExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public static IDictionary<TKey,TValue> ToDictionary<TKey,TValue>(this IEnumerabl
1313
return source.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
1414
}
1515

16+
public static IDictionary<TKey,TValue> ToDictionary<TKey,TValue>(this IEnumerable<KeyValuePair<TKey,TValue>> source, IEqualityComparer<TKey> equalityComparer)
17+
{
18+
var dict = source as IDictionary<TKey, TValue>;
19+
if (dict != null) return new Dictionary<TKey, TValue>(dict);
20+
return source.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, equalityComparer);
21+
}
22+
1623
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source)
1724
{
1825
var buffer = default(T);

0 commit comments

Comments
 (0)