OleDbDataReader_GetValue.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.OleDb;
  26. using MonoTests.System.Data.Utils;
  27. using MonoTests.System.Data.Utils.Data;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OleDb
  30. {
  31. [TestFixture]
  32. public class OleDbDataReader_GetValue : ADONetTesterClass
  33. {
  34. Exception exp;
  35. OleDbConnection con;
  36. char [] Result;
  37. [SetUp]
  38. public void SetUp()
  39. {
  40. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  41. con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  42. Result = new char[100];
  43. con.Open();
  44. }
  45. [TearDown]
  46. public void TearDown()
  47. {
  48. if (con.State == ConnectionState.Open) con.Close();
  49. }
  50. public static void Main()
  51. {
  52. OleDbDataReader_GetValue tc = new OleDbDataReader_GetValue();
  53. tc.exp = null;
  54. try
  55. {
  56. tc.BeginTest("OleDbDataReader_GetValue");
  57. tc.SetUp();
  58. tc.run();
  59. }
  60. catch (Exception ex)
  61. {
  62. tc.exp = ex;
  63. }
  64. finally
  65. {
  66. tc.EndTest(tc.exp);
  67. tc.TearDown();
  68. }
  69. }
  70. [Test]
  71. public void run()
  72. {
  73. TypesTests(ConnectedDataProvider.GetSimpleDbTypesParameters());
  74. TypesTests(ConnectedDataProvider.GetExtendedDbTypesParameters());
  75. }
  76. [Test]
  77. public void SimpleTest()
  78. {
  79. OleDbDataReader rdr = null;
  80. exp = null;
  81. try
  82. {
  83. BeginCase("check value");
  84. OleDbCommand cmd = new OleDbCommand("Select LastName From Employees Where EmployeeID = 100", con);
  85. rdr = cmd.ExecuteReader();
  86. rdr.Read();
  87. Object obj = rdr.GetValue(0);
  88. Compare(obj.ToString(), "Last100");
  89. }
  90. catch (Exception ex)
  91. {
  92. exp = ex;
  93. }
  94. finally
  95. {
  96. if (rdr != null)
  97. {
  98. rdr.Close();
  99. }
  100. EndCase(exp);
  101. exp = null;
  102. }
  103. }
  104. private void TypesTests(DbTypeParametersCollection typesToTest)
  105. {
  106. exp = null;
  107. DbTypeParametersCollection currentlyTested = new DbTypeParametersCollection(typesToTest.TableName);
  108. string rowId = "13289";
  109. object dbValue;
  110. OleDbDataReader rdr = null;
  111. OleDbConnection selectCon = null;
  112. DataBaseServer testedDbServer = ConnectedDataProvider.GetDbType();
  113. foreach (DbTypeParameter currentParamType in typesToTest)
  114. {
  115. BeginCase("Test value of db type: " + currentParamType.DbTypeName);
  116. //Prepare data:
  117. rowId = string.Format("13289_{0}", this.TestCaseNumber);
  118. currentlyTested.Clear();
  119. currentlyTested.Add(currentParamType);
  120. currentlyTested.ExecuteInsert(rowId);
  121. try
  122. {
  123. currentlyTested.ExecuteSelectReader(rowId, out rdr, out selectCon);
  124. rdr.Read();
  125. dbValue = WorkaroundOracleCharsPaddingLimitation(testedDbServer, currentParamType, rdr.GetValue(0));
  126. if (currentParamType.Value.GetType().IsArray)
  127. {
  128. Compare(dbValue as Array, currentParamType.Value as Array);
  129. }
  130. else
  131. {
  132. Compare(dbValue, currentParamType.Value);
  133. }
  134. }
  135. catch(Exception ex)
  136. {
  137. exp = ex;
  138. }
  139. finally
  140. {
  141. EndCase(exp);
  142. exp = null;
  143. if (rdr != null && !rdr.IsClosed)
  144. {
  145. rdr.Close();
  146. }
  147. if (selectCon != null && selectCon.State != ConnectionState.Closed)
  148. {
  149. selectCon.Close();
  150. }
  151. currentlyTested.ExecuteDelete(rowId);
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// This is a workaround for the extra white spaces added in oracle to NCHAR & NVARCHAR values.
  157. /// The problem is a documented GH limitation, see bug #3417.
  158. /// The workaround is to trim the lemgth of the returned string to the specified length of the parameter/column.
  159. /// </summary>
  160. /// <param name="testedServer">The database server we are currently running on.</param>
  161. /// <param name="val">The value returned from the database.</param>
  162. /// <returns>The normalized value..</returns>
  163. private object WorkaroundOracleCharsPaddingLimitation(DataBaseServer testedServer, DbTypeParameter currentParam, object val)
  164. {
  165. object origVal = val;
  166. string dbTypeName = currentParam.DbTypeName.ToUpper();
  167. if ( (testedServer == DataBaseServer.Oracle) && (dbTypeName == "CHAR" || dbTypeName == "NCHAR") )
  168. {
  169. val = ((string)val).Substring(0, currentParam.Size);
  170. Log(string.Format("Worked around oracle chars padding limitation by triming '{0}' to '{1}'", origVal, val));
  171. }
  172. return val;
  173. }
  174. }
  175. }