2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.Data / System.Data.OleDb / OleDbCommandBuilder.cs
blob93a15db03aa8109922d1b82ad078e5577663188f
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
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:
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.
35 using System.ComponentModel;
36 using System.Data;
37 using System.Data.Common;
39 namespace System.Data.OleDb
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
51 #region Fields
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;
63 bool _disposed;
64 #endif
65 #endregion // Fields
67 #region Constructors
69 public OleDbCommandBuilder ()
71 #if !NET_2_0
72 quotePrefix = String.Empty;
73 quoteSuffix = String.Empty;
74 #endif
77 public OleDbCommandBuilder (OleDbDataAdapter adapter)
78 : this ()
80 this.adapter = adapter;
83 #endregion // Constructors
85 #region Properties
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;
95 set {
96 adapter = value;
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;
108 set {
109 quotePrefix = value;
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;
120 set {
121 quoteSuffix = value;
124 #endif
126 #endregion // Properties
128 #region Methods
130 #if NET_2_0
131 protected override void ApplyParameterInfo (DbParameter dbParameter,
132 DataRow row,
133 StatementType statementType,
134 bool whereClause)
136 OleDbParameter parameter = (OleDbParameter) dbParameter;
137 parameter.Size = int.Parse (row ["ColumnSize"].ToString ());
138 if (row ["NumericPrecision"] != DBNull.Value) {
139 parameter.Precision = byte.Parse (row ["NumericPrecision"].ToString ());
141 if (row ["NumericScale"] != DBNull.Value) {
142 parameter.Scale = byte.Parse (row ["NumericScale"].ToString ());
144 parameter.DbType = (DbType) row ["ProviderType"];
146 #endif
148 [MonoTODO]
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" +
154 " StoredProcedure");
156 // FIXME: Retrive info from server
157 throw new NotImplementedException ();
160 #if ONLY_1_1
161 protected override void Dispose (bool disposing)
163 if (_disposed)
164 return;
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 ();
172 _insertCommand = null;
173 _updateCommand = null;
174 _deleteCommand = null;
175 _schema = null;
177 _disposed = true;
179 #endif
181 [MonoTODO]
182 public new OleDbCommand GetDeleteCommand ()
184 throw new NotImplementedException ();
187 #if NET_2_0
188 [MonoTODO]
189 public new OleDbCommand GetDeleteCommand (bool useColumnsForParameterNames)
191 throw new NotImplementedException ();
193 #endif
195 [MonoTODO]
196 public new OleDbCommand GetInsertCommand ()
198 throw new NotImplementedException ();
201 #if NET_2_0
202 [MonoTODO]
203 public new OleDbCommand GetInsertCommand (bool useColumnsForParameterNames)
205 throw new NotImplementedException ();
208 protected override string GetParameterName (int position)
210 return String.Format("@p{0}", position);
213 protected override string GetParameterName (string parameterName)
215 return String.Format("@{0}", parameterName);
218 protected override string GetParameterPlaceholder (int position)
220 return GetParameterName (position);
223 #endif
225 [MonoTODO]
226 public new OleDbCommand GetUpdateCommand ()
228 throw new NotImplementedException ();
231 #if NET_2_0
232 [MonoTODO]
233 public new OleDbCommand GetUpdateCommand (bool useColumnsForParameterNames)
235 throw new NotImplementedException ();
238 [MonoTODO]
239 public override string QuoteIdentifier(string unquotedIdentifier)
241 return base.QuoteIdentifier (unquotedIdentifier);
244 [MonoTODO]
245 public string QuoteIdentifier(string unquotedIdentifier, OleDbConnection connection)
247 throw new NotImplementedException ();
250 [MonoTODO]
251 protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
253 throw new NotImplementedException ();
256 [MonoTODO]
257 public override string UnquoteIdentifier(string quotedIdentifier)
259 return base.UnquoteIdentifier (quotedIdentifier);
262 [MonoTODO]
263 public string UnquoteIdentifier(string quotedIdentifier, OleDbConnection connection)
265 throw new NotImplementedException ();
267 #else
268 private OleDbCommand SelectCommand
270 get {
271 if (DataAdapter == null)
272 return null;
273 return DataAdapter.SelectCommand;
277 public void RefreshSchema ()
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");
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 ();
293 reader.Close ();
295 // force creation of commands
296 _insertCommand = null;
297 _updateCommand = null;
298 _deleteCommand = null;
299 _tableName = String.Empty;
301 #endif
303 #endregion // Methods