Skip to content

Commit e14a746

Browse files
authored
Merge pull request #74 from DataObjects-NET/disposable-querycommand
Disposable QueryCommand
2 parents 2d297b4 + ddee18c commit e14a746

File tree

9 files changed

+82
-32
lines changed

9 files changed

+82
-32
lines changed

ChangeLog/6.0.3_Z_Final.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[main] Provider for MS SQL Server was branched out into a separate project
22
[main] Events for changes cancelling ported from 5.0.x
3+
[main] QueryCommand now implements IDisposable interface
34
[main] TupleDescriptor no longer implements IList<T>
45
[main] TupleDescriptor.GetCommonPartLength(TupleDescriptor) method was removed
56
[main] TupleDescriptor.IsValueType(int) method was removed

Extensions/Xtensive.Orm.BulkOperations/Internals/BulkDeleteOperation.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
// Copyright (C) 2019-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
26
using System.Linq;
37
using Xtensive.Orm.Linq;
48
using Xtensive.Orm.Providers;
@@ -15,16 +19,15 @@ internal class BulkDeleteOperation<T> : QueryOperation<T>
1519

1620
protected override int ExecuteInternal()
1721
{
18-
base.ExecuteInternal();
22+
_ = base.ExecuteInternal();
1923
QueryTranslationResult request = GetRequest(query);
2024
Bindings = request.ParameterBindings.ToList();
2125
if (PrimaryIndexes.Length > 1)
2226
throw new NotImplementedException("Inheritance is not implemented");
2327
SqlDelete delete = SqlDml.Delete(SqlDml.TableRef(PrimaryIndexes[0].Table));
2428
Join(delete, (SqlSelect) request.Query);
25-
QueryCommand command = ToCommand(delete);
26-
int result = command.ExecuteNonQuery();
27-
return result;
29+
using var command = ToCommand(delete);
30+
return command.ExecuteNonQuery();
2831
}
2932

3033
protected override SqlTableRef GetStatementTable(SqlStatement statement)

Extensions/Xtensive.Orm.BulkOperations/Internals/BulkUpdateOperation.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
// Copyright (C) 2007-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
26
using System.Collections.Generic;
37
using System.Linq;
48
using System.Linq.Expressions;
@@ -22,7 +26,7 @@ internal class BulkUpdateOperation<T> : QueryOperation<T>
2226

2327
protected override int ExecuteInternal()
2428
{
25-
base.ExecuteInternal();
29+
_ = base.ExecuteInternal();
2630
QueryTranslationResult request = GetRequest(query);
2731
Bindings = request.ParameterBindings.ToList();
2832
if (PrimaryIndexes.Length > 1)
@@ -31,7 +35,7 @@ protected override int ExecuteInternal()
3135
setOperation.Statement = SetStatement.Create(update);
3236
Join(update, (SqlSelect) request.Query);
3337
setOperation.AddValues();
34-
QueryCommand command = ToCommand(update);
38+
using var command = ToCommand(update);
3539
return command.ExecuteNonQuery();
3640
}
3741

Extensions/Xtensive.Orm.BulkOperations/Internals/InsertOperation.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
// Copyright (C) 2019-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
26
using System.Collections.Generic;
37
using System.Linq;
48
using System.Linq.Expressions;
@@ -25,7 +29,7 @@ protected override int ExecuteInternal()
2529
SqlInsert insert = SqlDml.Insert(SqlDml.TableRef(PrimaryIndexes[0].Table));
2630
setOperation.Statement = SetStatement.Create(insert);
2731
setOperation.AddValues();
28-
QueryCommand command = ToCommand(insert);
32+
using var command = ToCommand(insert);
2933
return command.ExecuteNonQuery();
3034
}
3135

Extensions/Xtensive.Orm.BulkOperations/Internals/Operation.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
// Copyright (C) 2019-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
26
using System.Collections.Generic;
37
using System.Data.Common;
48
using System.Linq;

Orm/Xtensive.Orm.Tests/Issues/IssueJira0571_MySqlKeyGenerationProblem.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2014 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2015-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Alexey Kulakov
55
// Created: 2015.02.03
66

@@ -138,7 +138,8 @@ private void CheckSchemaGenerators(Schema schema, string generatorName, Session
138138
var query = GetQuery(generatorTable);
139139
var queryBuilder = GetQueryBuilder(session);
140140
var queryCompilationResult = queryBuilder.CompileQuery(query);
141-
var command = queryBuilder.CreateCommand(queryBuilder.CreateRequest(queryCompilationResult, Enumerable.Empty<Services.QueryParameterBinding>()));
141+
var queryRequest = queryBuilder.CreateRequest(queryCompilationResult, Enumerable.Empty<Services.QueryParameterBinding>())
142+
using var command = queryBuilder.CreateCommand(queryRequest);
142143
var rowCount = command.ExecuteScalar();
143144
Assert.AreEqual(expectedValue, rowCount);
144145
}

