2002-08-18 Rodrigo Moya <rodrigo@ximian.com>
[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 using System.ComponentModel;
13 using System.Data;
14 using System.Data.Common;
15
16 namespace System.Data.OleDb
17 {
18         public sealed class OleDbConnection : Component, ICloneable, IDbConnection
19         {
20                 #region Fields
21
22                 string connectionString;
23                 int connectionTimeout;
24                 OleDbDataReader dataReader;
25                 bool dataReaderOpen;
26                 IntPtr gdaConnection;
27
28                 #endregion
29
30                 #region Constructors
31                 
32                 public OleDbConnection ()
33                 {
34                         libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
35                         gdaConnection = IntPtr.Zero;
36                         connectionTimeout = 15;
37                         connectionString = null;
38                 }
39
40                 public OleDbConnection (string connectionString) : this ()
41                 {
42                         this.connectionString = connectionString;
43                 }
44
45                 #endregion // Constructors
46
47                 #region Properties
48
49                 public string ConnectionString {
50                         get {
51                                 return connectionString;
52                         }
53                         set {
54                                 connectionString = value;
55                         }
56                 }
57
58                 public int ConnectionTimeout {
59                         get {
60                                 return connectionTimeout;
61                         }
62                 }
63
64                 public string Database { 
65                         get {
66                                 if (gdaConnection != IntPtr.Zero
67                                     && libgda.gda_connection_is_open (gdaConnection)) {
68                                         return libgda.gda_connection_get_database (gdaConnection);
69                                 }
70
71                                 return null;
72                         }
73                 }
74
75                 public string DataSource {
76                         get {
77                                 if (gdaConnection != IntPtr.Zero
78                                     && libgda.gda_connection_is_open (gdaConnection)) {
79                                         return libgda.gda_connection_get_dsn (gdaConnection);
80                                 }
81
82                                 return null;
83                         }
84                 }
85
86                 public string Provider {
87                         get {
88                                 if (gdaConnection != IntPtr.Zero
89                                     && libgda.gda_connection_is_open (gdaConnection)) {
90                                         return libgda.gda_connection_get_provider (gdaConnection);
91                                 }
92
93                                 return null;
94                         }
95                 }
96
97                 public string ServerVersion {
98                         [MonoTODO]
99                         get {
100                                 throw new NotImplementedException ();
101                         }
102                 }
103
104                 public ConnectionState State
105                 {
106                         get {
107                                 if (gdaConnection != IntPtr.Zero) {
108                                         if (libgda.gda_connection_is_open (gdaConnection))
109                                                 return ConnectionState.Open;
110                                 }
111
112                                 return ConnectionState.Closed;
113                         }
114                 }
115
116                 internal IntPtr GdaConnection
117                 {
118                         get {
119                                 return gdaConnection;
120                         }
121                 }
122
123                 #endregion // Properties
124         
125                 #region Methods
126         
127                 public OleDbTransaction BeginTransaction ()
128                 {
129                         if (gdaConnection != IntPtr.Zero)
130                                 return new OleDbTransaction (this);
131
132                         return null;
133                 }
134
135                 IDbTransaction IDbConnection.BeginTransaction ()
136                 {
137                         return BeginTransaction ();
138                 }
139                 
140                 public OleDbTransaction BeginTransaction (IsolationLevel level)
141                 {
142                         if (gdaConnection != IntPtr.Zero)
143                                 return new OleDbTransaction (this, level);
144
145                         return null;
146                 }
147
148                 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
149                 {
150                         return BeginTransaction (level);
151                 }
152
153                 public void ChangeDatabase (string name)
154                 {
155                         // FIXME: see http://bugzilla.gnome.org/show_bug.cgi?id=83315
156                 }
157
158                 public void Close ()
159                 {
160                         if (gdaConnection != IntPtr.Zero) {
161                                 libgda.gda_connection_close (gdaConnection);
162                                 gdaConnection = IntPtr.Zero;
163                         }
164                 }
165
166                 public OleDbCommand CreateCommand ()
167                 {
168                         if (gdaConnection != IntPtr.Zero
169                             && libgda.gda_connection_is_open (gdaConnection))
170                                 return new OleDbCommand (null, this);
171
172                         return null;
173                 }
174
175                 [MonoTODO]
176                 protected override void Dispose (bool disposing)
177                 {
178                         throw new NotImplementedException ();
179                 }
180
181                 [MonoTODO]
182                 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
183                 {
184                         throw new NotImplementedException ();
185                 }
186
187                 [MonoTODO]
188                 object ICloneable.Clone ()
189                 {
190                         throw new NotImplementedException();
191                 }
192
193                 IDbCommand IDbConnection.CreateCommand ()
194                 {
195                         return CreateCommand ();
196                 }
197
198                 public void Open ()
199                 {
200                         if (State == ConnectionState.Open)
201                                 throw new InvalidOperationException ();
202
203                         gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
204                                                                            connectionString,
205                                                                            "", "");
206                 }
207
208                 [MonoTODO]
209                 public static void ReleaseObjectPool ()
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 #endregion
215
216                 #region Events and Delegates
217
218                 public event OleDbInfoMessageEventHandler InfoMessage;
219                 public event StateChangeEventHandler StateChange;
220
221                 #endregion
222         }
223 }