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 ;
78using System . Data . Common ;
9+ using System . Runtime . CompilerServices ;
810using Xtensive . Orm . Providers ;
911
1012namespace 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