Orm/Xtensive.Orm.Tests/Storage/QueryBuilderTest.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2012 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2012-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2012.02.27
66

@@ -68,8 +68,10 @@ public void ModifyQueryTest()
6868
var command = builder.CreateCommand(request);
6969
Assert.That(command, Is.Not.Null);
7070

71-
var result = Convert.ToInt32(command.ExecuteScalar());
72-
Assert.That(result, Is.EqualTo(1));
71+
using (command) {
72+
var result = Convert.ToInt32(command.ExecuteScalar());
73+
Assert.That(result, Is.EqualTo(1));
74+
}
7375

7476
tx.Complete();
7577
}
@@ -83,20 +85,22 @@ public void ComposeQuery()
8385
var builder = session.Services.Get<QueryBuilder>();
8486
Assert.That(builder, Is.Not.Null);
8587

86-
var binding = builder.CreateParameterBinding(typeof (int), () => 43);
88+
var binding = builder.CreateParameterBinding(typeof(int), () => 43);
8789
var select = SqlDml.Select(binding.ParameterReference);
8890

8991
var compiled = builder.CompileQuery(select);
9092
Assert.That(compiled, Is.Not.Null);
9193

92-
var request = builder.CreateRequest(compiled, new[] {binding});
94+
var request = builder.CreateRequest(compiled, new[] { binding });
9395
Assert.That(request, Is.Not.Null);
9496

9597
var command = builder.CreateCommand(request);
9698
Assert.That(command, Is.Not.Null);
9799

98-
var result = Convert.ToInt32(command.ExecuteScalar());
99-
Assert.That(result, Is.EqualTo(43));
100+
using (command) {
101+
var result = Convert.ToInt32(command.ExecuteScalar());
102+
Assert.That(result, Is.EqualTo(43));
103+
}
100104

101105
tx.Complete();
102106
}

Orm/Xtensive.Orm.Tests/Upgrade/SchemaSharing/QueryBuilder/Model.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2017 Xtensive LLC.
1+
// Copyright (C) 2017 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Alexey Kulakov
@@ -316,8 +316,7 @@ private object ExecuteScalar(Services.QueryBuilder queryBuilder, ISqlCompileUnit
316316
{
317317
var commandtext = queryBuilder.CompileQuery(query);
318318
var request = queryBuilder.CreateRequest(commandtext, Enumerable.Empty<Services.QueryParameterBinding>());
319-
var command = queryBuilder.CreateCommand(request);
320-
319+
using var command = queryBuilder.CreateCommand(request);
321320
return command.ExecuteScalar();
322321
}
323322

Orm/Xtensive.Orm/Orm/Services/QueryBuilding/QueryCommand.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// Copyright (C) 2012 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2012-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2012.02.27
66

7+
using System;
78
using System.Data.Common;
9+
using System.Runtime.CompilerServices;
810
using Xtensive.Orm.Providers;
911

1012
namespace Xtensive.Orm.Services
@@ -14,16 +16,24 @@ namespace Xtensive.Orm.Services
1416
/// Unlike <see cref="DbCommand"/> this type is aware of <see cref="Session.Events"/>
1517
/// and does all nessesary logging of executed SQL.
1618
/// </summary>
17-
public sealed class QueryCommand
19+
public sealed class QueryCommand : IDisposable
1820
{
1921
private readonly StorageDriver driver;
2022
private readonly Session session;
2123
private readonly DbCommand realCommand;
2224

25+
private bool disposed;
26+
2327
/// <summary>
2428
/// Gets SQL query to execute.
2529
/// </summary>
26-
public string CommandText { get { return realCommand.CommandText; } }
30+
public string CommandText
31+
{
32+
get {
33+
EnsureNotDisposed();
34+
return realCommand.CommandText;
35+
}
36+
}
2737

2838
/// <summary>
2939
/// Executes query and returns <see cref="DbDataReader"/>
@@ -41,6 +51,7 @@ public DbDataReader ExecuteReader()
4151
/// <returns>Number of affected rows.</returns>
4252
public int ExecuteNonQuery()
4353
{
54+
EnsureNotDisposed();
4455
return driver.ExecuteNonQuery(session, realCommand);
4556
}
4657

@@ -50,9 +61,28 @@ public int ExecuteNonQuery()
5061
/// <returns>Scalar result of query.</returns>
5162
public object ExecuteScalar()
5263
{
64+
EnsureNotDisposed();
5365
return driver.ExecuteScalar(session, realCommand);
5466
}
5567

68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
69+
private void EnsureNotDisposed()
70+
{
71+
if (disposed) {
72+
throw new ObjectDisposedException(null);
73+
}
74+
}
75+
76+
/// <inheritdoc/>
77+
public void Dispose()
78+
{
79+
if (disposed) {
80+
return;
81+
}
82+
disposed = true;
83+
realCommand?.Dispose();
84+
}
85+
5686
// Constructors
5787

5888
internal QueryCommand(StorageDriver driver, Session session, DbCommand realCommand)

0 commit comments

Comments
 (0)