2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Data / Linq / Database / Implementation / TransactionalCommand.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 // \r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 // \r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Data;\r
29 \r
30 #if MONO_STRICT\r
31 using System.Data.Linq;\r
32 #endif\r
33 \r
34 using DbLinq.Data.Linq.Sql;\r
35 \r
36 namespace DbLinq.Data.Linq.Database.Implementation\r
37 {\r
38     /// <summary>\r
39     /// Transactional command\r
40     /// </summary>\r
41 #if !MONO_STRICT\r
42     public\r
43 #endif\r
44     class TransactionalCommand : ITransactionalCommand\r
45     {\r
46         private readonly IDisposable _connection;\r
47         /// <summary>\r
48         /// Ambient transaction\r
49         /// </summary>\r
50         private readonly IDatabaseTransaction _transaction;\r
51 \r
52         private readonly IDbCommand _command;\r
53 \r
54         private readonly bool haveHigherTransaction;\r
55 \r
56         /// <summary>\r
57         /// Gets the command.\r
58         /// </summary>\r
59         /// <value>The command.</value>\r
60         public IDbCommand Command\r
61         {\r
62             get\r
63             {\r
64                 return _command;\r
65             }\r
66         }\r
67 \r
68 \r
69         /// <summary>\r
70         /// Commits current transaction.\r
71         /// </summary>\r
72         public virtual void Dispose()\r
73         {\r
74             Command.Dispose();\r
75             if (_transaction != null)\r
76                 _transaction.Dispose();\r
77             _connection.Dispose();\r
78         }\r
79 \r
80         /// <summary>\r
81         /// Commits the current transaction.\r
82         /// throws NRE if _transaction is null. Behavior is intentional.\r
83         /// </summary>\r
84         public void Commit()\r
85         {\r
86             // TODO: do not commit if participating in a higher transaction\r
87             if (!haveHigherTransaction)\r
88                 _transaction.Commit();\r
89         }\r
90 \r
91         public TransactionalCommand(string commandText, bool createTransaction, DataContext dataContext)\r
92         {\r
93             // TODO: check if all this stuff is necessary\r
94             // the OpenConnection() checks that the connection is already open\r
95             // TODO: see if we can move this here (in theory the final DataContext shouldn't use)\r
96             _connection = dataContext.DatabaseContext.OpenConnection();\r
97                 \r
98             _command = dataContext.DatabaseContext.CreateCommand();\r
99             haveHigherTransaction = dataContext.Transaction != null;\r
100             // the transaction is optional\r
101             if (createTransaction && !haveHigherTransaction)\r
102             {\r
103                 _transaction = dataContext.DatabaseContext.Transaction();\r
104                 _command.Transaction = _transaction.Transaction;\r
105             }\r
106             else\r
107                 _command.Transaction = dataContext.Transaction;\r
108             Command.CommandText = commandText;\r
109         }\r
110 \r
111     }\r
112 }\r