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

Commit cb75369

Browse files
committed
Added SqlList and SqlScalar to exec custom SQL as-is
1 parent f756b06 commit cb75369

File tree

3 files changed

+215
-29
lines changed

3 files changed

+215
-29
lines changed

src/ServiceStack.OrmLite/OrmLiteReadConnectionExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,35 @@ public static T QueryScalar<T>(this IDbConnection dbConn, string sql, object ano
188188
{
189189
return dbConn.Exec(dbCmd => dbCmd.QueryScalar<T>(sql, anonType));
190190
}
191+
192+
public static List<T> SqlList<T>(this IDbConnection dbConn, string sql)
193+
where T : new()
194+
{
195+
return dbConn.Exec(dbCmd => dbCmd.SqlList<T>(sql));
196+
}
197+
198+
public static List<T> SqlList<T>(this IDbConnection dbConn, string sql, object anonType)
199+
where T : new()
200+
{
201+
return dbConn.Exec(dbCmd => dbCmd.SqlList<T>(sql, anonType));
202+
}
203+
204+
public static List<T> SqlList<T>(this IDbConnection dbConn, string sql, Dictionary<string, object> dict)
205+
where T : new()
206+
{
207+
return dbConn.Exec(dbCmd => dbCmd.SqlList<T>(sql, dict));
208+
}
191209

210+
public static T SqlScalar<T>(this IDbConnection dbConn, string sql, object anonType = null)
211+
{
212+
return dbConn.Exec(dbCmd => dbCmd.SqlScalar<T>(sql, anonType));
213+
}
214+
215+
public static T SqlScalar<T>(this IDbConnection dbConn, string sql, Dictionary<string, object> dict)
216+
{
217+
return dbConn.Exec(dbCmd => dbCmd.SqlScalar<T>(sql, dict));
218+
}
219+
192220
public static List<T> ByExampleWhere<T>(this IDbConnection dbConn, object anonType)
193221
where T : new()
194222
{

src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public static List<T> Where<T>(this IDbCommand dbCmd, object anonType)
387387
}
388388

389389
[Obsolete(UseDbConnectionExtensions)]
390-
public static List<T> Query<T>(this IDbCommand dbCmd, string sql, object anonType)
390+
public static List<T> Query<T>(this IDbCommand dbCmd, string sql, object anonType = null)
391391
where T : new()
392392
{
393393
if (anonType != null) dbCmd.SetParameters(anonType, true);
@@ -400,7 +400,7 @@ public static List<T> Query<T>(this IDbCommand dbCmd, string sql, object anonTyp
400400
}
401401

402402
[Obsolete(UseDbConnectionExtensions)]
403-
public static List<T> Query<T>(this IDbCommand dbCmd, string sql, Dictionary<string, object> dict = null)
403+
public static List<T> Query<T>(this IDbCommand dbCmd, string sql, Dictionary<string, object> dict)
404404
where T : new()
405405
{
406406
if (dict != null) dbCmd.SetParameters(dict, true);
@@ -431,6 +431,48 @@ public static T QueryScalar<T>(this IDbCommand dbCmd, string sql, object anonTyp
431431
return GetScalar<T>(dbReader);
432432
}
433433

434+
internal static List<T> SqlList<T>(this IDbCommand dbCmd, string sql, object anonType = null)
435+
where T : new()
436+
{
437+
if (anonType != null) dbCmd.SetParameters(anonType, true);
438+
dbCmd.CommandText = sql;
439+
440+
using (var dbReader = dbCmd.ExecuteReader())
441+
return typeof(T).IsValueType
442+
? dbReader.GetFirstColumn<T>()
443+
: dbReader.ConvertToList<T>();
444+
}
445+
446+
internal static List<T> SqlList<T>(this IDbCommand dbCmd, string sql, Dictionary<string, object> dict)
447+
where T : new()
448+
{
449+
if (dict != null) dbCmd.SetParameters(dict, true);
450+
dbCmd.CommandText = sql;
451+
452+
using (var dbReader = dbCmd.ExecuteReader())
453+
return typeof(T).IsValueType
454+
? dbReader.GetFirstColumn<T>()
455+
: dbReader.ConvertToList<T>();
456+
}
457+
458+
internal static T SqlScalar<T>(this IDbCommand dbCmd, string sql, object anonType = null)
459+
{
460+
if (anonType != null) dbCmd.SetParameters(anonType, true);
461+
dbCmd.CommandText = sql;
462+
463+
using (var dbReader = dbCmd.ExecuteReader())
464+
return GetScalar<T>(dbReader);
465+
}
466+
467+
internal static T SqlScalar<T>(this IDbCommand dbCmd, string sql, Dictionary<string, object> dict)
468+
{
469+
if (dict != null) dbCmd.SetParameters(dict, true);
470+
dbCmd.CommandText = sql;
471+
472+
using (var dbReader = dbCmd.ExecuteReader())
473+
return GetScalar<T>(dbReader);
474+
}
475+
434476
[Obsolete(UseDbConnectionExtensions)]
435477
public static List<T> ByExampleWhere<T>(this IDbCommand dbCmd, object anonType)
436478
where T : new()
Lines changed: 143 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,149 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
25
using NUnit.Framework;
3-
using ServiceStack.OrmLite.SqlServer;
6+
using ServiceStack.Common;
7+
using ServiceStack.Text;
48

59
namespace ServiceStack.OrmLite.Tests
610
{
7-
[TestFixture]
8-
public class SqlServerProviderTests
9-
{
10-
[TestFixtureSetUp]
11-
public void TestFixtureSetUp()
12-
{
13-
OrmLiteConfig.DialectProvider = SqlServerOrmLiteDialectProvider.Instance;
14-
}
15-
16-
public class OrderDataSubset
17-
{
18-
public DateTime CreatedDate { get; set; }
19-
}
20-
21-
[Ignore("Integration test")][Test]
22-
public void Can_do_SqlServer_DateTime_fields()
23-
{
24-
var connString = "Data Source=chi-prod-analytics-db;Initial Catalog=2010-04_MonthlySnapshot;User Id=admin;Password=xxx;";
25-
26-
using (var db = connString.OpenDbConnection())
27-
{
28-
var order = db.First<OrderDataSubset>("SELECT TOP 1 CreatedDate FROM OrderData");
29-
Console.WriteLine(order);
30-
}
31-
}
32-
}
11+
public class DummyTable
12+
{
13+
public int Id { get; set; }
14+
public string Name { get; set; }
15+
}
16+
17+
[TestFixture]
18+
public class SqlServerProviderTests
19+
{
20+
private IDbConnection db;
21+
22+
[TestFixtureSetUp]
23+
public void TestFixtureSetUp()
24+
{
25+
db = Config.OpenDbConnection();
26+
}
27+
28+
[TestFixtureTearDown]
29+
public void TearDown()
30+
{
31+
db.Dispose();
32+
}
33+
34+
[Test]
35+
public void Can_SqlList_StoredProc_returning_Table()
36+
{
37+
var sql = @"CREATE PROCEDURE dbo.DummyTable
38+
@Times integer
39+
AS
40+
BEGIN
41+
SET NOCOUNT ON;
42+
43+
CREATE TABLE #Temp
44+
(
45+
Id integer NOT NULL,
46+
Name nvarchar(50) COLLATE DATABASE_DEFAULT NOT NULL
47+
);
48+
49+
declare @i int
50+
set @i=1
51+
WHILE @i < @Times
52+
BEGIN
53+
INSERT INTO #Temp (Id, Name) VALUES (@i, CAST(@i as nvarchar))
54+
SET @i = @i + 1
55+
END
56+
57+
SELECT * FROM #Temp;
58+
59+
DROP TABLE #Temp;
60+
END;";
61+
db.ExecuteSql("IF OBJECT_ID('DummyTable') IS NOT NULL DROP PROC DummyTable");
62+
db.ExecuteSql(sql);
63+
64+
var expected = 0;
65+
10.Times(i => expected += i);
66+
67+
var results = db.SqlList<DummyTable>("EXEC DummyTable @Times", new { Times = 10 });
68+
results.PrintDump();
69+
Assert.That(results.Sum(x => x.Id), Is.EqualTo(expected));
70+
71+
results = db.SqlList<DummyTable>("EXEC DummyTable 10");
72+
Assert.That(results.Sum(x => x.Id), Is.EqualTo(expected));
73+
74+
results = db.SqlList<DummyTable>("EXEC DummyTable @Times", new Dictionary<string, object> { {"Times", 10}});
75+
Assert.That(results.Sum(x => x.Id), Is.EqualTo(expected));
76+
}
77+
78+
[Test]
79+
public void Can_SqlList_StoredProc_returning_Column()
80+
{
81+
var sql = @"CREATE PROCEDURE dbo.DummyColumn
82+
@Times integer
83+
AS
84+
BEGIN
85+
SET NOCOUNT ON;
86+
87+
CREATE TABLE #Temp
88+
(
89+
Id integer NOT NULL,
90+
);
91+
92+
declare @i int
93+
set @i=1
94+
WHILE @i < @Times
95+
BEGIN
96+
INSERT INTO #Temp (Id) VALUES (@i)
97+
SET @i = @i + 1
98+
END
99+
100+
SELECT * FROM #Temp;
101+
102+
DROP TABLE #Temp;
103+
END;";
104+
db.ExecuteSql("IF OBJECT_ID('DummyColumn') IS NOT NULL DROP PROC DummyColumn");
105+
db.ExecuteSql(sql);
106+
107+
var expected = 0;
108+
10.Times(i => expected += i);
109+
110+
var results = db.SqlList<int>("EXEC DummyColumn @Times", new { Times = 10 });
111+
results.PrintDump();
112+
Assert.That(results.Sum(), Is.EqualTo(expected));
113+
114+
results = db.SqlList<int>("EXEC DummyColumn 10");
115+
Assert.That(results.Sum(), Is.EqualTo(expected));
116+
117+
results = db.SqlList<int>("EXEC DummyTable @Times", new Dictionary<string, object> { { "Times", 10 } });
118+
Assert.That(results.Sum(), Is.EqualTo(expected));
119+
}
120+
121+
[Test]
122+
public void Can_SqlList_StoredProc_returning_Scalar()
123+
{
124+
var sql = @"CREATE PROCEDURE dbo.DummyScalar
125+
@Times integer
126+
AS
127+
BEGIN
128+
SET NOCOUNT ON;
129+
130+
SELECT @Times AS Id
131+
END;";
132+
db.ExecuteSql("IF OBJECT_ID('DummyScalar') IS NOT NULL DROP PROC DummyScalar");
133+
db.ExecuteSql(sql);
134+
135+
const int expected = 10;
136+
137+
var result = db.SqlScalar<int>("EXEC DummyScalar @Times", new { Times = 10 });
138+
result.PrintDump();
139+
Assert.That(result, Is.EqualTo(expected));
140+
141+
result = db.SqlScalar<int>("EXEC DummyScalar 10");
142+
Assert.That(result, Is.EqualTo(expected));
143+
144+
result = db.SqlScalar<int>("EXEC DummyScalar @Times", new Dictionary<string, object> { { "Times", 10 } });
145+
Assert.That(result, Is.EqualTo(expected));
146+
}
147+
148+
}
33149
}

0 commit comments

Comments
 (0)