OdbcCommandBuilder.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. //
  2. // System.Data.Odbc.OdbcCommandBuilder
  3. //
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Sureshkumar T ([email protected])
  7. //
  8. // Copyright (C) Novell Inc, 2004
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Text;
  33. using System.Data;
  34. using System.Data.Common;
  35. using System.ComponentModel;
  36. namespace System.Data.Odbc
  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 OdbcCommandBuilder : DbCommandBuilder
  42. {
  43. #region Fields
  44. private OdbcDataAdapter _adapter;
  45. private DataTable _schema;
  46. private string _tableName;
  47. private OdbcCommand _insertCommand;
  48. private OdbcCommand _updateCommand;
  49. private OdbcCommand _deleteCommand;
  50. bool _disposed;
  51. private OdbcRowUpdatingEventHandler rowUpdatingHandler;
  52. #endregion // Fields
  53. #region Constructors
  54. public OdbcCommandBuilder ()
  55. {
  56. }
  57. public OdbcCommandBuilder (OdbcDataAdapter adapter)
  58. : this ()
  59. {
  60. DataAdapter = adapter;
  61. }
  62. #endregion // Constructors
  63. #region Properties
  64. [OdbcDescriptionAttribute ("The DataAdapter for which to automatically generate OdbcCommands")]
  65. [DefaultValue (null)]
  66. public
  67. new
  68. OdbcDataAdapter DataAdapter {
  69. get {
  70. return _adapter;
  71. }
  72. set {
  73. if (_adapter == value)
  74. return;
  75. if (rowUpdatingHandler != null)
  76. rowUpdatingHandler = new OdbcRowUpdatingEventHandler (OnRowUpdating);
  77. if (_adapter != null)
  78. _adapter.RowUpdating -= rowUpdatingHandler;
  79. _adapter = value;
  80. if (_adapter != null)
  81. _adapter.RowUpdating += rowUpdatingHandler;
  82. }
  83. }
  84. private OdbcCommand SelectCommand {
  85. get {
  86. if (DataAdapter == null)
  87. return null;
  88. return DataAdapter.SelectCommand;
  89. }
  90. }
  91. private DataTable Schema {
  92. get {
  93. if (_schema == null)
  94. RefreshSchema ();
  95. return _schema;
  96. }
  97. }
  98. private string TableName {
  99. get {
  100. if (_tableName != string.Empty)
  101. return _tableName;
  102. DataRow [] schemaRows = Schema.Select ("BaseTableName is not null and BaseTableName <> ''");
  103. if (schemaRows.Length > 1) {
  104. string tableName = (string) schemaRows [0] ["BaseTableName"];
  105. foreach (DataRow schemaRow in schemaRows) {
  106. if ( (string) schemaRow ["BaseTableName"] != tableName)
  107. throw new InvalidOperationException ("Dynamic SQL generation is not supported against multiple base tables.");
  108. }
  109. }
  110. if (schemaRows.Length == 0)
  111. throw new InvalidOperationException ("Cannot determine the base table name. Cannot proceed");
  112. _tableName = schemaRows [0] ["BaseTableName"].ToString ();
  113. return _tableName;
  114. }
  115. }
  116. #endregion // Properties
  117. #region Methods
  118. [MonoTODO]
  119. public static void DeriveParameters (OdbcCommand command)
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. new
  124. void Dispose (bool disposing)
  125. {
  126. if (_disposed)
  127. return;
  128. if (disposing) {
  129. // dispose managed resource
  130. if (_insertCommand != null)
  131. _insertCommand.Dispose ();
  132. if (_updateCommand != null)
  133. _updateCommand.Dispose ();
  134. if (_deleteCommand != null)
  135. _deleteCommand.Dispose ();
  136. if (_schema != null)
  137. _schema.Dispose ();
  138. _insertCommand = null;
  139. _updateCommand = null;
  140. _deleteCommand = null;
  141. _schema = null;
  142. }
  143. _disposed = true;
  144. }
  145. private bool IsUpdatable (DataRow schemaRow)
  146. {
  147. if ( (! schemaRow.IsNull ("IsAutoIncrement") && (bool) schemaRow ["IsAutoIncrement"])
  148. || (! schemaRow.IsNull ("IsRowVersion") && (bool) schemaRow ["IsRowVersion"])
  149. || (! schemaRow.IsNull ("IsReadOnly") && (bool) schemaRow ["IsReadOnly"])
  150. || (schemaRow.IsNull ("BaseTableName") || ((string) schemaRow ["BaseTableName"]).Length == 0)
  151. )
  152. return false;
  153. return true;
  154. }
  155. private string GetColumnName (DataRow schemaRow)
  156. {
  157. string columnName = schemaRow.IsNull ("BaseColumnName") ? String.Empty : (string) schemaRow ["BaseColumnName"];
  158. if (columnName == String.Empty)
  159. columnName = schemaRow.IsNull ("ColumnName") ? String.Empty : (string) schemaRow ["ColumnName"];
  160. return columnName;
  161. }
  162. private OdbcParameter AddParameter (OdbcCommand cmd, string paramName, OdbcType odbcType,
  163. int length, string sourceColumnName, DataRowVersion rowVersion)
  164. {
  165. OdbcParameter param;
  166. if (length >= 0 && sourceColumnName != String.Empty)
  167. param = cmd.Parameters.Add (paramName, odbcType, length, sourceColumnName);
  168. else
  169. param = cmd.Parameters.Add (paramName, odbcType);
  170. param.SourceVersion = rowVersion;
  171. return param;
  172. }
  173. /*
  174. * creates where clause for optimistic concurrency
  175. */
  176. private string CreateOptWhereClause (OdbcCommand command, int paramCount)
  177. {
  178. string [] whereClause = new string [Schema.Rows.Count];
  179. int partCount = 0;
  180. foreach (DataRow schemaRow in Schema.Rows) {
  181. // exclude non updatable columns
  182. if (! IsUpdatable (schemaRow))
  183. continue;
  184. string columnName = GetColumnName (schemaRow);
  185. if (columnName == String.Empty)
  186. throw new InvalidOperationException ("Cannot form delete command. Column name is missing!");
  187. bool allowNull = schemaRow.IsNull ("AllowDBNull") || (bool) schemaRow ["AllowDBNull"];
  188. OdbcType sqlDbType = schemaRow.IsNull ("ProviderType") ? OdbcType.VarChar : (OdbcType) schemaRow ["ProviderType"];
  189. int length = schemaRow.IsNull ("ColumnSize") ? -1 : (int) schemaRow ["ColumnSize"];
  190. if (allowNull) {
  191. whereClause [partCount++] = String.Format ("((? = 1 AND {0} IS NULL) OR ({0} = ?))",
  192. GetQuotedString (columnName));
  193. OdbcParameter nullParam = AddParameter (
  194. command,
  195. GetParameterName (++paramCount),
  196. OdbcType.Int,
  197. length,
  198. columnName,
  199. DataRowVersion.Original);
  200. nullParam.Value = 1;
  201. AddParameter (command, GetParameterName (++paramCount),
  202. sqlDbType, length, columnName,
  203. DataRowVersion.Original);
  204. } else {
  205. whereClause [partCount++] = String.Format ("({0} = ?)",
  206. GetQuotedString (columnName));
  207. AddParameter (command, GetParameterName (++paramCount),
  208. sqlDbType, length, columnName,
  209. DataRowVersion.Original);
  210. }
  211. }
  212. return String.Join (" AND ", whereClause, 0, partCount);
  213. }
  214. private void CreateNewCommand (ref OdbcCommand command)
  215. {
  216. OdbcCommand sourceCommand = SelectCommand;
  217. if (command == null) {
  218. command = new OdbcCommand ();
  219. command.Connection = sourceCommand.Connection;
  220. command.CommandTimeout = sourceCommand.CommandTimeout;
  221. command.Transaction = sourceCommand.Transaction;
  222. }
  223. command.CommandType = CommandType.Text;
  224. command.UpdatedRowSource = UpdateRowSource.None;
  225. command.Parameters.Clear ();
  226. }
  227. private OdbcCommand CreateInsertCommand (bool option)
  228. {
  229. CreateNewCommand (ref _insertCommand);
  230. string query = String.Format ("INSERT INTO {0}", GetQuotedString (TableName));
  231. string [] columns = new string [Schema.Rows.Count];
  232. string [] values = new string [Schema.Rows.Count];
  233. int count = 0;
  234. foreach (DataRow schemaRow in Schema.Rows) {
  235. // exclude non updatable columns
  236. if (! IsUpdatable (schemaRow))
  237. continue;
  238. string columnName = GetColumnName (schemaRow);
  239. if (columnName == String.Empty)
  240. throw new InvalidOperationException ("Cannot form insert command. Column name is missing!");
  241. // create column string & value string
  242. columns [count] = GetQuotedString (columnName);
  243. values [count++] = "?";
  244. // create parameter and add
  245. OdbcType sqlDbType = schemaRow.IsNull ("ProviderType") ? OdbcType.VarChar : (OdbcType) schemaRow ["ProviderType"];
  246. int length = schemaRow.IsNull ("ColumnSize") ? -1 : (int) schemaRow ["ColumnSize"];
  247. AddParameter (_insertCommand, GetParameterName (count),
  248. sqlDbType, length, columnName, DataRowVersion.Current);
  249. }
  250. query = String.Format (
  251. "{0} ({1}) VALUES ({2})",
  252. query,
  253. String.Join (", ", columns, 0, count),
  254. String.Join (", ", values, 0, count));
  255. _insertCommand.CommandText = query;
  256. return _insertCommand;
  257. }
  258. public
  259. new
  260. OdbcCommand GetInsertCommand ()
  261. {
  262. // FIXME: check validity of adapter
  263. if (_insertCommand != null)
  264. return _insertCommand;
  265. if (_schema == null)
  266. RefreshSchema ();
  267. return CreateInsertCommand (false);
  268. }
  269. public new OdbcCommand GetInsertCommand (bool useColumnsForParameterNames)
  270. {
  271. // FIXME: check validity of adapter
  272. if (_insertCommand != null)
  273. return _insertCommand;
  274. if (_schema == null)
  275. RefreshSchema ();
  276. return CreateInsertCommand (useColumnsForParameterNames);
  277. }
  278. private OdbcCommand CreateUpdateCommand (bool option)
  279. {
  280. CreateNewCommand (ref _updateCommand);
  281. string query = String.Format ("UPDATE {0} SET", GetQuotedString (TableName));
  282. string [] setClause = new string [Schema.Rows.Count];
  283. int count = 0;
  284. foreach (DataRow schemaRow in Schema.Rows) {
  285. // exclude non updatable columns
  286. if (! IsUpdatable (schemaRow))
  287. continue;
  288. string columnName = GetColumnName (schemaRow);
  289. if (columnName == String.Empty)
  290. throw new InvalidOperationException ("Cannot form update command. Column name is missing!");
  291. OdbcType sqlDbType = schemaRow.IsNull ("ProviderType") ? OdbcType.VarChar : (OdbcType) schemaRow ["ProviderType"];
  292. int length = schemaRow.IsNull ("ColumnSize") ? -1 : (int) schemaRow ["ColumnSize"];
  293. // create column = value string
  294. setClause [count++] = String.Format ("{0} = ?", GetQuotedString (columnName));
  295. AddParameter (_updateCommand, GetParameterName (count),
  296. sqlDbType, length, columnName, DataRowVersion.Current);
  297. }
  298. // create where clause. odbc uses positional parameters. so where class
  299. // is created seperate from the above loop.
  300. string whereClause = CreateOptWhereClause (_updateCommand, count);
  301. query = String.Format (
  302. "{0} {1} WHERE ({2})",
  303. query,
  304. String.Join (", ", setClause, 0, count),
  305. whereClause);
  306. _updateCommand.CommandText = query;
  307. return _updateCommand;
  308. }
  309. public
  310. new
  311. OdbcCommand GetUpdateCommand ()
  312. {
  313. // FIXME: check validity of adapter
  314. if (_updateCommand != null)
  315. return _updateCommand;
  316. if (_schema == null)
  317. RefreshSchema ();
  318. return CreateUpdateCommand (false);
  319. }
  320. public new OdbcCommand GetUpdateCommand (bool useColumnsForParameterNames)
  321. {
  322. // FIXME: check validity of adapter
  323. if (_updateCommand != null)
  324. return _updateCommand;
  325. if (_schema == null)
  326. RefreshSchema ();
  327. return CreateUpdateCommand (useColumnsForParameterNames);
  328. }
  329. private OdbcCommand CreateDeleteCommand (bool option)
  330. {
  331. CreateNewCommand (ref _deleteCommand);
  332. string query = String.Format (
  333. "DELETE FROM {0}",
  334. GetQuotedString (TableName));
  335. string whereClause = CreateOptWhereClause (_deleteCommand, 0);
  336. query = String.Format (
  337. "{0} WHERE ({1})",
  338. query,
  339. whereClause);
  340. _deleteCommand.CommandText = query;
  341. return _deleteCommand;
  342. }
  343. public
  344. new
  345. OdbcCommand GetDeleteCommand ()
  346. {
  347. // FIXME: check validity of adapter
  348. if (_deleteCommand != null)
  349. return _deleteCommand;
  350. if (_schema == null)
  351. RefreshSchema ();
  352. return CreateDeleteCommand (false);
  353. }
  354. public new OdbcCommand GetDeleteCommand (bool useColumnsForParameterNames)
  355. {
  356. // FIXME: check validity of adapter
  357. if (_deleteCommand != null)
  358. return _deleteCommand;
  359. if (_schema == null)
  360. RefreshSchema ();
  361. return CreateDeleteCommand (useColumnsForParameterNames);
  362. }
  363. new
  364. void RefreshSchema ()
  365. {
  366. // creates metadata
  367. if (SelectCommand == null)
  368. throw new InvalidOperationException ("SelectCommand should be valid");
  369. if (SelectCommand.Connection == null)
  370. throw new InvalidOperationException ("SelectCommand's Connection should be valid");
  371. CommandBehavior behavior = CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
  372. if (SelectCommand.Connection.State != ConnectionState.Open) {
  373. SelectCommand.Connection.Open ();
  374. behavior |= CommandBehavior.CloseConnection;
  375. }
  376. OdbcDataReader reader = SelectCommand.ExecuteReader (behavior);
  377. _schema = reader.GetSchemaTable ();
  378. reader.Close ();
  379. // force creation of commands
  380. _insertCommand = null;
  381. _updateCommand = null;
  382. _deleteCommand = null;
  383. _tableName = String.Empty;
  384. }
  385. protected override
  386. string GetParameterName (int parameterOrdinal)
  387. {
  388. return String.Format ("p{0}", parameterOrdinal);
  389. }
  390. protected override void ApplyParameterInfo (DbParameter parameter,
  391. DataRow datarow,
  392. StatementType statementType,
  393. bool whereClause)
  394. {
  395. OdbcParameter odbcParam = (OdbcParameter) parameter;
  396. odbcParam.Size = int.Parse (datarow ["ColumnSize"].ToString ());
  397. if (datarow ["NumericPrecision"] != DBNull.Value)
  398. odbcParam.Precision = byte.Parse (datarow ["NumericPrecision"].ToString ());
  399. if (datarow ["NumericScale"] != DBNull.Value)
  400. odbcParam.Scale = byte.Parse (datarow ["NumericScale"].ToString ());
  401. odbcParam.DbType = (DbType) datarow ["ProviderType"];
  402. }
  403. protected override string GetParameterName (string parameterName)
  404. {
  405. return String.Format("@{0}", parameterName);
  406. }
  407. protected override string GetParameterPlaceholder (int parameterOrdinal)
  408. {
  409. return GetParameterName (parameterOrdinal);
  410. }
  411. // FIXME: According to MSDN - "if this method is called again with
  412. // the same DbDataAdapter, the DbCommandBuilder is unregistered for
  413. // that DbDataAdapter's RowUpdating event" - this behaviour is yet
  414. // to be verified
  415. protected override void SetRowUpdatingHandler (DbDataAdapter adapter)
  416. {
  417. if (!(adapter is OdbcDataAdapter))
  418. throw new InvalidOperationException ("Adapter needs to be a SqlDataAdapter");
  419. if (rowUpdatingHandler == null)
  420. rowUpdatingHandler = new OdbcRowUpdatingEventHandler (OnRowUpdating);
  421. ((OdbcDataAdapter) adapter).RowUpdating += rowUpdatingHandler;
  422. }
  423. public override string QuoteIdentifier (string unquotedIdentifier)
  424. {
  425. return QuoteIdentifier (unquotedIdentifier, null);
  426. }
  427. public string QuoteIdentifier (string unquotedIdentifier, OdbcConnection connection)
  428. {
  429. if (unquotedIdentifier == null)
  430. throw new ArgumentNullException ("unquotedIdentifier");
  431. string prefix = QuotePrefix;
  432. string suffix = QuoteSuffix;
  433. if (QuotePrefix.Length == 0) {
  434. if (connection == null)
  435. throw new InvalidOperationException (
  436. "An open connection is required if "
  437. + "QuotePrefix is not set.");
  438. prefix = suffix = GetQuoteCharacter (connection);
  439. }
  440. if (prefix.Length > 0 && prefix != " ") {
  441. string escaped;
  442. if (suffix.Length > 0)
  443. escaped = unquotedIdentifier.Replace (
  444. suffix, suffix + suffix);
  445. else
  446. escaped = unquotedIdentifier;
  447. return string.Concat (prefix, escaped, suffix);
  448. }
  449. return unquotedIdentifier;
  450. }
  451. public string UnquoteIdentifier (string quotedIdentifier, OdbcConnection connection)
  452. {
  453. return UnquoteIdentifier (quotedIdentifier);
  454. }
  455. public override string UnquoteIdentifier (string quotedIdentifier)
  456. {
  457. if (quotedIdentifier == null || quotedIdentifier.Length == 0)
  458. return quotedIdentifier;
  459. StringBuilder sb = new StringBuilder (quotedIdentifier.Length);
  460. sb.Append (quotedIdentifier);
  461. if (quotedIdentifier.StartsWith (QuotePrefix))
  462. sb.Remove (0,QuotePrefix.Length);
  463. if (quotedIdentifier.EndsWith (QuoteSuffix))
  464. sb.Remove (sb.Length - QuoteSuffix.Length, QuoteSuffix.Length );
  465. return sb.ToString ();
  466. }
  467. private void OnRowUpdating (object sender, OdbcRowUpdatingEventArgs args)
  468. {
  469. if (args.Command != null)
  470. return;
  471. try {
  472. switch (args.StatementType) {
  473. case StatementType.Insert:
  474. args.Command = GetInsertCommand ();
  475. break;
  476. case StatementType.Update:
  477. args.Command = GetUpdateCommand ();
  478. break;
  479. case StatementType.Delete:
  480. args.Command = GetDeleteCommand ();
  481. break;
  482. }
  483. } catch (Exception e) {
  484. args.Errors = e;
  485. args.Status = UpdateStatus.ErrorsOccurred;
  486. }
  487. }
  488. string GetQuotedString (string unquotedIdentifier)
  489. {
  490. string prefix = QuotePrefix;
  491. string suffix = QuoteSuffix;
  492. if (prefix.Length == 0 && suffix.Length == 0)
  493. return unquotedIdentifier;
  494. return String.Format ("{0}{1}{2}", prefix,
  495. unquotedIdentifier, suffix);
  496. }
  497. bool IsCommandGenerated {
  498. get {
  499. return (_insertCommand != null || _updateCommand != null || _deleteCommand != null);
  500. }
  501. }
  502. string GetQuoteCharacter (OdbcConnection conn)
  503. {
  504. return conn.GetInfo (OdbcInfo.IdentifierQuoteChar);
  505. }
  506. #endregion // Methods
  507. }
  508. }