1
- using System ;
2
- using System . Data ;
3
- using System . Data . Common ;
4
- using System . Data . SqlClient ;
5
- using ServiceStack . DataAccess ;
6
-
7
- namespace ServiceStack . OrmLite
8
- {
9
- /// <summary>
10
- /// Wrapper IDbConnection class to allow for connection sharing, mocking, etc.
11
- /// </summary>
12
- public class OrmLiteConnection
13
- : IDbConnection , IHasDbConnection
14
- {
15
- public readonly OrmLiteConnectionFactory Factory ;
16
- public IDbTransaction Transaction { get ; private set ; }
17
- private IDbConnection dbConnection ;
18
- private bool isOpen ;
19
-
20
- public OrmLiteConnection ( OrmLiteConnectionFactory factory )
21
- {
22
- this . Factory = factory ;
23
- }
24
-
25
- public IDbConnection DbConnection
26
- {
27
- get
28
- {
29
- if ( dbConnection == null )
30
- {
31
- dbConnection = Factory . ConnectionString . ToDbConnection ( Factory . DialectProvider ) ;
32
- }
33
- return dbConnection ;
34
- }
35
- }
36
-
37
- public void Dispose ( )
38
- {
39
- if ( Factory . OnDispose != null ) Factory . OnDispose ( this ) ;
40
- if ( ! Factory . AutoDisposeConnection ) return ;
41
-
42
- DbConnection . Dispose ( ) ;
43
- dbConnection = null ;
44
- isOpen = false ;
45
- }
46
-
47
- public IDbTransaction BeginTransaction ( )
48
- {
49
- if ( Factory . AlwaysReturnTransaction != null )
50
- return Factory . AlwaysReturnTransaction ;
51
-
52
- Transaction = DbConnection . BeginTransaction ( ) ;
53
- return Transaction ;
54
- }
55
-
56
- public IDbTransaction BeginTransaction ( IsolationLevel isolationLevel )
57
- {
58
- if ( Factory . AlwaysReturnTransaction != null )
59
- return Factory . AlwaysReturnTransaction ;
60
-
61
- Transaction = DbConnection . BeginTransaction ( isolationLevel ) ;
62
- return Transaction ;
63
- }
64
-
65
- public void Close ( )
66
- {
67
- DbConnection . Close ( ) ;
68
- }
69
-
70
- public void ChangeDatabase ( string databaseName )
71
- {
72
- DbConnection . ChangeDatabase ( databaseName ) ;
73
- }
74
-
75
- public IDbCommand CreateCommand ( )
76
- {
77
- if ( Factory . AlwaysReturnCommand != null )
78
- return Factory . AlwaysReturnCommand ;
79
-
80
- var cmd = DbConnection . CreateCommand ( ) ;
81
- if ( Transaction != null ) { cmd . Transaction = Transaction ; }
82
- return cmd ;
83
- }
84
-
85
- public void Open ( )
86
- {
87
- if ( isOpen ) return ;
88
-
89
- DbConnection . Open ( ) ;
90
- //so the internal connection is wrapped for example by miniprofiler
91
- if ( Factory . ConnectionFilter != null ) { dbConnection = Factory . ConnectionFilter ( dbConnection ) ; }
92
- isOpen = true ;
93
- }
94
-
95
- public string ConnectionString
96
- {
97
- get { return Factory . ConnectionString ; }
98
- set { Factory . ConnectionString = value ; }
99
- }
100
-
101
- public int ConnectionTimeout
102
- {
103
- get { return DbConnection . ConnectionTimeout ; }
104
- }
105
-
106
- public string Database
107
- {
108
- get { return DbConnection . Database ; }
109
- }
110
-
111
- public ConnectionState State
112
- {
113
- get { return DbConnection . State ; }
114
- }
115
-
116
- public static explicit operator SqlConnection ( OrmLiteConnection dbConn )
117
- {
118
- return ( SqlConnection ) dbConn . DbConnection ;
119
- }
120
-
121
- public static explicit operator DbConnection ( OrmLiteConnection dbConn )
122
- {
123
- return ( DbConnection ) dbConn . DbConnection ;
124
- }
125
- }
1
+ using System ;
2
+ using System . Data ;
3
+ using System . Data . Common ;
4
+ using System . Data . SqlClient ;
5
+ using ServiceStack . DataAccess ;
6
+
7
+ namespace ServiceStack . OrmLite
8
+ {
9
+ /// <summary>
10
+ /// Wrapper IDbConnection class to allow for connection sharing, mocking, etc.
11
+ /// </summary>
12
+ public class OrmLiteConnection
13
+ : IDbConnection , IHasDbConnection
14
+ {
15
+ public readonly OrmLiteConnectionFactory Factory ;
16
+ public IDbTransaction Transaction { get ; internal set ; }
17
+ private IDbConnection dbConnection ;
18
+ private bool isOpen ;
19
+
20
+ public OrmLiteConnection ( OrmLiteConnectionFactory factory )
21
+ {
22
+ this . Factory = factory ;
23
+ }
24
+
25
+ public IDbConnection DbConnection
26
+ {
27
+ get
28
+ {
29
+ if ( dbConnection == null )
30
+ {
31
+ dbConnection = Factory . ConnectionString . ToDbConnection ( Factory . DialectProvider ) ;
32
+ }
33
+ return dbConnection ;
34
+ }
35
+ }
36
+
37
+ public void Dispose ( )
38
+ {
39
+ if ( Factory . OnDispose != null ) Factory . OnDispose ( this ) ;
40
+ if ( ! Factory . AutoDisposeConnection ) return ;
41
+
42
+ DbConnection . Dispose ( ) ;
43
+ dbConnection = null ;
44
+ isOpen = false ;
45
+ }
46
+
47
+ public IDbTransaction BeginTransaction ( )
48
+ {
49
+ if ( Factory . AlwaysReturnTransaction != null )
50
+ return Factory . AlwaysReturnTransaction ;
51
+
52
+ Transaction = DbConnection . BeginTransaction ( ) ;
53
+ return Transaction ;
54
+ }
55
+
56
+ public IDbTransaction BeginTransaction ( IsolationLevel isolationLevel )
57
+ {
58
+ if ( Factory . AlwaysReturnTransaction != null )
59
+ return Factory . AlwaysReturnTransaction ;
60
+
61
+ Transaction = DbConnection . BeginTransaction ( isolationLevel ) ;
62
+ return Transaction ;
63
+ }
64
+
65
+ public void Close ( )
66
+ {
67
+ DbConnection . Close ( ) ;
68
+ }
69
+
70
+ public void ChangeDatabase ( string databaseName )
71
+ {
72
+ DbConnection . ChangeDatabase ( databaseName ) ;
73
+ }
74
+
75
+ public IDbCommand CreateCommand ( )
76
+ {
77
+ if ( Factory . AlwaysReturnCommand != null )
78
+ return Factory . AlwaysReturnCommand ;
79
+
80
+ var cmd = DbConnection . CreateCommand ( ) ;
81
+ if ( Transaction != null ) { cmd . Transaction = Transaction ; }
82
+ return cmd ;
83
+ }
84
+
85
+ public void Open ( )
86
+ {
87
+ if ( isOpen ) return ;
88
+
89
+ DbConnection . Open ( ) ;
90
+ //so the internal connection is wrapped for example by miniprofiler
91
+ if ( Factory . ConnectionFilter != null ) { dbConnection = Factory . ConnectionFilter ( dbConnection ) ; }
92
+ isOpen = true ;
93
+ }
94
+
95
+ public string ConnectionString
96
+ {
97
+ get { return Factory . ConnectionString ; }
98
+ set { Factory . ConnectionString = value ; }
99
+ }
100
+
101
+ public int ConnectionTimeout
102
+ {
103
+ get { return DbConnection . ConnectionTimeout ; }
104
+ }
105
+
106
+ public string Database
107
+ {
108
+ get { return DbConnection . Database ; }
109
+ }
110
+
111
+ public ConnectionState State
112
+ {
113
+ get { return DbConnection . State ; }
114
+ }
115
+
116
+ public static explicit operator SqlConnection ( OrmLiteConnection dbConn )
117
+ {
118
+ return ( SqlConnection ) dbConn . DbConnection ;
119
+ }
120
+
121
+ public static explicit operator DbConnection ( OrmLiteConnection dbConn )
122
+ {
123
+ return ( DbConnection ) dbConn . DbConnection ;
124
+ }
125
+ }
126
126
}
0 commit comments