Skip to content

Commit 0218ae3

Browse files
committed
Improved: explain plan uses user connection
Therefore respects current session parameters
1 parent e366af4 commit 0218ae3

File tree

5 files changed

+92
-70
lines changed

5 files changed

+92
-70
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SQLPad 0.4.0.372
1+
SQLPad 0.4.0.373
22
================
33

44
SQLPad is an experimental SQL editor focused to quick and comfortable work.

SqlPad.Oracle/DatabaseConnection/OracleConnectionAdapter.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ namespace SqlPad.Oracle.DatabaseConnection
2929
public class OracleConnectionAdapter : OracleConnectionAdapterBase
3030
{
3131
private const string ModuleNameSqlPadDatabaseModelBase = "Database model";
32-
private const string UserCommand = "User command";
3332

3433
private static int _counter;
3534

@@ -285,7 +284,23 @@ public override async Task<ExecutionPlanItemCollection> ExplainPlanAsync(Stateme
285284
var planKey = Convert.ToString(DateTime.UtcNow.Ticks);
286285
var explainPlanDataProvider = new ExplainPlanDataProvider(executionModel.StatementText, planKey, targetTable);
287286

288-
await OracleDatabaseModel.UpdateModelAsync(_databaseModel.ConnectionString.ConnectionString, _currentSchema, false, cancellationToken, explainPlanDataProvider.CreateExplainPlanUpdater, explainPlanDataProvider.LoadExplainPlanUpdater);
287+
_isExecuting = true;
288+
289+
try
290+
{
291+
if (!HasActiveTransaction && _userTransaction != null)
292+
{
293+
await _userTransaction.RollbackAsynchronous();
294+
DisposeUserTransaction();
295+
}
296+
297+
await EnsureUserConnectionOpen(cancellationToken);
298+
await OracleDatabaseModel.UpdateModelAsync(_userConnection, _currentSchema, false, cancellationToken, explainPlanDataProvider.CreateExplainPlanUpdater, explainPlanDataProvider.LoadExplainPlanUpdater);
299+
}
300+
finally
301+
{
302+
_isExecuting = false;
303+
}
289304

290305
return explainPlanDataProvider.ItemCollection;
291306
}
@@ -421,11 +436,13 @@ private void DisposeUserTransaction()
421436
{
422437
_userTransactionInfo = null;
423438

424-
if (_userTransaction != null)
439+
if (_userTransaction == null)
425440
{
426-
_userTransaction.Dispose();
427-
_userTransaction = null;
441+
return;
428442
}
443+
444+
_userTransaction.Dispose();
445+
_userTransaction = null;
429446
}
430447

431448
private void DisposeUserConnection()
@@ -741,7 +758,7 @@ private async Task<StatementExecutionBatchResult> ExecuteUserStatementAsync(Stat
741758
await _databaseModel.UpdateModelAsync(true, cancellationToken, _executionStatisticsDataProvider.SessionBeginExecutionStatisticsDataProvider);
742759
}
743760

744-
_userConnection.ActionName = UserCommand;
761+
_userConnection.ActionName = "User command";
745762

746763
foreach (var executionModel in batchExecutionModel.Statements)
747764
{

SqlPad.Oracle/DatabaseConnection/OracleDatabaseModel.cs

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -421,86 +421,91 @@ internal Task UpdateModelAsync(bool suppressException, CancellationToken cancell
421421

422422
internal static async Task UpdateModelAsync(string connectionString, string currentSchema, bool suppressException, CancellationToken cancellationToken, params IModelDataProvider[] updaters)
423423
{
424-
using (var connection = new OracleConnection())
424+
using (var connection = new OracleConnection { ConnectionString = connectionString })
425425
{
426-
using (var command = connection.CreateCommand())
427-
{
428-
command.BindByName = true;
426+
await UpdateModelAsync(connection, currentSchema, suppressException, cancellationToken, updaters);
427+
await connection.CloseAsynchronous(cancellationToken);
428+
}
429+
}
430+
431+
internal static async Task UpdateModelAsync(OracleConnection connection, string currentSchema, bool suppressException, CancellationToken cancellationToken, params IModelDataProvider[] updaters)
432+
{
433+
using (var command = connection.CreateCommand())
434+
{
435+
command.BindByName = true;
429436

430-
OracleTransaction transaction = null;
437+
OracleTransaction transaction = null;
431438

432-
try
439+
try
440+
{
441+
foreach (var updater in updaters)
433442
{
434-
foreach (var updater in updaters)
435-
{
436-
command.ResetParametersToAvoidOdacBug();
437-
command.CommandText = String.Empty;
438-
command.CommandType = CommandType.Text;
439-
updater.InitializeCommand(command);
443+
command.ResetParametersToAvoidOdacBug();
444+
command.CommandText = String.Empty;
445+
command.CommandType = CommandType.Text;
446+
updater.InitializeCommand(command);
440447

441-
try
448+
try
449+
{
450+
if (updater.IsValid)
442451
{
443-
if (updater.IsValid)
452+
if (connection.State == ConnectionState.Closed)
444453
{
445-
if (await connection.EnsureConnectionOpen(connectionString, cancellationToken))
446-
{
447-
connection.ModuleName = "SQLPad backround";
448-
connection.ActionName = "Model data provider";
454+
await connection.OpenAsynchronous(cancellationToken);
455+
connection.ModuleName = "SQLPad backround";
456+
connection.ActionName = "Model data provider";
449457

450-
if (!String.IsNullOrEmpty(currentSchema))
458+
if (!String.IsNullOrEmpty(currentSchema))
459+
{
460+
using (var setSchemaCommand = connection.CreateCommand())
451461
{
452-
using (var setSchemaCommand = connection.CreateCommand())
453-
{
454-
await setSchemaCommand.SetSchema(currentSchema, cancellationToken);
455-
}
462+
await setSchemaCommand.SetSchema(currentSchema, cancellationToken);
456463
}
457-
458-
transaction = connection.BeginTransaction();
459464
}
460465

461-
if (updater.HasScalarResult)
466+
transaction = connection.BeginTransaction();
467+
}
468+
469+
if (updater.HasScalarResult)
470+
{
471+
var result = await command.ExecuteScalarAsynchronous(cancellationToken);
472+
updater.MapScalarValue(result);
473+
}
474+
else
475+
{
476+
using (var reader = await command.ExecuteReaderAsynchronous(CommandBehavior.Default, cancellationToken))
462477
{
463-
var result = await command.ExecuteScalarAsynchronous(cancellationToken);
464-
updater.MapScalarValue(result);
465-
}
466-
else
467-
{
468-
using (var reader = await command.ExecuteReaderAsynchronous(CommandBehavior.Default, cancellationToken))
469-
{
470-
await updater.MapReaderData(reader, cancellationToken);
471-
}
478+
await updater.MapReaderData(reader, cancellationToken);
472479
}
473480
}
474481
}
475-
catch (OracleException exception)
482+
}
483+
catch (OracleException exception)
484+
{
485+
if (exception.Number == (int)OracleErrorCode.UserInvokedCancellation)
476486
{
477-
if (exception.Number == (int)OracleErrorCode.UserInvokedCancellation)
478-
{
479-
break;
480-
}
481-
482-
throw;
487+
break;
483488
}
484-
}
485-
}
486-
catch (Exception e)
487-
{
488-
Trace.WriteLine($"Update model failed: {e}");
489489

490-
if (!suppressException)
491-
{
492490
throw;
493491
}
494492
}
495-
finally
496-
{
497-
if (transaction != null)
498-
{
499-
await transaction.RollbackAsynchronous();
500-
transaction.Dispose();
501-
}
493+
}
494+
catch (Exception e)
495+
{
496+
Trace.WriteLine($"Update model failed: {e}");
502497

503-
await connection.CloseAsynchronous(cancellationToken);
498+
if (!suppressException)
499+
{
500+
throw;
501+
}
502+
}
503+
finally
504+
{
505+
if (transaction != null)
506+
{
507+
await transaction.RollbackAsynchronous();
508+
transaction.Dispose();
504509
}
505510
}
506511
}

SqlPad.Oracle/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// You can specify all the values or you can default the Build and Revision Numbers
3838
// by using the '*' as shown below:
3939
// [assembly: AssemblyVersion("1.0.*")]
40-
[assembly: AssemblyVersion("0.4.0.372")]
41-
[assembly: AssemblyFileVersion("0.4.0.372")]
42-
[assembly: AssemblyBuildInfo("1e378d9", "2016-07-03 21:02:37")]
40+
[assembly: AssemblyVersion("0.4.0.373")]
41+
[assembly: AssemblyFileVersion("0.4.0.373")]
42+
[assembly: AssemblyBuildInfo("e366af4", "2016-07-03 21:55:39")]
4343
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

SqlPad/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@
5454
// You can specify all the values or you can default the Build and Revision Numbers
5555
// by using the '*' as shown below:
5656
// [assembly: AssemblyVersion("1.0.*")]
57-
[assembly: AssemblyVersion("0.4.0.372")]
58-
[assembly: AssemblyFileVersion("0.4.0.372")]
59-
[assembly: AssemblyBuildInfo("1e378d9", "2016-07-03 21:02:37")]
57+
[assembly: AssemblyVersion("0.4.0.373")]
58+
[assembly: AssemblyFileVersion("0.4.0.373")]
59+
[assembly: AssemblyBuildInfo("e366af4", "2016-07-03 21:55:39")]

0 commit comments

Comments
 (0)