forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiledConnection.cs
More file actions
198 lines (165 loc) · 5.72 KB
/
ProfiledConnection.cs
File metadata and controls
198 lines (165 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Data;
using System.Data.Common;
using ServiceStack.Data;
namespace ServiceStack.MiniProfiler.Data
{
/// <summary>
/// Wraps a database connection, allowing sql execution timings to be collected when a <see cref="IDbProfiler"/> session is started.
/// </summary>
public class ProfiledConnection : DbConnection, IHasDbConnection
{
private DbConnection _conn;
private IDbProfiler _profiler;
/// <summary>
/// Returns a new <see cref="ProfiledConnection"/> that wraps <paramref name="connection"/>,
/// providing query execution profiling. If profiler is null, no profiling will occur.
/// </summary>
/// <param name="connection">Your provider-specific flavor of connection, e.g. SqlConnection, OracleConnection</param>
/// <param name="profiler">The currently started <see cref="IDbProfiler"/> or null.</param>
/// <param name="autoDisposeConnection">Determines whether the ProfiledDbConnection will dispose the underlying connection.</param>
public ProfiledConnection(DbConnection connection, IDbProfiler profiler, bool autoDisposeConnection = true)
{
Init(connection, profiler, autoDisposeConnection);
}
private void Init(DbConnection connection, IDbProfiler profiler, bool autoDisposeConnection)
{
if (connection == null) throw new ArgumentNullException("connection");
AutoDisposeConnection = autoDisposeConnection;
_conn = connection;
_conn.StateChange += StateChangeHandler;
if (profiler != null)
{
_profiler = profiler;
}
}
public ProfiledConnection(IDbConnection connection, IDbProfiler profiler, bool autoDisposeConnection = true)
{
var hasConn = connection as IHasDbConnection;
if (hasConn != null) connection = hasConn.DbConnection;
var dbConn = connection as DbConnection;
if (dbConn == null)
throw new ArgumentException(connection.GetType().FullName + " does not inherit DbConnection");
Init(dbConn, profiler, autoDisposeConnection);
}
#pragma warning disable 1591 // xml doc comments warnings
/// <summary>
/// The underlying, real database connection to your db provider.
/// </summary>
public DbConnection InnerConnection
{
get { return _conn; }
protected set { _conn = value; }
}
public IDbConnection DbConnection
{
get { return _conn; }
}
/// <summary>
/// The current profiler instance; could be null.
/// </summary>
public IDbProfiler Profiler
{
get { return _profiler; }
protected set { _profiler = value; }
}
/// <summary>
/// The raw connection this is wrapping
/// </summary>
public DbConnection WrappedConnection
{
get { return _conn; }
}
protected
#if !NETSTANDARD1_3
override
#endif
bool CanRaiseEvents
{
get { return true; }
}
public override string ConnectionString
{
get { return _conn.ConnectionString; }
set { _conn.ConnectionString = value; }
}
public override int ConnectionTimeout
{
get { return _conn.ConnectionTimeout; }
}
public override string Database
{
get { return _conn.Database; }
}
public override string DataSource
{
get { return _conn.DataSource; }
}
public override string ServerVersion
{
get { return _conn.ServerVersion; }
}
public override ConnectionState State
{
get { return _conn.State; }
}
protected bool AutoDisposeConnection { get; set; }
public override void ChangeDatabase(string databaseName)
{
_conn.ChangeDatabase(databaseName);
}
public override void Close()
{
if (AutoDisposeConnection)
_conn.Close();
}
//public override void EnlistTransaction(System.Transactions.Transaction transaction)
//{
// _conn.EnlistTransaction(transaction);
//}
#if !NETSTANDARD1_3
public override DataTable GetSchema()
{
return _conn.GetSchema();
}
public override DataTable GetSchema(string collectionName)
{
return _conn.GetSchema(collectionName);
}
public override DataTable GetSchema(string collectionName, string[] restrictionValues)
{
return _conn.GetSchema(collectionName, restrictionValues);
}
#endif
public override void Open()
{
if (_conn.State != ConnectionState.Open)
_conn.Open();
}
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
{
return new ProfiledDbTransaction(_conn.BeginTransaction(isolationLevel), this);
}
protected override DbCommand CreateDbCommand()
{
return new ProfiledCommand(_conn.CreateCommand(), this, _profiler);
}
protected override void Dispose(bool disposing)
{
if (disposing && _conn != null)
{
_conn.StateChange -= StateChangeHandler;
if (AutoDisposeConnection)
_conn.Dispose();
}
_conn = null;
_profiler = null;
base.Dispose(disposing);
}
void StateChangeHandler(object sender, StateChangeEventArgs e)
{
OnStateChange(e);
}
}
}
#pragma warning restore 1591 // xml doc comments warnings