OleDbDataReader_GetDateTime.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Text;
  25. using System.Data;
  26. using System.Data.OleDb;
  27. using MonoTests.System.Data.Utils;
  28. using MonoTests.System.Data.Utils.Data;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Data.OleDb
  31. {
  32. [TestFixture]
  33. public class OleDbDataReader_GetDateTime : ADONetTesterClass
  34. {
  35. private OleDbConnection con;
  36. private OleDbCommand cmd;
  37. private OleDbDataReader rdr;
  38. Exception exp;
  39. [SetUp]
  40. [TearDown]
  41. public void TearDown()
  42. {
  43. if (con != null && con.State == ConnectionState.Open) con.Close();
  44. }
  45. public static void Main()
  46. {
  47. OleDbDataReader_GetDateTime tc = new OleDbDataReader_GetDateTime();
  48. Exception exp = null;
  49. try
  50. {
  51. tc.BeginTest("OleDbDataReader_GetDateTime");
  52. tc.run();
  53. }
  54. catch(Exception ex){exp = ex;}
  55. finally {tc.EndTest(exp);}
  56. }
  57. public void run()
  58. {
  59. SimpleValue();
  60. MinDate();
  61. }
  62. [Test]
  63. public void SimpleValue()
  64. {
  65. try
  66. {
  67. BeginCase("check simple value");
  68. //prepare data
  69. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  70. con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  71. con.Open();
  72. cmd = new OleDbCommand("Select BirthDate From Employees where EmployeeID = 100", con);
  73. rdr = cmd.ExecuteReader();
  74. rdr.Read();
  75. DateTime dt = rdr.GetDateTime(0); //will be 1988-May-31 15:33:44
  76. Compare(dt,new DateTime(1988,5,31,15,33,44,00));
  77. }
  78. catch(Exception ex)
  79. {
  80. exp = ex;
  81. }
  82. finally
  83. {
  84. EndCase(exp);
  85. if (rdr != null && !rdr.IsClosed)
  86. {
  87. rdr.Close();
  88. }
  89. if (con != null && con.State != ConnectionState.Closed)
  90. {
  91. con.Close();
  92. }
  93. exp = null;
  94. }
  95. }
  96. [Test]
  97. public void MinDate()
  98. {
  99. BeginCase("Test Min date.");
  100. exp = null;
  101. string[] dateColumns;
  102. DateTime[] expectedValues;
  103. InitMinDates(out dateColumns, out expectedValues);
  104. try
  105. {
  106. con = new OleDbConnection(ConnectedDataProvider.ConnectionString);
  107. cmd = new OleDbCommand();
  108. cmd.Connection = con;
  109. cmd.CommandText = BuildMinDateTimeSelectSql(dateColumns);
  110. con.Open();
  111. rdr = cmd.ExecuteReader();
  112. Compare(true, rdr.HasRows);
  113. bool b = rdr.Read();
  114. for (int i=0; i<dateColumns.Length && i<expectedValues.Length; i++)
  115. {
  116. int j=-1;
  117. j = rdr.GetOrdinal(dateColumns[i]);
  118. //DateTime result = rdr.GetDateTime(j);
  119. object result = rdr.GetValue(j);
  120. Compare(result, expectedValues[i]);
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. exp = ex;
  126. }
  127. finally
  128. {
  129. EndCase(exp);
  130. if (rdr != null && !rdr.IsClosed)
  131. {
  132. rdr.Close();
  133. }
  134. if (con != null && con.State != ConnectionState.Closed)
  135. {
  136. con.Close();
  137. }
  138. }
  139. }
  140. private void InitMinDates(out string[] columns, out DateTime[] values)
  141. {
  142. switch(ConnectedDataProvider.GetDbType())
  143. {
  144. case DataBaseServer.SQLServer:
  145. case DataBaseServer.Sybase:
  146. columns = new string[] {"T_DATETIME", "T_SMALLDATETIME"};
  147. values = new DateTime[] {new DateTime(1753, 01, 01,00, 00, 00), // 01/01/1753 00:00:00.000
  148. new DateTime(1900, 01, 01,00, 00, 00)}; // 01/01/1900 00:00
  149. break;
  150. case DataBaseServer.Oracle:
  151. columns = new string[] {"T_DATE"};
  152. values = new DateTime[] {new DateTime(0001, 01, 01,00, 00, 00)}; //01-Jan-0001 12:00:00 AM
  153. break;
  154. case DataBaseServer.PostgreSQL:
  155. columns = new string[] {"T_TIMESTAMP"};
  156. values = new DateTime[] {new DateTime(1753, 01, 01,00, 00, 00)}; //01-Jan-1753 12:00:00 AM
  157. break;
  158. case DataBaseServer.DB2:
  159. columns = new string[] {"T_DATE", "T_TIMESTAMP"};
  160. values = new DateTime[] {new DateTime(0001, 01, 01), // 1/1/0001
  161. new DateTime(0001, 01, 01, 00,00,00,0)}; // 1/1/0001 00:00:00.000
  162. break;
  163. default:
  164. throw new ApplicationException(string.Format("GHT ERROR: Unknown DB server [{0}].",ConnectedDataProvider.GetDbType()));
  165. }
  166. }
  167. private string BuildMinDateTimeSelectSql(string[] dateColumns)
  168. {
  169. StringBuilder sqlBuilder = new StringBuilder();
  170. sqlBuilder.Append("SELECT ");
  171. foreach(string col in dateColumns)
  172. {
  173. sqlBuilder.Append(col + ", ");
  174. }
  175. sqlBuilder.Remove(sqlBuilder.Length - 2, 2);
  176. sqlBuilder.Append(" FROM TYPES_EXTENDED WHERE ID='MIN'");
  177. return sqlBuilder.ToString();
  178. }
  179. }
  180. }