Datei: NDODLL/SqlPassThroughHandler.cs

Last Commit (e34b4f0)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
 
 
 
 
23 using System;
24 using System.Collections.Generic;
25 using System.Linq;
26 using System.Text;
27 using NDOInterfaces;
28 using NDO.Mapping;
29 using System.Data;
30 using Microsoft. Extensions. Logging;
31 using Microsoft.Extensions.DependencyInjection;
32
33 namespace NDO
34 {
35 ····internal class SqlPassThroughHandler : ISqlPassThroughHandler
36 ····{
37 ········PersistenceManager pm;
38 ········Connection connection;
39 ········TransactionMode oldTransactionMode;
40 ········bool forcedTransactionMode = false;
 
41
42 ········public SqlPassThroughHandler(PersistenceManager pm, Connection connection)
43 ········{
44 ············this.pm = pm;
45 ············this.connection = connection;
 
46 ········}
47
48 ········public void BeginTransaction()
49 ········{
50 ············this.forcedTransactionMode = true;
51 ············this.oldTransactionMode = this.pm.TransactionMode;
52 ············this.pm.TransactionMode = TransactionMode.Pessimistic;
53 ············this.pm.TransactionScope.CheckTransaction();
54 ········}
55
56 ········public void CommitTransaction()
57 ········{
58 ············this.pm.TransactionScope.Complete();
59 ············if (this.forcedTransactionMode)
60 ················this.pm.TransactionMode = this.oldTransactionMode;
61 ············this.forcedTransactionMode = false;
62 ········}
63
64 ········/// <summary>
65 ········/// Executes the given command.
66 ········/// </summary>
67 ········/// <param name="command">A SQL command string</param>
68 ········/// <param name="returnReader">Determines, if the command should return a reader</param>
69 ········/// <param name="parameters">Optional command parameters</param>
70 ········/// <returns>A DataReader object which may be empty.</returns>
71 ········/// <remarks>
72 ········/// The command string must be formatted for the given database.
73 ········/// The names of the parameters in the query must have the names @px,
74 ········/// where x is the index of the parameter in the parameters array.
75 ········/// </remarks>
76 ········public IDataReader Execute( string command, bool returnReader = false, params object[] parameters )
77 ········{
78 ············this.pm.TransactionScope.CheckTransaction();
79 this. pm. ServiceProvider. GetService<ILogger<SqlPassThroughHandler>>( ) . LogDebug( command ) ;
80
81 ············IProvider provider = this.pm.NDOMapping.GetProvider( this.connection );
82
83 var dbConnection = this. pm. TransactionScope. GetConnection( this. connection. ID, ( ) => provider. NewConnection( this. connection. Name) ) ;
 
 
 
 
 
 
 
 
 
 
 
84
85 ············IDbCommand cmd = provider.NewSqlCommand( dbConnection );
86 ············cmd.CommandText = command;
87 ············var tx = this.pm.TransactionScope.GetTransaction(this.connection.ID);
88 ············if (tx != null)
89 ················cmd.Transaction = tx;
90
91 ············int pcount = 0;
92 ············foreach (var par in parameters)
93 ············{
94 ················var dbpar = cmd.CreateParameter();
95 ················dbpar.ParameterName = $"@p{pcount++}";
96 ················dbpar.Value = par ?? DBNull.Value;
97 ················cmd.Parameters.Add( dbpar );
98 ············}
99
100 ············if (dbConnection.State == ConnectionState.Closed)
101 ················dbConnection.Open();
102
103 ············if (returnReader)
104 ················return cmd.ExecuteReader();
105
106 ············cmd.ExecuteNonQuery();
107 ············return null;
108 ········}
109
110 ········public IProvider Provider
111 ········{
112 ············get
113 ············{
114 ················return this.pm.NDOMapping.GetProvider( this.connection );
115 ············}
116 ········}
117
118 ········public void Dispose()
119 ········{
120 ············this.pm.TransactionScope.Dispose();
121 ············if (this.forcedTransactionMode)
122 ············{
123 ················this.pm.TransactionMode = this.oldTransactionMode;
124 ················this.forcedTransactionMode = false;
125 ············}
126 ········}
127 ····}
128 }
129
New Commit (14cacf2)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using Microsoft.Extensions.DependencyInjection;
24 using Microsoft.Extensions.Logging;
25 using NDO.Mapping;
26 using NDOInterfaces;
27 using System;
 
 
 
 
 
