@@ -23,17 +23,20 @@ private sealed class InitializationSqlExtension
2323
2424 public void ApplyNodeConfiguration ( SqlConnection connection , NodeConfiguration nodeConfiguration )
2525 {
26- if ( nodeConfiguration . ConnectionInfo != null )
26+ if ( nodeConfiguration . ConnectionInfo != null ) {
2727 connection . ConnectionInfo = nodeConfiguration . ConnectionInfo ;
28+ }
2829
29- if ( ! string . IsNullOrEmpty ( nodeConfiguration . ConnectionInitializationSql ) )
30+ if ( ! string . IsNullOrEmpty ( nodeConfiguration . ConnectionInitializationSql ) ) {
3031 SetInitializationSql ( connection , nodeConfiguration . ConnectionInitializationSql ) ;
32+ }
3133 }
3234
3335 public SqlConnection CreateConnection ( Session session )
3436 {
35- if ( isLoggingEnabled )
37+ if ( isLoggingEnabled ) {
3638 SqlLog . Info ( Strings . LogSessionXCreatingConnection , session . ToStringSafely ( ) ) ;
39+ }
3740
3841 SqlConnection connection ;
3942 try {
@@ -44,20 +47,24 @@ public SqlConnection CreateConnection(Session session)
4447 }
4548
4649 var sessionConfiguration = GetConfiguration ( session ) ;
47- if ( sessionConfiguration . ConnectionInfo != null )
50+ if ( sessionConfiguration . ConnectionInfo != null ) {
4851 connection . ConnectionInfo = sessionConfiguration . ConnectionInfo ;
52+ }
53+
4954 connection . CommandTimeout = sessionConfiguration . DefaultCommandTimeout ;
5055
51- if ( ! string . IsNullOrEmpty ( configuration . ConnectionInitializationSql ) )
56+ if ( ! string . IsNullOrEmpty ( configuration . ConnectionInitializationSql ) ) {
5257 SetInitializationSql ( connection , configuration . ConnectionInitializationSql ) ;
58+ }
5359
5460 return connection ;
5561 }
5662
5763 public void OpenConnection ( Session session , SqlConnection connection )
5864 {
59- if ( isLoggingEnabled )
65+ if ( isLoggingEnabled ) {
6066 SqlLog . Info ( Strings . LogSessionXOpeningConnectionY , session . ToStringSafely ( ) , connection . ConnectionInfo ) ;
67+ }
6168
6269 try {
6370 connection . Open ( ) ;
@@ -67,28 +74,32 @@ public void OpenConnection(Session session, SqlConnection connection)
6774 }
6875
6976 var extension = connection . Extensions . Get < InitializationSqlExtension > ( ) ;
70- if ( ! string . IsNullOrEmpty ( extension ? . Script ) )
71- using ( var command = connection . CreateCommand ( extension . Script ) )
77+ if ( ! string . IsNullOrEmpty ( extension ? . Script ) ) {
78+ using ( var command = connection . CreateCommand ( extension . Script ) ) {
7279 ExecuteNonQuery ( session , command ) ;
80+ }
81+ }
7382 }
7483
75- public Task OpenConnectionAsync ( Session session , SqlConnection connection )
76- {
77- return OpenConnectionAsync ( session , connection , CancellationToken . None ) ;
78- }
84+ public Task OpenConnectionAsync ( Session session , SqlConnection connection ) =>
85+ OpenConnectionAsync ( session , connection , CancellationToken . None ) ;
7986
80- public async Task OpenConnectionAsync ( Session session , SqlConnection connection , CancellationToken cancellationToken )
87+ public async Task OpenConnectionAsync ( Session session , SqlConnection connection ,
88+ CancellationToken cancellationToken )
8189 {
82- if ( isLoggingEnabled )
90+ if ( isLoggingEnabled ) {
8391 SqlLog . Info ( Strings . LogSessionXOpeningConnectionY , session . ToStringSafely ( ) , connection . ConnectionInfo ) ;
92+ }
8493
8594 var extension = connection . Extensions . Get < InitializationSqlExtension > ( ) ;
8695
8796 try {
88- if ( ! string . IsNullOrEmpty ( extension ? . Script ) )
97+ if ( ! string . IsNullOrEmpty ( extension ? . Script ) ) {
8998 await connection . OpenAndInitializeAsync ( extension . Script , cancellationToken ) . ConfigureAwait ( false ) ;
90- else
99+ }
100+ else {
91101 await connection . OpenAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
102+ }
92103 }
93104 catch ( OperationCanceledException ) {
94105 throw ;
@@ -100,25 +111,28 @@ public async Task OpenConnectionAsync(Session session, SqlConnection connection,
100111
101112 public void EnsureConnectionIsOpen ( Session session , SqlConnection connection )
102113 {
103- if ( connection . State != ConnectionState . Open )
114+ if ( connection . State != ConnectionState . Open ) {
104115 OpenConnection ( session , connection ) ;
116+ }
105117 }
106118
107119 public async Task EnsureConnectionIsOpenAsync (
108120 Session session , SqlConnection connection , CancellationToken cancellationToken )
109121 {
110- if ( connection . State != ConnectionState . Open ) {
122+ if ( connection . State != ConnectionState . Open ) {
111123 await OpenConnectionAsync ( session , connection , cancellationToken ) . ConfigureAwait ( false ) ;
112124 }
113125 }
114126
115127 public void CloseConnection ( Session session , SqlConnection connection )
116128 {
117- if ( connection . State != ConnectionState . Open )
129+ if ( connection . State != ConnectionState . Open ) {
118130 return ;
131+ }
119132
120- if ( isLoggingEnabled )
133+ if ( isLoggingEnabled ) {
121134 SqlLog . Info ( Strings . LogSessionXClosingConnectionY , session . ToStringSafely ( ) , connection . ConnectionInfo ) ;
135+ }
122136
123137 try {
124138 connection . Close ( ) ;
@@ -130,7 +144,7 @@ public void CloseConnection(Session session, SqlConnection connection)
130144
131145 public async Task CloseConnectionAsync ( Session session , SqlConnection connection )
132146 {
133- if ( connection . State != ConnectionState . Open ) {
147+ if ( connection . State != ConnectionState . Open ) {
134148 return ;
135149 }
136150
@@ -148,8 +162,9 @@ public async Task CloseConnectionAsync(Session session, SqlConnection connection
148162
149163 public void DisposeConnection ( Session session , SqlConnection connection )
150164 {
151- if ( isLoggingEnabled )
165+ if ( isLoggingEnabled ) {
152166 SqlLog . Info ( Strings . LogSessionXDisposingConnection , session . ToStringSafely ( ) ) ;
167+ }
153168
154169 try {
155170 connection . Dispose ( ) ;
@@ -175,11 +190,14 @@ public async Task DisposeConnectionAsync(Session session, SqlConnection connecti
175190
176191 public void BeginTransaction ( Session session , SqlConnection connection , IsolationLevel ? isolationLevel )
177192 {
178- if ( isLoggingEnabled )
179- SqlLog . Info ( Strings . LogSessionXBeginningTransactionWithYIsolationLevel , session . ToStringSafely ( ) , isolationLevel ) ;
193+ if ( isLoggingEnabled ) {
194+ SqlLog . Info ( Strings . LogSessionXBeginningTransactionWithYIsolationLevel , session . ToStringSafely ( ) ,
195+ isolationLevel ) ;
196+ }
180197
181- if ( isolationLevel == null )
198+ if ( isolationLevel == null ) {
182199 isolationLevel = IsolationLevelConverter . Convert ( GetConfiguration ( session ) . DefaultIsolationLevel ) ;
200+ }
183201
184202 try {
185203 connection . BeginTransaction ( isolationLevel . Value ) ;
@@ -191,8 +209,9 @@ public void BeginTransaction(Session session, SqlConnection connection, Isolatio
191209
192210 public void CommitTransaction ( Session session , SqlConnection connection )
193211 {
194- if ( isLoggingEnabled )
212+ if ( isLoggingEnabled ) {
195213 SqlLog . Info ( Strings . LogSessionXCommitTransaction , session . ToStringSafely ( ) ) ;
214+ }
196215
197216 try {
198217 connection . Commit ( ) ;
@@ -204,8 +223,9 @@ public void CommitTransaction(Session session, SqlConnection connection)
204223
205224 public void RollbackTransaction ( Session session , SqlConnection connection )
206225 {
207- if ( isLoggingEnabled )
226+ if ( isLoggingEnabled ) {
208227 SqlLog . Info ( Strings . LogSessionXRollbackTransaction , session . ToStringSafely ( ) ) ;
228+ }
209229
210230 try {
211231 connection . Rollback ( ) ;
@@ -217,11 +237,13 @@ public void RollbackTransaction(Session session, SqlConnection connection)
217237
218238 public void MakeSavepoint ( Session session , SqlConnection connection , string name )
219239 {
220- if ( isLoggingEnabled )
240+ if ( isLoggingEnabled ) {
221241 SqlLog . Info ( Strings . LogSessionXMakeSavepointY , session . ToStringSafely ( ) , name ) ;
242+ }
222243
223- if ( ! hasSavepoints )
244+ if ( ! hasSavepoints ) {
224245 return ; // Driver does not support savepoints, so let's fail later (on rollback)
246+ }
225247
226248 try {
227249 connection . MakeSavepoint ( name ) ;
@@ -233,11 +255,13 @@ public void MakeSavepoint(Session session, SqlConnection connection, string name
233255
234256 public void RollbackToSavepoint ( Session session , SqlConnection connection , string name )
235257 {
236- if ( isLoggingEnabled )
258+ if ( isLoggingEnabled ) {
237259 SqlLog . Info ( Strings . LogSessionXRollbackToSavepointY , session . ToStringSafely ( ) , name ) ;
260+ }
238261
239- if ( ! hasSavepoints )
262+ if ( ! hasSavepoints ) {
240263 throw new NotSupportedException ( Strings . ExCurrentStorageProviderDoesNotSupportSavepoints ) ;
264+ }
241265
242266 try {
243267 connection . RollbackToSavepoint ( name ) ;
@@ -249,8 +273,9 @@ public void RollbackToSavepoint(Session session, SqlConnection connection, strin
249273
250274 public void ReleaseSavepoint ( Session session , SqlConnection connection , string name )
251275 {
252- if ( isLoggingEnabled )
276+ if ( isLoggingEnabled ) {
253277 SqlLog . Info ( Strings . LogSessionXReleaseSavepointY , session . ToStringSafely ( ) , name ) ;
278+ }
254279
255280 try {
256281 connection . ReleaseSavepoint ( name ) ;
@@ -262,67 +287,50 @@ public void ReleaseSavepoint(Session session, SqlConnection connection, string n
262287
263288 #region Sync Execute methods
264289
265- public int ExecuteNonQuery ( Session session , DbCommand command )
266- {
267- return ExecuteCommand ( session , command , c => c . ExecuteNonQuery ( ) ) ;
268- }
290+ public int ExecuteNonQuery ( Session session , DbCommand command ) =>
291+ ExecuteCommand ( session , command , c => c . ExecuteNonQuery ( ) ) ;
269292
270- public object ExecuteScalar ( Session session , DbCommand command )
271- {
272- return ExecuteCommand ( session , command , c => c . ExecuteScalar ( ) ) ;
273- }
293+ public object ExecuteScalar ( Session session , DbCommand command ) =>
294+ ExecuteCommand ( session , command , c => c . ExecuteScalar ( ) ) ;
274295
275- public DbDataReader ExecuteReader ( Session session , DbCommand command )
276- {
277- return ExecuteCommand ( session , command , c => c . ExecuteReader ( ) ) ;
278- }
296+ public DbDataReader ExecuteReader ( Session session , DbCommand command ) =>
297+ ExecuteCommand ( session , command , c => c . ExecuteReader ( ) ) ;
279298
280299 #endregion
281300
282301 #region Async Execute methods
283302
284- public Task < int > ExecuteNonQueryAsync ( Session session , DbCommand command )
285- {
286- return ExecuteCommandAsync ( session , command , CancellationToken . None ,
303+ public Task < int > ExecuteNonQueryAsync ( Session session , DbCommand command ) =>
304+ ExecuteCommandAsync ( session , command , CancellationToken . None ,
287305 ( c , ct ) => c . ExecuteNonQueryAsync ( ct ) ) ;
288- }
289306
290- public Task < int > ExecuteNonQueryAsync ( Session session , DbCommand command , CancellationToken cancellationToken )
291- {
292- return ExecuteCommandAsync ( session , command , cancellationToken ,
307+ public Task < int > ExecuteNonQueryAsync ( Session session , DbCommand command , CancellationToken cancellationToken ) =>
308+ ExecuteCommandAsync ( session , command , cancellationToken ,
293309 ( c , ct ) => c . ExecuteNonQueryAsync ( ct ) ) ;
294- }
295310
296- public Task < object > ExecuteScalarAsync ( Session session , DbCommand command )
297- {
298- return ExecuteCommandAsync ( session , command , CancellationToken . None ,
311+ public Task < object > ExecuteScalarAsync ( Session session , DbCommand command ) =>
312+ ExecuteCommandAsync ( session , command , CancellationToken . None ,
299313 ( c , ct ) => c . ExecuteScalarAsync ( ct ) ) ;
300- }
301314
302- public Task < object > ExecuteScalarAsync ( Session session , DbCommand command , CancellationToken cancellationToken )
303- {
304- return ExecuteCommandAsync ( session , command , cancellationToken ,
315+ public Task < object > ExecuteScalarAsync ( Session session , DbCommand command , CancellationToken cancellationToken ) =>
316+ ExecuteCommandAsync ( session , command , cancellationToken ,
305317 ( c , ct ) => c . ExecuteScalarAsync ( ct ) ) ;
306- }
307318
308- public Task < DbDataReader > ExecuteReaderAsync ( Session session , DbCommand command )
309- {
310- return ExecuteCommandAsync ( session , command , CancellationToken . None ,
319+ public Task < DbDataReader > ExecuteReaderAsync ( Session session , DbCommand command ) =>
320+ ExecuteCommandAsync ( session , command , CancellationToken . None ,
311321 ( c , ct ) => c . ExecuteReaderAsync ( ct ) ) ;
312- }
313322
314- public Task < DbDataReader > ExecuteReaderAsync ( Session session , DbCommand command , CancellationToken cancellationToken )
315- {
316- return ExecuteCommandAsync ( session , command , cancellationToken ,
323+ public Task < DbDataReader > ExecuteReaderAsync ( Session session , DbCommand command , CancellationToken cancellationToken ) =>
324+ ExecuteCommandAsync ( session , command , cancellationToken ,
317325 ( c , ct ) => c . ExecuteReaderAsync ( ct ) ) ;
318- }
319326
320327 #endregion
321328
322329 private TResult ExecuteCommand < TResult > ( Session session , DbCommand command , Func < DbCommand , TResult > action )
323330 {
324- if ( isLoggingEnabled )
331+ if ( isLoggingEnabled ) {
325332 SqlLog . Info ( Strings . LogSessionXQueryY , session . ToStringSafely ( ) , command . ToHumanReadableString ( ) ) ;
333+ }
326334
327335 session ? . Events . NotifyDbCommandExecuting ( command ) ;
328336
@@ -341,10 +349,12 @@ private TResult ExecuteCommand<TResult>(Session session, DbCommand command, Func
341349 return result ;
342350 }
343351
344- private async Task < TResult > ExecuteCommandAsync < TResult > ( Session session , DbCommand command , CancellationToken cancellationToken , Func < DbCommand , CancellationToken , Task < TResult > > action )
352+ private async Task < TResult > ExecuteCommandAsync < TResult > ( Session session , DbCommand command ,
353+ CancellationToken cancellationToken , Func < DbCommand , CancellationToken , Task < TResult > > action )
345354 {
346- if ( isLoggingEnabled )
355+ if ( isLoggingEnabled ) {
347356 SqlLog . Info ( Strings . LogSessionXQueryY , session . ToStringSafely ( ) , command . ToHumanReadableString ( ) ) ;
357+ }
348358
349359 cancellationToken . ThrowIfCancellationRequested ( ) ;
350360 session ? . Events . NotifyDbCommandExecuting ( command ) ;
@@ -370,16 +380,15 @@ private async Task<TResult> ExecuteCommandAsync<TResult>(Session session, DbComm
370380 private void SetInitializationSql ( SqlConnection connection , string script )
371381 {
372382 var extension = connection . Extensions . Get < InitializationSqlExtension > ( ) ;
373- if ( extension == null ) {
383+ if ( extension == null ) {
374384 extension = new InitializationSqlExtension ( ) ;
375385 connection . Extensions . Set ( extension ) ;
376386 }
387+
377388 extension . Script = script ;
378389 }
379390
380- private SessionConfiguration GetConfiguration ( Session session )
381- {
382- return session != null ? session . Configuration : configuration . Sessions . System ;
383- }
391+ private SessionConfiguration GetConfiguration ( Session session ) =>
392+ session != null ? session . Configuration : configuration . Sessions . System ;
384393 }
385394}
0 commit comments