Merge pull request #1155 from steffen-kiess/json-string
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbConnection.cs
1 //
2 // System.Data.OleDb.OleDbConnection
3 //
4 // Authors:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.ComponentModel;
36 using System.Data;
37 using System.Data.Common;
38 using System.EnterpriseServices;
39 using System.Transactions;
40
41 namespace System.Data.OleDb
42 {
43         [DefaultEvent ("InfoMessage")]
44         public sealed class OleDbConnection : DbConnection, ICloneable
45         {
46                 #region Fields
47
48                 string connectionString;
49                 int connectionTimeout;
50                 IntPtr gdaConnection;
51
52                 #endregion
53
54                 #region Constructors
55                 
56                 public OleDbConnection ()
57                 {
58                         gdaConnection = IntPtr.Zero;
59                         connectionTimeout = 15;
60                 }
61
62                 public OleDbConnection (string connectionString) : this ()
63                 {
64                         this.connectionString = connectionString;
65                 }
66
67                 #endregion // Constructors
68
69                 #region Properties
70                 
71                 [DataCategory ("Data")]
72                 [DefaultValue ("")]
73                 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
74                 [RecommendedAsConfigurable (true)]
75                 [RefreshPropertiesAttribute (RefreshProperties.All)]
76                 public override string ConnectionString {
77                         get {
78                                 if (connectionString == null)
79                                         return string.Empty;
80                                 return connectionString;
81                         }
82                         set {
83                                 connectionString = value;
84                         }
85                 }
86
87                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
88                 public override int ConnectionTimeout {
89                         get {
90                                 return connectionTimeout;
91                         }
92                 }
93
94                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
95                 public override 
96                 string Database {
97                         get {
98                                 if (gdaConnection != IntPtr.Zero
99                                         && libgda.gda_connection_is_open (gdaConnection)) {
100                                         return libgda.gda_connection_get_database (gdaConnection);
101                                 }
102
103                                 return string.Empty;
104                         }
105                 }
106
107                 [BrowsableAttribute (true)]
108                 public override string DataSource {
109                         get {
110                                 if (gdaConnection != IntPtr.Zero
111                                         && libgda.gda_connection_is_open (gdaConnection)) {
112                                         return libgda.gda_connection_get_dsn (gdaConnection);
113                                 }
114
115                                 return string.Empty;
116                         }
117                 }
118
119                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
120                 [BrowsableAttribute (true)]
121                 public string Provider {
122                         get {
123                                 if (gdaConnection != IntPtr.Zero
124                                         && libgda.gda_connection_is_open (gdaConnection)) {
125                                         return libgda.gda_connection_get_provider (gdaConnection);
126                                 }
127
128                                 return string.Empty;
129                         }
130                 }
131
132                 public override string ServerVersion {
133                         get {
134                                 if (State == ConnectionState.Closed)
135                                         throw ExceptionHelper.ConnectionClosed ();
136                                 return libgda.gda_connection_get_server_version (gdaConnection);
137                         }
138                 }
139
140                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
141                 [BrowsableAttribute (false)]
142                 public override ConnectionState State {
143                         get {
144                                 if (gdaConnection != IntPtr.Zero) {
145                                         if (libgda.gda_connection_is_open (gdaConnection))
146                                                 return ConnectionState.Open;
147                                 }
148
149                                 return ConnectionState.Closed;
150                         }
151                 }
152
153                 internal IntPtr GdaConnection {
154                         get {
155                                 return gdaConnection;
156                         }
157                 }
158                 
159                 #endregion // Properties
160         
161                 #region Methods
162         
163                 public new OleDbTransaction BeginTransaction ()
164                 {
165                         if (State == ConnectionState.Closed)
166                                 throw ExceptionHelper.ConnectionClosed ();
167                         return new OleDbTransaction (this);
168                 }
169
170                 public new OleDbTransaction BeginTransaction (IsolationLevel isolationLevel)
171                 {
172                         if (State == ConnectionState.Closed)
173                                 throw ExceptionHelper.ConnectionClosed ();
174                         return new OleDbTransaction (this, isolationLevel);
175                 }
176
177                 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
178                 {
179                         return BeginTransaction (isolationLevel);
180                 }
181
182                 protected override DbCommand CreateDbCommand()
183                 {
184                         return CreateCommand ();
185                 }
186
187                 public override void ChangeDatabase (string value)
188                 {
189                         if (State != ConnectionState.Open)
190                                 throw new InvalidOperationException ();
191
192                         if (!libgda.gda_connection_change_database (gdaConnection, value))
193                                 throw new OleDbException (this);
194                 }
195
196                 public override void Close ()
197                 {
198                         if (State == ConnectionState.Open) {
199                                 libgda.gda_connection_close (gdaConnection);
200                                 gdaConnection = IntPtr.Zero;
201                         }
202                 }
203
204                 public new OleDbCommand CreateCommand ()
205                 {
206                         if (State == ConnectionState.Open)
207                                 return new OleDbCommand (null, this);
208
209                         return null;
210                 }
211
212                 [MonoTODO]
213                 protected override void Dispose (bool disposing)
214                 {
215                         throw new NotImplementedException ();
216                 }
217
218                 [MonoTODO]
219                 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
220                 {
221                         throw new NotImplementedException ();
222                 }
223
224                 [MonoTODO]
225                 object ICloneable.Clone ()
226                 {
227                         throw new NotImplementedException();
228                 }
229
230                 public
231                 override
232                 void Open ()
233                 {
234 //                      string provider = "Default";
235 //                      string gdaCncStr = string.Empty;
236 //                      string[] args;
237 //                      int len;
238 //                      char [] separator = { ';' };
239                         
240                         if (State == ConnectionState.Open)
241                                 throw new InvalidOperationException ();
242
243                         libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
244
245                         gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
246                                 ConnectionString, string.Empty, string.Empty, 0);
247
248                         if (gdaConnection == IntPtr.Zero)
249                                 throw new OleDbException (this);
250                         /* convert the connection string to its GDA equivalent */
251                         //args = connectionString.Split (';');
252                         //len = args.Length;
253                         //for (int i = 0; i < len; i++) {
254                         //      string[] values = args[i].Split (separator, 2);
255                         //      if (values[0] == "Provider") {
256                         //              if (values[1] == "SQLOLEDB")
257                         //                      provider = "FreeTDS";
258                         //              else if (values[1] == "MSDAORA")
259                         //                      provider = "Oracle";
260                         //              else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
261                         //                      provider = "MS Access";
262                         //              else
263                         //                      provider = values[2];
264                         //      }
265                         //      else if (values[0] == "Addr" || values[0] == "Address")
266                         //              gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
267                         //      else if (values[0] == "Database")
268                         //              gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
269                         //      else if (values[0] == "Connection Lifetime")
270                         //              connectionTimeout = System.Convert.ToInt32 (values[1]);
271                         //      else if (values[0] == "File Name")
272                         //              gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
273                         //      else if (values[0] == "Password" || values[0] == "Pwd")
274                         //              gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
275                         //      else if (values[0] == "User ID")
276                         //              gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
277                         //}
278
279                         /* open the connection */
280                         //System.Console.WriteLine ("Opening connection for provider " +
281                         //                provider + " with " + gdaCncStr);
282                         //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
283                         //                                                             provider,
284                         //                                                             gdaCncStr);
285                 }
286
287                 [MonoTODO]
288                 public static void ReleaseObjectPool ()
289                 {
290                         throw new NotImplementedException ();
291                 }
292
293                 [MonoTODO]
294                 public void EnlistDistributedTransaction (ITransaction transaction)
295                 {
296                         throw new NotImplementedException ();
297                 }
298
299                 [MonoTODO]
300                 public override void EnlistTransaction (Transaction transaction)
301                 {
302                         throw new NotImplementedException ();
303                 }
304
305                 [MonoTODO]
306                 public override DataTable GetSchema ()
307                 {
308                         if (State == ConnectionState.Closed)
309                                 throw ExceptionHelper.ConnectionClosed ();
310                         throw new NotImplementedException ();
311                 }
312
313                 [MonoTODO]
314                 public override DataTable GetSchema(string collectionName)
315                 {
316                         return GetSchema (collectionName, null);
317                 }
318
319                 [MonoTODO]
320                 public override DataTable GetSchema (String collectionName, string [] restrictionValues)
321                 {
322                         if (State == ConnectionState.Closed)
323                                 throw ExceptionHelper.ConnectionClosed ();
324                         throw new NotImplementedException ();
325                 }
326
327                 [MonoTODO]
328                 [EditorBrowsable (EditorBrowsableState.Advanced)]
329                 public void ResetState ()
330                 {
331                         throw new NotImplementedException ();
332                 }
333
334                 #endregion
335
336                 #region Events and Delegates
337
338                 [DataCategory ("DataCategory_InfoMessage")]
339                 public event OleDbInfoMessageEventHandler InfoMessage;
340
341
342                 #endregion
343         }
344 }