28 using System.Data;
29 using System. Transactions;
 
30
31 namespace NDO
32 {
33 ····internal class SqlPassThroughHandler : ISqlPassThroughHandler
34 ····{
35 ········PersistenceManager pm;
36 ········Connection connection;
37 ········TransactionMode oldTransactionMode;
38 ········bool forcedTransactionMode = false;
39 ········ILogger logger;
40
41 ········public SqlPassThroughHandler(PersistenceManager pm, Connection connection)
42 ········{
43 ············this.pm = pm;
44 ············this.connection = connection;
45 ············this.logger = pm.ServiceProvider.GetService<ILogger<SqlPassThroughHandler>>();
46 ········}
47
48 ········public void BeginTransaction()
49 ········{
50 ············this.forcedTransactionMode = true;
51 ············this.oldTransactionMode = this.pm.TransactionMode;
52 ············this.pm.TransactionMode = TransactionMode.Pessimistic;
53 ············this.pm.TransactionScope.CheckTransaction();
54 ········}
55
56 ········public void CommitTransaction()
57 ········{
58 ············this.pm.TransactionScope.Complete();
59 ············if (this.forcedTransactionMode)
60 ················this.pm.TransactionMode = this.oldTransactionMode;
61 ············this.forcedTransactionMode = false;
62 ········}
63
64 ········/// <summary>
65 ········/// Executes the given command.
66 ········/// </summary>
67 ········/// <param name="command">A SQL command string</param>
68 ········/// <param name="returnReader">Determines, if the command should return a reader</param>
69 ········/// <param name="parameters">Optional command parameters</param>
70 ········/// <returns>A DataReader object which may be empty.</returns>
71 ········/// <remarks>
72 ········/// The command string must be formatted for the given database.
73 ········/// The names of the parameters in the query must have the names @px,
74 ········/// where x is the index of the parameter in the parameters array.
75 ········/// </remarks>
76 ········public IDataReader Execute( string command, bool returnReader = false, params object[] parameters )
77 ········{
78 ············this.pm.TransactionScope.CheckTransaction();
79 this. logger. LogDebug( $"SqlPassThroughHandler: { command} " ) ;
80
81 ············IProvider provider = this.pm.NDOMapping.GetProvider( this.connection );
82
83 var ndoConn = this. connection;
84 ············// This code is identical to the code in PersistenceManager.
85 ············var dbConnection = this.pm.TransactionScope.GetConnection(ndoConn, () =>
86 ············{
87 ················IProvider p = ndoConn.Parent.GetProvider( ndoConn );
88 ················string connStr = this.pm.OnNewConnection( ndoConn );
89 ················var connection = p.NewConnection( connStr );
90 ················if (connection == null)
91 ····················throw new NDOException( 119, $"Can't construct connection for {connStr}. The provider returns null." );
92 ················this.logger.LogDebug( $"Creating a connection object for '{ndoConn.DisplayName}'" );
93 ················return connection;
94 ············} );
95
96 ············IDbCommand cmd = provider.NewSqlCommand( dbConnection );
97 ············cmd.CommandText = command;
98 ············var tx = this.pm.TransactionScope.GetTransaction(this.connection.ID);
99 ············if (tx != null)
100 ················cmd.Transaction = tx;
101
102 ············int pcount = 0;
103 ············foreach (var par in parameters)
104 ············{
105 ················var dbpar = cmd.CreateParameter();
106 ················dbpar.ParameterName = $"@p{pcount++}";
107 ················dbpar.Value = par ?? DBNull.Value;
108 ················cmd.Parameters.Add( dbpar );
109 ············}
110
111 ············if (dbConnection.State == ConnectionState.Closed)
112 ················dbConnection.Open();
113
114 ············if (returnReader)
115 ················return cmd.ExecuteReader();
116
117 ············cmd.ExecuteNonQuery();
118 ············return null;
119 ········}
120
121 ········public IProvider Provider
122 ········{
123 ············get
124 ············{
125 ················return this.pm.NDOMapping.GetProvider( this.connection );
126 ············}
127 ········}
128
129 ········public void Dispose()
130 ········{
131 ············this.pm.TransactionScope.Dispose();
132 ············if (this.forcedTransactionMode)
133 ············{
134 ················this.pm.TransactionMode = this.oldTransactionMode;
135 ················this.forcedTransactionMode = false;
136 ············}
137 ········}
138 ····}
139 }
140