OleDbCommand_ExecuteReader.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #if DAAB
  30. using Microsoft.ApplicationBlocks;
  31. #endif
  32. namespace MonoTests.System.Data.OleDb
  33. {
  34. [TestFixture]
  35. public class OleDbCommand_ExecuteReader : ADONetTesterClass
  36. {
  37. OleDbConnection con;
  38. OleDbCommand cmd;
  39. [SetUp]
  40. public void SetUp()
  41. {
  42. Exception exp = null;
  43. BeginCase("Setup");
  44. try
  45. {
  46. con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  47. cmd = new OleDbCommand("", 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. OleDbCommand_ExecuteReader tc = new OleDbCommand_ExecuteReader();
  65. Exception exp = null;
  66. try
  67. {
  68. tc.BeginTest("OleDbCommand_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. OleDbDataReader 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. OleDbCommand prepare = new OleDbCommand("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. OleDbCommand InsertCmd = new OleDbCommand(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. OleDbCommand cleanup = new OleDbCommand("DELETE FROM Categories WHERE CategoryName='__TEST_RECORD__'", con);
  188. cleanup.ExecuteNonQuery();
  189. }
  190. }
  191. if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
  192. {
  193. try
  194. {
  195. BeginCase("Use out refcursor implicitly to get resultset from stored-procedure in command type text.");
  196. cmd.CommandText = "{ call ghsp_types_simple_1 (null, null, 1.234, null, null, null, null)}";
  197. cmd.CommandType = CommandType.Text;
  198. rdr = cmd.ExecuteReader();
  199. Compare(rdr.HasRows, true);
  200. }
  201. catch(Exception ex){exp = ex;}
  202. finally
  203. {
  204. if (rdr != null) rdr.Close();
  205. EndCase(exp);
  206. exp = null;
  207. }
  208. try
  209. {
  210. BeginCase("Use out refcursor implicitly to get resultset from stored-procedure in CommandType.StoredProcedure.");
  211. cmd.CommandText = "GHSP_TYPES_SIMPLE_1";
  212. cmd.CommandType = CommandType.StoredProcedure;
  213. cmd.Parameters.Add("", 123456);
  214. cmd.Parameters.Add("", "asdasd");
  215. cmd.Parameters.Add("", 1.234);
  216. cmd.Parameters.Add("", "asdasd");
  217. cmd.Parameters.Add("", "asdasd");
  218. cmd.Parameters.Add("", "asdasd");
  219. cmd.Parameters.Add("", "asdasd");
  220. rdr = cmd.ExecuteReader();
  221. Compare(rdr.HasRows, true);
  222. }
  223. catch(Exception ex){exp = ex;}
  224. finally
  225. {
  226. if (rdr != null) rdr.Close();
  227. EndCase(exp);
  228. exp = null;
  229. }
  230. }
  231. }
  232. //Create the compund sql statement according to the dbserver.
  233. private string CreateCompundSqlStatement(string[] sqlStatements, DataBaseServer dbServer)
  234. {
  235. string beginStatement;
  236. string endStatement;
  237. string commandDelimiter;
  238. GetDBSpecificSyntax(dbServer, out beginStatement, out endStatement, out commandDelimiter);
  239. StringBuilder cmdBuilder = new StringBuilder();
  240. cmdBuilder.Append(beginStatement);
  241. cmdBuilder.Append(" ");
  242. foreach (string statement in sqlStatements)
  243. {
  244. cmdBuilder.Append(statement);
  245. cmdBuilder.Append(commandDelimiter);
  246. cmdBuilder.Append(" ");
  247. }
  248. cmdBuilder.Append(endStatement);
  249. return cmdBuilder.ToString();
  250. }
  251. private void GetDBSpecificSyntax(DataBaseServer dbServer, out string beginStatement, out string endStatement, out string commandDelimiter)
  252. {
  253. switch (dbServer)
  254. {
  255. case DataBaseServer.SQLServer:
  256. case DataBaseServer.PostgreSQL:
  257. beginStatement = "";
  258. endStatement = "";
  259. commandDelimiter = ";";
  260. break;
  261. case DataBaseServer.Sybase:
  262. beginStatement = "BEGIN";
  263. endStatement = "END";
  264. commandDelimiter = "";
  265. break;
  266. case DataBaseServer.Oracle:
  267. beginStatement = "BEGIN";
  268. endStatement = "END;";
  269. commandDelimiter = ";";
  270. break;
  271. case DataBaseServer.DB2:
  272. beginStatement = "BEGIN ATOMIC";
  273. endStatement = "END";
  274. commandDelimiter = ";";
  275. break;
  276. default:
  277. this.Fail("Unknown DataBaseServer type");
  278. throw new ApplicationException("Unknown DataBaseServer type");
  279. }
  280. }
  281. [Test]
  282. public void testBug3965()
  283. {
  284. Exception exp=null;
  285. BeginCase("Test for bug #3965");
  286. OleDbConnection con = new OleDbConnection("Provider=SQLOLEDB.1;Data Source=TESTDIR;User ID=ghuser;Password=ghuser;Persist Security Info=True;Initial Catalog=default_grasshopper_db;Packet Size=4096;Connect Timeout=60");
  287. con.Open();
  288. string text = "SELECT td.TESTCYCL.TC_TEST_ID, td.TESTCYCL.TC_STATUS, td.BUG.BG_STATUS, td.BUG.BG_BUG_ID, td.BUG.BG_USER_03, td.BUG.BG_USER_09,";
  289. text+= " td.BUG.BG_USER_10, td.BUG.BG_SUMMARY,BG_DETECTION_VERSION";
  290. text+= " FROM td.TESTCYCL INNER JOIN";
  291. text+= " td.TEST ON td.TESTCYCL.TC_TEST_ID = td.TEST.TS_TEST_ID INNER JOIN";
  292. text+= " td.ALL_LISTS ON td.TEST.TS_SUBJECT = td.ALL_LISTS.AL_ITEM_ID RIGHT OUTER JOIN";
  293. text+= " td.BUG ON td.TEST.TS_TEST_ID = td.BUG.BG_TEST_REFERENCE";
  294. OleDbCommand cmd = new OleDbCommand(text,con);
  295. try
  296. {
  297. cmd.ExecuteReader();
  298. Compare(true,true);
  299. }
  300. catch (Exception ex)
  301. {
  302. exp = ex;
  303. }
  304. finally
  305. {
  306. EndCase(exp);
  307. }
  308. }
  309. [Test]
  310. public void TestMultipleResultsets()
  311. {
  312. #if !JAVA
  313. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.Oracle)
  314. {
  315. //In .NET there is a bug when calling a SP with multiple REFCURSORS, the workaround is to use OracleClient and not OleDb.
  316. //In GH we are not bug complient in this issue, because there is no workaround (We do not support the OracleClient namespace.
  317. this.Log("Not testing multi result set Oracle on .NET");
  318. return;
  319. }
  320. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  321. {
  322. // fail to work on .NET OLEDB
  323. //reader = Microsoft.ApplicationBlocks.Data.PostgresOleDbHelper.ADOExecuteReader(cmd1);
  324. this.Log("Not testing PostgreSQL CommandType.StoredProcedure which return SETOF");
  325. return;
  326. }
  327. #endif
  328. Exception exp = null;
  329. BeginCase("Test multi result set from stored procedure");
  330. OleDbDataReader reader = null;
  331. OleDbTransaction tr = null;
  332. try
  333. {
  334. //Check SP with the structre : insert Select + update Select + delete Select
  335. if (con.State != ConnectionState.Open)
  336. {
  337. con.Open();
  338. }
  339. // transaction use was add for PostgreSQL
  340. tr = con.BeginTransaction();
  341. OleDbCommand cmd1 = new OleDbCommand("GHSP_TYPES_SIMPLE_4", con, tr);
  342. cmd1.CommandType = CommandType.StoredProcedure;
  343. OleDbParameter param = new OleDbParameter();
  344. param.ParameterName = "ID1";
  345. param.Value = string.Format("13268_{0}", this.TestCaseNumber);
  346. param.OleDbType = OleDbType.VarWChar;
  347. cmd1.Parameters.Add(param);
  348. reader = cmd1.ExecuteReader();
  349. //Count the number of result sets.
  350. int resultSetCount = 0;
  351. //Count the number of the records
  352. int recordCounter=0;
  353. do
  354. {
  355. //this.Log(string.Format("resultSetCount:{0}",resultSetCount));
  356. while (reader.Read())
  357. {
  358. recordCounter++;
  359. }
  360. //this.Log(string.Format("recordCounter:{0}",recordCounter));
  361. if (resultSetCount != 2)
  362. {
  363. Compare(recordCounter,1); //Insert + update
  364. }
  365. else
  366. {
  367. Compare(recordCounter,0); //Delete
  368. }
  369. recordCounter=0;
  370. resultSetCount++;
  371. }while (reader.NextResult());
  372. Compare(resultSetCount,3);
  373. }
  374. catch (Exception ex)
  375. {
  376. exp=ex;
  377. }
  378. finally
  379. {
  380. EndCase(exp);
  381. if (reader != null) reader.Close();
  382. tr.Commit();
  383. con.Close();
  384. }
  385. }
  386. [Test]
  387. public void TestCompoundVariable()
  388. {
  389. OleDbDataReader rdr = null;
  390. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  391. {
  392. this.Log("not testing PostgreSQL");
  393. return;
  394. }
  395. Exception exp = null;
  396. try
  397. {
  398. BeginCase("Check sql statement that declares a local variable and uses it.");
  399. if (con.State != ConnectionState.Open)
  400. {
  401. con.Open();
  402. }
  403. string sqlTxt = "";
  404. switch (ConnectedDataProvider.GetDbType(cmd.Connection))
  405. {
  406. case DataBaseServer.SQLServer:
  407. sqlTxt = "declare @var int; select @var=1;";
  408. break;
  409. case DataBaseServer.Sybase:
  410. sqlTxt = "declare @var int select @var=1";
  411. break;
  412. case DataBaseServer.Oracle:
  413. sqlTxt = "declare var int;begin var:=1;end;";
  414. break;
  415. case DataBaseServer.DB2:
  416. sqlTxt = "begin atomic declare var integer; set var = 1; end";
  417. break;
  418. case DataBaseServer.PostgreSQL:
  419. // we don't know how the heck to do this in PostgreSQL
  420. sqlTxt = "";
  421. break;
  422. default:
  423. throw new ApplicationException(string.Format("GHT: Unknown DataBaseServer '{0}'", ConnectedDataProvider.GetDbType(cmd.Connection)));
  424. }
  425. cmd.CommandText = sqlTxt;
  426. rdr = cmd.ExecuteReader();
  427. Compare(rdr.Read(), false);
  428. }
  429. catch(Exception ex){exp = ex;}
  430. finally
  431. {
  432. if (rdr != null) rdr.Close();
  433. EndCase(exp);
  434. exp = null;
  435. }
  436. }
  437. }
  438. }