Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b4e7bca

Browse files
committed
Add new db.KeyValuePairs/KeyValuePairsAsync APIs
1 parent f7ce670 commit b4e7bca

File tree

8 files changed

+220
-62
lines changed

8 files changed

+220
-62
lines changed

src/ServiceStack.OrmLite/Async/OrmLiteReadCommandExtensionsAsync.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,26 @@ internal static Task<Dictionary<K, V>> DictionaryAsync<K, V>(this IDataReader re
305305
}, map, token);
306306
}
307307

308+
internal static Task<List<KeyValuePair<K, V>>> KeyValuePairsAsync<K, V>(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
309+
{
310+
if (anonType != null)
311+
dbCmd.SetParameters(anonType.ToObjectDictionary(), excludeDefaults: false, sql:ref sql);
312+
313+
return dbCmd.KeyValuePairsAsync<K, V>(sql, token);
314+
}
315+
316+
internal static Task<List<KeyValuePair<K, V>>> KeyValuePairsAsync<K, V>(this IDataReader reader, IOrmLiteDialectProvider dialectProvider, CancellationToken token)
317+
{
318+
var to = new List<KeyValuePair<K, V>>();
319+
320+
return dialectProvider.ReaderEach(reader, () =>
321+
{
322+
var key = (K)dialectProvider.FromDbValue(reader, 0, typeof(K));
323+
var value = (V)dialectProvider.FromDbValue(reader, 1, typeof(V));
324+
to.Add(new KeyValuePair<K, V>(key, value));
325+
}, to, token);
326+
}
327+
308328
internal static async Task<bool> ExistsAsync<T>(this IDbCommand dbCmd, object anonType, CancellationToken token)
309329
{
310330
string sql = null;

src/ServiceStack.OrmLite/Async/OrmLiteResultsFilterExtensionsAsync.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ internal static async Task<Dictionary<K, V>> DictionaryAsync<K, V>(this IDbComma
286286
}
287287
}
288288

289+
internal static Task<List<KeyValuePair<K, V>>> KeyValuePairsAsync<K, V>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
290+
{
291+
return dbCmd.SetParameters(sqlParams).KeyValuePairsAsync<K, V>(sql, token);
292+
}
293+
294+
internal static async Task<List<KeyValuePair<K, V>>> KeyValuePairsAsync<K, V>(this IDbCommand dbCmd, string sql, CancellationToken token)
295+
{
296+
if (sql != null)
297+
dbCmd.CommandText = sql;
298+
299+
if (OrmLiteConfig.ResultsFilter != null)
300+
return OrmLiteConfig.ResultsFilter.GetKeyValuePairs<K, V>(dbCmd);
301+
302+
var dialectProvider = dbCmd.GetDialectProvider();
303+
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
304+
{
305+
return await reader.KeyValuePairsAsync<K, V>(dialectProvider, token);
306+
}
307+
}
308+
289309
internal static Task<Dictionary<K, List<V>>> LookupAsync<K, V>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
290310
{
291311
return dbCmd.SetParameters(sqlParams).LookupAsync<K, V>(sql, token);

src/ServiceStack.OrmLite/OrmLiteReadApi.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,24 @@ public static Dictionary<K, V> Dictionary<K, V>(this IDbConnection dbConn, strin
355355
return dbConn.Exec(dbCmd => dbCmd.Dictionary<K, V>(sql, anonType));
356356
}
357357

358+
/// <summary>
359+
/// Returns a list KeyValuePairs from the first 2 columns: Column 1 (Keys), Column 2 (Values) using an SqlExpression. E.g:
360+
/// <para>db.KeyValuePairs&lt;int, string&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, x.LastName }).Where(x => x.Age < 50))</para>
361+
/// </summary>
362+
public static List<KeyValuePair<K, V>> KeyValuePairs<K, V>(this IDbConnection dbConn, ISqlExpression query)
363+
{
364+
return dbConn.Exec(dbCmd => dbCmd.KeyValuePairs<K, V>(query));
365+
}
366+
367+
/// <summary>
368+
/// Returns a list of KeyValuePairs from the first 2 columns: Column 1 (Keys), Column 2 (Values) using sql. E.g:
369+
/// <para>db.KeyValuePairs&lt;int, string&gt;("SELECT Id, LastName FROM Person WHERE Age &lt; @age", new { age = 50 })</para>
370+
/// </summary>
371+
public static List<KeyValuePair<K, V>> KeyValuePairs<K, V>(this IDbConnection dbConn, string sql, object anonType = null)
372+
{
373+
return dbConn.Exec(dbCmd => dbCmd.KeyValuePairs<K, V>(sql, anonType));
374+
}
375+
358376
/// <summary>
359377
/// Returns true if the Query returns any records that match the LINQ expression, E.g:
360378
/// <para>db.Exists&lt;Person&gt;(x =&gt; x.Age &lt; 50)</para>

src/ServiceStack.OrmLite/OrmLiteReadApiAsync.cs

Lines changed: 84 additions & 56 deletions
Large diffs are not rendered by default.

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,28 @@ internal static Dictionary<K, V> Dictionary<K, V>(this IDataReader reader, IOrmL
837837
return map;
838838
}
839839

840+
internal static List<KeyValuePair<K, V>> KeyValuePairs<K, V>(this IDbCommand dbCmd, string sql, object anonType = null)
841+
{
842+
if (anonType != null) SetParameters(dbCmd, anonType.ToObjectDictionary(), excludeDefaults: false, sql:ref sql);
843+
844+
return dbCmd.KeyValuePairs<K, V>(sql);
845+
}
846+
847+
internal static List<KeyValuePair<K, V>> KeyValuePairs<K, V>(this IDataReader reader, IOrmLiteDialectProvider dialectProvider)
848+
{
849+
var to = new List<KeyValuePair<K, V>>();
850+
851+
while (reader.Read())
852+
{
853+
var key = (K)dialectProvider.FromDbValue(reader, 0, typeof(K));
854+
var value = (V)dialectProvider.FromDbValue(reader, 1, typeof(V));
855+
856+
to.Add(new KeyValuePair<K,V>(key, value));
857+
}
858+
859+
return to;
860+
}
861+
840862
internal static bool Exists<T>(this IDbCommand dbCmd, object anonType)
841863
{
842864
string sql = null;

src/ServiceStack.OrmLite/OrmLiteResultsFilter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public interface IOrmLiteResultsFilter
3333
HashSet<T> GetColumnDistinct<T>(IDbCommand dbCmd);
3434

3535
Dictionary<K, V> GetDictionary<K, V>(IDbCommand dbCmd);
36+
37+
List<KeyValuePair<K, V>> GetKeyValuePairs<K, V>(IDbCommand dbCmd);
3638

3739
Dictionary<K, List<V>> GetLookup<K, V>(IDbCommand dbCmd);
3840

@@ -272,6 +274,8 @@ public Dictionary<K, V> GetDictionary<K, V>(IDbCommand dbCmd)
272274
return to;
273275
}
274276

277+
public List<KeyValuePair<K, V>> GetKeyValuePairs<K, V>(IDbCommand dbCmd) => GetDictionary<K, V>(dbCmd).ToList();
278+
275279
public Dictionary<K, List<V>> GetLookup<K, V>(IDbCommand dbCmd)
276280
{
277281
Filter(dbCmd);
@@ -284,8 +288,7 @@ public Dictionary<K, List<V>> GetLookup<K, V>(IDbCommand dbCmd)
284288
{
285289
var key = (K)entry.Key;
286290

287-
List<V> list;
288-
if (!to.TryGetValue(key, out list))
291+
if (!to.TryGetValue(key, out var list))
289292
{
290293
to[key] = list = new List<V>();
291294
}

src/ServiceStack.OrmLite/OrmLiteResultsFilterExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,33 @@ internal static Dictionary<K, V> Dictionary<K, V>(this IDbCommand dbCmd, ISqlExp
322322
}
323323
}
324324

325+
internal static List<KeyValuePair<K, V>> KeyValuePairs<K, V>(this IDbCommand dbCmd, string sql = null)
326+
{
327+
if (sql != null)
328+
dbCmd.CommandText = sql;
329+
330+
if (OrmLiteConfig.ResultsFilter != null)
331+
return OrmLiteConfig.ResultsFilter.GetKeyValuePairs<K, V>(dbCmd);
332+
333+
using (var reader = dbCmd.ExecReader(dbCmd.CommandText))
334+
{
335+
return reader.KeyValuePairs<K, V>(dbCmd.GetDialectProvider());
336+
}
337+
}
338+
339+
internal static List<KeyValuePair<K, V>> KeyValuePairs<K, V>(this IDbCommand dbCmd, ISqlExpression expression)
340+
{
341+
dbCmd.PopulateWith(expression);
342+
343+
if (OrmLiteConfig.ResultsFilter != null)
344+
return OrmLiteConfig.ResultsFilter.GetKeyValuePairs<K,V>(dbCmd);
345+
346+
using (var reader = dbCmd.ExecReader(dbCmd.CommandText))
347+
{
348+
return reader.KeyValuePairs<K, V>(dbCmd.GetDialectProvider());
349+
}
350+
}
351+
325352
internal static Dictionary<K, List<V>> Lookup<K, V>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams)
326353
{
327354
return dbCmd.SetParameters(sqlParams).Lookup<K, V>(sql);

tests/ServiceStack.OrmLite.Tests/Issues/LRAIssues.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Linq;
5+
using System.Net;
56
using System.Threading.Tasks;
67
using NUnit.Framework;
78
using ServiceStack.DataAnnotations;
@@ -134,8 +135,7 @@ public void Does_InsertIntoSelect_LRARichiesta()
134135
{
135136
using (var db = OpenDbConnection())
136137
{
137-
db.DropAndCreateTable<LRARichiesta>();
138-
db.DropAndCreateTable<LRARisultato>();
138+
RecreateLRARichiesta(db);
139139

140140
long numeroRichieste = db.Count<LRARichiesta>();
141141

@@ -158,13 +158,33 @@ public void Does_InsertIntoSelect_LRARichiesta()
158158
}
159159
}
160160

161+
private static void RecreateLRARichiesta(IDbConnection db)
162+
{
163+
db.DropTable<LRAAnalisi>();
164+
db.DropTable<LRAContenitore>();
165+
db.DropTable<LRARichiesta>();
166+
db.DropTable<LRARisultato>();
167+
db.DropTable<LRDLaboratorio>();
168+
db.DropTable<LRAPaziente>();
169+
db.DropTable<LRDReparto>();
170+
db.DropTable<LRDPriorita>();
171+
172+
db.CreateTable<LRDLaboratorio>();
173+
db.CreateTable<LRAPaziente>();
174+
db.CreateTable<LRDReparto>();
175+
db.CreateTable<LRDPriorita>();
176+
db.CreateTable<LRARisultato>();
177+
db.CreateTable<LRARichiesta>();
178+
db.CreateTable<LRAContenitore>();
179+
db.CreateTable<LRAAnalisi>();
180+
}
181+
161182
[Test]
162183
public async Task Does_InsertIntoSelect_LRARichiesta_Async()
163184
{
164185
using (var db = OpenDbConnection())
165186
{
166-
db.DropAndCreateTable<LRARichiesta>();
167-
db.DropAndCreateTable<LRARisultato>();
187+
RecreateLRARichiesta(db);
168188

169189
long numeroRichieste = await db.CountAsync<LRARichiesta>();
170190

0 commit comments

Comments
 (0)