OdbcCommandBuilderTest.cs 30 KB

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