Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbCommandBuilder.cs
1 //
2 // System.Data.OleDb.OleDbCommandBuilder
3 //
4 // Author:
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
39 namespace System.Data.OleDb
40 {
41         /// <summary>
42         /// Provides a means of automatically generating single-table commands used to reconcile changes made to a DataSet with the associated database. This class cannot be inherited.
43         /// </summary>
44         public sealed class OleDbCommandBuilder :
45 #if NET_2_0
46                 DbCommandBuilder
47 #else
48                 Component
49 #endif
50         {
51                 #region Fields
52
53                 OleDbDataAdapter adapter;
54 #if ONLY_1_1
55                 string quotePrefix;
56                 string quoteSuffix;
57                 private DataTable               _schema;
58                 private string                  _tableName;
59                 private OleDbCommand    _insertCommand;
60                 private OleDbCommand    _updateCommand;
61                 private OleDbCommand    _deleteCommand;
62
63                 bool _disposed;
64 #endif
65                 #endregion // Fields
66
67                 #region Constructors
68                 
69                 public OleDbCommandBuilder ()
70                 {
71 #if !NET_2_0
72                         quotePrefix = String.Empty;
73                         quoteSuffix = String.Empty;
74 #endif
75                 }
76
77                 public OleDbCommandBuilder (OleDbDataAdapter adapter) 
78                         : this ()
79                 {
80                         this.adapter = adapter;
81                 }
82
83                 #endregion // Constructors
84
85                 #region Properties
86
87 #if !NET_2_0
88                 [DataSysDescriptionAttribute ("The DataAdapter for which to automatically generate OleDbCommands")]
89 #endif
90                 [DefaultValue (null)]
91                 public new OleDbDataAdapter DataAdapter {
92                         get {
93                                 return adapter;
94                         }
95                         set {
96                                 adapter = value;
97                         }
98                 }
99
100 #if !NET_2_0
101                 [BrowsableAttribute (false)]
102                 [DataSysDescriptionAttribute ("The prefix string wrapped around sql objects")]
103                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
104                 public string QuotePrefix {
105                         get {
106                                 return quotePrefix;
107                         }
108                         set {
109                                 quotePrefix = value;
110                         }
111                 }
112
113                 [BrowsableAttribute (false)]
114                 [DataSysDescriptionAttribute ("The suffix string wrapped around sql objects")]
115                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
116                 public string QuoteSuffix {
117                         get {
118                                 return quoteSuffix;
119                         }
120                         set {
121                                 quoteSuffix = value;
122                         }
123                 }
124 #endif
125
126                 #endregion // Properties
127
128                 #region Methods
129
130 #if NET_2_0
131                 protected override void ApplyParameterInfo (DbParameter parameter,
132                                                                     DataRow datarow,
133                                                                     StatementType statementType,
134                                                                     bool whereClause)
135                 {
136                         OleDbParameter p = (OleDbParameter) parameter;
137                         p.Size = int.Parse (datarow ["ColumnSize"].ToString ());
138                         if (datarow ["NumericPrecision"] != DBNull.Value) {
139                                 p.Precision = byte.Parse (datarow ["NumericPrecision"].ToString ());
140                         }
141                         if (datarow ["NumericScale"] != DBNull.Value) {
142                                 p.Scale = byte.Parse (datarow ["NumericScale"].ToString ());
143                         }
144                         p.DbType = (DbType) datarow ["ProviderType"];
145                 }
146 #endif
147
148                 [MonoTODO]
149                 public static void DeriveParameters (OleDbCommand command)
150                 {
151                         if (command.CommandType != CommandType.StoredProcedure) {
152                                 throw new InvalidOperationException ("You can perform this " +
153                                                                              "operation only on CommandTye" + 
154                                                                              " StoredProcedure");
155                         }
156                         // FIXME: Retrive info from server
157                         throw new NotImplementedException ();
158                 }
159
160 #if ONLY_1_1
161                 protected override void Dispose (bool disposing)
162                 {
163                         if (_disposed)
164                                 return;
165                         
166                         if (disposing) {
167                                 // dispose managed resource
168                                 if (_insertCommand != null) _insertCommand.Dispose ();
169                                 if (_updateCommand != null) _updateCommand.Dispose ();
170                                 if (_deleteCommand != null) _deleteCommand.Dispose ();
171
172                                 _insertCommand = null;
173                                 _updateCommand = null;
174                                 _deleteCommand = null;
175                                 _schema = null;
176                         }
177                         _disposed = true;
178                 }
179 #endif
180
181                 [MonoTODO]
182                 public new OleDbCommand GetDeleteCommand ()
183                 {
184                         throw new NotImplementedException ();
185                 }
186
187 #if NET_2_0
188                 [MonoTODO]
189                 public new OleDbCommand GetDeleteCommand (bool useColumnsForParameterNames)
190                 {
191                         throw new NotImplementedException ();
192                 }
193 #endif
194
195                 [MonoTODO]
196                 public new OleDbCommand GetInsertCommand ()
197                 {
198                         throw new NotImplementedException ();
199                 }
200
201 #if NET_2_0
202                 [MonoTODO]
203                 public new OleDbCommand GetInsertCommand (bool useColumnsForParameterNames)
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 protected override string GetParameterName (int parameterOrdinal)
209                 {
210                         return String.Format("@p{0}", parameterOrdinal);
211                 }
212
213                 protected override string GetParameterName (string parameterName)
214                 {
215                         return String.Format("@{0}", parameterName);                       
216                 }
217                 
218                 protected override string GetParameterPlaceholder (int parameterOrdinal)
219                 {
220                         return GetParameterName (parameterOrdinal);
221                 }
222                 
223 #endif
224
225                 [MonoTODO]
226                 public new OleDbCommand GetUpdateCommand ()
227                 {
228                         throw new NotImplementedException ();
229                 }
230
231 #if NET_2_0
232                 [MonoTODO]
233                 public new OleDbCommand GetUpdateCommand (bool useColumnsForParameterNames)
234                 {
235                         throw new NotImplementedException ();
236                 }
237
238                 [MonoTODO]
239                 public override string QuoteIdentifier(string unquotedIdentifier)
240                 {
241                         return base.QuoteIdentifier (unquotedIdentifier);
242                 }
243
244                 [MonoTODO]
245                 public string QuoteIdentifier(string unquotedIdentifier, OleDbConnection connection)
246                 {
247                         throw new NotImplementedException ();
248                 }
249
250                 [MonoTODO]
251                 protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
252                 {
253                         throw new NotImplementedException ();
254                 }
255
256                 [MonoTODO]
257                 public override string UnquoteIdentifier(string quotedIdentifier)
258                 {
259                         return base.UnquoteIdentifier (quotedIdentifier);
260                 }
261
262                 [MonoTODO]
263                 public string UnquoteIdentifier(string quotedIdentifier, OleDbConnection connection)
264                 {
265                         throw new NotImplementedException ();
266                 }
267 #else
268                 private OleDbCommand SelectCommand
269                 {
270                         get {
271                                 if (DataAdapter == null)
272                                         return null;
273                                 return DataAdapter.SelectCommand;
274                         }
275                 }
276
277                 public void RefreshSchema ()
278                 {
279                         // creates metadata
280                         if (SelectCommand == null)
281                                 throw new InvalidOperationException ("SelectCommand should be valid");
282                         if (SelectCommand.Connection == null)
283                                 throw new InvalidOperationException ("SelectCommand's Connection should be valid");
284                         
285                         CommandBehavior behavior = CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
286                         if (SelectCommand.Connection.State != ConnectionState.Open) {
287                                 SelectCommand.Connection.Open ();
288                                 behavior |= CommandBehavior.CloseConnection;
289                         }
290                         
291                         OleDbDataReader reader = SelectCommand.ExecuteReader (behavior);
292                         _schema = reader.GetSchemaTable ();
293                         reader.Close ();
294                         
295                         // force creation of commands
296                         _insertCommand  = null;
297                         _updateCommand  = null;
298                         _deleteCommand  = null;
299                         _tableName      = String.Empty;
300                 }
301 #endif
302
303                 #endregion // Methods
304         }
305 }