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.OleDb ;
29 using MonoTests.System.Data.Utils;
32 using NUnit.Framework;
34 namespace MonoTests.System.Data.OleDb
37 public class OleDbDataReader_NextResult : ADONetTesterClass
39 OleDbConnection con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
44 base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
49 public void TearDown() {
50 if (con.State == ConnectionState.Open) con.Close();
53 public static void Main()
55 OleDbDataReader_NextResult tc = new OleDbDataReader_NextResult();
59 tc.BeginTest("OleDbDataReader_NextResult");
62 catch(Exception ex){exp = ex;}
63 finally {tc.EndTest(exp);}
70 base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
73 TestMultipleResultSetsWithSP();
74 TestMultipleResultSetsWithSQLText();
76 if (con.State == ConnectionState.Open) con.Close();
81 public void TestMultipleResultSetsWithSQLText()
84 if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
86 this.Log("Multiple result sets by sql text is not tested in oracle.");
90 if (ConnectedDataProvider.GetDbType() == DataBaseServer.DB2)
92 this.Log("Multiple result sets using compound statement not supported at DB2.");
96 bool NextResultExists = false;
97 OleDbDataReader rdr = null;
104 BeginCase("Setup: Get expected results.");
106 //get excpected results
107 GetExcpectedResults(ref TblResult0, ref TblResult1, ref TblResult2);
108 this.Pass("Setup: Get expected results ended.");
120 string cmdTxt = BuildCommandText();
121 cmd = new OleDbCommand(cmdTxt, con);
122 cmd.CommandType = CommandType.Text;
123 rdr = cmd.ExecuteReader();
124 // -------------- ResultSet 1 ------------
128 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 1 exists");
129 Compare(rdr != null, true);
142 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 1 contains data");
143 NextResultExists = rdr.Read();
144 Compare(NextResultExists, true);
158 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 1 Data");
163 Compare(i, TblResult0);
176 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 1 Schema");
177 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
187 // -------------- ResultSet 2 ------------
191 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 2 exists");
192 NextResultExists = rdr.NextResult();
193 Compare(NextResultExists, true);
206 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 2 contains data");
207 NextResultExists = rdr.Read();
208 Compare(NextResultExists, true);
221 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 2 Data");
227 Compare(i, TblResult1);
240 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 2 Schema");
241 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CATEGORYID");
247 // -------------- ResultSet 3 ------------
255 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 3 exists");
256 NextResultExists = rdr.NextResult();
257 Compare(NextResultExists, true);
270 BeginCase("(Multiple Resultsets sql text) - Check if ResultSet 3 contains data");
271 NextResultExists = rdr.Read();
272 Compare(NextResultExists, true);
285 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 3 Data");
291 Compare(i, TblResult2);
304 BeginCase("(Multiple Resultsets sql text) - Check ResultSet 3 Schema");
305 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "REGIONID");
318 BeginCase("(Multiple Resultsets sql text) - Check that resultset 4 does not exist.");
319 NextResultExists = rdr.NextResult();
320 Compare(NextResultExists, false);
333 BeginCase("(Multiple Resultsets sql text) - Check that resultset 4 does not contain data.");
334 NextResultExists = rdr.Read();
335 Compare(NextResultExists, false);
350 public void TestMultipleResultSetsWithSP()
353 if (ConnectedDataProvider.GetDbType() == DataBaseServer.Oracle)
355 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).");
359 if (ConnectedDataProvider.GetDbType(con) == DataBaseServer.PostgreSQL)
361 // fail to work on .NET OLEDB
362 this.Log("Not testing PostgreSQL CommandType.StoredProcedure which return SETOF");
367 bool NextResultExists = false;
368 // transaction use was add for PostgreSQL
369 OleDbTransaction tr = con.BeginTransaction();
370 OleDbCommand cmd = new OleDbCommand("GH_MULTIRECORDSETS", con, tr);
371 cmd.CommandType = CommandType.StoredProcedure;
372 OleDbDataReader rdr = cmd.ExecuteReader();
377 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 exists");
378 Compare(rdr != null, true);
391 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 1 contains data");
392 NextResultExists = rdr.Read();
393 Compare(NextResultExists, true);
406 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Data");
407 Compare(rdr.GetValue(1).ToString(), "Yavine");
420 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 1 Schema");
421 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "EMPLOYEEID");
433 // -------------- ResultSet 2 ------------
437 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 exists");
438 NextResultExists = rdr.NextResult();
439 Compare(NextResultExists, true);
452 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 2 contains data");
453 NextResultExists = rdr.Read();
454 Compare(NextResultExists, true);
467 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Data");
468 Compare(rdr.GetValue(1).ToString(), "Morgenstern Gesundkost");
481 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 2 Schema");
482 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "CUSTOMERID");
493 // -------------- ResultSet 3 ------------
497 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 exists");
498 NextResultExists = rdr.NextResult();
499 Compare(NextResultExists, true);
512 BeginCase("(Multiple Resultsets stored proc.) - Check if ResultSet 3 contains data");
513 NextResultExists = rdr.Read();
514 Compare(NextResultExists, false);
527 BeginCase("(Multiple Resultsets stored proc.) - Check ResultSet 3 Schema");
528 Compare(rdr.GetSchemaTable().Rows[0].ItemArray.GetValue(0).ToString().ToUpper(), "ORDERID");
541 BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not exist.");
542 NextResultExists = rdr.NextResult();
543 Compare(NextResultExists, false);
556 BeginCase("(Multiple Resultsets stored proc.) - Check that resultset 4 does not contain data.");
557 NextResultExists = rdr.Read();
558 Compare(NextResultExists, false);
575 // transaction use was add for PostgreSQL
581 #region "Private Utilities"
582 private string BuildCommandText()
584 string beginStatement;
586 string commandDelimiter;
587 string[] commands = new string[] {"select * from Customers", "select * from Categories", "select * from Region"};
589 GetDBSpecificSyntax(ConnectedDataProvider.GetDbType(), out beginStatement, out endStatement, out commandDelimiter);
591 StringBuilder cmdBuilder = new StringBuilder();
592 cmdBuilder.Append(beginStatement);
593 cmdBuilder.Append(" ");
594 foreach (string statement in commands)
596 cmdBuilder.Append(statement);
597 cmdBuilder.Append(commandDelimiter);
598 cmdBuilder.Append(" ");
600 cmdBuilder.Append(endStatement);
602 return cmdBuilder.ToString();
604 private void GetDBSpecificSyntax(DataBaseServer dbServer, out string beginStatement, out string endStatement, out string commandDelimiter)
608 case DataBaseServer.SQLServer:
609 beginStatement = "BEGIN";
610 endStatement = "END";
611 commandDelimiter = ";";
613 case DataBaseServer.Sybase:
614 beginStatement = "BEGIN";
615 endStatement = "END";
616 commandDelimiter = "\r\n";
618 case DataBaseServer.Oracle:
619 beginStatement = "BEGIN";
620 endStatement = "END;";
621 commandDelimiter = ";";
624 case DataBaseServer.DB2:
629 commandDelimiter = ";";
632 case DataBaseServer.PostgreSQL:
635 commandDelimiter = ";";
639 this.Fail("Unknown DataBaseServer type");
640 throw new ApplicationException("Unknown DataBaseServer type");
643 private void GetExcpectedResults(ref int TblResult0, ref int TblResult1, ref int TblResult2)
645 // get excpected results
647 // transaction use was add for PostgreSQL
648 OleDbTransaction tr = con.BeginTransaction();
649 OleDbCommand cmd = new OleDbCommand("", con,tr);
650 cmd.CommandText = "Select count(*) from Customers";
651 TblResult0 = Int32.Parse(cmd.ExecuteScalar().ToString());
652 cmd.CommandText = "Select count(*) from Categories";
653 TblResult1 = Int32.Parse(cmd.ExecuteScalar().ToString());
654 cmd.CommandText = "Select count(*) from Region";
655 TblResult2 = Int32.Parse(cmd.ExecuteScalar().ToString());