11using System . Text . RegularExpressions ;
22using NUnit . Framework ;
33using ServiceStack . DataAnnotations ;
4+ using ServiceStack . Text ;
45
56namespace ServiceStack . OrmLite . Tests
67{
@@ -86,5 +87,50 @@ public void DoubleWhereLeftJoinTest ()
8687 var stmt = OrmLiteConfig . DialectProvider . ToSelectStatement ( typeof ( User ) , joinQuery ) ;
8788 Assert . That ( Regex . Matches ( stmt , @"(\b|\n)FROM(\b|\n)" , RegexOptions . IgnoreCase ) . Count , Is . EqualTo ( 1 ) ) ;
8889 }
90+
91+ [ Test ]
92+ public void Can_execute_JoinSqlBuilder_as_SqlExpression ( )
93+ {
94+ var joinQuery = new JoinSqlBuilder < User , User > ( )
95+ . LeftJoin < User , WithAliasAddress > ( x => x . Id , x => x . UserId
96+ , sourceWhere : x => x . Age > 18
97+ , destinationWhere : x => x . Country == "Italy" ) ;
98+
99+ using ( var db = OpenDbConnection ( ) )
100+ {
101+ db . DropAndCreateTable < User > ( ) ;
102+ db . DropAndCreateTable < WithAliasAddress > ( ) ;
103+
104+ var userId = db . Insert ( new User { Age = 27 , Name = "Foo" } , selectIdentity : true ) ;
105+ db . Insert ( new WithAliasAddress { City = "Rome" , Country = "Italy" , UserId = ( int ) userId } ) ;
106+
107+ var results = db . Select < User > ( joinQuery ) ;
108+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
109+ }
110+ }
111+
112+ [ Test ]
113+ public void Can_execute_SqlBuilder_templates_as_SqlExpression ( )
114+ {
115+ var sb = new SqlBuilder ( ) ;
116+
117+ var tmpl = sb . AddTemplate ( "SELECT * FROM User u INNER JOIN Addresses a on a.UserId = u.Id /**where**/" ) ;
118+ sb . Where ( "Age > @age" , new { age = 18 } ) ;
119+ sb . Where ( "Countryalias = @country" , new { country = "Italy" } ) ;
120+
121+
122+ using ( var db = OpenDbConnection ( ) )
123+ {
124+ db . DropAndCreateTable < User > ( ) ;
125+ db . DropAndCreateTable < WithAliasAddress > ( ) ;
126+
127+ var userId = db . Insert ( new User { Age = 27 , Name = "Foo" } , selectIdentity : true ) ;
128+ db . Insert ( new WithAliasAddress { City = "Rome" , Country = "Italy" , UserId = ( int ) userId } ) ;
129+
130+ var results = db . Select < User > ( tmpl , tmpl . Parameters ) ;
131+
132+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
133+ }
134+ }
89135 }
90136}
0 commit comments