1- using System ;
2- using System . Collections . Generic ;
31using System . Linq . Expressions ;
42
53namespace ServiceStack . OrmLite . Firebird
64{
75 public class FirebirdSqlExpression < T > : ParameterizedSqlExpression < T >
86 {
9- private readonly string _trueExpression ;
10- private readonly string _falseExpression ;
11-
127 public FirebirdSqlExpression ( IOrmLiteDialectProvider dialectProvider )
13- : base ( dialectProvider )
14- {
15- _trueExpression = string . Format ( "({0}={1})" , GetQuotedTrueValue ( ) , GetQuotedTrueValue ( ) ) ;
16- _falseExpression = string . Format ( "({0}={1})" , GetQuotedTrueValue ( ) , GetQuotedFalseValue ( ) ) ;
17- }
18-
19- protected override object VisitBinary ( BinaryExpression b )
20- {
21- object left , right ;
22- var operand = BindOperant ( b . NodeType ) ; //sep= " " ??
23- if ( operand == "AND" || operand == "OR" )
24- {
25- var m = b . Left as MemberExpression ;
26- if ( m != null && m . Expression != null
27- && m . Expression . NodeType == ExpressionType . Parameter )
28- left = new PartialSqlString ( string . Format ( "{0}={1}" , VisitMemberAccess ( m ) , GetQuotedTrueValue ( ) ) ) ;
29- else
30- left = Visit ( b . Left ) ;
31-
32- m = b . Right as MemberExpression ;
33- if ( m != null && m . Expression != null
34- && m . Expression . NodeType == ExpressionType . Parameter )
35- right = new PartialSqlString ( string . Format ( "{0}={1}" , VisitMemberAccess ( m ) , GetQuotedTrueValue ( ) ) ) ;
36- else
37- right = Visit ( b . Right ) ;
38-
39- if ( left as PartialSqlString == null && right as PartialSqlString == null )
40- {
41- var result = Expression . Lambda ( b ) . Compile ( ) . DynamicInvoke ( ) ;
42- return new PartialSqlString ( base . DialectProvider . GetQuotedValue ( result , result . GetType ( ) ) ) ;
43- }
44-
45- if ( left as PartialSqlString == null )
46- left = ( ( bool ) left ) ? GetTrueExpression ( ) : GetFalseExpression ( ) ;
47- if ( right as PartialSqlString == null )
48- right = ( ( bool ) right ) ? GetTrueExpression ( ) : GetFalseExpression ( ) ;
49- }
50- else
51- {
52- left = Visit ( b . Left ) ;
53- right = Visit ( b . Right ) ;
54-
55- var leftEnum = left as EnumMemberAccess ;
56- var rightEnum = right as EnumMemberAccess ;
57-
58- var rightNeedsCoercing = leftEnum != null && rightEnum == null ;
59- var leftNeedsCoercing = rightEnum != null && leftEnum == null ;
60-
61- if ( rightNeedsCoercing )
62- {
63- var rightPartialSql = right as PartialSqlString ;
64- if ( rightPartialSql == null )
65- {
66- right = GetValue ( right , leftEnum . EnumType ) ;
67- }
68- }
69- else if ( leftNeedsCoercing )
70- {
71- var leftPartialSql = left as PartialSqlString ;
72- if ( leftPartialSql == null )
73- {
74- left = DialectProvider . GetQuotedValue ( left , rightEnum . EnumType ) ;
75- }
76- }
77- else if ( left as PartialSqlString == null && right as PartialSqlString == null )
78- {
79- var result = Expression . Lambda ( b ) . Compile ( ) . DynamicInvoke ( ) ;
80- return result ;
81- }
82- else if ( left as PartialSqlString == null )
83- {
84- left = DialectProvider . GetQuotedValue ( left , left != null ? left . GetType ( ) : null ) ;
85- }
86- else if ( right as PartialSqlString == null )
87- {
88- right = GetValue ( right , right != null ? right . GetType ( ) : null ) ;
89- }
90- }
91-
92- if ( operand == "=" && right . ToString ( ) . EqualsIgnoreCase ( "null" ) )
93- operand = "is" ;
94- else if ( operand == "<>" && right . ToString ( ) . EqualsIgnoreCase ( "null" ) )
95- operand = "is not" ;
96- else if ( operand == "=" || operand == "<>" )
97- {
98- if ( IsTrueExpression ( right ) ) right = GetQuotedTrueValue ( ) ;
99- else if ( IsFalseExpression ( right ) ) right = GetQuotedFalseValue ( ) ;
100-
101- if ( IsTrueExpression ( left ) ) left = GetQuotedTrueValue ( ) ;
102- else if ( IsFalseExpression ( left ) ) left = GetQuotedFalseValue ( ) ;
103-
104- }
105-
106- switch ( operand )
107- {
108- case "MOD" :
109- case "COALESCE" :
110- return new PartialSqlString ( string . Format ( "{0}({1},{2})" , operand , left , right ) ) ;
111- default :
112- return new PartialSqlString ( "(" + left + Sep + operand + Sep + right + ")" ) ;
113- }
114- }
115-
116- protected override object VisitConstant ( ConstantExpression c )
117- {
118- if ( c . Value == null )
119- return new PartialSqlString ( "null" ) ;
120-
121- if ( c . Value is bool )
122- {
123- object o = base . DialectProvider . GetQuotedValue ( c . Value , c . Value . GetType ( ) ) ;
124- return new PartialSqlString ( string . Format ( "({0}={1})" , GetQuotedTrueValue ( ) , o ) ) ;
125- }
126-
127- return c . Value ;
128- }
8+ : base ( dialectProvider ) { }
1299
13010 protected override object VisitColumnAccessMethod ( MethodCallExpression m )
13111 {
@@ -149,16 +29,6 @@ protected override object VisitColumnAccessMethod(MethodCallExpression m)
14929 }
15030 return new PartialSqlString ( statement ) ;
15131 }
152-
153- private bool IsTrueExpression ( object exp )
154- {
155- return ( exp . ToString ( ) == _trueExpression ) ;
156- }
157-
158- private bool IsFalseExpression ( object exp )
159- {
160- return ( exp . ToString ( ) == _falseExpression ) ;
161- }
16232 }
16333}
16434
0 commit comments