Skip to content

Commit db0ab52

Browse files
committed
Makes QueryCommand disposable
1 parent 2d297b4 commit db0ab52

File tree

8 files changed

+59
-26
lines changed

8 files changed

+59
-26
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using Xtensive.Orm.Linq;
44
using Xtensive.Orm.Providers;
@@ -15,16 +15,15 @@ internal class BulkDeleteOperation<T> : QueryOperation<T>
1515

1616
protected override int ExecuteInternal()
1717
{
18-
base.ExecuteInternal();
18+
_ = base.ExecuteInternal();
1919
QueryTranslationResult request = GetRequest(query);
2020
Bindings = request.ParameterBindings.ToList();
2121
if (PrimaryIndexes.Length > 1)
2222
throw new NotImplementedException("Inheritance is not implemented");
2323
SqlDelete delete = SqlDml.Delete(SqlDml.TableRef(PrimaryIndexes[0].Table));
2424
Join(delete, (SqlSelect) request.Query);
25-
QueryCommand command = ToCommand(delete);
26-
int result = command.ExecuteNonQuery();
27-
return result;
25+
using var command = ToCommand(delete);
26+
return command.ExecuteNonQuery();
2827
}
2928

3029
protected override SqlTableRef GetStatementTable(SqlStatement statement)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Linq.Expressions;
@@ -22,7 +22,7 @@ internal class BulkUpdateOperation<T> : QueryOperation<T>
2222

2323
protected override int ExecuteInternal()
2424
{
25-
base.ExecuteInternal();
25+
_ = base.ExecuteInternal();
2626
QueryTranslationResult request = GetRequest(query);
2727
Bindings = request.ParameterBindings.ToList();
2828
if (PrimaryIndexes.Length > 1)
@@ -31,7 +31,7 @@ protected override int ExecuteInternal()
3131
setOperation.Statement = SetStatement.Create(update);
3232
Join(update, (SqlSelect) request.Query);
3333
setOperation.AddValues();
34-
QueryCommand command = ToCommand(update);
34+
using var command = ToCommand(update);
3535
return command.ExecuteNonQuery();
3636
}
3737

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Linq.Expressions;
@@ -25,7 +25,7 @@ protected override int ExecuteInternal()
2525
SqlInsert insert = SqlDml.Insert(SqlDml.TableRef(PrimaryIndexes[0].Table));
2626
setOperation.Statement = SetStatement.Create(insert);
2727
setOperation.AddValues();
28-
QueryCommand command = ToCommand(insert);
28+
using var command = ToCommand(insert);
2929
return command.ExecuteNonQuery();
3030
}
3131

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Data.Common;
44
using System.Linq;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2014 Xtensive LLC.
1+
// Copyright (C) 2014 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Alexey Kulakov
@@ -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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012 Xtensive LLC.
1+
// Copyright (C) 2012 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Denis Krjuchkov
@@ -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: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// Copyright (C) 2012 Xtensive LLC.
1+
// Copyright (C) 2012 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
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)