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

Commit 370ba31

Browse files
committed
Add support for db.Select<List<object>>() and db.Select<Dictionary<string,object>>() for returning unstructured data
1 parent ee3680c commit 370ba31

File tree

4 files changed

+193
-12
lines changed

4 files changed

+193
-12
lines changed

src/ServiceStack.OrmLite/Async/OrmLiteUtilExtensionsAsync.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,35 @@ public static Task<List<T>> ConvertToListAsync<T>(this IDataReader dataReader, I
3737
{
3838
var fieldDefs = ModelDefinition<T>.Definition.AllFieldDefinitionsArray;
3939
var indexCache = dataReader.GetIndexFieldsCache(ModelDefinition<T>.Definition);
40+
var isObjectList = typeof(T) == typeof(List<object>);
41+
var isObjectDict = typeof(T) == typeof(Dictionary<string,object>);
42+
4043
return dialectProvider.ReaderEach(dataReader, () =>
4144
{
42-
var row = CreateInstance<T>();
43-
row.PopulateWithSqlReader(dialectProvider, dataReader, fieldDefs, indexCache);
44-
return row;
45+
if (isObjectList)
46+
{
47+
var row = new List<object>();
48+
for (var i = 0; i < dataReader.FieldCount; i++)
49+
{
50+
row.Add(dataReader.GetValue(i));
51+
}
52+
return (T)(object)row;
53+
}
54+
else if (isObjectDict)
55+
{
56+
var row = new Dictionary<string,object>();
57+
for (var i = 0; i < dataReader.FieldCount; i++)
58+
{
59+
row[dataReader.GetName(i)] = dataReader.GetValue(i);
60+
}
61+
return (T)(object)row;
62+
}
63+
else
64+
{
65+
var row = CreateInstance<T>();
66+
row.PopulateWithSqlReader(dialectProvider, dataReader, fieldDefs, indexCache);
67+
return row;
68+
}
4569
}, token).Then(t => {
4670
dataReader.Dispose();
4771
return t;

src/ServiceStack.OrmLite/OrmLiteUtils.cs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,55 @@ public static List<T> ConvertToList<T>(this IDataReader dataReader, IOrmLiteDial
5454
{
5555
var fieldDefs = ModelDefinition<T>.Definition.AllFieldDefinitionsArray;
5656

57-
var to = new List<T>();
58-
using (dataReader)
57+
if (typeof(T) == typeof(List<object>))
5958
{
60-
var indexCache = dataReader.GetIndexFieldsCache(ModelDefinition<T>.Definition);
61-
while (dataReader.Read())
59+
var to = new List<List<object>>();
60+
using (dataReader)
6261
{
63-
var row = CreateInstance<T>();
64-
row.PopulateWithSqlReader(dialectProvider, dataReader, fieldDefs, indexCache);
62+
var row = new List<object>();
63+
while (dataReader.Read())
64+
{
65+
for (var i = 0; i < dataReader.FieldCount; i++)
66+
{
67+
row.Add(dataReader.GetValue(i));
68+
}
69+
}
6570
to.Add(row);
6671
}
72+
return (List<T>)(object)to;
73+
}
74+
else if (typeof(T) == typeof(Dictionary<string, object>))
75+
{
76+
var to = new List<Dictionary<string,object>>();
77+
using (dataReader)
78+
{
79+
var row = new Dictionary<string,object>();
80+
while (dataReader.Read())
81+
{
82+
for (var i = 0; i < dataReader.FieldCount; i++)
83+
{
84+
row[dataReader.GetName(i)] = dataReader.GetValue(i);
85+
}
86+
}
87+
to.Add(row);
88+
}
89+
return (List<T>)(object)to;
90+
}
91+
else
92+
{
93+
var to = new List<T>();
94+
using (dataReader)
95+
{
96+
var indexCache = dataReader.GetIndexFieldsCache(ModelDefinition<T>.Definition);
97+
while (dataReader.Read())
98+
{
99+
var row = CreateInstance<T>();
100+
row.PopulateWithSqlReader(dialectProvider, dataReader, fieldDefs, indexCache);
101+
to.Add(row);
102+
}
103+
}
104+
return to;
67105
}
68-
return to;
69106
}
70107

71108
public static object ConvertTo(this IDataReader dataReader, IOrmLiteDialectProvider dialectProvider, Type type)

tests/ServiceStack.OrmLite.Tests/Expression/SqlExpressionTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,58 @@ public void Can_select_ColumnDistinct_with_SqlExpression()
9090
}
9191
}
9292

93+
[Test]
94+
public void Can_Select_as_List_Object()
95+
{
96+
using (var db = OpenDbConnection())
97+
{
98+
InitLetters(db);
99+
100+
var query = db.From<LetterFrequency>()
101+
.Select("COUNT(*), MAX(Id), MIN(Id), Sum(Id)");
102+
103+
query.ToSelectStatement().Print();
104+
105+
var results = db.Select<List<object>>(query);
106+
107+
Assert.That(results.Count, Is.EqualTo(1));
108+
109+
var result = results[0];
110+
Assert.That(result[0], Is.EqualTo(10));
111+
Assert.That(result[1], Is.EqualTo(10));
112+
Assert.That(result[2], Is.EqualTo(1));
113+
Assert.That(result[3], Is.EqualTo(55));
114+
115+
results.PrintDump();
116+
}
117+
}
118+
119+
[Test]
120+
public void Can_Select_as_Dictionary_Object()
121+
{
122+
using (var db = OpenDbConnection())
123+
{
124+
InitLetters(db);
125+
126+
var query = db.From<LetterFrequency>()
127+
.Select("COUNT(*) Count, MAX(Id) Max, MIN(Id) Min, Sum(Id) Sum");
128+
129+
query.ToSelectStatement().Print();
130+
131+
var results = db.Select<Dictionary<string,object>>(query);
132+
133+
Assert.That(results.Count, Is.EqualTo(1));
134+
135+
var result = results[0];
136+
Assert.That(result["Count"], Is.EqualTo(10));
137+
Assert.That(result["Max"], Is.EqualTo(10));
138+
Assert.That(result["Min"], Is.EqualTo(1));
139+
Assert.That(result["Sum"], Is.EqualTo(55));
140+
141+
results.PrintDump();
142+
}
143+
}
144+
93145
[Test]
94146
public void Can_select_limit_with_SqlExpression()
95147
{

tests/ServiceStack.OrmLiteV45.Tests/SqlExpressionTests.cs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using System.Threading.Tasks;
24
using NUnit.Framework;
35
using ServiceStack.OrmLite.Tests.UseCase;
46
using ServiceStack.Text;
@@ -8,6 +10,73 @@ namespace ServiceStack.OrmLite.Tests
810
public class SqlExpressionTests
911
: OrmLiteTestBase
1012
{
13+
public static void InitLetters(IDbConnection db)
14+
{
15+
db.DropAndCreateTable<LetterFrequency>();
16+
17+
db.Insert(new LetterFrequency { Letter = "A" });
18+
db.Insert(new LetterFrequency { Letter = "B" });
19+
db.Insert(new LetterFrequency { Letter = "B" });
20+
db.Insert(new LetterFrequency { Letter = "C" });
21+
db.Insert(new LetterFrequency { Letter = "C" });
22+
db.Insert(new LetterFrequency { Letter = "C" });
23+
db.Insert(new LetterFrequency { Letter = "D" });
24+
db.Insert(new LetterFrequency { Letter = "D" });
25+
db.Insert(new LetterFrequency { Letter = "D" });
26+
db.Insert(new LetterFrequency { Letter = "D" });
27+
}
28+
29+
[Test]
30+
public async Task Can_Select_as_List_Object_Async()
31+
{
32+
using (var db = OpenDbConnection())
33+
{
34+
InitLetters(db);
35+
36+
var query = db.From<LetterFrequency>()
37+
.Select("COUNT(*), MAX(Id), MIN(Id), Sum(Id)");
38+
39+
query.ToSelectStatement().Print();
40+
41+
var results = await db.SelectAsync<List<object>>(query);
42+
43+
Assert.That(results.Count, Is.EqualTo(1));
44+
45+
var result = results[0];
46+
Assert.That(result[0], Is.EqualTo(10));
47+
Assert.That(result[1], Is.EqualTo(10));
48+
Assert.That(result[2], Is.EqualTo(1));
49+
Assert.That(result[3], Is.EqualTo(55));
50+
51+
results.PrintDump();
52+
}
53+
}
54+
55+
[Test]
56+
public async Task Can_Select_as_Dictionary_Object_Async()
57+
{
58+
using (var db = OpenDbConnection())
59+
{
60+
InitLetters(db);
61+
62+
var query = db.From<LetterFrequency>()
63+
.Select("COUNT(*) Count, MAX(Id) Max, MIN(Id) Min, Sum(Id) Sum");
64+
65+
query.ToSelectStatement().Print();
66+
67+
var results = await db.SelectAsync<Dictionary<string, object>>(query);
68+
69+
Assert.That(results.Count, Is.EqualTo(1));
70+
71+
var result = results[0];
72+
Assert.That(result["Count"], Is.EqualTo(10));
73+
Assert.That(result["Max"], Is.EqualTo(10));
74+
Assert.That(result["Min"], Is.EqualTo(1));
75+
Assert.That(result["Sum"], Is.EqualTo(55));
76+
77+
results.PrintDump();
78+
}
79+
}
1180
[Test]
1281
public async Task Can_select_limit_on_Table_with_References_Async()
1382
{
@@ -48,6 +117,5 @@ public async Task Can_select_limit_on_Table_with_References_Async()
48117
Assert.That(results[0].Orders.Count, Is.EqualTo(2));
49118
}
50119
}
51-
52120
}
53121
}

0 commit comments

Comments
 (0)