OleDbCommand_ExecuteReader.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. // testing only SQLServerr
  285. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) {
  286. Log("This test is relevant only for MSSQLServer!");
  287. return;
  288. }
  289. Exception exp=null;
  290. BeginCase("Test for bug #3965");
  291. 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");
  292. con.Open();
  293. 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,";
  294. text+= " td.BUG.BG_USER_10, td.BUG.BG_SUMMARY,BG_DETECTION_VERSION";
  295. text+= " FROM td.TESTCYCL INNER JOIN";
  296. text+= " td.TEST ON td.TESTCYCL.TC_TEST_ID = td.TEST.TS_TEST_ID INNER JOIN";
  297. text+= " td.ALL_LISTS ON td.TEST.TS_SUBJECT = td.ALL_LISTS.AL_ITEM_ID RIGHT OUTER JOIN";
  298. text+= " td.BUG ON td.TEST.TS_TEST_ID = td.BUG.BG_TEST_REFERENCE";
  299. OleDbCommand cmd = new OleDbCommand(text,con);
  300. try
  301. {
  302. cmd.ExecuteReader();
  303. Compare(true,true);
  304. }
  305. catch (Exception ex)
  306. {
  307. exp = ex;
  308. }
  309. finally
  310. {
  311. EndCase(exp);
  312. }
  313. }
  314. [Test]
  315. public void TestMultipleResultsets()
  316. {
  317. #if !JAVA
  318. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.Oracle)
  319. {
  320. //In .NET there is a bug when calling a SP with multiple REFCURSORS, the workaround is to use OracleClient and not OleDb.
  321. //In GH we are not bug complient in this issue, because there is no workaround (We do not support the OracleClient namespace.
  322. this.Log("Not testing multi result set Oracle on .NET");
  323. return;
  324. }
  325. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  326. {
  327. // fail to work on .NET OLEDB
  328. //reader = Microsoft.ApplicationBlocks.Data.PostgresOleDbHelper.ADOExecuteReader(cmd1);
  329. this.Log("Not testing PostgreSQL CommandType.StoredProcedure which return SETOF");
  330. return;
  331. }
  332. #endif
  333. Exception exp = null;
  334. BeginCase("Test multi result set from stored procedure");
  335. OleDbDataReader reader = null;
  336. OleDbTransaction tr = null;
  337. try
  338. {
  339. //Check SP with the structre : insert Select + update Select + delete Select
  340. if (con.State != ConnectionState.Open)
  341. {
  342. con.Open();
  343. }
  344. // transaction use was add for PostgreSQL
  345. tr = con.BeginTransaction();
  346. OleDbCommand cmd1 = new OleDbCommand("GHSP_TYPES_SIMPLE_4", con, tr);
  347. cmd1.CommandType = CommandType.StoredProcedure;
  348. OleDbParameter param = new OleDbParameter();
  349. param.ParameterName = "ID1";
  350. param.Value = string.Format("13268_{0}", this.TestCaseNumber);
  351. param.OleDbType = OleDbType.VarWChar;
  352. cmd1.Parameters.Add(param);
  353. reader = cmd1.ExecuteReader();
  354. //Count the number of result sets.
  355. int resultSetCount = 0;
  356. //Count the number of the records
  357. int recordCounter=0;
  358. do
  359. {
  360. //this.Log(string.Format("resultSetCount:{0}",resultSetCount));
  361. while (reader.Read())
  362. {
  363. recordCounter++;
  364. }
  365. //this.Log(string.Format("recordCounter:{0}",recordCounter));
  366. if (resultSetCount != 2)
  367. {
  368. Compare(recordCounter,1); //Insert + update
  369. }
  370. else
  371. {
  372. Compare(recordCounter,0); //Delete
  373. }
  374. recordCounter=0;
  375. resultSetCount++;
  376. }while (reader.NextResult());
  377. Compare(resultSetCount,3);
  378. }
  379. catch (Exception ex)
  380. {
  381. exp=ex;
  382. }
  383. finally
  384. {
  385. EndCase(exp);
  386. if (reader != null) reader.Close();
  387. tr.Commit();
  388. con.Close();
  389. }
  390. }
  391. [Test]
  392. public void TestCompoundVariable()
  393. {
  394. OleDbDataReader rdr = null;
  395. if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
  396. {
  397. this.Log("not testing PostgreSQL");
  398. return;
  399. }
  400. Exception exp = null;
  401. try
  402. {
  403. BeginCase("Check sql statement that declares a local variable and uses it.");
  404. if (con.State != ConnectionState.Open)
  405. {
  406. con.Open();
  407. }
  408. string sqlTxt = "";
  409. switch (ConnectedDataProvider.GetDbType(cmd.Connection))
  410. {
  411. case DataBaseServer.SQLServer:
  412. sqlTxt = "declare @var int; select @var=1;";
  413. break;
  414. case DataBaseServer.Sybase:
  415. sqlTxt = "declare @var int select @var=1";
  416. break;
  417. case DataBaseServer.Oracle:
  418. sqlTxt = "declare var int;begin var:=1;end;";
  419. break;
  420. case DataBaseServer.DB2:
  421. sqlTxt = "begin atomic declare var integer; set var = 1; end";
  422. break;
  423. case DataBaseServer.PostgreSQL:
  424. // we don't know how the heck to do this in PostgreSQL
  425. sqlTxt = "";
  426. break;
  427. default:
  428. throw new ApplicationException(string.Format("GHT: Unknown DataBaseServer '{0}'", ConnectedDataProvider.GetDbType(cmd.Connection)));
  429. }
  430. cmd.CommandText = sqlTxt;
  431. rdr = cmd.ExecuteReader();
  432. Compare(rdr.Read(), false);
  433. }
  434. catch(Exception ex){exp = ex;}
  435. finally
  436. {
  437. if (rdr != null) rdr.Close();
  438. EndCase(exp);
  439. exp = null;
  440. }
  441. }
  442. }
  443. }