2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Data / Linq / Database / Implementation / DbLinqCommand.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 using System;\r
27 using System.Data;\r
28 \r
29 #if MONO_STRICT\r
30 using System.Data.Linq.Sql;\r
31 using System.Data.Linq;\r
32 #else\r
33 using DbLinq.Data.Linq.Sql;\r
34 #endif\r
35 \r
36 namespace DbLinq.Data.Linq.Database.Implementation\r
37 {\r
38     public class DbLinqCommand : IDbLinqCommand\r
39     {\r
40         private IDisposable _connection;\r
41         private IDatabaseTransaction _transaction;\r
42         private readonly IDbCommand cmd;\r
43         public IDbCommand Command\r
44         {\r
45             get\r
46             {\r
47                 return cmd;\r
48             }\r
49         }\r
50 \r
51 \r
52         public virtual void Dispose()\r
53         {\r
54             Command.Dispose();\r
55             if (_transaction != null)\r
56                 _transaction.Dispose();\r
57             _connection.Dispose();\r
58         }\r
59 \r
60         /// <summary>\r
61         /// Commits the current transaction.\r
62         /// throws NRE if _transaction is null. Behavior is intentional.\r
63         /// </summary>\r
64         public void Commit()\r
65         {\r
66             // TODO: do not commit if participating in a higher transaction\r
67             _transaction.Commit();\r
68         }\r
69 \r
70         public DbLinqCommand(string commandText, bool createTransaction, DataContext dataContext)\r
71         {\r
72             // TODO: check if all this stuff is necessary\r
73             // the OpenConnection() checks that the connection is already open\r
74             // TODO: see if we can move this here (in theory the final DataContext shouldn't use)\r
75             _connection = dataContext.DatabaseContext.OpenConnection();\r
76             // the transaction is optional\r
77             if (createTransaction)\r
78                 _transaction = dataContext.DatabaseContext.Transaction();\r
79             cmd = dataContext.DatabaseContext.CreateCommand();\r
80             Command.CommandText = commandText;\r
81             if (createTransaction)\r
82                 Command.Transaction = _transaction.Transaction;\r
83         }\r
84 \r
85     }\r
86 }\r