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 . Tests . Expression
10
+ {
11
+ public class Bar : IHasGuidId
12
+ {
13
+ [ PrimaryKey ]
14
+ public Guid Id { get ; set ; }
15
+
16
+ [ Required ]
17
+ public string Name { get ; set ; }
18
+ }
19
+
20
+ public class FooBar : IHasIntId
21
+ {
22
+ [ AutoIncrement ]
23
+ [ PrimaryKey ]
24
+ public int Id { get ; set ; }
25
+
26
+ // And a foreign key to the foo table as well, but that is not necessary to show the problem.
27
+
28
+ [ Alias ( "fkBarId" ) ]
29
+ [ ForeignKey ( typeof ( Bar ) , ForeignKeyName = "fk_FooBar_Bar" ) ]
30
+ public Guid BarId { get ; set ; }
31
+ }
32
+
33
+ public class Baz : IHasIntId
34
+ {
35
+ [ PrimaryKey ]
36
+ [ AutoIncrement ]
37
+ public int Id { get ; set ; }
38
+
39
+ [ Required ]
40
+ public string Name { get ; set ; }
41
+ }
42
+
43
+ public class FooBarBaz : IHasIntId
44
+ {
45
+ [ AutoIncrement ]
46
+ [ PrimaryKey ]
47
+ public int Id { get ; set ; }
48
+
49
+ [ Alias ( "fkFooBarId" ) ]
50
+ [ ForeignKey ( typeof ( FooBar ) , ForeignKeyName = "fk_FooBarbaz_FooBar" , OnDelete = "CASCADE" ) ]
51
+ public int FooBarId { get ; set ; }
52
+
53
+ [ Alias ( "fkBazId" ) ]
54
+ [ ForeignKey ( typeof ( Baz ) , ForeignKeyName = "fk_FooBarBaz_Baz" , OnDelete = "CASCADE" ) ]
55
+ public int BazId { get ; set ; }
56
+
57
+ [ Required ]
58
+ public decimal Amount { get ; set ; }
59
+ }
60
+
61
+ internal class JoinResult
62
+ {
63
+ [ BelongTo ( typeof ( FooBar ) ) ]
64
+ public int Id { get ; set ; }
65
+
66
+ [ Alias ( "fkBazId" ) ]
67
+ [ BelongTo ( typeof ( FooBarBaz ) ) ]
68
+ public int FooBarBazId { get ; set ; }
69
+
70
+ [ BelongTo ( typeof ( FooBarBaz ) ) ]
71
+ public decimal Amount { get ; set ; }
72
+
73
+ [ Alias ( "fkBarId" ) ]
74
+ [ BelongTo ( typeof ( Bar ) ) ]
75
+ public Guid BarId { get ; set ; }
76
+
77
+ [ BelongTo ( typeof ( Bar ) ) ]
78
+ public string BarName { get ; set ; }
79
+
80
+ [ Alias ( "fkBazId" ) ]
81
+ [ BelongTo ( typeof ( Baz ) ) ]
82
+ public int BazId { get ; set ; }
83
+
84
+ [ BelongTo ( typeof ( Baz ) ) ]
85
+ public string BazName { get ; set ; }
86
+ }
87
+
88
+
89
+ [ TestFixture ]
90
+ public class ComplexJoinTests : OrmLiteTestBase
91
+ {
92
+ private static void InitTables ( IDbConnection db )
93
+ {
94
+ db . DropTable < FooBarBaz > ( ) ;
95
+ db . DropTable < FooBar > ( ) ;
96
+ db . DropTable < Bar > ( ) ;
97
+ db . DropTable < Baz > ( ) ;
98
+
99
+ db . CreateTable < Baz > ( ) ;
100
+ db . CreateTable < Bar > ( ) ;
101
+ db . CreateTable < FooBar > ( ) ;
102
+ db . CreateTable < FooBarBaz > ( ) ;
103
+
104
+ var bar1Id = new Guid ( "5bd67b84-bfdb-4057-9799-5e7a72a6eaa9" ) ;
105
+ var bar2Id = new Guid ( "a8061d08-6816-4e1e-b3d7-1178abcefa0d" ) ;
106
+
107
+ db . Insert ( new Bar { Id = bar1Id , Name = "Banana" , } ) ;
108
+ db . Insert ( new Bar { Id = bar2Id , Name = "Orange" , } ) ;
109
+
110
+ db . Insert ( new Baz { Id = 1 , Name = "Large" } ) ;
111
+ db . Insert ( new Baz { Id = 2 , Name = "Huge" } ) ;
112
+
113
+ db . Insert ( new FooBar { Id = 1 , BarId = bar1Id , } ) ;
114
+ db . Insert ( new FooBar { Id = 2 , BarId = bar2Id , } ) ;
115
+
116
+ db . Insert ( new FooBarBaz { Amount = 42 , FooBarId = 1 , BazId = 2 } ) ;
117
+ db . Insert ( new FooBarBaz { Amount = 50 , FooBarId = 1 , BazId = 1 } ) ;
118
+ db . Insert ( new FooBarBaz { Amount = 2 , FooBarId = 2 , BazId = 1 } ) ;
119
+ }
120
+
121
+ [ Test ]
122
+ public void ComplexJoin_with_JoinSqlBuilder ( )
123
+ {
124
+ using ( var db = OpenDbConnection ( ) )
125
+ {
126
+ InitTables ( db ) ;
127
+
128
+ /* This gives the expected values for BazId */
129
+ var jn = new JoinSqlBuilder < JoinResult , FooBar > ( )
130
+ . Join < FooBar , Bar > (
131
+ sourceColumn : dp => dp . BarId ,
132
+ destinationColumn : p => p . Id ,
133
+ destinationTableColumnSelection : p => new { BarName = p . Name , BarId = p . Id } )
134
+ . Join < FooBar , FooBarBaz > (
135
+ sourceColumn : dp => dp . Id ,
136
+ destinationColumn : dpss => dpss . FooBarId ,
137
+ destinationTableColumnSelection : dpss => new { dpss . Amount , FooBarBazId = dpss . Id } ) ;
138
+ jn . Join < FooBarBaz , Baz > (
139
+ sourceColumn : dpss => dpss . BazId ,
140
+ destinationColumn : ss => ss . Id ,
141
+ destinationTableColumnSelection : ss => new { BazId = ss . Id , BazName = ss . Name } ) ;
142
+ jn . Select < FooBar > ( dp => new { Id = dp . Id , } ) ;
143
+
144
+ var results = db . Select < JoinResult > ( jn . ToSql ( ) ) ;
145
+ db . GetLastSql ( ) . Print ( ) ;
146
+
147
+ results . PrintDump ( ) ;
148
+
149
+ var fooBarBaz = results . First ( x => x . FooBarBazId == 1 ) ;
150
+ Assert . That ( fooBarBaz . BazId , Is . EqualTo ( 2 ) ) ;
151
+ fooBarBaz = results . First ( x => x . FooBarBazId == 2 ) ;
152
+ Assert . That ( fooBarBaz . BazId , Is . EqualTo ( 1 ) ) ;
153
+ fooBarBaz = results . First ( x => x . FooBarBazId == 2 ) ;
154
+ Assert . That ( fooBarBaz . BazId , Is . EqualTo ( 1 ) ) ;
155
+ }
156
+ }
157
+
158
+ [ Test ]
159
+ public void ComplexJoin_with_SqlExpression ( )
160
+ {
161
+ using ( var db = OpenDbConnection ( ) )
162
+ {
163
+ InitTables ( db ) ;
164
+
165
+ var q = db . From < FooBar > ( )
166
+ . Join < Bar > ( ( dp , p ) => dp . BarId == p . Id )
167
+ . Join < FooBarBaz > ( ( dp , dpss ) => dp . Id == dpss . FooBarId )
168
+ . Join < FooBarBaz , Baz > ( ( dpss , ss ) => dpss . BazId == ss . Id ) ;
169
+
170
+ var results = db . Select < JoinResult > ( q ) ;
171
+
172
+ db . GetLastSql ( ) . Replace ( "INNER" , "\n INNER" ) . Print ( ) ;
173
+
174
+ results . PrintDump ( ) ;
175
+
176
+ var fooBarBaz = results . First ( x => x . FooBarBazId == 1 ) ;
177
+ Assert . That ( fooBarBaz . BazId , Is . EqualTo ( 2 ) ) ;
178
+ fooBarBaz = results . First ( x => x . FooBarBazId == 2 ) ;
179
+ Assert . That ( fooBarBaz . BazId , Is . EqualTo ( 1 ) ) ;
180
+ fooBarBaz = results . First ( x => x . FooBarBazId == 2 ) ;
181
+ Assert . That ( fooBarBaz . BazId , Is . EqualTo ( 1 ) ) ;
182
+ }
183
+ }
184
+ }
185
+ }
0 commit comments