2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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         /// <summary>\r
54         /// Gets the command.\r
55         /// </summary>\r
56         /// <value>The command.</value>\r
57         public IDbCommand Command\r
58         {\r
59             get\r
60             {\r
61                 return _command;\r
62             }\r
63         }\r
64 \r
65 \r
66         /// <summary>\r
67         /// Commits current transaction.\r
68         /// </summary>\r
69         public virtual void Dispose()\r
70         {\r
71             Command.Dispose();\r
72             if (_transaction != null)\r
73                 _transaction.Dispose();\r
74             _connection.Dispose();\r
75         }\r
76 \r
77         /// <summary>\r
78         /// Commits the current transaction.\r
79         /// throws NRE if _transaction is null. Behavior is intentional.\r
80         /// </summary>\r
81         public void Commit()\r
82         {\r
83             // TODO: do not commit if participating in a higher transaction\r
84             _transaction.Commit();\r
85         }\r
86 \r
87         public TransactionalCommand(string commandText, bool createTransaction, DataContext dataContext)\r
88         {\r
89             // TODO: check if all this stuff is necessary\r
90             // the OpenConnection() checks that the connection is already open\r
91             // TODO: see if we can move this here (in theory the final DataContext shouldn't use)\r
92             _connection = dataContext.DatabaseContext.OpenConnection();\r
93             // the transaction is optional\r
94             if (createTransaction)\r
95                 _transaction = dataContext.DatabaseContext.Transaction();\r
96             _command = dataContext.DatabaseContext.CreateCommand();\r
97             Command.CommandText = commandText;\r
98             if (createTransaction)\r
99                 Command.Transaction = _transaction.Transaction;\r
100         }\r
101 \r
102     }\r
103 }\r