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.OracleClient ; 27 28 using MonoTests.System.Data.Utils; 29 30 using MonoTests.System.Data.Utils.Data; 31 32 using NUnit.Framework; 33 34 namespace MonoTests.System.Data.OracleClient 35 { 36 [TestFixture] 37 public class OracleCommand_Parameters : ADONetTesterClass 38 { 39 //Used to test GUID. 40 private const string TEST_GUID_STRING = "239A3C5E-8D41-11D1-B675-00C04FA3C554"; 41 42 private Exception exp; Main()43 public static void Main() 44 { 45 OracleCommand_Parameters tc = new OracleCommand_Parameters(); 46 Exception exp = null; 47 try 48 { 49 tc.BeginTest("OracleCommand_Parameters"); 50 tc.run(); 51 } 52 catch(Exception ex) 53 { 54 exp = ex; 55 } 56 finally 57 { 58 tc.EndTest(exp); 59 } 60 } 61 62 [Test] run()63 public void run() 64 { 65 66 #region Simple Tests 67 //string str=""; 68 string sql; 69 OracleConnection con = null; 70 71 sql = "UPDATE Employees SET Region = :Region, TitleOfCourtesy = :TitleOfCourtesy WHERE EmployeeID=1"; 72 con = new OracleConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString); 73 74 //not testing with DB2 provider 75 if (ConnectedDataProvider.GetDbType(con) != DataBaseServer.DB2) 76 { 77 78 OracleCommand cmd = new OracleCommand(sql, con); 79 cmd.Parameters.Add(new OracleParameter(":Region", OracleType.VarChar)); 80 cmd.Parameters.Add(new OracleParameter(":TitleOfCourtesy", OracleType.VarChar)); 81 82 con.Open(); 83 cmd.Parameters[":Region"].Value = "WA"; 84 cmd.Parameters[":TitleOfCourtesy"].Value = "Mr"; 85 86 //return the number of rows affected 87 int i = cmd.ExecuteNonQuery(); 88 89 try 90 { 91 BeginCase("Check row count"); 92 Compare(i, 1); 93 } 94 catch(Exception ex){exp = ex;} 95 finally{EndCase(exp); exp = null;} 96 } 97 98 if (con.State == ConnectionState.Open) con.Close(); 99 #endregion 100 #region Test Parameter Types 101 #region General 102 TypesSubTests(ConnectedDataProvider.GetSimpleDbTypesParameters()); 103 TypesSubTests(ConnectedDataProvider.GetExtendedDbTypesParameters()); 104 #endregion 105 #endregion 106 } 107 TypesSubTests(DbTypeParametersCollection typesToTest)108 private void TypesSubTests(DbTypeParametersCollection typesToTest) 109 { 110 DbTypeParametersCollection currentlyTested = new DbTypeParametersCollection(typesToTest.TableName); 111 int affectedRows; 112 string rowId = string.Empty; 113 114 foreach (DbTypeParameter currentParamType in typesToTest) 115 { 116 try 117 { 118 BeginCase("Test INSERT with parameter of type: " + currentParamType.DbTypeName); 119 rowId = string.Format("13282_{0}", this.TestCaseNumber); 120 currentlyTested.Clear(); 121 currentlyTested.Add(currentParamType); 122 affectedRows = currentlyTested.ExecuteInsert(rowId); 123 Compare(affectedRows, 1); 124 } 125 catch(Exception ex) 126 { 127 exp = ex; 128 } 129 finally 130 { 131 EndCase(exp); 132 exp = null; 133 currentlyTested.ExecuteDelete(rowId); 134 } 135 } 136 } 137 138 //Test insertion of a GUID parameter on MSSQLServer. 139 [Test] DoTestGUIDOnMSSQLServer()140 public void DoTestGUIDOnMSSQLServer() 141 { 142 if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) 143 return; 144 DbTypeParametersCollection guidRow = new DbTypeParametersCollection(ConnectedDataProvider.SPECIFIC_TYPES_TABLE_NAME); 145 guidRow.Add("UNIQUEIDENTIFIER", new Guid(TEST_GUID_STRING)); 146 TypesSubTests(guidRow); 147 } 148 //Test problems specific to the TIME type on DB2. 149 [Test] DoTestTimeOnDB2()150 public void DoTestTimeOnDB2() 151 { 152 if (ConnectedDataProvider.GetDbType() != DataBaseServer.DB2) 153 return; 154 155 OracleConnection conn = new OracleConnection(ConnectedDataProvider.ConnectionString); 156 string rowId = string .Empty; 157 158 try 159 { 160 BeginCase("Test INSERT to TIME column using only TimeOfDate part of DateTime"); 161 rowId = "13282_" + this.TestCaseNumber.ToString(); 162 OracleCommand cmd = new OracleCommand(); 163 conn.Open(); 164 cmd.Connection = conn; 165 cmd.CommandText = string.Format("INSERT INTO {0} (ID, T_TIME) VALUES ('{1}', ?)",ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME ,rowId); 166 cmd.Parameters.Add("time", DateTime.Now.TimeOfDay); 167 int affectedRows; 168 affectedRows = cmd.ExecuteNonQuery(); 169 Compare(affectedRows, 1); 170 } 171 catch (Exception ex) 172 { 173 exp = ex; 174 } 175 finally 176 { 177 EndCase(exp); 178 exp = null; 179 DbTypeParametersCollection.ExecuteDelete(ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME, rowId); 180 conn.Close(); 181 } 182 183 } 184 185 [Test] 186 #if JAVA 187 [Category("NotWorking")] 188 #endif DoTestTimeOnDB2_bug3391()189 public void DoTestTimeOnDB2_bug3391() 190 { 191 if (ConnectedDataProvider.GetDbType() != DataBaseServer.DB2) 192 return; 193 194 OracleConnection conn = new OracleConnection(ConnectedDataProvider.ConnectionString); 195 string rowId = string .Empty; 196 197 try { 198 BeginCase("Test INSERT to TIME column using all of the DateTime"); 199 rowId = "13282_" + this.TestCaseNumber.ToString(); 200 OracleCommand cmd = new OracleCommand(); 201 conn.Open(); 202 cmd.Connection = conn; 203 cmd.CommandText = string.Format("INSERT INTO {0} (ID, T_TIME) VALUES ('{1}', ?)",ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME ,rowId); 204 cmd.Parameters.Add("time", DateTime.Now); 205 try { 206 cmd.ExecuteNonQuery(); 207 ExpectedExceptionNotCaught("System.OracleException"); 208 } 209 catch (OracleException ex) { 210 ExpectedExceptionCaught(ex); 211 } 212 } 213 catch (Exception ex) { 214 exp = ex; 215 } 216 finally { 217 EndCase(exp); 218 exp = null; 219 DbTypeParametersCollection.ExecuteDelete(ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME, rowId); 220 conn.Close(); 221 } 222 } 223 } 224 } 225