OleDbCommandBuilder.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // System.Data.OleDb.OleDbCommandBuilder
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Rodrigo Moya, 2002
  9. // Copyright (C) Tim Coleman, 2002
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.ComponentModel;
  34. using System.Data;
  35. using System.Data.Common;
  36. namespace System.Data.OleDb
  37. {
  38. /// <summary>
  39. /// 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.
  40. /// </summary>
  41. public sealed class OleDbCommandBuilder :
  42. #if NET_2_0
  43. DbCommandBuilder
  44. #else
  45. Component
  46. #endif
  47. {
  48. #region Fields
  49. OleDbDataAdapter adapter;
  50. #if ONLY_1_1
  51. string quotePrefix;
  52. string quoteSuffix;
  53. #endif
  54. private DataTable _schema;
  55. private string _tableName;
  56. private OleDbCommand _insertCommand;
  57. private OleDbCommand _updateCommand;
  58. private OleDbCommand _deleteCommand;
  59. bool _disposed;
  60. #endregion // Fields
  61. #region Constructors
  62. public OleDbCommandBuilder ()
  63. {
  64. #if !NET_2_0
  65. quotePrefix = String.Empty;
  66. quoteSuffix = String.Empty;
  67. #endif
  68. }
  69. public OleDbCommandBuilder (OleDbDataAdapter adapter)
  70. : this ()
  71. {
  72. this.adapter = adapter;
  73. }
  74. #endregion // Constructors
  75. #region Properties
  76. #if !NET_2_0
  77. [DataSysDescriptionAttribute ("The DataAdapter for which to automatically generate OleDbCommands")]
  78. #endif
  79. [DefaultValue (null)]
  80. public new OleDbDataAdapter DataAdapter {
  81. get {
  82. return adapter;
  83. }
  84. set {
  85. adapter = value;
  86. }
  87. }
  88. #if !NET_2_0
  89. [BrowsableAttribute (false)]
  90. [DataSysDescriptionAttribute ("The prefix string wrapped around sql objects")]
  91. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  92. public string QuotePrefix {
  93. get {
  94. return quotePrefix;
  95. }
  96. set {
  97. quotePrefix = value;
  98. }
  99. }
  100. [BrowsableAttribute (false)]
  101. [DataSysDescriptionAttribute ("The suffix string wrapped around sql objects")]
  102. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  103. public string QuoteSuffix {
  104. get {
  105. return quoteSuffix;
  106. }
  107. set {
  108. quoteSuffix = value;
  109. }
  110. }
  111. #endif
  112. #endregion // Properties
  113. #region Methods
  114. #if NET_2_0
  115. protected override void ApplyParameterInfo (DbParameter dbParameter,
  116. DataRow row,
  117. StatementType statementType,
  118. bool whereClause)
  119. {
  120. OleDbParameter parameter = (OleDbParameter) dbParameter;
  121. parameter.Size = int.Parse (row ["ColumnSize"].ToString ());
  122. if (row ["NumericPrecision"] != DBNull.Value) {
  123. parameter.Precision = byte.Parse (row ["NumericPrecision"].ToString ());
  124. }
  125. if (row ["NumericScale"] != DBNull.Value) {
  126. parameter.Scale = byte.Parse (row ["NumericScale"].ToString ());
  127. }
  128. parameter.DbType = (DbType) row ["ProviderType"];
  129. }
  130. #endif
  131. [MonoTODO]
  132. public static void DeriveParameters (OleDbCommand command)
  133. {
  134. if (command.CommandType != CommandType.StoredProcedure) {
  135. throw new InvalidOperationException ("You can perform this " +
  136. "operation only on CommandTye" +
  137. " StoredProcedure");
  138. }
  139. // FIXME: Retrive info from server
  140. throw new NotImplementedException ();
  141. }
  142. #if ONLY_1_1
  143. protected override void Dispose (bool disposing)
  144. {
  145. if (_disposed)
  146. return;
  147. if (disposing) {
  148. // dispose managed resource
  149. if (_insertCommand != null) _insertCommand.Dispose ();
  150. if (_updateCommand != null) _updateCommand.Dispose ();
  151. if (_deleteCommand != null) _deleteCommand.Dispose ();
  152. _insertCommand = null;
  153. _updateCommand = null;
  154. _deleteCommand = null;
  155. _schema = null;
  156. }
  157. _disposed = true;
  158. }
  159. #endif
  160. [MonoTODO]
  161. public new OleDbCommand GetDeleteCommand ()
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. #if NET_2_0
  166. [MonoTODO]
  167. public new OleDbCommand GetDeleteCommand (bool useColumnsForParameterNames)
  168. {
  169. throw new NotImplementedException ();
  170. }
  171. #endif
  172. [MonoTODO]
  173. public new OleDbCommand GetInsertCommand ()
  174. {
  175. throw new NotImplementedException ();
  176. }
  177. #if NET_2_0
  178. [MonoTODO]
  179. public new OleDbCommand GetInsertCommand (bool useColumnsForParameterNames)
  180. {
  181. throw new NotImplementedException ();
  182. }
  183. protected override string GetParameterName (int position)
  184. {
  185. return String.Format("@p{0}", position);
  186. }
  187. protected override string GetParameterName (string parameterName)
  188. {
  189. return String.Format("@{0}", parameterName);
  190. }
  191. protected override string GetParameterPlaceholder (int position)
  192. {
  193. return GetParameterName (position);
  194. }
  195. #endif
  196. [MonoTODO]
  197. public new OleDbCommand GetUpdateCommand ()
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. private OleDbCommand SelectCommand
  202. {
  203. get {
  204. if (DataAdapter == null)
  205. return null;
  206. return DataAdapter.SelectCommand;
  207. }
  208. }
  209. #if NET_2_0
  210. [MonoTODO]
  211. public new OleDbCommand GetUpdateCommand (bool useColumnsForParameterNames)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. [MonoTODO]
  216. public override string QuoteIdentifier(string unquotedIdentifier)
  217. {
  218. return base.QuoteIdentifier (unquotedIdentifier);
  219. }
  220. [MonoTODO]
  221. public string QuoteIdentifier(string unquotedIdentifier, OleDbConnection connection)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. [MonoTODO]
  226. protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
  227. {
  228. throw new NotImplementedException ();
  229. }
  230. [MonoTODO]
  231. public override string UnquoteIdentifier(string quotedIdentifier)
  232. {
  233. return base.UnquoteIdentifier (quotedIdentifier);
  234. }
  235. [MonoTODO]
  236. public string UnquoteIdentifier(string quotedIdentifier, OleDbConnection connection)
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. #else
  241. public void RefreshSchema ()
  242. {
  243. // creates metadata
  244. if (SelectCommand == null)
  245. throw new InvalidOperationException ("SelectCommand should be valid");
  246. if (SelectCommand.Connection == null)
  247. throw new InvalidOperationException ("SelectCommand's Connection should be valid");
  248. CommandBehavior behavior = CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
  249. if (SelectCommand.Connection.State != ConnectionState.Open) {
  250. SelectCommand.Connection.Open ();
  251. behavior |= CommandBehavior.CloseConnection;
  252. }
  253. OleDbDataReader reader = SelectCommand.ExecuteReader (behavior);
  254. _schema = reader.GetSchemaTable ();
  255. reader.Close ();
  256. // force creation of commands
  257. _insertCommand = null;
  258. _updateCommand = null;
  259. _deleteCommand = null;
  260. _tableName = String.Empty;
  261. }
  262. #endif
  263. #endregion // Methods
  264. }
  265. }