@@ -147,53 +147,34 @@ public virtual SqlExpression<T> Where(string sqlFilter, params object[] filterPa
147147
148148 public virtual SqlExpression < T > Where ( Expression < Func < T , bool > > predicate )
149149 {
150- if ( predicate != null )
151- {
152- And ( predicate ) ;
153- }
154- else
155- {
156- underlyingExpression = null ;
157- whereExpression = string . Empty ;
158- }
159-
150+ AppendToWhere ( "AND" , predicate ) ;
160151 return this ;
161152 }
162153
163154 public virtual SqlExpression < T > And ( Expression < Func < T , bool > > predicate )
164155 {
165- if ( predicate != null )
166- {
167- if ( underlyingExpression == null )
168- underlyingExpression = predicate ;
169- else
170- underlyingExpression = underlyingExpression . And ( predicate ) ;
171-
172- ProcessInternalExpression ( ) ;
173- }
156+ AppendToWhere ( "AND" , predicate ) ;
174157 return this ;
175158 }
176159
177160 public virtual SqlExpression < T > Or ( Expression < Func < T , bool > > predicate )
178161 {
179- if ( predicate != null )
180- {
181- if ( underlyingExpression == null )
182- underlyingExpression = predicate ;
183- else
184- underlyingExpression = underlyingExpression . Or ( predicate ) ;
185-
186- ProcessInternalExpression ( ) ;
187- }
162+ AppendToWhere ( "OR" , predicate ) ;
188163 return this ;
189164 }
190165
191- private void ProcessInternalExpression ( )
166+ protected void AppendToWhere ( string operand , Expression predicate )
192167 {
168+ if ( predicate == null )
169+ return ;
170+
193171 useFieldName = true ;
194172 sep = " " ;
195- whereExpression = Visit ( underlyingExpression ) . ToString ( ) ;
196- if ( ! string . IsNullOrEmpty ( whereExpression ) ) whereExpression = ( WhereStatementWithoutWhereString ? "" : "WHERE " ) + whereExpression ;
173+ var newExpr = Visit ( predicate ) . ToString ( ) ;
174+ whereExpression = string . IsNullOrEmpty ( whereExpression )
175+ ? ( WhereStatementWithoutWhereString ? "" : "WHERE " )
176+ : whereExpression + " " + operand + " " ;
177+ whereExpression += newExpr ;
197178 }
198179
199180 public virtual SqlExpression < T > GroupBy ( )
@@ -1263,6 +1244,8 @@ protected virtual object VisitColumnAccessMethod(MethodCallExpression m)
12631244 var quotedColName = Visit ( m . Object ) ;
12641245 var statement = "" ;
12651246
1247+ var wildcardArg = args . Count > 0 ? OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) : "" ;
1248+ var escapeSuffix = wildcardArg . IndexOf ( '^' ) >= 0 ? " escape '^'" : "" ;
12661249 switch ( m . Method . Name )
12671250 {
12681251 case "Trim" :
@@ -1283,43 +1266,43 @@ protected virtual object VisitColumnAccessMethod(MethodCallExpression m)
12831266 case "StartsWith" :
12841267 if ( ! OrmLiteConfig . StripUpperInLike )
12851268 {
1286- statement = string . Format ( "upper({0}) like {1} escape '^' " ,
1269+ statement = string . Format ( "upper({0}) like {1}{2} " ,
12871270 quotedColName , OrmLiteConfig . DialectProvider . GetQuotedValue (
1288- OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) . ToUpper ( ) + "%" ) ) ;
1271+ wildcardArg . ToUpper ( ) + "%" ) , escapeSuffix ) ;
12891272 }
12901273 else
12911274 {
1292- statement = string . Format ( "{0} like {1} escape '^' " ,
1275+ statement = string . Format ( "{0} like {1}{2} " ,
12931276 quotedColName , OrmLiteConfig . DialectProvider . GetQuotedValue (
1294- OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) + "%" ) ) ;
1277+ wildcardArg + "%" ) , escapeSuffix ) ;
12951278 }
12961279 break ;
12971280 case "EndsWith" :
12981281 if ( ! OrmLiteConfig . StripUpperInLike )
12991282 {
1300- statement = string . Format ( "upper({0}) like {1} escape '^' " ,
1283+ statement = string . Format ( "upper({0}) like {1}{2} " ,
13011284 quotedColName , OrmLiteConfig . DialectProvider . GetQuotedValue ( "%" +
1302- OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) . ToUpper ( ) ) ) ;
1285+ wildcardArg . ToUpper ( ) ) , escapeSuffix ) ;
13031286 }
13041287 else
13051288 {
1306- statement = string . Format ( "{0} like {1} escape '^' " ,
1289+ statement = string . Format ( "{0} like {1}{2} " ,
13071290 quotedColName , OrmLiteConfig . DialectProvider . GetQuotedValue ( "%" +
1308- OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) ) ) ;
1291+ wildcardArg ) , escapeSuffix ) ;
13091292 }
13101293 break ;
13111294 case "Contains" :
13121295 if ( ! OrmLiteConfig . StripUpperInLike )
13131296 {
1314- statement = string . Format ( "upper({0}) like {1} escape '^' " ,
1297+ statement = string . Format ( "upper({0}) like {1}{2} " ,
13151298 quotedColName , OrmLiteConfig . DialectProvider . GetQuotedValue ( "%" +
1316- OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) . ToUpper ( ) + "%" ) ) ;
1299+ wildcardArg . ToUpper ( ) + "%" ) , escapeSuffix ) ;
13171300 }
13181301 else
13191302 {
1320- statement = string . Format ( "{0} like {1} escape '^' " ,
1303+ statement = string . Format ( "{0} like {1}{2} " ,
13211304 quotedColName , OrmLiteConfig . DialectProvider . GetQuotedValue ( "%" +
1322- OrmLiteConfig . DialectProvider . EscapeWildcards ( args [ 0 ] . ToString ( ) ) + "%" ) ) ;
1305+ wildcardArg + "%" ) , escapeSuffix ) ;
13231306 }
13241307 break ;
13251308 case "Substring" :
0 commit comments