OleDbDataAdapter_TableMappings.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.Data;
  25. using System.Data.Common;
  26. using System.Data.OleDb;
  27. using MonoTests.System.Data.Utils;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OleDb
  30. {
  31. [TestFixture]
  32. public class OleDbDataAdapter_TableMappings : ADONetTesterClass
  33. {
  34. public static void Main()
  35. {
  36. OleDbDataAdapter_TableMappings tc = new OleDbDataAdapter_TableMappings();
  37. Exception exp = null;
  38. try
  39. {
  40. tc.BeginTest("OleDbDataAdapter_TableMappings");
  41. tc.run();
  42. }
  43. catch(Exception ex)
  44. {
  45. exp = ex;
  46. }
  47. finally
  48. {
  49. tc.EndTest(exp);
  50. }
  51. }
  52. //public TestClass():base(true){}
  53. //Activate this constructor to log Failures to a log file
  54. //public TestClass(System.IO.TextWriter tw):base(tw, false){}
  55. //Activate this constructor to log All to a log file
  56. //public TestClass(System.IO.TextWriter tw):base(tw, true){}
  57. //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
  58. [Test]
  59. public void run()
  60. {
  61. // open a transaction for PostgreSQL
  62. OleDbDataAdapter oleDBda = new OleDbDataAdapter();
  63. oleDBda.SelectCommand = new OleDbCommand("",new OleDbConnection());
  64. DataAdapter_TableMappings((DbDataAdapter)oleDBda);
  65. }
  66. public void DataAdapter_TableMappings(DbDataAdapter dbDA)
  67. {
  68. Exception exp = null;
  69. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  70. IDbCommand ICmd = Ida.SelectCommand;
  71. IDbConnection IConn = ICmd.Connection;
  72. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  73. IConn.Open();
  74. ICmd.Transaction = IConn.BeginTransaction();
  75. //--- Default value ---
  76. try
  77. {
  78. BeginCase("TableMappings Default value");
  79. Compare(dbDA.TableMappings.Count ,0);
  80. }
  81. catch(Exception ex) {exp = ex;}
  82. finally {EndCase(exp); exp = null;}
  83. //init dataset
  84. DataSet ds = new DataSet();
  85. ds.Tables.Add("CustTable");
  86. ds.Tables.Add("EmplTable");
  87. //load the tables
  88. dbDA.TableMappings.Add("Table","CustTable");
  89. ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  90. dbDA.Fill(ds);
  91. dbDA.TableMappings.Clear();
  92. dbDA.TableMappings.Add("Table","EmplTable");
  93. ICmd.CommandText = " SELECT EmployeeID, LastName, FirstName, Title FROM Employees where EmployeeID > 0";
  94. dbDA.Fill(ds);
  95. try
  96. {
  97. BeginCase("TableMappings.Count");
  98. Compare(ds.Tables.Count ,2);
  99. }
  100. catch(Exception ex) {exp = ex;}
  101. finally {EndCase(exp); exp = null;}
  102. try
  103. {
  104. BeginCase("Customers rows count");
  105. Compare(ds.Tables["CustTable"].Rows.Count > 0,true);
  106. }
  107. catch(Exception ex) {exp = ex;}
  108. finally {EndCase(exp); exp = null;}
  109. try
  110. {
  111. BeginCase("Employees rows count");
  112. Compare(ds.Tables["EmplTable"].Rows.Count > 0,true);
  113. }
  114. catch(Exception ex) {exp = ex;}
  115. finally {EndCase(exp); exp = null;}
  116. try
  117. {
  118. BeginCase("Employees Columns");
  119. Compare(ds.Tables["EmplTable"].Columns.IndexOf("EmployeeID") >= 0,true);
  120. }
  121. catch(Exception ex) {exp = ex;}
  122. finally {EndCase(exp); exp = null;}
  123. try
  124. {
  125. BeginCase("Customer Columns");
  126. Compare(ds.Tables["CustTable"].Columns.IndexOf("CustomerID") >= 0,true);
  127. }
  128. catch(Exception ex) {exp = ex;}
  129. finally {EndCase(exp); exp = null;}
  130. //Another checking
  131. if (ConnectedDataProvider.GetDbType() != DataBaseServer.DB2) {
  132. BeginCase("Check table mappings with stored procedure of another owner");
  133. try
  134. {
  135. DataSet ds1 = new DataSet();
  136. ds1.Tables.Add("EmplTable");
  137. dbDA.TableMappings.Clear();
  138. dbDA.TableMappings.Add("Table","EmplTable");
  139. ICmd.CommandType = CommandType.StoredProcedure;
  140. switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString))
  141. {
  142. case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
  143. // On ORACLE, the Scheam is the user is the owner.
  144. ICmd.CommandText = "GHTDB.GH_DUMMY";
  145. break;
  146. case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
  147. ICmd.CommandText = "public.gh_dummy";
  148. break;
  149. default:
  150. ICmd.CommandText = "mainsoft.GH_DUMMY";
  151. break;
  152. }
  153. // investigate the SP parameters
  154. OleDbCommandBuilder.DeriveParameters((OleDbCommand)ICmd);
  155. Compare(2,ICmd.Parameters.Count);
  156. // add one numeric parameter, and Fill
  157. ICmd.Parameters.Clear();
  158. ICmd.Parameters.Add(new OleDbParameter("EmployeeIDPrm",1));
  159. dbDA.Fill(ds1);
  160. Compare(ds1.Tables.Count,1);
  161. Compare(ds1.Tables[0].Rows.Count >0,true);
  162. }
  163. catch(Exception ex) {exp = ex;}
  164. finally {EndCase(exp); exp = null;}
  165. }
  166. //
  167. ((IDbDataAdapter)dbDA).SelectCommand.Transaction.Commit();
  168. //close connection
  169. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  170. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  171. }
  172. }
  173. }