OracleCommand_ExecuteReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #if DAAB
  30. using Microsoft.ApplicationBlocks;
  31. #endif
  32. namespace MonoTests.System.Data.OracleClient
  33. {
  34. [TestFixture]
  35. public class OracleCommand_ExecuteReader : ADONetTesterClass
  36. {
  37. OracleConnection con;
  38. OracleCommand cmd;
  39. [SetUp]
  40. public void SetUp()
  41. {
  42. Exception exp = null;
  43. BeginCase("Setup");
  44. try
  45. {
  46. con = new OracleConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  47. cmd = new OracleCommand("", con);
  48. con.Open();
  49. this.Pass("Setup.");
  50. }
  51. catch(Exception ex) {exp = ex;}
  52. finally {EndCase(exp); exp = null;}
  53. }
  54. [TearDown]
  55. public void TearDown()
  56. {
  57. if (con != null)
  58. {
  59. if (con.State == ConnectionState.Open) con.Close();
  60. }
  61. }
  62. public static void Main()
  63. {
  64. OracleCommand_ExecuteReader tc = new OracleCommand_ExecuteReader();
  65. Exception exp = null;
  66. try
  67. {
  68. tc.BeginTest("OracleCommand_ExecuteReader");
  69. tc.SetUp();
  70. tc.run();
  71. tc.TearDown();
  72. }
  73. catch(Exception ex){exp = ex;}
  74. finally {tc.EndTest(exp);}
  75. }
  76. [Test]
  77. public void run()
  78. {
  79. Exception exp = null;
  80. bool RecordsExists = false;
  81. OracleDataReader rdr =null;
  82. // testBug3965();
  83. // TestMultipleResultsets();
  84. // TestCompoundVariable();
  85. cmd.CommandText = "Select FirstName,City From Employees";
  86. if (con.State != ConnectionState.Open)
  87. {
  88. con.Open();
  89. }
  90. try
  91. {
  92. BeginCase("check reader is null");
  93. rdr = cmd.ExecuteReader();
  94. Compare(rdr==null, false);
  95. }
  96. catch(Exception ex){exp = ex;}
  97. finally
  98. {
  99. if (rdr != null) rdr.Close();
  100. EndCase(exp);
  101. exp = null;
  102. }
  103. try
  104. {
  105. BeginCase("check reader.read");
  106. rdr = cmd.ExecuteReader();
  107. RecordsExists = rdr.Read();
  108. Compare(RecordsExists ,true);
  109. }
  110. catch(Exception ex){exp = ex;}
  111. finally
  112. {
  113. if (rdr != null) rdr.Close();
  114. EndCase(exp);
  115. exp = null;
  116. }
  117. try
  118. {
  119. BeginCase("execute reader again ");
  120. rdr = cmd.ExecuteReader();
  121. Compare(rdr==null, false);
  122. }
  123. catch(Exception ex){exp = ex;}
  124. finally
  125. {
  126. if (rdr != null) rdr.Close();
  127. EndCase(exp);
  128. exp = null;
  129. }
  130. try
  131. {
  132. BeginCase("Test compound SQL statement");
  133. //Build a compund SQL command.
  134. string[] sqlStatements = new string[] {
  135. "INSERT INTO Categories (CategoryName, Description) VALUES('__TEST_RECORD__', 'Inserted')",
  136. "UPDATE Categories SET Description='Updated' WHERE CategoryName='__TEST_RECORD__'",
  137. "DELETE FROM Categories WHERE CategoryName='__TEST_RECORD__'" ,
  138. };
  139. cmd.CommandText = CreateCompundSqlStatement(sqlStatements, ConnectedDataProvider.GetDbType());
  140. rdr = cmd.ExecuteReader();
  141. Compare(rdr.Read(), false);
  142. }
  143. catch(Exception ex){exp = ex;}
  144. finally
  145. {
  146. if (rdr != null) rdr.Close();
  147. EndCase(exp);
  148. exp = null;
  149. }
  150. if (ConnectedDataProvider.GetDbType() != DataBaseServer.Oracle)
  151. {
  152. try
  153. {
  154. BeginCase("Check that in a compound SQL statement, resultsets are returned only for SELECT statements. (bug #3358)");
  155. //prepare db:
  156. OracleCommand prepare = new OracleCommand("DELETE FROM Categories WHERE CategoryName='__TEST_RECORD__'", con);
  157. prepare.ExecuteNonQuery();
  158. //Test body
  159. int resultSetCount ;
  160. //Build a compund SQL command that contains only one select statement.
  161. string[] sqlStatements = new string[] {
  162. "INSERT INTO Categories (CategoryName, Description) VALUES('__TEST_RECORD__', 'Inserted')",
  163. "UPDATE Categories SET Description='Updated' WHERE CategoryName='__TEST_RECORD__'",
  164. "DELETE FROM Categories WHERE CategoryName='__TEST_RECORD__'" ,
  165. "SELECT * FROM Categories "
  166. };
  167. string insertCmdTxt = CreateCompundSqlStatement(sqlStatements, ConnectedDataProvider.GetDbType());
  168. //this.Log(insertCmdTxt);
  169. OracleCommand InsertCmd = new OracleCommand(insertCmdTxt, con);
  170. rdr = InsertCmd.ExecuteReader();
  171. //Count the number of result sets.
  172. resultSetCount = 0;
  173. do
  174. {
  175. resultSetCount++;
  176. }while (rdr.NextResult());
  177. //Test that there is only one result set.
  178. Compare(resultSetCount, 1);
  179. }
  180. catch(Exception ex){exp = ex;}
  181. finally
  182. {
  183. if (rdr != null) rdr.Close();
  184. EndCase(exp);
  185. exp = null;
  186. //cleanup db:
  187. OracleCommand cleanup = new OracleCommand("DELETE FROM Categories WHERE CategoryName='__TEST_RECORD__'", con);
  188. cleanup.ExecuteNonQuery();
  189. }
  190. }
  191. }
  192. //Create the compund sql statement according to the dbserver.
  193. private string CreateCompundSqlStatement(string[] sqlStatements, DataBaseServer dbServer)
  194. {
  195. string beginStatement;
  196. string endStatement;
  197. string commandDelimiter;
  198. GetDBSpecificSyntax(dbServer, out beginStatement, out endStatement, out commandDelimiter);
  199. StringBuilder cmdBuilder = new StringBuilder();
  200. cmdBuilder.Append(beginStatement);
  201. cmdBuilder.Append(" ");
  202. foreach (string statement in sqlStatements)
  203. {
  204. cmdBuilder.Append(statement);
  205. cmdBuilder.Append(commandDelimiter);
  206. cmdBuilder.Append(" ");
  207. }
  208. cmdBuilder.Append(endStatement);
  209. return cmdBuilder.ToString();
  210. }
  211. private void GetDBSpecificSyntax(DataBaseServer dbServer, out string beginStatement, out string endStatement, out string commandDelimiter)
  212. {
  213. switch (dbServer)
  214. {
  215. case DataBaseServer.SQLServer:
  216. case DataBaseServer.PostgreSQL:
  217. beginStatement = "";
  218. endStatement = "";
  219. commandDelimiter = ";";
  220. break;
  221. case DataBaseServer.Sybase:
  222. beginStatement = "BEGIN";
  223. endStatement = "END";
  224. commandDelimiter = "";
  225. break;
  226. case DataBaseServer.Oracle:
  227. beginStatement = "BEGIN";
  228. endStatement = "END;";
  229. commandDelimiter = ";";
  230. break;
  231. case DataBaseServer.DB2:
  232. beginStatement = "BEGIN ATOMIC";
  233. endStatement = "END";
  234. commandDelimiter = ";";
  235. break;
  236. default:
  237. this.Fail("Unknown DataBaseServer type");
  238. throw new ApplicationException("Unknown DataBaseServer type");
  239. }
  240. }
  241. [Test]
  242. public void TestMultipleResultsets()
  243. {
  244. #if !JAVA
  245. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.Oracle)
  246. {
  247. //In .NET there is a bug when calling a SP with multiple REFCURSORS, the workaround is to use OracleClient and not Oracle.
  248. //In GH we are not bug complient in this issue, because there is no workaround (We do not support the OracleClient namespace.
  249. this.Log("Not testing multi result set Oracle on .NET");
  250. return;
  251. }
  252. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  253. {
  254. // fail to work on .NET OLEDB
  255. //reader = Microsoft.ApplicationBlocks.Data.PostgresOracleHelper.ADOExecuteReader(cmd1);
  256. this.Log("Not testing PostgreSQL CommandType.StoredProcedure which return SETOF");
  257. return;
  258. }
  259. #endif
  260. Exception exp = null;
  261. BeginCase("Test multi result set from stored procedure");
  262. OracleDataReader reader = null;
  263. OracleTransaction tr = null;
  264. try
  265. {
  266. //Check SP with the structre : insert Select + update Select + delete Select
  267. if (con.State != ConnectionState.Open)
  268. {
  269. con.Open();
  270. }
  271. // transaction use was add for PostgreSQL
  272. tr = con.BeginTransaction();
  273. OracleCommand cmd1 = new OracleCommand("GHSP_TYPES_SIMPLE_4", con, tr);
  274. cmd1.CommandType = CommandType.StoredProcedure;
  275. OracleParameter param = new OracleParameter();
  276. param.ParameterName = "ID1";
  277. param.Value = string.Format("13268_{0}", this.TestCaseNumber);
  278. param.OracleType = OracleType.VarChar;
  279. cmd1.Parameters.Add(param);
  280. cmd1.Parameters.Add(new OracleParameter("RESULT", OracleType.Cursor)).Direction = ParameterDirection.Output;
  281. cmd1.Parameters.Add(new OracleParameter("RESULT1", OracleType.Cursor)).Direction = ParameterDirection.Output;
  282. cmd1.Parameters.Add(new OracleParameter("RESULT2", OracleType.Cursor)).Direction = ParameterDirection.Output;
  283. reader = cmd1.ExecuteReader();
  284. //Count the number of result sets.
  285. int resultSetCount = 0;
  286. //Count the number of the records
  287. int recordCounter=0;
  288. do
  289. {
  290. //this.Log(string.Format("resultSetCount:{0}",resultSetCount));
  291. while (reader.Read())
  292. {
  293. recordCounter++;
  294. }
  295. //this.Log(string.Format("recordCounter:{0}",recordCounter));
  296. if (resultSetCount != 2)
  297. {
  298. Compare(recordCounter,1); //Insert + update
  299. }
  300. else
  301. {
  302. Compare(recordCounter,0); //Delete
  303. }
  304. recordCounter=0;
  305. resultSetCount++;
  306. }while (reader.NextResult());
  307. Compare(resultSetCount,3);
  308. }
  309. catch (Exception ex)
  310. {
  311. exp=ex;
  312. }
  313. finally
  314. {
  315. EndCase(exp);
  316. if (reader != null) reader.Close();
  317. tr.Commit();
  318. con.Close();
  319. }
  320. }
  321. [Test]
  322. public void TestCompoundVariable()
  323. {
  324. OracleDataReader rdr = null;
  325. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  326. {
  327. this.Log("not testing PostgreSQL");
  328. return;
  329. }
  330. Exception exp = null;
  331. try
  332. {
  333. BeginCase("Check sql statement that declares a local variable and uses it.");
  334. if (con.State != ConnectionState.Open)
  335. {
  336. con.Open();
  337. }
  338. string sqlTxt = "";
  339. switch (ConnectedDataProvider.GetDbType(cmd.Connection))
  340. {
  341. case DataBaseServer.SQLServer:
  342. sqlTxt = "declare @var int; select @var=1;";
  343. break;
  344. case DataBaseServer.Sybase:
  345. sqlTxt = "declare @var int select @var=1";
  346. break;
  347. case DataBaseServer.Oracle:
  348. sqlTxt = "declare var int;begin var:=1;end;";
  349. break;
  350. case DataBaseServer.DB2:
  351. sqlTxt = "begin atomic declare var integer; set var = 1; end";
  352. break;
  353. case DataBaseServer.PostgreSQL:
  354. // we don't know how the heck to do this in PostgreSQL
  355. sqlTxt = "";
  356. break;
  357. default:
  358. throw new ApplicationException(string.Format("GHT: Unknown DataBaseServer '{0}'", ConnectedDataProvider.GetDbType(cmd.Connection)));
  359. }
  360. cmd.CommandText = sqlTxt;
  361. rdr = cmd.ExecuteReader();
  362. Compare(rdr.Read(), false);
  363. }
  364. catch(Exception ex){exp = ex;}
  365. finally
  366. {
  367. if (rdr != null) rdr.Close();
  368. EndCase(exp);
  369. exp = null;
  370. }
  371. }
  372. }
  373. }