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

Commit d9f96d7

Browse files
committed
Add examples of partial select statements
1 parent 9a6fbfe commit d9f96d7

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

src/ServiceStack.OrmLite/Expressions/ReadConnectionExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,22 @@ public static List<T> Select<T>(this IDbConnection dbConn, Func<SqlExpression<T>
9999

100100
/// <summary>
101101
/// Returns results from using an SqlExpression lambda. E.g:
102-
/// <para>db.Select(db.SqlExpression&lt;Person&gt;().Where(x =&gt; x.Age &gt; 40))</para>
102+
/// <para>db.Select(db.From&lt;Person&gt;().Where(x =&gt; x.Age &gt; 40))</para>
103103
/// </summary>
104104
public static List<T> Select<T>(this IDbConnection dbConn, SqlExpression<T> expression)
105105
{
106106
return dbConn.Exec(dbCmd => dbCmd.Select(expression));
107107
}
108108

109+
/// <summary>
110+
/// Returns results from using an SqlExpression lambda. E.g:
111+
/// <para>db.Select(db.From&lt;Person&gt;().Where(x =&gt; x.Age &gt; 40))</para>
112+
/// </summary>
113+
public static List<T> Select<T>(this IDbConnection dbConn, ISqlExpression expression)
114+
{
115+
return dbConn.Exec(dbCmd => dbCmd.SqlList<T>(expression.ToSelectStatement()));
116+
}
117+
109118
/// <summary>
110119
/// Returns a single result from using a LINQ Expression. E.g:
111120
/// <para>db.Single&lt;Person&gt;(x =&gt; x.Age == 42)</para>
@@ -172,7 +181,7 @@ public static long Count<T>(this IDbConnection dbConn, Func<SqlExpression<T>, Sq
172181

173182
/// <summary>
174183
/// Returns the count of rows that match the supplied SqlExpression, E.g:
175-
/// <para>db.Count(db.SqlExpression&lt;Person&gt;().Where(x =&gt; x.Age &lt; 50))</para>
184+
/// <para>db.Count(db.From&lt;Person&gt;().Where(x =&gt; x.Age &lt; 50))</para>
176185
/// </summary>
177186
public static long Count<T>(this IDbConnection dbConn, SqlExpression<T> expression)
178187
{

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using NUnit.Framework;
4+
using ServiceStack.DataAnnotations;
45
using ServiceStack.Text;
56

67
namespace ServiceStack.OrmLite.Tests.Expression
@@ -77,6 +78,65 @@ public void Can_select_on_Dates()
7778
Is.EqualTo(3));
7879
}
7980
}
81+
82+
public class Shipper
83+
{
84+
[AutoIncrement]
85+
public int Id { get; set; }
86+
87+
public string CompanyName { get; set; }
88+
89+
public string Phone { get; set; }
90+
91+
public int ShipperTypeId { get; set; }
92+
}
93+
94+
public class SubsetOfShipper
95+
{
96+
public string Phone { get; set; }
97+
public string CompanyName { get; set; }
98+
}
99+
100+
[Test]
101+
public void Can_select_Partial_SQL_Statements()
102+
{
103+
using (var db = OpenDbConnection())
104+
{
105+
db.DropAndCreateTable<Shipper>();
106+
107+
db.Insert(new Shipper { CompanyName = "Trains R Us", Phone = "555-TRAINS", ShipperTypeId = 1 });
108+
db.Insert(new Shipper { CompanyName = "Planes R Us", Phone = "555-PLANES", ShipperTypeId = 2 });
109+
db.Insert(new Shipper { CompanyName = "We do everything!", Phone = "555-UNICORNS", ShipperTypeId = 2 });
110+
111+
var partialColumns = db.Select<SubsetOfShipper>(
112+
db.From<Shipper>().Where(q => q.ShipperTypeId == 2));
113+
114+
Assert.That(partialColumns.Map(x => x.Phone),
115+
Is.EquivalentTo(new[] { "555-UNICORNS", "555-PLANES" }));
116+
Assert.That(partialColumns.Map(x => x.CompanyName),
117+
Is.EquivalentTo(new[] { "Planes R Us", "We do everything!" }));
118+
119+
120+
var partialDto = db.Select<Shipper>(q =>
121+
q.Select(x => new { x.Phone, x.CompanyName })
122+
.Where(x => x.ShipperTypeId == 2));
123+
124+
Assert.That(partialDto.Map(x => x.Phone),
125+
Is.EquivalentTo(new[] { "555-UNICORNS", "555-PLANES" }));
126+
Assert.That(partialDto.Map(x => x.CompanyName),
127+
Is.EquivalentTo(new[] { "Planes R Us", "We do everything!" }));
128+
129+
130+
partialDto = db.Select<Shipper>(q =>
131+
q.Select("Phone, CompanyName")
132+
.Where(x => x.ShipperTypeId == 2));
133+
134+
Assert.That(partialDto.Map(x => x.Phone),
135+
Is.EquivalentTo(new[] { "555-UNICORNS", "555-PLANES" }));
136+
Assert.That(partialDto.Map(x => x.CompanyName),
137+
Is.EquivalentTo(new[] { "Planes R Us", "We do everything!" }));
138+
}
139+
}
80140
}
81141

