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

Commit c346a96

Browse files
committed
Add new multiple select tests
1 parent d056272 commit c346a96

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ public virtual SqlExpression<T> Select<TKey>(Expression<Func<T, TKey>> fields)
124124
return this;
125125
}
126126

127+
public virtual SqlExpression<T> Select<Table1, Table2>(Expression<Func<Table1, Table2, object>> fields)
128+
{
129+
sep = string.Empty;
130+
useFieldName = true;
131+
BuildSelectExpression(Visit(fields).ToString(), false);
132+
return this;
133+
}
134+
135+
public virtual SqlExpression<T> Select<Table1, Table2, Table3>(Expression<Func<Table1, Table2, Table3, object>> fields)
136+
{
137+
sep = string.Empty;
138+
useFieldName = true;
139+
BuildSelectExpression(Visit(fields).ToString(), false);
140+
return this;
141+
}
142+
127143
public virtual SqlExpression<T> SelectDistinct<TKey>(Expression<Func<T, TKey>> fields)
128144
{
129145
sep = string.Empty;
@@ -132,6 +148,22 @@ public virtual SqlExpression<T> SelectDistinct<TKey>(Expression<Func<T, TKey>> f
132148
return this;
133149
}
134150

151+
public virtual SqlExpression<T> SelectDistinct<Table1, Table2>(Expression<Func<Table1, Table2, object>> fields)
152+
{
153+
sep = string.Empty;
154+
useFieldName = true;
155+
BuildSelectExpression(Visit(fields).ToString(), true);
156+
return this;
157+
}
158+
159+
public virtual SqlExpression<T> SelectDistinct<Table1, Table2, Table3>(Expression<Func<Table1, Table2, Table3, object>> fields)
160+
{
161+
sep = string.Empty;
162+
useFieldName = true;
163+
BuildSelectExpression(Visit(fields).ToString(), true);
164+
return this;
165+
}
166+
135167
public virtual SqlExpression<T> SelectDistinct()
136168
{
137169
selectDistinct = true;

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Data;
34
using System.Linq;
45
using NUnit.Framework;
@@ -154,13 +155,13 @@ private static void InitTables(IDbConnection db)
154155
db.Insert(new BarJoin { Id = bar1Id, Name = "Banana", });
155156
db.Insert(new BarJoin { Id = bar2Id, Name = "Orange", });
156157

157-
_baz1Id = (int) db.Insert(new Baz {Name = "Large"}, true);
158-
_baz2Id = (int) db.Insert(new Baz {Name = "Huge"}, true);
158+
_baz1Id = (int)db.Insert(new Baz { Name = "Large" }, true);
159+
_baz2Id = (int)db.Insert(new Baz { Name = "Huge" }, true);
159160

160-
_fooBar1Id = (int) db.Insert(new FooBar { BarId = bar1Id, }, true);
161-
_fooBar2Id = (int) db.Insert(new FooBar { BarId = bar2Id, }, true);
161+
_fooBar1Id = (int)db.Insert(new FooBar { BarId = bar1Id, }, true);
162+
_fooBar2Id = (int)db.Insert(new FooBar { BarId = bar2Id, }, true);
162163

163-
_fooBarBaz1Id = (int) db.Insert(new FooBarBaz { Amount = 42, FooBarId = _fooBar1Id, BazId = _baz2Id }, true);
164+
_fooBarBaz1Id = (int)db.Insert(new FooBarBaz { Amount = 42, FooBarId = _fooBar1Id, BazId = _baz2Id }, true);
164165
_fooBarBaz2Id = (int)db.Insert(new FooBarBaz { Amount = 50, FooBarId = _fooBar1Id, BazId = _baz1Id }, true);
165166
_fooBarBaz3Id = (int)db.Insert(new FooBarBaz { Amount = 2, FooBarId = _fooBar2Id, BazId = _baz1Id }, true);
166167
}
@@ -181,7 +182,7 @@ public void Can_query_contains_on_joined_table_column()
181182

182183
q = db.From<FooBar>()
183184
.Join<BarJoin>((dp, p) => dp.BarId == p.Id)
184-
.Where<FooBar, BarJoin>((f,x) => x.Name.Contains("an"));
185+
.Where<FooBar, BarJoin>((f, x) => x.Name.Contains("an"));
185186

186187
results = db.Select<JoinResult>(q);
187188
Assert.That(results.Count, Is.EqualTo(2));
@@ -252,6 +253,31 @@ public void ComplexJoin_with_SqlExpression()
252253
}
253254
}
254255

256+
[Test]
257+
public void Can_select_dictionary_from_multiple_tables()
258+
{
259+
using (var db = OpenDbConnection())
260+
{
261+
InitTables(db);
262+
263+
var q = db.From<FooBar>()
264+
.Join<BarJoin>()
265+
.Select<FooBar, BarJoin>((f, b) => new { f.Id, b.Name });
266+
267+
var results = db.Dictionary<int, string>(q);
268+
269+
var sql = db.GetLastSql();
270+
sql.Print();
271+
272+
results.PrintDump();
273+
274+
Assert.That(results, Is.EquivalentTo(new Dictionary<int, string> {
275+
{1,"Banana"},
276+
{2,"Orange"},
277+
}));
278+
}
279+
}
280+
255281
[Test]
256282
public void Can_limit_ComplexJoin_query()
257283
{

0 commit comments

Comments
 (0)