OleDbDataReader_NextResult.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. //
  2. // Copyright (c) 2006 Mainsoft Co.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Text;
  25. using System.Data;
  26. using System.Data.OleDb ;
  27. using MonoTests.System.Data.Utils;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OleDb
  30. {
  31. [TestFixture]
  32. public class OleDbDataReader_NextResult : ADONetTesterClass
  33. {
  34. OleDbConnection con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  35. Exception exp = null;
  36. [SetUp]
  37. public void SetUp() {
  38. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  39. con.Open();
  40. }
  41. [TearDown]
  42. public void TearDown() {
  43. if (con.State == ConnectionState.Open) con.Close();
  44. }
  45. public static void Main()
  46. {
  47. OleDbDataReader_NextResult tc = new OleDbDataReader_NextResult();
  48. Exception exp = null;
  49. try
  50. {
  51. tc.BeginTest("OleDbDataReader_NextResult");
  52. tc.run();
  53. }
  54. catch(Exception ex){exp = ex;}
  55. finally {tc.EndTest(exp);}
  56. }
  57. public void run()
  58. {
  59. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  60. con.Open();
  61. TestMultipleResultSetsWithSP();
  62. TestMultipleResultSetsWithSQLText();
  63. if (con.State == ConnectionState.Open) con.Close();
  64. }
  65. [Test]
  66. public void TestMultipleResultSetsWithSQLText()
  67. {
  68. if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
  69. {
  70. this.Log("Multiple result sets by sql text is not tested in oracle.");
  71. return;
  72. }
  73. if (ConnectedDataProvider.GetDbType() == DataBaseServer.DB2)
  74. {
  75. this.Log("Multiple result sets using compound statement not supported at DB2.");
  76. return;
  77. }
  78. bool NextResultExists = false;
  79. OleDbDataReader rdr = null;
  80. OleDbCommand cmd;
  81. int TblResult0=-1;
  82. int TblResult1=-1;
  83. int TblResult2=-1;
  84. try
  85. {
  86. BeginCase("Setup: Get expected results.");
  87. //get excpected results
  88. GetExcpectedResults(ref TblResult0, ref TblResult1, ref TblResult2);
  89. this.Pass("Setup: Get expected results ended.");
  90. }
  91. catch (Exception ex)
  92. {
  93. exp = ex;
  94. }
  95. finally
  96. {
  97. EndCase(exp);
  98. }
  99. string cmdTxt = BuildCommandText();
  100. cmd = new OleDbCommand(cmdTxt, con);
  101. cmd.CommandType = CommandType.Text;
  102. rdr = cmd.ExecuteReader();
  103. // -------------- ResultSet 1 ------------
  104. try
  105. {
  106. exp = null;
  107. BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 1 exists");
  108. Compare(rdr != null, true);
  109. }
  110. catch(Exception ex)
  111. {
  112. exp = ex;
  113. }
  114. finally
  115. {
  116. EndCase(exp);
  117. }
  118. try
  119. {
  120. exp = null;
  121. BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 1 contains data");
  122. NextResultExists = rdr.Read();
  123. Compare(NextResultExists, true);
  124. }
  125. catch(Exception ex)
  126. {
  127. exp = ex;
  128. }
  129. finally
  130. {
  131. EndCase(exp);
  132. }
  133. int i = 1;
  134. try
  135. {
  136. exp = null;
  137. BeginCase("(Multiple Resultsets sql text) - Check ResultSet 1 Data");
  138. while (rdr.Read())
  139. {
  140. i++;
  141. }
  142. Compare(i, TblResult0);
  143. }
  144. catch(Exception ex)
  145. {
  146. exp = ex;
  147. }
  148. finally
  149. {
  150. EndCase(exp);
  151. }
  152. try
  153. {
  154. exp = null;
  155. BeginCase("(Multiple Resultsets sql text) - Check ResultSet 1 Schema");
  156. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
  157. }
  158. catch(Exception ex)
  159. {
  160. exp = ex;
  161. }
  162. finally
  163. {
  164. EndCase(exp);
  165. }
  166. // -------------- ResultSet 2 ------------
  167. try
  168. {
  169. exp = null;
  170. BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 2 exists");
  171. NextResultExists = rdr.NextResult();
  172. Compare(NextResultExists, true);
  173. }
  174. catch(Exception ex)
  175. {
  176. exp = ex;
  177. }
  178. finally
  179. {
  180. EndCase(exp);
  181. }
  182. try
  183. {
  184. exp = null;
  185. BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 2 contains data");
  186. NextResultExists = rdr.Read();
  187. Compare(NextResultExists, true);
  188. }
  189. catch(Exception ex)
  190. {
  191. exp = ex;
  192. }
  193. finally
  194. {
  195. EndCase(exp);
  196. }
  197. try
  198. {
  199. exp = null;
  200. BeginCase("(Multiple Resultsets sql text) - Check ResultSet 2 Data");
  201. i = 1;
  202. while (rdr.Read())
  203. {
  204. i++;
  205. }
  206. Compare(i, TblResult1);
  207. }
  208. catch(Exception ex)
  209. {
  210. exp = ex;
  211. }
  212. finally
  213. {
  214. EndCase(exp);
  215. }
  216. try
  217. {
  218. exp = null;
  219. BeginCase("(Multiple Resultsets sql text) - Check ResultSet 2 Schema");
  220. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CATEGORYID");
  221. }
  222. catch(Exception ex)
  223. {
  224. exp = ex;
  225. }
  226. // -------------- ResultSet 3 ------------
  227. finally
  228. {
  229. EndCase(exp);
  230. }
  231. try
  232. {
  233. exp = null;
  234. BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 3 exists");
  235. NextResultExists = rdr.NextResult();
  236. Compare(NextResultExists, true);
  237. }
  238. catch(Exception ex)
  239. {
  240. exp = ex;
  241. }
  242. finally
  243. {
  244. EndCase(exp);
  245. }
  246. try
  247. {
  248. exp = null;
  249. BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 3 contains data");
  250. NextResultExists = rdr.Read();
  251. Compare(NextResultExists, true);
  252. }
  253. catch(Exception ex)
  254. {
  255. exp = ex;
  256. }
  257. finally
  258. {
  259. EndCase(exp);
  260. }
  261. try
  262. {
  263. exp = null;
  264. BeginCase("(Multiple Resultsets sql text) - Check ResultSet 3 Data");
  265. i = 1;
  266. while (rdr.Read())
  267. {
  268. i++;
  269. }
  270. Compare(i, TblResult2);
  271. }
  272. catch(Exception ex)
  273. {
  274. exp = ex;
  275. }
  276. finally
  277. {
  278. EndCase(exp);
  279. }
  280. try
  281. {
  282. exp = null;
  283. BeginCase("(Multiple Resultsets sql text) - Check ResultSet 3 Schema");
  284. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "REGIONID");
  285. }
  286. catch(Exception ex)
  287. {
  288. exp = ex;
  289. }
  290. finally
  291. {
  292. EndCase(exp);
  293. }
  294. try
  295. {
  296. exp = null;
  297. BeginCase("(Multiple Resultsets sql text) - Check that resultset 4 does not exist.");
  298. NextResultExists = rdr.NextResult();
  299. Compare(NextResultExists, false);
  300. }
  301. catch(Exception ex)
  302. {
  303. exp = ex;
  304. }
  305. finally
  306. {
  307. EndCase(exp);
  308. }
  309. try
  310. {
  311. exp = null;
  312. BeginCase("(Multiple Resultsets sql text) - Check that resultset 4 does not contain data.");
  313. NextResultExists = rdr.Read();
  314. Compare(NextResultExists, false);
  315. }
  316. catch(Exception ex)
  317. {
  318. exp = ex;
  319. }
  320. finally
  321. {
  322. EndCase(exp);
  323. }
  324. if (!rdr.IsClosed)
  325. rdr.Close();
  326. }
  327. [Test]
  328. public void TestMultipleResultSetsWithSP()
  329. {
  330. #if !JAVA
  331. if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
  332. {
  333. this.Log("Not testing Stored procedures with multiple ref-cursors on Oracle with .NET due to bug in .NET (only the first ref-cursor is retrived).");
  334. return;
  335. }
  336. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  337. {
  338. // fail to work on .NET OLEDB
  339. this.Log("Not testing PostgreSQL CommandType.StoredProcedure which return SETOF");
  340. return;
  341. }
  342. #endif
  343. bool NextResultExists = false;
  344. // transaction use was add for PostgreSQL
  345. OleDbTransaction tr = con.BeginTransaction();
  346. OleDbCommand cmd = new OleDbCommand("GH_MULTIRECORDSETS", con, tr);
  347. cmd.CommandType = CommandType.StoredProcedure;
  348. OleDbDataReader rdr = cmd.ExecuteReader();
  349. try
  350. {
  351. exp = null;
  352. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 exists");
  353. Compare(rdr != null, true);
  354. }
  355. catch(Exception ex)
  356. {
  357. exp = ex;
  358. }
  359. finally
  360. {
  361. EndCase(exp);
  362. }
  363. try
  364. {
  365. exp = null;
  366. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 contains data");
  367. NextResultExists = rdr.Read();
  368. Compare(NextResultExists, true);
  369. }
  370. catch(Exception ex)
  371. {
  372. exp = ex;
  373. }
  374. finally
  375. {
  376. EndCase(exp);
  377. }
  378. try
  379. {
  380. exp = null;
  381. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Data");
  382. Compare(rdr.GetValue(1).ToString(), "Yavine");
  383. }
  384. catch(Exception ex)
  385. {
  386. exp = ex;
  387. }
  388. finally
  389. {
  390. EndCase(exp);
  391. }
  392. try
  393. {
  394. exp = null;
  395. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Schema");
  396. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "EMPLOYEEID");
  397. }
  398. catch(Exception ex)
  399. {
  400. exp = ex;
  401. }
  402. finally
  403. {
  404. EndCase(exp);
  405. }
  406. // -------------- ResultSet 2 ------------
  407. try
  408. {
  409. exp = null;
  410. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 exists");
  411. NextResultExists = rdr.NextResult();
  412. Compare(NextResultExists, true);
  413. }
  414. catch(Exception ex)
  415. {
  416. exp = ex;
  417. }
  418. finally
  419. {
  420. EndCase(exp);
  421. }
  422. try
  423. {
  424. exp = null;
  425. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 contains data");
  426. NextResultExists = rdr.Read();
  427. Compare(NextResultExists, true);
  428. }
  429. catch(Exception ex)
  430. {
  431. exp = ex;
  432. }
  433. finally
  434. {
  435. EndCase(exp);
  436. }
  437. try
  438. {
  439. exp = null;
  440. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Data");
  441. Compare(rdr.GetValue(1).ToString(), "Morgenstern Gesundkost");
  442. }
  443. catch(Exception ex)
  444. {
  445. exp = ex;
  446. }
  447. finally
  448. {
  449. EndCase(exp);
  450. }
  451. try
  452. {
  453. exp = null;
  454. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Schema");
  455. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
  456. }
  457. catch(Exception ex)
  458. {
  459. exp = ex;
  460. }
  461. finally
  462. {
  463. EndCase(exp);
  464. }
  465. // -------------- ResultSet 3 ------------
  466. try
  467. {
  468. exp = null;
  469. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 exists");
  470. NextResultExists = rdr.NextResult();
  471. Compare(NextResultExists, true);
  472. }
  473. catch(Exception ex)
  474. {
  475. exp = ex;
  476. }
  477. finally
  478. {
  479. EndCase(exp);
  480. }
  481. try
  482. {
  483. exp = null;
  484. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 contains data");
  485. NextResultExists = rdr.Read();
  486. Compare(NextResultExists, false);
  487. }
  488. catch(Exception ex)
  489. {
  490. exp = ex;
  491. }
  492. finally
  493. {
  494. EndCase(exp);
  495. }
  496. try
  497. {
  498. exp = null;
  499. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 3 Schema");
  500. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "ORDERID");
  501. }
  502. catch(Exception ex)
  503. {
  504. exp = ex;
  505. }
  506. finally
  507. {
  508. EndCase(exp);
  509. }
  510. try
  511. {
  512. exp = null;
  513. BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not exist.");
  514. NextResultExists = rdr.NextResult();
  515. Compare(NextResultExists, false);
  516. }
  517. catch(Exception ex)
  518. {
  519. exp = ex;
  520. }
  521. finally
  522. {
  523. EndCase(exp);
  524. }
  525. try
  526. {
  527. exp = null;
  528. BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not contain data.");
  529. NextResultExists = rdr.Read();
  530. Compare(NextResultExists, false);
  531. }
  532. catch(Exception ex)
  533. {
  534. exp = ex;
  535. }
  536. finally
  537. {
  538. EndCase(exp);
  539. }
  540. //Cleanup:
  541. if (!rdr.IsClosed)
  542. {
  543. rdr.Close();
  544. }
  545. // transaction use was add for PostgreSQL
  546. tr.Commit();
  547. }
  548. #region "Private Utilities"
  549. private string BuildCommandText()
  550. {
  551. string beginStatement;
  552. string endStatement;
  553. string commandDelimiter;
  554. string[] commands = new string[] {"select * from Customers", "select * from Categories", "select * from Region"};
  555. GetDBSpecificSyntax(ConnectedDataProvider.GetDbType(), out beginStatement, out endStatement, out commandDelimiter);
  556. StringBuilder cmdBuilder = new StringBuilder();
  557. cmdBuilder.Append(beginStatement);
  558. cmdBuilder.Append(" ");
  559. foreach (string statement in commands)
  560. {
  561. cmdBuilder.Append(statement);
  562. cmdBuilder.Append(commandDelimiter);
  563. cmdBuilder.Append(" ");
  564. }
  565. cmdBuilder.Append(endStatement);
  566. return cmdBuilder.ToString();
  567. }
  568. private void GetDBSpecificSyntax(DataBaseServer dbServer, out string beginStatement, out string endStatement, out string commandDelimiter)
  569. {
  570. switch (dbServer)
  571. {
  572. case DataBaseServer.SQLServer:
  573. beginStatement = "BEGIN";
  574. endStatement = "END";
  575. commandDelimiter = ";";
  576. break;
  577. case DataBaseServer.Sybase:
  578. beginStatement = "BEGIN";
  579. endStatement = "END";
  580. commandDelimiter = "\r\n";
  581. break;
  582. case DataBaseServer.Oracle:
  583. beginStatement = "BEGIN";
  584. endStatement = "END;";
  585. commandDelimiter = ";";
  586. break;
  587. case DataBaseServer.DB2:
  588. {
  589. beginStatement = "";
  590. endStatement = "";
  591. }
  592. commandDelimiter = ";";
  593. break;
  594. case DataBaseServer.PostgreSQL:
  595. beginStatement = "";
  596. endStatement = "";
  597. commandDelimiter = ";";
  598. break;
  599. default:
  600. this.Fail("Unknown DataBaseServer type");
  601. throw new ApplicationException("Unknown DataBaseServer type");
  602. }
  603. }
  604. private void GetExcpectedResults(ref int TblResult0, ref int TblResult1, ref int TblResult2)
  605. {
  606. // get excpected results
  607. // transaction use was add for PostgreSQL
  608. OleDbTransaction tr = con.BeginTransaction();
  609. OleDbCommand cmd = new OleDbCommand("", con,tr);
  610. cmd.CommandText = "Select count(*) from Customers";
  611. TblResult0 = Int32.Parse(cmd.ExecuteScalar().ToString());
  612. cmd.CommandText = "Select count(*) from Categories";
  613. TblResult1 = Int32.Parse(cmd.ExecuteScalar().ToString());
  614. cmd.CommandText = "Select count(*) from Region";
  615. TblResult2 = Int32.Parse(cmd.ExecuteScalar().ToString());
  616. tr.Commit();
  617. }
  618. #endregion
  619. }
  620. }