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