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