1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Data ;
4
- using System . Linq . Expressions ;
5
-
6
- namespace ServiceStack . OrmLite
7
- {
8
- public static class ReadConnectionExtensions
9
- {
10
- [ ThreadStatic ]
11
- internal static string LastCommandText ;
12
-
13
- public static SqlExpressionVisitor < T > CreateExpression < T > ( this IDbConnection dbConn ) where T : new ( )
14
- {
15
- return dbConn . GetDialectProvider ( ) . ExpressionVisitor < T > ( ) ;
16
- }
17
-
18
- public static T Exec < T > ( this IDbConnection dbConn , Func < IDbCommand , T > filter )
19
- {
20
- var holdProvider = OrmLiteConfig . TSDialectProvider ;
21
- try
22
- {
23
- var ormLiteDbConn = dbConn as OrmLiteConnection ;
24
- if ( ormLiteDbConn != null )
25
- OrmLiteConfig . TSDialectProvider = ormLiteDbConn . Factory . DialectProvider ;
26
-
27
- using ( var dbCmd = dbConn . CreateCommand ( ) )
28
- {
29
- dbCmd . Transaction = ( ormLiteDbConn != null ) ? ormLiteDbConn . Transaction : OrmLiteConfig . TSTransaction ;
30
-
31
- var ret = filter ( dbCmd ) ;
32
- LastCommandText = dbCmd . CommandText ;
33
- return ret ;
34
- }
35
- }
36
- finally
37
- {
38
- OrmLiteConfig . TSDialectProvider = holdProvider ;
39
- }
40
- }
41
-
42
- public static void Exec ( this IDbConnection dbConn , Action < IDbCommand > filter )
43
- {
44
- var dialectProvider = OrmLiteConfig . DialectProvider ;
45
- try
46
- {
47
- var ormLiteDbConn = dbConn as OrmLiteConnection ;
48
- if ( ormLiteDbConn != null )
49
- OrmLiteConfig . DialectProvider = ormLiteDbConn . Factory . DialectProvider ;
50
-
51
- using ( var dbCmd = dbConn . CreateCommand ( ) )
52
- {
53
- dbCmd . Transaction = ( ormLiteDbConn != null ) ? ormLiteDbConn . Transaction : OrmLiteConfig . TSTransaction ;
54
-
55
- filter ( dbCmd ) ;
56
- LastCommandText = dbCmd . CommandText ;
57
- }
58
- }
59
- finally
60
- {
61
- OrmLiteConfig . DialectProvider = dialectProvider ;
62
- }
63
- }
64
-
65
- public static IEnumerable < T > ExecLazy < T > ( this IDbConnection dbConn , Func < IDbCommand , IEnumerable < T > > filter )
66
- {
67
- var dialectProvider = OrmLiteConfig . DialectProvider ;
68
- try
69
- {
70
- var ormLiteDbConn = dbConn as OrmLiteConnection ;
71
- if ( ormLiteDbConn != null )
72
- OrmLiteConfig . DialectProvider = ormLiteDbConn . Factory . DialectProvider ;
73
-
74
- using ( var dbCmd = dbConn . CreateCommand ( ) )
75
- {
76
- dbCmd . Transaction = ( ormLiteDbConn != null ) ? ormLiteDbConn . Transaction : OrmLiteConfig . TSTransaction ;
77
-
78
- var results = filter ( dbCmd ) ;
79
- LastCommandText = dbCmd . CommandText ;
80
- foreach ( var item in results )
81
- {
82
- yield return item ;
83
- }
84
- }
85
- }
86
- finally
87
- {
88
- OrmLiteConfig . DialectProvider = dialectProvider ;
89
- }
90
- }
91
-
92
- public static IDbTransaction OpenTransaction ( this IDbConnection dbConn )
93
- {
94
- return new OrmLiteTransaction ( dbConn , dbConn . BeginTransaction ( ) ) ;
95
- }
96
-
97
- public static IDbTransaction OpenTransaction ( this IDbConnection dbConn , IsolationLevel isolationLevel )
98
- {
99
- return new OrmLiteTransaction ( dbConn , dbConn . BeginTransaction ( isolationLevel ) ) ;
100
- }
101
-
102
- public static IOrmLiteDialectProvider GetDialectProvider ( this IDbConnection dbConn )
103
- {
104
- var ormLiteDbConn = dbConn as OrmLiteConnection ;
105
- return ormLiteDbConn != null
106
- ? ormLiteDbConn . Factory . DialectProvider
107
- : OrmLiteConfig . DialectProvider ;
108
- }
109
-
110
- public static SqlExpressionVisitor < T > CreateExpression < T > ( )
111
- {
112
- return OrmLiteConfig . DialectProvider . ExpressionVisitor < T > ( ) ;
113
- }
114
-
115
- public static List < T > Select < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
116
- where T : new ( )
117
- {
118
- return dbConn . Exec ( dbCmd => dbCmd . Select ( predicate ) ) ;
119
- }
120
-
121
- public static List < T > Select < T > ( this IDbConnection dbConn , Func < SqlExpressionVisitor < T > , SqlExpressionVisitor < T > > expression )
122
- where T : new ( )
123
- {
124
- return dbConn . Exec ( dbCmd => dbCmd . Select ( expression ) ) ;
125
- }
126
-
127
- public static List < T > Select < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
128
- where T : new ( )
129
- {
130
- return dbConn . Exec ( dbCmd => dbCmd . Select ( expression ) ) ;
131
- }
132
-
133
- /// <summary>
134
- /// Performs the same function as Select() except arguments are passed as parameters to the generated SQL.
135
- /// Currently does not support complex SQL.## , .StartsWith(), EndsWith() and Contains() operators
136
- /// </summary>
137
- public static List < T > SelectParam < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
138
- where T : new ( )
139
- {
140
- return dbConn . Exec ( dbCmd => dbCmd . SelectParam ( predicate ) ) ;
141
- }
142
-
143
- public static T First < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
144
- where T : new ( )
145
- {
146
- return dbConn . Exec ( dbCmd => dbCmd . First ( predicate ) ) ;
147
- }
148
-
149
- public static T First < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
150
- where T : new ( )
151
- {
152
- return dbConn . Exec ( dbCmd => dbCmd . First ( expression ) ) ;
153
- }
154
-
155
- public static T FirstOrDefault < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
156
- where T : new ( )
157
- {
158
- return dbConn . Exec ( dbCmd => dbCmd . FirstOrDefault ( predicate ) ) ;
159
- }
160
-
161
- public static T FirstOrDefault < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
162
- where T : new ( )
163
- {
164
- return dbConn . Exec ( dbCmd => dbCmd . FirstOrDefault ( expression ) ) ;
165
- }
166
-
167
- public static TKey GetScalar < T , TKey > ( this IDbConnection dbConn , Expression < Func < T , TKey > > field )
168
- where T : new ( )
169
- {
170
- return dbConn . Exec ( dbCmd => dbCmd . GetScalar ( field ) ) ;
171
- }
172
-
173
- public static TKey GetScalar < T , TKey > ( this IDbConnection dbConn , Expression < Func < T , TKey > > field ,
174
- Expression < Func < T , bool > > predicate )
175
- where T : new ( )
176
- {
177
- return dbConn . Exec ( dbCmd => dbCmd . GetScalar ( field , predicate ) ) ;
178
- }
179
-
180
- public static long Count < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
181
- where T : new ( )
182
- {
183
- return dbConn . Exec ( dbCmd => dbCmd . Count ( expression ) ) ;
184
- }
185
-
186
- public static long Count < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > expression )
187
- where T : new ( )
188
- {
189
- return dbConn . Exec ( dbCmd => dbCmd . Count ( expression ) ) ;
190
- }
191
-
192
- public static long Count < T > ( this IDbConnection dbConn )
193
- where T : new ( )
194
- {
195
- SqlExpressionVisitor < T > expression = OrmLiteConfig . DialectProvider . ExpressionVisitor < T > ( ) ;
196
- return dbConn . Exec ( dbCmd => dbCmd . Count ( expression ) ) ;
197
- }
198
- }
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Data ;
4
+ using System . Linq . Expressions ;
5
+
6
+ namespace ServiceStack . OrmLite
7
+ {
8
+ public static class ReadConnectionExtensions
9
+ {
10
+ [ ThreadStatic ]
11
+ internal static string LastCommandText ;
12
+
13
+ public static SqlExpressionVisitor < T > CreateExpression < T > ( this IDbConnection dbConn )
14
+ {
15
+ return dbConn . GetDialectProvider ( ) . ExpressionVisitor < T > ( ) ;
16
+ }
17
+
18
+ public static T Exec < T > ( this IDbConnection dbConn , Func < IDbCommand , T > filter )
19
+ {
20
+ var holdProvider = OrmLiteConfig . TSDialectProvider ;
21
+ try
22
+ {
23
+ var ormLiteDbConn = dbConn as OrmLiteConnection ;
24
+ if ( ormLiteDbConn != null )
25
+ OrmLiteConfig . TSDialectProvider = ormLiteDbConn . Factory . DialectProvider ;
26
+
27
+ using ( var dbCmd = dbConn . CreateCommand ( ) )
28
+ {
29
+ dbCmd . Transaction = ( ormLiteDbConn != null ) ? ormLiteDbConn . Transaction : OrmLiteConfig . TSTransaction ;
30
+
31
+ var ret = filter ( dbCmd ) ;
32
+ LastCommandText = dbCmd . CommandText ;
33
+ return ret ;
34
+ }
35
+ }
36
+ finally
37
+ {
38
+ OrmLiteConfig . TSDialectProvider = holdProvider ;
39
+ }
40
+ }
41
+
42
+ public static void Exec ( this IDbConnection dbConn , Action < IDbCommand > filter )
43
+ {
44
+ var dialectProvider = OrmLiteConfig . DialectProvider ;
45
+ try
46
+ {
47
+ var ormLiteDbConn = dbConn as OrmLiteConnection ;
48
+ if ( ormLiteDbConn != null )
49
+ OrmLiteConfig . DialectProvider = ormLiteDbConn . Factory . DialectProvider ;
50
+
51
+ using ( var dbCmd = dbConn . CreateCommand ( ) )
52
+ {
53
+ dbCmd . Transaction = ( ormLiteDbConn != null ) ? ormLiteDbConn . Transaction : OrmLiteConfig . TSTransaction ;
54
+
55
+ filter ( dbCmd ) ;
56
+ LastCommandText = dbCmd . CommandText ;
57
+ }
58
+ }
59
+ finally
60
+ {
61
+ OrmLiteConfig . DialectProvider = dialectProvider ;
62
+ }
63
+ }
64
+
65
+ public static IEnumerable < T > ExecLazy < T > ( this IDbConnection dbConn , Func < IDbCommand , IEnumerable < T > > filter )
66
+ {
67
+ var dialectProvider = OrmLiteConfig . DialectProvider ;
68
+ try
69
+ {
70
+ var ormLiteDbConn = dbConn as OrmLiteConnection ;
71
+ if ( ormLiteDbConn != null )
72
+ OrmLiteConfig . DialectProvider = ormLiteDbConn . Factory . DialectProvider ;
73
+
74
+ using ( var dbCmd = dbConn . CreateCommand ( ) )
75
+ {
76
+ dbCmd . Transaction = ( ormLiteDbConn != null ) ? ormLiteDbConn . Transaction : OrmLiteConfig . TSTransaction ;
77
+
78
+ var results = filter ( dbCmd ) ;
79
+ LastCommandText = dbCmd . CommandText ;
80
+ foreach ( var item in results )
81
+ {
82
+ yield return item ;
83
+ }
84
+ }
85
+ }
86
+ finally
87
+ {
88
+ OrmLiteConfig . DialectProvider = dialectProvider ;
89
+ }
90
+ }
91
+
92
+ public static IDbTransaction OpenTransaction ( this IDbConnection dbConn )
93
+ {
94
+ return new OrmLiteTransaction ( dbConn , dbConn . BeginTransaction ( ) ) ;
95
+ }
96
+
97
+ public static IDbTransaction OpenTransaction ( this IDbConnection dbConn , IsolationLevel isolationLevel )
98
+ {
99
+ return new OrmLiteTransaction ( dbConn , dbConn . BeginTransaction ( isolationLevel ) ) ;
100
+ }
101
+
102
+ public static IOrmLiteDialectProvider GetDialectProvider ( this IDbConnection dbConn )
103
+ {
104
+ var ormLiteDbConn = dbConn as OrmLiteConnection ;
105
+ return ormLiteDbConn != null
106
+ ? ormLiteDbConn . Factory . DialectProvider
107
+ : OrmLiteConfig . DialectProvider ;
108
+ }
109
+
110
+ public static SqlExpressionVisitor < T > CreateExpression < T > ( )
111
+ {
112
+ return OrmLiteConfig . DialectProvider . ExpressionVisitor < T > ( ) ;
113
+ }
114
+
115
+ public static List < T > Select < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
116
+ {
117
+ return dbConn . Exec ( dbCmd => dbCmd . Select ( predicate ) ) ;
118
+ }
119
+
120
+ public static List < T > Select < T > ( this IDbConnection dbConn , Func < SqlExpressionVisitor < T > , SqlExpressionVisitor < T > > expression )
121
+ {
122
+ return dbConn . Exec ( dbCmd => dbCmd . Select ( expression ) ) ;
123
+ }
124
+
125
+ public static List < T > Select < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
126
+ {
127
+ return dbConn . Exec ( dbCmd => dbCmd . Select ( expression ) ) ;
128
+ }
129
+
130
+ /// <summary>
131
+ /// Performs the same function as Select() except arguments are passed as parameters to the generated SQL.
132
+ /// Currently does not support complex SQL.## , .StartsWith(), EndsWith() and Contains() operators
133
+ /// </summary>
134
+ public static List < T > SelectParam < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
135
+ {
136
+ return dbConn . Exec ( dbCmd => dbCmd . SelectParam ( predicate ) ) ;
137
+ }
138
+
139
+ public static T First < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
140
+ {
141
+ return dbConn . Exec ( dbCmd => dbCmd . First ( predicate ) ) ;
142
+ }
143
+
144
+ public static T First < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
145
+ {
146
+ return dbConn . Exec ( dbCmd => dbCmd . First ( expression ) ) ;
147
+ }
148
+
149
+ public static T FirstOrDefault < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > predicate )
150
+ {
151
+ return dbConn . Exec ( dbCmd => dbCmd . FirstOrDefault ( predicate ) ) ;
152
+ }
153
+
154
+ public static T FirstOrDefault < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
155
+ {
156
+ return dbConn . Exec ( dbCmd => dbCmd . FirstOrDefault ( expression ) ) ;
157
+ }
158
+
159
+ public static TKey GetScalar < T , TKey > ( this IDbConnection dbConn , Expression < Func < T , TKey > > field )
160
+ {
161
+ return dbConn . Exec ( dbCmd => dbCmd . GetScalar ( field ) ) ;
162
+ }
163
+
164
+ public static TKey GetScalar < T , TKey > ( this IDbConnection dbConn , Expression < Func < T , TKey > > field ,
165
+ Expression < Func < T , bool > > predicate )
166
+ {
167
+ return dbConn . Exec ( dbCmd => dbCmd . GetScalar ( field , predicate ) ) ;
168
+ }
169
+
170
+ public static long Count < T > ( this IDbConnection dbConn , SqlExpressionVisitor < T > expression )
171
+ {
172
+ return dbConn . Exec ( dbCmd => dbCmd . Count ( expression ) ) ;
173
+ }
174
+
175
+ public static long Count < T > ( this IDbConnection dbConn , Expression < Func < T , bool > > expression )
176
+ {
177
+ return dbConn . Exec ( dbCmd => dbCmd . Count ( expression ) ) ;
178
+ }
179
+
180
+ public static long Count < T > ( this IDbConnection dbConn )
181
+ {
182
+ SqlExpressionVisitor < T > expression = OrmLiteConfig . DialectProvider . ExpressionVisitor < T > ( ) ;
183
+ return dbConn . Exec ( dbCmd => dbCmd . Count ( expression ) ) ;
184
+ }
185
+ }
199
186
}
0 commit comments