2 // Copyright (c) 2006 Mainsoft Co.
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
27 using System.Data.OracleClient ;
29 using MonoTests.System.Data.Utils;
32 using NUnit.Framework;
34 namespace MonoTests.System.Data.OracleClient
37 public class OracleDataReader_NextResult : ADONetTesterClass
44 base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
45 con = new OracleConnection (MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
50 public void TearDown() {
51 if (con.State == ConnectionState.Open) con.Close();
54 public static void Main()
56 OracleDataReader_NextResult tc = new OracleDataReader_NextResult();
60 tc.BeginTest("OracleDataReader_NextResult");
63 catch(Exception ex){exp = ex;}
64 finally {tc.EndTest(exp);}
71 base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
74 TestMultipleResultSetsWithSP();
75 TestMultipleResultSetsWithSQLText();
77 if (con.State == ConnectionState.Open) con.Close();
83 public void TestMultipleResultSetsWithSQLText()
86 if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
88 this.Log("Multiple result sets by sql text is not tested in oracle.");
92 if (ConnectedDataProvider.GetDbType() == DataBaseServer.DB2)
94 this.Log("Multiple result sets using compound statement not supported at DB2.");
98 bool NextResultExists = false;
99 OracleDataReader rdr = null;
106 BeginCase("Setup: Get expected results.");
108 //get excpected results
109 GetExcpectedResults(ref TblResult0, ref TblResult1, ref TblResult2);
110 this.Pass("Setup: Get expected results ended.");
122 string cmdTxt = BuildCommandText();
123 cmd = new OracleCommand(cmdTxt, con);
124 cmd.CommandType = CommandType.Text;
125 rdr = cmd.ExecuteReader();
126 // -------------- ResultSet 1 ------------
130 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 1 exists");
131 Compare(rdr != null, true);
144 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 1 contains data");
145 NextResultExists = rdr.Read();
146 Compare(NextResultExists, true);
160 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 1 Data");
165 Compare(i, TblResult0);
178 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 1 Schema");
179 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
189 // -------------- ResultSet 2 ------------
193 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 2 exists");
194 NextResultExists = rdr.NextResult();
195 Compare(NextResultExists, true);
208 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 2 contains data");
209 NextResultExists = rdr.Read();
210 Compare(NextResultExists, true);
223 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 2 Data");
229 Compare(i, TblResult1);
242 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 2 Schema");
243 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CATEGORYID");
249 // -------------- ResultSet 3 ------------
257 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 3 exists");
258 NextResultExists = rdr.NextResult();
259 Compare(NextResultExists, true);
272 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 3 contains data");
273 NextResultExists = rdr.Read();
274 Compare(NextResultExists, true);
287 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 3 Data");
293 Compare(i, TblResult2);
306 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 3 Schema");
307 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "REGIONID");
320 BeginCase("(Multiple Resultsets sql text) - Check that resultset 4 does not exist.");
321 NextResultExists = rdr.NextResult();
322 Compare(NextResultExists, false);
335 BeginCase("(Multiple Resultsets sql text) - Check that resultset 4 does not contain data.");
336 NextResultExists = rdr.Read();
337 Compare(NextResultExists, false);
352 [Ignore ("JVM test")]
353 public void TestMultipleResultSetsWithSP()
356 if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
358 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).");
362 if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
364 // fail to work on .NET OLEDB
365 this.Log("Not testing PostgreSQL CommandType.StoredProcedure which return SETOF");
370 bool NextResultExists = false;
371 // transaction use was add for PostgreSQL
372 OracleTransaction tr = con.BeginTransaction();
373 OracleCommand cmd = new OracleCommand("GH_MULTIRECORDSETS", con, tr);
374 cmd.Parameters.Add(new OracleParameter("RCT_Employees", OracleType.Cursor)).Direction = ParameterDirection.Output;
375 cmd.Parameters.Add(new OracleParameter("RCT_Customers", OracleType.Cursor)).Direction = ParameterDirection.Output;
376 cmd.Parameters.Add(new OracleParameter("RCT_Orders", OracleType.Cursor)).Direction = ParameterDirection.Output;
377 cmd.CommandType = CommandType.StoredProcedure;
378 OracleDataReader rdr = cmd.ExecuteReader();
383 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 exists");
384 Compare(rdr != null, true);
397 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 contains data");
398 NextResultExists = rdr.Read();
399 Compare(NextResultExists, true);
412 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Data");
413 Compare(rdr.GetValue(1).ToString(), "Yavine");
426 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Schema");
427 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "EMPLOYEEID");
439 // -------------- ResultSet 2 ------------
443 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 exists");
444 NextResultExists = rdr.NextResult();
445 Compare(NextResultExists, true);
458 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 contains data");
459 NextResultExists = rdr.Read();
460 Compare(NextResultExists, true);
473 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Data");
474 Compare(rdr.GetValue(1).ToString(), "Morgenstern Gesundkost");
487 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Schema");
488 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
499 // -------------- ResultSet 3 ------------
503 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 exists");
504 NextResultExists = rdr.NextResult();
505 Compare(NextResultExists, true);
518 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 contains data");
519 NextResultExists = rdr.Read();
520 Compare(NextResultExists, false);
533 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 3 Schema");
534 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "ORDERID");
547 BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not exist.");
548 NextResultExists = rdr.NextResult();
549 Compare(NextResultExists, false);
562 BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not contain data.");
563 NextResultExists = rdr.Read();
564 Compare(NextResultExists, false);
581 // transaction use was add for PostgreSQL
587 #region "Private Utilities"
588 private string BuildCommandText()
590 string beginStatement;
592 string commandDelimiter;
593 string[] commands = new string[] {"select * from Customers", "select * from Categories", "select * from Region"};
595 GetDBSpecificSyntax(ConnectedDataProvider.GetDbType(), out beginStatement, out endStatement, out commandDelimiter);
597 StringBuilder cmdBuilder = new StringBuilder();
598 cmdBuilder.Append(beginStatement);
599 cmdBuilder.Append(" ");
600 foreach (string statement in commands)
602 cmdBuilder.Append(statement);
603 cmdBuilder.Append(commandDelimiter);
604 cmdBuilder.Append(" ");
606 cmdBuilder.Append(endStatement);
608 return cmdBuilder.ToString();
610 private void GetDBSpecificSyntax(DataBaseServer dbServer, out string beginStatement, out string endStatement, out string commandDelimiter)
614 case DataBaseServer.SQLServer:
615 beginStatement = "BEGIN";
616 endStatement = "END";
617 commandDelimiter = ";";
619 case DataBaseServer.Sybase:
620 beginStatement = "BEGIN";
621 endStatement = "END";
622 commandDelimiter = "\r\n";
624 case DataBaseServer.Oracle:
625 beginStatement = "BEGIN";
626 endStatement = "END;";
627 commandDelimiter = ";";
630 case DataBaseServer.DB2:
635 commandDelimiter = ";";
638 case DataBaseServer.PostgreSQL:
641 commandDelimiter = ";";
645 this.Fail("Unknown DataBaseServer type");
646 throw new ApplicationException("Unknown DataBaseServer type");
649 private void GetExcpectedResults(ref int TblResult0, ref int TblResult1, ref int TblResult2)
651 // get excpected results
653 // transaction use was add for PostgreSQL
654 OracleTransaction tr = con.BeginTransaction();
655 OracleCommand cmd = new OracleCommand("", con,tr);
656 cmd.CommandText = "Select count(*) from Customers";
657 TblResult0 = Int32.Parse(cmd.ExecuteScalar().ToString());
658 cmd.CommandText = "Select count(*) from Categories";
659 TblResult1 = Int32.Parse(cmd.ExecuteScalar().ToString());
660 cmd.CommandText = "Select count(*) from Region";
661 TblResult2 = Int32.Parse(cmd.ExecuteScalar().ToString());