OracleDataReader_NextResult.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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.OracleClient ;
  27. using MonoTests.System.Data.Utils;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OracleClient
  30. {
  31. [TestFixture]
  32. public class OracleDataReader_NextResult : ADONetTesterClass
  33. {
  34. OracleConnection con = new OracleConnection(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. OracleDataReader_NextResult tc = new OracleDataReader_NextResult();
  48. Exception exp = null;
  49. try
  50. {
  51. tc.BeginTest("OracleDataReader_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. OracleDataReader rdr = null;
  80. OracleCommand 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 OracleCommand(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. OracleTransaction tr = con.BeginTransaction();
  346. OracleCommand cmd = new OracleCommand("GH_MULTIRECORDSETS", con, tr);
  347. cmd.Parameters.Add(new OracleParameter("RCT_Employees", OracleType.Cursor)).Direction = ParameterDirection.Output;
  348. cmd.Parameters.Add(new OracleParameter("RCT_Customers", OracleType.Cursor)).Direction = ParameterDirection.Output;
  349. cmd.Parameters.Add(new OracleParameter("RCT_Orders", OracleType.Cursor)).Direction = ParameterDirection.Output;
  350. cmd.CommandType = CommandType.StoredProcedure;
  351. OracleDataReader rdr = cmd.ExecuteReader();
  352. try
  353. {
  354. exp = null;
  355. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 exists");
  356. Compare(rdr != null, true);
  357. }
  358. catch(Exception ex)
  359. {
  360. exp = ex;
  361. }
  362. finally
  363. {
  364. EndCase(exp);
  365. }
  366. try
  367. {
  368. exp = null;
  369. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 contains data");
  370. NextResultExists = rdr.Read();
  371. Compare(NextResultExists, true);
  372. }
  373. catch(Exception ex)
  374. {
  375. exp = ex;
  376. }
  377. finally
  378. {
  379. EndCase(exp);
  380. }
  381. try
  382. {
  383. exp = null;
  384. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Data");
  385. Compare(rdr.GetValue(1).ToString(), "Yavine");
  386. }
  387. catch(Exception ex)
  388. {
  389. exp = ex;
  390. }
  391. finally
  392. {
  393. EndCase(exp);
  394. }
  395. try
  396. {
  397. exp = null;
  398. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Schema");
  399. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "EMPLOYEEID");
  400. }
  401. catch(Exception ex)
  402. {
  403. exp = ex;
  404. }
  405. finally
  406. {
  407. EndCase(exp);
  408. }
  409. // -------------- ResultSet 2 ------------
  410. try
  411. {
  412. exp = null;
  413. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 exists");
  414. NextResultExists = rdr.NextResult();
  415. Compare(NextResultExists, true);
  416. }
  417. catch(Exception ex)
  418. {
  419. exp = ex;
  420. }
  421. finally
  422. {
  423. EndCase(exp);
  424. }
  425. try
  426. {
  427. exp = null;
  428. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 contains data");
  429. NextResultExists = rdr.Read();
  430. Compare(NextResultExists, true);
  431. }
  432. catch(Exception ex)
  433. {
  434. exp = ex;
  435. }
  436. finally
  437. {
  438. EndCase(exp);
  439. }
  440. try
  441. {
  442. exp = null;
  443. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Data");
  444. Compare(rdr.GetValue(1).ToString(), "Morgenstern Gesundkost");
  445. }
  446. catch(Exception ex)
  447. {
  448. exp = ex;
  449. }
  450. finally
  451. {
  452. EndCase(exp);
  453. }
  454. try
  455. {
  456. exp = null;
  457. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Schema");
  458. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
  459. }
  460. catch(Exception ex)
  461. {
  462. exp = ex;
  463. }
  464. finally
  465. {
  466. EndCase(exp);
  467. }
  468. // -------------- ResultSet 3 ------------
  469. try
  470. {
  471. exp = null;
  472. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 exists");
  473. NextResultExists = rdr.NextResult();
  474. Compare(NextResultExists, true);
  475. }
  476. catch(Exception ex)
  477. {
  478. exp = ex;
  479. }
  480. finally
  481. {
  482. EndCase(exp);
  483. }
  484. try
  485. {
  486. exp = null;
  487. BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 contains data");
  488. NextResultExists = rdr.Read();
  489. Compare(NextResultExists, false);
  490. }
  491. catch(Exception ex)
  492. {
  493. exp = ex;
  494. }
  495. finally
  496. {
  497. EndCase(exp);
  498. }
  499. try
  500. {
  501. exp = null;
  502. BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 3 Schema");
  503. Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "ORDERID");
  504. }
  505. catch(Exception ex)
  506. {
  507. exp = ex;
  508. }
  509. finally
  510. {
  511. EndCase(exp);
  512. }
  513. try
  514. {
  515. exp = null;
  516. BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not exist.");
  517. NextResultExists = rdr.NextResult();
  518. Compare(NextResultExists, false);
  519. }
  520. catch(Exception ex)
  521. {
  522. exp = ex;
  523. }
  524. finally
  525. {
  526. EndCase(exp);
  527. }
  528. try
  529. {
  530. exp = null;
  531. BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not contain data.");
  532. NextResultExists = rdr.Read();
  533. Compare(NextResultExists, false);
  534. }
  535. catch(Exception ex)
  536. {
  537. exp = ex;
  538. }
  539. finally
  540. {
  541. EndCase(exp);
  542. }
  543. //Cleanup:
  544. if (!rdr.IsClosed)
  545. {
  546. rdr.Close();
  547. }
  548. // transaction use was add for PostgreSQL
  549. tr.Commit();
  550. }
  551. #region "Private Utilities"
  552. private string BuildCommandText()
  553. {
  554. string beginStatement;
  555. string endStatement;
  556. string commandDelimiter;
  557. string[] commands = new string[] {"select * from Customers", "select * from Categories", "select * from Region"};
  558. GetDBSpecificSyntax(ConnectedDataProvider.GetDbType(), out beginStatement, out endStatement, out commandDelimiter);
  559. StringBuilder cmdBuilder = new StringBuilder();
  560. cmdBuilder.Append(beginStatement);
  561. cmdBuilder.Append(" ");
  562. foreach (string statement in commands)
  563. {
  564. cmdBuilder.Append(statement);
  565. cmdBuilder.Append(commandDelimiter);
  566. cmdBuilder.Append(" ");
  567. }
  568. cmdBuilder.Append(endStatement);
  569. return cmdBuilder.ToString();
  570. }
  571. private void GetDBSpecificSyntax(DataBaseServer dbServer, out string beginStatement, out string endStatement, out string commandDelimiter)
  572. {
  573. switch (dbServer)
  574. {
  575. case DataBaseServer.SQLServer:
  576. beginStatement = "BEGIN";
  577. endStatement = "END";
  578. commandDelimiter = ";";
  579. break;
  580. case DataBaseServer.Sybase:
  581. beginStatement = "BEGIN";
  582. endStatement = "END";
  583. commandDelimiter = "\r\n";
  584. break;
  585. case DataBaseServer.Oracle:
  586. beginStatement = "BEGIN";
  587. endStatement = "END;";
  588. commandDelimiter = ";";
  589. break;
  590. case DataBaseServer.DB2:
  591. {
  592. beginStatement = "";
  593. endStatement = "";
  594. }
  595. commandDelimiter = ";";
  596. break;
  597. case DataBaseServer.PostgreSQL:
  598. beginStatement = "";
  599. endStatement = "";
  600. commandDelimiter = ";";
  601. break;
  602. default:
  603. this.Fail("Unknown DataBaseServer type");
  604. throw new ApplicationException("Unknown DataBaseServer type");
  605. }
  606. }
  607. private void GetExcpectedResults(ref int TblResult0, ref int TblResult1, ref int TblResult2)
  608. {
  609. // get excpected results
  610. // transaction use was add for PostgreSQL
  611. OracleTransaction tr = con.BeginTransaction();
  612. OracleCommand cmd = new OracleCommand("", con,tr);
  613. cmd.CommandText = "Select count(*) from Customers";
  614. TblResult0 = Int32.Parse(cmd.ExecuteScalar().ToString());
  615. cmd.CommandText = "Select count(*) from Categories";
  616. TblResult1 = Int32.Parse(cmd.ExecuteScalar().ToString());
  617. cmd.CommandText = "Select count(*) from Region";
  618. TblResult2 = Int32.Parse(cmd.ExecuteScalar().ToString());
  619. tr.Commit();
  620. }
  621. #endregion
  622. }
  623. }