2 // System.Data.OleDb.OleDbCommandBuilder
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 using System.ComponentModel;
37 using System.Data.Common;
39 namespace System.Data.OleDb
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.
44 public sealed class OleDbCommandBuilder :
53 OleDbDataAdapter adapter;
57 private DataTable _schema;
58 private string _tableName;
59 private OleDbCommand _insertCommand;
60 private OleDbCommand _updateCommand;
61 private OleDbCommand _deleteCommand;
69 public OleDbCommandBuilder ()
72 quotePrefix = String.Empty;
73 quoteSuffix = String.Empty;
77 public OleDbCommandBuilder (OleDbDataAdapter adapter)
80 this.adapter = adapter;
83 #endregion // Constructors
88 [DataSysDescriptionAttribute ("The DataAdapter for which to automatically generate OleDbCommands")]
91 public new OleDbDataAdapter DataAdapter {
101 [BrowsableAttribute (false)]
102 [DataSysDescriptionAttribute ("The prefix string wrapped around sql objects")]
103 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
104 public string QuotePrefix {
113 [BrowsableAttribute (false)]
114 [DataSysDescriptionAttribute ("The suffix string wrapped around sql objects")]
115 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
116 public string QuoteSuffix {
126 #endregion // Properties
131 protected override void ApplyParameterInfo (DbParameter parameter,
133 StatementType statementType,
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 ());
141 if (datarow ["NumericScale"] != DBNull.Value) {
142 p.Scale = byte.Parse (datarow ["NumericScale"].ToString ());
144 p.DbType = (DbType) datarow ["ProviderType"];
149 public static void DeriveParameters (OleDbCommand command)
151 if (command.CommandType != CommandType.StoredProcedure) {
152 throw new InvalidOperationException ("You can perform this " +
153 "operation only on CommandTye" +
156 // FIXME: Retrive info from server
157 throw new NotImplementedException ();
161 protected override void Dispose (bool disposing)
167 // dispose managed resource
168 if (_insertCommand != null) _insertCommand.Dispose ();
169 if (_updateCommand != null) _updateCommand.Dispose ();
170 if (_deleteCommand != null) _deleteCommand.Dispose ();
172 _insertCommand = null;
173 _updateCommand = null;
174 _deleteCommand = null;
182 public new OleDbCommand GetDeleteCommand ()
184 throw new NotImplementedException ();
189 public new OleDbCommand GetDeleteCommand (bool useColumnsForParameterNames)
191 throw new NotImplementedException ();
196 public new OleDbCommand GetInsertCommand ()
198 throw new NotImplementedException ();
203 public new OleDbCommand GetInsertCommand (bool useColumnsForParameterNames)
205 throw new NotImplementedException ();
208 protected override string GetParameterName (int parameterOrdinal)
210 return String.Format("@p{0}", parameterOrdinal);
213 protected override string GetParameterName (string parameterName)
215 return String.Format("@{0}", parameterName);
218 protected override string GetParameterPlaceholder (int parameterOrdinal)
220 return GetParameterName (parameterOrdinal);
226 public new OleDbCommand GetUpdateCommand ()
228 throw new NotImplementedException ();
233 public new OleDbCommand GetUpdateCommand (bool useColumnsForParameterNames)
235 throw new NotImplementedException ();
239 public override string QuoteIdentifier(string unquotedIdentifier)
241 return base.QuoteIdentifier (unquotedIdentifier);
245 public string QuoteIdentifier(string unquotedIdentifier, OleDbConnection connection)
247 throw new NotImplementedException ();
251 protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
253 throw new NotImplementedException ();
257 public override string UnquoteIdentifier(string quotedIdentifier)
259 return base.UnquoteIdentifier (quotedIdentifier);
263 public string UnquoteIdentifier(string quotedIdentifier, OleDbConnection connection)
265 throw new NotImplementedException ();
268 private OleDbCommand SelectCommand
271 if (DataAdapter == null)
273 return DataAdapter.SelectCommand;
277 public void RefreshSchema ()
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");
285 CommandBehavior behavior = CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
286 if (SelectCommand.Connection.State != ConnectionState.Open) {
287 SelectCommand.Connection.Open ();
288 behavior |= CommandBehavior.CloseConnection;
291 OleDbDataReader reader = SelectCommand.ExecuteReader (behavior);
292 _schema = reader.GetSchemaTable ();
295 // force creation of commands
296 _insertCommand = null;
297 _updateCommand = null;
298 _deleteCommand = null;
299 _tableName = String.Empty;
303 #endregion // Methods