OdbcCommandBuilderTest.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. // OdbcCommandBuilderTest.cs - NUnit Test Cases for testing the
  2. // OdbcCommandBuilder Test.
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed on the
  8. // ChangeLog entries.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person
  12. // obtaining a copy of this software and associated documentation
  13. // files (the "Software"), to deal in the Software without
  14. // restriction, including without limitation the rights to use, copy,
  15. // modify, merge, publish, distribute, sublicense, and/or sell copies
  16. // of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. // SOFTWARE.
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Data.Odbc;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Data.Connected.Odbc
  36. {
  37. [TestFixture]
  38. [Category ("odbc")]
  39. public class OdbcCommandBuilderTest
  40. {
  41. [Test]
  42. public void GetInsertCommandTest ()
  43. {
  44. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  45. OdbcCommand cmd = null;
  46. try {
  47. string selectQuery = "select id, lname from employee where id = 3";
  48. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  49. DataSet ds = new DataSet ();
  50. da.Fill (ds, "IntTest");
  51. Assert.AreEqual (1, ds.Tables.Count);
  52. OdbcCommandBuilder cb;
  53. cb = new OdbcCommandBuilder (da);
  54. cmd = cb.GetInsertCommand ();
  55. Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
  56. cmd.CommandText, "#A1");
  57. Assert.AreSame (conn, cmd.Connection, "#A2");
  58. AssertInsertParameters (cmd, "#A3:");
  59. cb = new OdbcCommandBuilder (da);
  60. cb.QuotePrefix = "\"";
  61. cmd = cb.GetInsertCommand ();
  62. Assert.AreEqual ("INSERT INTO \"employee (\"id, \"lname) VALUES (?, ?)",
  63. cmd.CommandText, "#B1");
  64. Assert.AreSame (conn, cmd.Connection, "#B2");
  65. AssertInsertParameters (cmd, "#B3:");
  66. cb = new OdbcCommandBuilder (da);
  67. cb.QuoteSuffix = "´";
  68. cmd = cb.GetInsertCommand ();
  69. Assert.AreEqual ("INSERT INTO employee´ (id´, lname´) VALUES (?, ?)",
  70. cmd.CommandText, "#C1");
  71. Assert.AreSame (conn, cmd.Connection, "#C2");
  72. AssertInsertParameters (cmd, "#C3:");
  73. cb = new OdbcCommandBuilder (da);
  74. cb.QuotePrefix = "\"";
  75. cb.QuoteSuffix = "´";
  76. cmd = cb.GetInsertCommand ();
  77. Assert.AreEqual ("INSERT INTO \"employee´ (\"id´, \"lname´) VALUES (?, ?)",
  78. cmd.CommandText, "#D1");
  79. Assert.AreSame (conn, cmd.Connection, "#D2");
  80. AssertInsertParameters (cmd, "#D3:");
  81. } finally {
  82. if (cmd != null)
  83. cmd.Dispose ();
  84. ConnectionManager.Instance.Odbc.CloseConnection ();
  85. }
  86. }
  87. [Test]
  88. public void GetInsertCommandTestWithExpression ()
  89. {
  90. if (ConnectionManager.Instance.Odbc.EngineConfig.Type == EngineType.MySQL)
  91. Assert.Ignore ("Schema info from MySQL is incomplete");
  92. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  93. OdbcCommand cmd = null;
  94. try {
  95. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  96. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  97. DataSet ds = new DataSet ();
  98. da.Fill (ds, "IntTest");
  99. Assert.AreEqual (1, ds.Tables.Count);
  100. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  101. cmd = cb.GetInsertCommand ();
  102. Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
  103. cmd.CommandText, "#1");
  104. Assert.AreSame (conn, cmd.Connection, "#2");
  105. AssertInsertParameters (cmd, "#3:");
  106. } finally {
  107. if (cmd != null)
  108. cmd.Dispose ();
  109. ConnectionManager.Instance.Odbc.CloseConnection ();
  110. }
  111. }
  112. [Test]
  113. public void GetUpdateCommandTest ()
  114. {
  115. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  116. OdbcCommand cmd = null;
  117. try {
  118. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  119. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  120. DataSet ds = new DataSet ();
  121. da.Fill (ds, "IntTest");
  122. Assert.AreEqual (1, ds.Tables.Count);
  123. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  124. cmd = cb.GetUpdateCommand ();
  125. Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  126. cmd.CommandText, "#A1");
  127. Assert.AreSame (conn, cmd.Connection, "#A2");
  128. AssertUpdateParameters (cmd, "#A3:");
  129. cb = new OdbcCommandBuilder (da);
  130. cb.QuotePrefix = "\"";
  131. cmd = cb.GetUpdateCommand ();
  132. Assert.AreEqual ("UPDATE \"employee SET \"id = ?, \"lname = ? WHERE ((\"id = ?) AND ((? = 1 AND \"lname IS NULL) OR (\"lname = ?)))",
  133. cmd.CommandText, "#B1");
  134. Assert.AreSame (conn, cmd.Connection, "#B2");
  135. AssertUpdateParameters (cmd, "#B3:");
  136. cb = new OdbcCommandBuilder (da);
  137. cb.QuoteSuffix = "´";
  138. cmd = cb.GetUpdateCommand ();
  139. Assert.AreEqual ("UPDATE employee´ SET id´ = ?, lname´ = ? WHERE ((id´ = ?) AND ((? = 1 AND lname´ IS NULL) OR (lname´ = ?)))",
  140. cmd.CommandText, "#C1");
  141. Assert.AreSame (conn, cmd.Connection, "#C2");
  142. AssertUpdateParameters (cmd, "#C3:");
  143. cb = new OdbcCommandBuilder (da);
  144. cb.QuotePrefix = "\"";
  145. cb.QuoteSuffix = "´";
  146. cmd = cb.GetUpdateCommand ();
  147. Assert.AreEqual ("UPDATE \"employee´ SET \"id´ = ?, \"lname´ = ? WHERE ((\"id´ = ?) AND ((? = 1 AND \"lname´ IS NULL) OR (\"lname´ = ?)))",
  148. cmd.CommandText, "#D1");
  149. Assert.AreSame (conn, cmd.Connection, "#D2");
  150. AssertUpdateParameters (cmd, "#D3:");
  151. } finally {
  152. if (cmd != null)
  153. cmd.Dispose ();
  154. ConnectionManager.Instance.Odbc.CloseConnection ();
  155. }
  156. }
  157. [Test]
  158. [Ignore ("FIXME: Auto SQL generation during Update requires a valid SelectCommand")]
  159. public void GetUpdateCommandDBConcurrencyExceptionTest ()
  160. {
  161. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  162. try {
  163. string selectQuery = "select id, lname from employee where id = 3";
  164. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  165. DataSet ds = new DataSet ();
  166. da.Fill (ds, "IntTest");
  167. Assert.AreEqual (1, ds.Tables.Count);
  168. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  169. Assert.IsNotNull (cb);
  170. DataRow [] rows = ds.Tables [0].Select ("id=1");
  171. rows [0] [0] = 6660; // non existent
  172. ds.Tables [0].AcceptChanges (); // moves 6660 to original value
  173. rows [0] [0] = 1; // moves 6660 as search key into db table
  174. try {
  175. da.Update (rows);
  176. Assert.Fail ("#1");
  177. } catch (DBConcurrencyException ex) {
  178. // Concurrency violation: the UpdateCommand
  179. // affected 0 records
  180. Assert.AreEqual (typeof (DBConcurrencyException), ex.GetType (), "#2");
  181. Assert.IsNull (ex.InnerException, "#3");
  182. Assert.IsNotNull (ex.Message, "#4");
  183. Assert.AreSame (rows [0], ex.Row, "#5");
  184. Assert.AreEqual (1, ex.RowCount, "#6");
  185. }
  186. } finally {
  187. ConnectionManager.Instance.Odbc.CloseConnection ();
  188. }
  189. }
  190. [Test]
  191. [Category("NotWorking")]
  192. public void GetInsertCommandTest_option_true ()
  193. {
  194. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  195. try {
  196. string selectQuery = "select id, lname from employee where id = 3";
  197. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  198. DataSet ds = new DataSet ();
  199. da.Fill (ds, "IntTest");
  200. Assert.AreEqual (1, ds.Tables.Count);
  201. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  202. OdbcCommand cmd = cb.GetInsertCommand (true);
  203. Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
  204. cmd.CommandText, "#1");
  205. Assert.AreSame (conn, cmd.Connection, "#2");
  206. AssertInsertParameters (cmd, "#3:");
  207. } finally {
  208. ConnectionManager.Instance.Odbc.CloseConnection ();
  209. }
  210. }
  211. [Test]
  212. public void GetInsertCommandTest_option_false ()
  213. {
  214. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  215. try {
  216. string selectQuery = "select id, lname from employee where id = 3";
  217. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  218. DataSet ds = new DataSet ();
  219. da.Fill (ds, "IntTest");
  220. Assert.AreEqual (1, ds.Tables.Count);
  221. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  222. OdbcCommand cmd = cb.GetInsertCommand (false);
  223. Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
  224. cmd.CommandText, "#1");
  225. Assert.AreSame (conn, cmd.Connection, "#2");
  226. AssertInsertParameters (cmd, "#3:");
  227. } finally {
  228. ConnectionManager.Instance.Odbc.CloseConnection ();
  229. }
  230. }
  231. [Test]
  232. [Category("NotWorking")]
  233. public void GetUpdateCommandTest_option_true ()
  234. {
  235. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  236. try {
  237. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  238. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  239. DataSet ds = new DataSet ();
  240. da.Fill (ds, "IntTest");
  241. Assert.AreEqual (1, ds.Tables.Count);
  242. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  243. OdbcCommand cmd = cb.GetUpdateCommand (true);
  244. Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  245. cmd.CommandText, "#1");
  246. Assert.AreSame (conn, cmd.Connection, "#2");
  247. AssertUpdateParameters (cmd, "#3:");
  248. } finally {
  249. ConnectionManager.Instance.Odbc.CloseConnection ();
  250. }
  251. }
  252. [Test]
  253. public void GetUpdateCommandTest_option_false ()
  254. {
  255. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  256. try {
  257. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  258. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  259. DataSet ds = new DataSet ();
  260. da.Fill (ds, "IntTest");
  261. Assert.AreEqual (1, ds.Tables.Count);
  262. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  263. OdbcCommand cmd = cb.GetUpdateCommand (false);
  264. Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  265. cmd.CommandText, "#1");
  266. Assert.AreSame (conn, cmd.Connection, "#2");
  267. AssertUpdateParameters (cmd, "#3:");
  268. } finally {
  269. ConnectionManager.Instance.Odbc.CloseConnection ();
  270. }
  271. }
  272. [Test]
  273. [Category("NotWorking")]
  274. public void GetDeleteCommandTest_option_true ()
  275. {
  276. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  277. try {
  278. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  279. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  280. DataSet ds = new DataSet ();
  281. da.Fill (ds, "IntTest");
  282. Assert.AreEqual (1, ds.Tables.Count);
  283. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  284. OdbcCommand cmd = cb.GetDeleteCommand (true);
  285. Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  286. cmd.CommandText, "#1");
  287. Assert.AreSame (conn, cmd.Connection, "#2");
  288. AssertDeleteParameters (cmd, "#3:");
  289. } finally {
  290. ConnectionManager.Instance.Odbc.CloseConnection ();
  291. }
  292. }
  293. [Test]
  294. public void GetDeleteCommandTest_option_false ()
  295. {
  296. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  297. try {
  298. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  299. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  300. DataSet ds = new DataSet ();
  301. da.Fill (ds, "IntTest");
  302. Assert.AreEqual (1, ds.Tables.Count);
  303. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  304. OdbcCommand cmd = cb.GetDeleteCommand (false);
  305. Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  306. cmd.CommandText, "#1");
  307. Assert.AreSame (conn, cmd.Connection, "#2");
  308. AssertDeleteParameters (cmd, "#3:");
  309. } finally {
  310. ConnectionManager.Instance.Odbc.CloseConnection ();
  311. }
  312. }
  313. [Test]
  314. public void GetDeleteCommandTest ()
  315. {
  316. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  317. OdbcCommand cmd = null;
  318. try {
  319. string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
  320. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  321. DataSet ds = new DataSet ();
  322. da.Fill (ds, "IntTest");
  323. Assert.AreEqual (1, ds.Tables.Count);
  324. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  325. cmd = cb.GetDeleteCommand ();
  326. Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
  327. cmd.CommandText, "#A1");
  328. Assert.AreSame (conn, cmd.Connection, "#A2");
  329. AssertDeleteParameters (cmd, "#A3:");
  330. cb = new OdbcCommandBuilder (da);
  331. cb.QuotePrefix = "\"";
  332. cmd = cb.GetDeleteCommand ();
  333. Assert.AreEqual ("DELETE FROM \"employee WHERE ((\"id = ?) AND ((? = 1 AND \"lname IS NULL) OR (\"lname = ?)))",
  334. cmd.CommandText, "#B1");
  335. Assert.AreSame (conn, cmd.Connection, "#B2");
  336. AssertDeleteParameters (cmd, "#B3:");
  337. cb = new OdbcCommandBuilder (da);
  338. cb.QuoteSuffix = "´";
  339. cmd = cb.GetDeleteCommand ();
  340. Assert.AreEqual ("DELETE FROM employee´ WHERE ((id´ = ?) AND ((? = 1 AND lname´ IS NULL) OR (lname´ = ?)))",
  341. cmd.CommandText, "#C1");
  342. Assert.AreSame (conn, cmd.Connection, "#C2");
  343. AssertDeleteParameters (cmd, "#C3:");
  344. cb = new OdbcCommandBuilder (da);
  345. cb.QuotePrefix = "\"";
  346. cb.QuoteSuffix = "´";
  347. cmd = cb.GetDeleteCommand ();
  348. Assert.AreEqual ("DELETE FROM \"employee´ WHERE ((\"id´ = ?) AND ((? = 1 AND \"lname´ IS NULL) OR (\"lname´ = ?)))",
  349. cmd.CommandText, "#D1");
  350. Assert.AreSame (conn, cmd.Connection, "#D2");
  351. AssertDeleteParameters (cmd, "#D3:");
  352. } finally {
  353. if (cmd != null)
  354. cmd.Dispose ();
  355. ConnectionManager.Instance.Odbc.CloseConnection ();
  356. }
  357. }
  358. [Test]
  359. [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
  360. public void QuotePrefix_DeleteCommand_Generated ()
  361. {
  362. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  363. OdbcCommand cmd = null;
  364. try {
  365. string selectQuery = "select id, lname from employee where id = 3";
  366. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  367. DataSet ds = new DataSet ();
  368. da.Fill (ds, "IntTest");
  369. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  370. cmd = cb.GetDeleteCommand ();
  371. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
  372. try {
  373. cb.QuotePrefix = "";
  374. Assert.Fail ("#2");
  375. } catch (InvalidOperationException ex) {
  376. // The QuotePrefix and QuoteSuffix properties
  377. // cannot be changed once an Insert, Update, or
  378. // Delete command has been generated
  379. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
  380. Assert.IsNull (ex.InnerException, "#4");
  381. Assert.IsNotNull (ex.Message, "#5");
  382. }
  383. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
  384. cb.RefreshSchema ();
  385. cb.QuotePrefix = "";
  386. } finally {
  387. if (cmd != null)
  388. cmd.Dispose ();
  389. ConnectionManager.Instance.Odbc.CloseConnection ();
  390. }
  391. }
  392. [Test]
  393. [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
  394. public void QuotePrefix_InsertCommand_Generated ()
  395. {
  396. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  397. OdbcCommand cmd = null;
  398. try {
  399. string selectQuery = "select id, lname from employee where id = 3";
  400. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  401. DataSet ds = new DataSet ();
  402. da.Fill (ds, "IntTest");
  403. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  404. cmd = cb.GetInsertCommand ();
  405. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
  406. try {
  407. cb.QuotePrefix = "";
  408. Assert.Fail ("#2");
  409. } catch (InvalidOperationException ex) {
  410. // The QuotePrefix and QuoteSuffix properties
  411. // cannot be changed once an Insert, Update, or
  412. // Delete command has been generated
  413. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
  414. Assert.IsNull (ex.InnerException, "#4");
  415. Assert.IsNotNull (ex.Message, "#5");
  416. }
  417. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
  418. cb.RefreshSchema ();
  419. cb.QuotePrefix = "";
  420. } finally {
  421. if (cmd != null)
  422. cmd.Dispose ();
  423. ConnectionManager.Instance.Odbc.CloseConnection ();
  424. }
  425. }
  426. [Test]
  427. [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
  428. public void QuotePrefix_UpdateCommand_Generated ()
  429. {
  430. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  431. OdbcCommand cmd = null;
  432. try {
  433. string selectQuery = "select id, lname from employee where id = 3";
  434. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  435. DataSet ds = new DataSet ();
  436. da.Fill (ds, "IntTest");
  437. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  438. cmd = cb.GetUpdateCommand ();
  439. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
  440. try {
  441. cb.QuotePrefix = "";
  442. Assert.Fail ("#2");
  443. } catch (InvalidOperationException ex) {
  444. // The QuotePrefix and QuoteSuffix properties
  445. // cannot be changed once an Insert, Update, or
  446. // Delete command has been generated
  447. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
  448. Assert.IsNull (ex.InnerException, "#4");
  449. Assert.IsNotNull (ex.Message, "#5");
  450. }
  451. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
  452. cb.RefreshSchema ();
  453. cb.QuotePrefix = "";
  454. } finally {
  455. if (cmd != null)
  456. cmd.Dispose ();
  457. ConnectionManager.Instance.Odbc.CloseConnection ();
  458. }
  459. }
  460. [Test]
  461. [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
  462. public void QuoteSuffix_DeleteCommand_Generated ()
  463. {
  464. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  465. OdbcCommand cmd = null;
  466. try {
  467. string selectQuery = "select id, lname from employee where id = 3";
  468. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  469. DataSet ds = new DataSet ();
  470. da.Fill (ds, "IntTest");
  471. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  472. cmd = cb.GetDeleteCommand ();
  473. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
  474. try {
  475. cb.QuoteSuffix = "";
  476. Assert.Fail ("#2");
  477. } catch (InvalidOperationException ex) {
  478. // The QuotePrefix and QuoteSuffix properties
  479. // cannot be changed once an Insert, Update, or
  480. // Delete command has been generated
  481. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
  482. Assert.IsNull (ex.InnerException, "#4");
  483. Assert.IsNotNull (ex.Message, "#5");
  484. }
  485. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#6");
  486. cb.RefreshSchema ();
  487. cb.QuoteSuffix = "";
  488. } finally {
  489. if (cmd != null)
  490. cmd.Dispose ();
  491. ConnectionManager.Instance.Odbc.CloseConnection ();
  492. }
  493. }
  494. [Test]
  495. [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
  496. public void QuoteSuffix_InsertCommand_Generated ()
  497. {
  498. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  499. OdbcCommand cmd = null;
  500. try {
  501. string selectQuery = "select id, lname from employee where id = 3";
  502. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  503. DataSet ds = new DataSet ();
  504. da.Fill (ds, "IntTest");
  505. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  506. cmd = cb.GetInsertCommand ();
  507. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
  508. try {
  509. cb.QuoteSuffix = "";
  510. Assert.Fail ("#2");
  511. } catch (InvalidOperationException ex) {
  512. // The QuotePrefix and QuoteSuffix properties
  513. // cannot be changed once an Insert, Update, or
  514. // Delete command has been generated
  515. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
  516. Assert.IsNull (ex.InnerException, "#4");
  517. Assert.IsNotNull (ex.Message, "#5");
  518. }
  519. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#6");
  520. cb.RefreshSchema ();
  521. cb.QuoteSuffix = "";
  522. } finally {
  523. if (cmd != null)
  524. cmd.Dispose ();
  525. ConnectionManager.Instance.Odbc.CloseConnection ();
  526. }
  527. }
  528. [Test]
  529. [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
  530. public void QuoteSuffix_UpdateCommand_Generated ()
  531. {
  532. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  533. OdbcCommand cmd = null;
  534. try {
  535. string selectQuery = "select id, lname from employee where id = 3";
  536. OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
  537. DataSet ds = new DataSet ();
  538. da.Fill (ds, "IntTest");
  539. OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
  540. cmd = cb.GetUpdateCommand ();
  541. Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
  542. try {
  543. cb.QuoteSuffix = "";
  544. Assert.Fail ("#2");
  545. } catch (InvalidOperationException ex) {
  546. // The QuotePrefix and QuoteSuffix properties
  547. // cannot be changed once an Insert, Update, or
  548. // Delete command has been generated
  549. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
  550. Assert.IsNull (ex.InnerException, "#4");
  551. Assert.IsNotNull (ex.Message, "#5");
  552. }
  553. Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
  554. cb.RefreshSchema ();
  555. cb.QuoteSuffix = "";
  556. } finally {
  557. if (cmd != null)
  558. cmd.Dispose ();
  559. ConnectionManager.Instance.Odbc.CloseConnection ();
  560. }
  561. }
  562. [Test] // QuoteIdentifier (String, OdbcConnection)
  563. public void QuoteIdentifier2 ()
  564. {
  565. OdbcCommandBuilder cb;
  566. OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
  567. string quote_char = ConnectionManager.Instance.Odbc.EngineConfig.QuoteCharacter;
  568. try {
  569. cb = new OdbcCommandBuilder ();
  570. Assert.AreEqual (quote_char + "mono" + quote_char, cb.QuoteIdentifier ("mono", conn), "#A1");
  571. Assert.AreEqual (quote_char + "Z" + quote_char, cb.QuoteIdentifier ("Z", conn), "#A2");
  572. Assert.AreEqual (quote_char + "abc" + quote_char, cb.QuoteIdentifier ("abc", conn), "#A3");
  573. Assert.AreEqual (quote_char + quote_char, cb.QuoteIdentifier (string.Empty, conn), "#A4");
  574. Assert.AreEqual (quote_char + " " + quote_char, cb.QuoteIdentifier (" ", conn), "#A5");
  575. Assert.AreEqual (quote_char + "\r" + quote_char, cb.QuoteIdentifier ("\r", conn), "#A6");
  576. cb.QuoteSuffix = "def";
  577. Assert.AreEqual (quote_char + "mono" + quote_char, cb.QuoteIdentifier ("mono", conn), "#A7");
  578. Assert.AreEqual (quote_char + "Z" + quote_char, cb.QuoteIdentifier ("Z", conn), "#A8");
  579. Assert.AreEqual (quote_char + "abc" + quote_char, cb.QuoteIdentifier ("abc", conn), "#A9");
  580. Assert.AreEqual (quote_char + quote_char, cb.QuoteIdentifier (string.Empty, conn), "#A10");
  581. Assert.AreEqual (quote_char + " " + quote_char, cb.QuoteIdentifier (" ", conn), "#A11");
  582. Assert.AreEqual (quote_char + "\r" + quote_char, cb.QuoteIdentifier ("\r", conn), "#A12");
  583. cb = new OdbcCommandBuilder ();
  584. cb.QuotePrefix = "abc";
  585. Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono", conn), "#B1");
  586. Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z", conn), "#B2");
  587. Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc", conn), "#B3");
  588. Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty, conn), "#B4");
  589. Assert.AreEqual ("abc ", cb.QuoteIdentifier (" ", conn), "#B5");
  590. Assert.AreEqual ("abc\r", cb.QuoteIdentifier ("\r", conn), "#B6");
  591. cb.QuoteSuffix = "def";
  592. Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono", conn), "#B7");
  593. Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z", conn), "#B8");
  594. Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc", conn), "#B9");
  595. Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty, conn), "#B10");
  596. Assert.AreEqual ("abc def", cb.QuoteIdentifier (" ", conn), "#B11");
  597. Assert.AreEqual ("abc\rdef", cb.QuoteIdentifier ("\r", conn), "#B12");
  598. cb.QuotePrefix = string.Empty;
  599. cb = new OdbcCommandBuilder ();
  600. cb.QuotePrefix = "X";
  601. Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono", conn), "#D1");
  602. Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z", conn), "#D2");
  603. Assert.AreEqual ("XX", cb.QuoteIdentifier ("X", conn), "#D3");
  604. Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty, conn), "#D4");
  605. Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D5");
  606. Assert.AreEqual ("X\r", cb.QuoteIdentifier ("\r", conn), "#D6");
  607. cb.QuoteSuffix = " ";
  608. Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono", conn), "#D7");
  609. Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z", conn), "#D8");
  610. Assert.AreEqual ("XX ", cb.QuoteIdentifier ("X", conn), "#D9");
  611. Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty, conn), "#D10");
  612. Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D11");
  613. Assert.AreEqual ("X\r ", cb.QuoteIdentifier ("\r", conn), "#D12");
  614. cb = new OdbcCommandBuilder ();
  615. cb.QuotePrefix = " ";
  616. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E1");
  617. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E2");
  618. Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E3");
  619. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E4");
  620. Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E5");
  621. Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E6");
  622. cb.QuoteSuffix = "def";
  623. Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E7");
  624. Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E8");
  625. Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E9");
  626. Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E10");
  627. Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E11");
  628. Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E12");
  629. } finally {
  630. ConnectionManager.Instance.Odbc.CloseConnection ();
  631. }
  632. }
  633. void AssertInsertParameters (OdbcCommand cmd, string prefix)
  634. {
  635. Assert.AreEqual (2, cmd.Parameters.Count, prefix + "Count");
  636. Assert.AreEqual (DbType.Int32, cmd.Parameters [0].DbType, prefix + "DbType (0)");
  637. Assert.AreEqual ("p1", cmd.Parameters [0].ParameterName, prefix + "ParameterName (0)");
  638. Assert.AreEqual ("id", cmd.Parameters [0].SourceColumn, prefix + "SourceColumn (0)");
  639. Assert.IsNull (cmd.Parameters [0].Value, prefix + "Value (0)");
  640. Assert.AreEqual (DbType.String, cmd.Parameters [1].DbType, prefix + "DbType (1)");
  641. Assert.AreEqual ("p2", cmd.Parameters [1].ParameterName, prefix + "ParameterName (1)");
  642. Assert.AreEqual ("lname", cmd.Parameters [1].SourceColumn, prefix + "SourceColumn (1)");
  643. Assert.IsNull (cmd.Parameters [1].Value, prefix + "Value (1)");
  644. }
  645. void AssertUpdateParameters (OdbcCommand cmd, string prefix)
  646. {
  647. Assert.AreEqual (5, cmd.Parameters.Count, prefix + "Count");
  648. Assert.AreEqual (DbType.Int32, cmd.Parameters [0].DbType, prefix + "DbType (0)");
  649. Assert.AreEqual ("p1", cmd.Parameters [0].ParameterName, prefix + "ParameterName (0)");
  650. Assert.AreEqual ("id", cmd.Parameters [0].SourceColumn, prefix + "SourceColumn (0)");
  651. Assert.IsNull (cmd.Parameters [0].Value, prefix + "Value (0)");
  652. Assert.AreEqual (DbType.String, cmd.Parameters [1].DbType, prefix + "DbType (1)");
  653. Assert.AreEqual ("p2", cmd.Parameters [1].ParameterName, prefix + "ParameterName (1)");
  654. Assert.AreEqual ("lname", cmd.Parameters [1].SourceColumn, prefix + "SourceColumn (1)");
  655. Assert.IsNull (cmd.Parameters [1].Value, prefix + "Value (1)");
  656. Assert.AreEqual (DbType.Int32, cmd.Parameters [2].DbType, prefix + "DbType (2)");
  657. Assert.AreEqual ("p3", cmd.Parameters [2].ParameterName, prefix + "ParameterName (2)");
  658. Assert.AreEqual ("id", cmd.Parameters [2].SourceColumn, prefix + "SourceColumn (2)");
  659. Assert.IsNull (cmd.Parameters [2].Value, prefix + "Value (2)");
  660. Assert.AreEqual (DbType.Int32, cmd.Parameters [3].DbType, prefix + "DbType (3)");
  661. Assert.AreEqual ("p4", cmd.Parameters [3].ParameterName, prefix + "ParameterName (3)");
  662. Assert.AreEqual ("lname", cmd.Parameters [3].SourceColumn, prefix + "SourceColumn (3)");
  663. Assert.AreEqual (1, cmd.Parameters [3].Value, prefix + "Value (3)");
  664. Assert.AreEqual (DbType.String, cmd.Parameters [4].DbType, prefix + "DbType (4)");
  665. Assert.AreEqual ("p5", cmd.Parameters [4].ParameterName, prefix + "ParameterName (4)");
  666. Assert.AreEqual ("lname", cmd.Parameters [4].SourceColumn, prefix + "SourceColumn (4)");
  667. Assert.IsNull (cmd.Parameters [4].Value, prefix + "Value (4)");
  668. }
  669. void AssertDeleteParameters (OdbcCommand cmd, string prefix)
  670. {
  671. Assert.AreEqual (3, cmd.Parameters.Count, prefix + "Count");
  672. Assert.AreEqual (DbType.Int32, cmd.Parameters [0].DbType, prefix + "DbType (0)");
  673. Assert.AreEqual ("p1", cmd.Parameters [0].ParameterName, prefix + "ParameterName (0)");
  674. Assert.AreEqual ("id", cmd.Parameters [0].SourceColumn, prefix + "SourceColumn (0)");
  675. Assert.IsNull (cmd.Parameters [0].Value, prefix + "Value (0)");
  676. Assert.AreEqual (DbType.Int32, cmd.Parameters [1].DbType, prefix + "DbType (1)");
  677. Assert.AreEqual ("p2", cmd.Parameters [1].ParameterName, prefix + "ParameterName (1)");
  678. Assert.AreEqual ("lname", cmd.Parameters [1].SourceColumn, prefix + "SourceColumn (1)");
  679. Assert.AreEqual (1, cmd.Parameters [1].Value, prefix + "Value (1)");
  680. Assert.AreEqual (DbType.String, cmd.Parameters [2].DbType, prefix + "DbType (2)");
  681. Assert.AreEqual ("p3", cmd.Parameters [2].ParameterName, prefix + "ParameterName (2)");
  682. Assert.AreEqual ("lname", cmd.Parameters [2].SourceColumn, prefix + "SourceColumn (2)");
  683. Assert.IsNull (cmd.Parameters [2].Value, prefix + "Value (2)");
  684. }
  685. // FIXME: test SetAllValues
  686. // FIXME: Add tests for examining RowError
  687. // FIXME: Add test for ContinueUpdateOnError property
  688. }
  689. }