82142
public class Submission

tests/ServiceStack.OrmLite.Tests/ShippersExample.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public void Shippers_UseCase()
8383
{
8484
db.Insert(new ShipperType { Name = "Automobiles" });
8585
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(3));
86-
8786
}
8887
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(2));
8988

@@ -95,8 +94,12 @@ public void Shippers_UseCase()
9594

9695
var trainsAreUs = db.SingleFmt<Shipper>("ShipperTypeId = {0}", trainsType.Id);
9796
Assert.That(trainsAreUs.CompanyName, Is.EqualTo("Trains R Us"));
98-
Assert.That(db.SelectFmt<Shipper>("CompanyName = {0} OR Phone = {1}", "Trains R Us", "555-UNICORNS"), Has.Count.EqualTo(2));
99-
Assert.That(db.SelectFmt<Shipper>("ShipperTypeId = {0}", planesType.Id), Has.Count.EqualTo(2));
97+
98+
Assert.That(db.Select<Shipper>(q => q.CompanyName == "Trains R Us" || q.Phone == "555-UNICORNS"), Has.Count.EqualTo(2));
99+
Assert.That(db.SelectFmt<Shipper>("CompanyName = {0} OR Phone = {1}", "Trains R Us", "555-UNICORNS"), Has.Count.EqualTo(2));
100+
101+
Assert.That(db.Select<Shipper>(q => q.ShipperTypeId == planesType.Id), Has.Count.EqualTo(2));
102+
Assert.That(db.SelectFmt<Shipper>("ShipperTypeId = {0}", planesType.Id), Has.Count.EqualTo(2));
100103

101104
//Lets update a record
102105
trainsAreUs.Phone = "666-TRAINS";
@@ -113,7 +116,11 @@ public void Shippers_UseCase()
113116

114117
//Performing custom queries
115118
//Select only a subset from the table
116-
var partialColumns = db.SelectFmt<SubsetOfShipper>(typeof (Shipper), "ShipperTypeId = {0}", planesType.Id);
119+
var partialColumns = db.Select<SubsetOfShipper>(
120+
db.From<Shipper>().Where(q => q.ShipperTypeId == 2));
121+
Assert.That(partialColumns, Has.Count.EqualTo(2));
122+
123+
partialColumns = db.SelectFmt<SubsetOfShipper>(typeof (Shipper), "ShipperTypeId = {0}", planesType.Id);
117124
Assert.That(partialColumns, Has.Count.EqualTo(2));
118125

119126
//Select into another POCO class that matches sql

0 commit comments

Comments
 (0)