1+ using System ;
2+ using System . Data ;
3+ using System . Linq ;
4+ using NUnit . Framework ;
5+ using ServiceStack . DataAnnotations ;
6+ using ServiceStack . Model ;
7+ using ServiceStack . Text ;
8+
9+ namespace ServiceStack . OrmLite . SqlServerTests . UseCase
10+ {
11+ public class Bar : IHasGuidId
12+ {
13+ [ PrimaryKey ]
14+ [ Alias ( "BarId" ) ]
15+ public Guid Id { get ; set ; }
16+
17+ [ Required ]
18+ public string Name { get ; set ; }
19+ }
20+
21+ public class Foo : IHasIntId
22+ {
23+ [ AutoIncrement ]
24+ [ PrimaryKey ]
25+ public int Id { get ; set ; }
26+
27+ [ Alias ( "FKBarId" ) ]
28+ [ ForeignKey ( typeof ( Bar ) , ForeignKeyName = "fk_Foo_Bar" ) ]
29+ public Guid BarId { get ; set ; }
30+ }
31+
32+ internal class FooBarJoin
33+ {
34+ [ BelongTo ( typeof ( Foo ) ) ]
35+ public int Id { get ; set ; }
36+
37+ [ BelongTo ( typeof ( Bar ) ) ]
38+ public Guid BarId { get ; set ; }
39+
40+ [ BelongTo ( typeof ( Bar ) ) ]
41+ public string Name { get ; set ; }
42+ }
43+
44+ [ TestFixture ]
45+ public class ComplexJoinWithLimitAndNoOrderByTests : OrmLiteTestBase
46+ {
47+ private static int _foo1Id ;
48+ private static int _foo2Id ;
49+ private static int _foo3Id ;
50+ private static Guid _bar1Id ;
51+ private static Guid _bar2Id ;
52+ private static Guid _bar3Id ;
53+
54+ private static void InitTables ( IDbConnection db )
55+ {
56+ db . DropTable < Foo > ( ) ;
57+ db . DropTable < Bar > ( ) ;
58+
59+ db . CreateTable < Bar > ( ) ;
60+ db . CreateTable < Foo > ( ) ;
61+
62+ _bar1Id = new Guid ( "5bd67b84-bfdb-4057-9799-5e7a72a6eaa9" ) ;
63+ _bar2Id = new Guid ( "a8061d08-6816-4e1e-b3d7-1178abcefa0d" ) ;
64+ _bar3Id = new Guid ( "84BF769D-5BA9-4506-A7D2-5030E5595EDC" ) ;
65+
66+ db . Insert ( new Bar { Id = _bar1Id , Name = "Banana" , } ) ;
67+ db . Insert ( new Bar { Id = _bar2Id , Name = "Orange" , } ) ;
68+ db . Insert ( new Bar { Id = _bar3Id , Name = "Apple" , } ) ;
69+
70+ _foo1Id = ( int ) db . Insert ( new Foo { BarId = _bar1Id , } , true ) ;
71+ _foo2Id = ( int ) db . Insert ( new Foo { BarId = _bar2Id , } , true ) ;
72+ _foo3Id = ( int ) db . Insert ( new Foo { BarId = _bar3Id , } , true ) ;
73+ }
74+
75+ [ Test ]
76+ public void ComplexJoin_with_JoinSqlBuilder_and_limit_and_no_orderby ( )
77+ {
78+ using ( var db = OpenDbConnection ( ) )
79+ {
80+ InitTables ( db ) ;
81+
82+ //JoinSqlBuilder is obsolete
83+ //var jn = new JoinSqlBuilder<FooBarJoin, Foo>()
84+ // .Join<Foo, Bar>(dp => dp.BarId, p => p.Id)
85+ // //.OrderBy<Foo>(f => f.Id) // Test fails without an explicity OrderBy because auto-generated OrderBy uses join table (FooBarJoin) name
86+ // .Limit(1, 2);
87+
88+ var jn = db . From < Foo > ( )
89+ . Join < Foo , Bar > ( ( f , b ) => f . BarId == b . Id )
90+ . Limit ( 1 , 2 ) ;
91+
92+ var results = db . Select < FooBarJoin > ( jn ) ;
93+ db . GetLastSql ( ) . Print ( ) ;
94+
95+ results . PrintDump ( ) ;
96+
97+ var fooBarJoin = results . FirstOrDefault ( x => x . BarId == _bar1Id ) ;
98+ Assert . IsNull ( fooBarJoin ) ;
99+ fooBarJoin = results . First ( x => x . BarId == _bar2Id ) ;
100+ Assert . That ( fooBarJoin . Id , Is . EqualTo ( _foo2Id ) ) ;
101+ fooBarJoin = results . First ( x => x . BarId == _bar3Id ) ;
102+ Assert . That ( fooBarJoin . Id , Is . EqualTo ( _foo3Id ) ) ;
103+ }
104+ }
105+ }
106+ }
0 commit comments