PostgresDataReaderTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // PostgresDataReaderTest.cs :- Defines a class 'PostgresRetrieve' derived from the
  3. // 'BaseRetrieve' class
  4. // - Contains code specific to postgres database
  5. //
  6. // Author:
  7. // Satya Sudha K ([email protected])
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using Npgsql;
  34. using System.Text.RegularExpressions;
  35. namespace MonoTests.System.Data {
  36. public class PostgresRetrieve : BaseRetrieve {
  37. public PostgresRetrieve (string database) : base (database)
  38. {
  39. }
  40. // returns a Open connection
  41. public override void GetConnection ()
  42. {
  43. string connectionString = null;
  44. try {
  45. connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
  46. } catch (Exception e) {
  47. Console.WriteLine ("Error reading the config file");
  48. Console.WriteLine (e.Message);
  49. con = null;
  50. return;
  51. }
  52. con = new NpgsqlConnection (connectionString);
  53. try {
  54. con.Open ();
  55. } catch (NpgsqlException e) {
  56. Console.WriteLine ("Cannot establish connection with the database");
  57. Console.WriteLine ("Probably the Database is down!!");
  58. con = null;
  59. } catch (InvalidOperationException e) {
  60. Console.WriteLine ("Cannot establish connection with the database");
  61. Console.WriteLine ("Probably connection is already open");
  62. con = null;
  63. } catch (Exception e) {
  64. Console.WriteLine ("Cannot establish connection with the database");
  65. con = null;
  66. }
  67. }
  68. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  69. {
  70. int month, day, year, hour, min, sec;
  71. month = day = year = hour = min = sec = 0;
  72. double msec = 0;
  73. msec = 0;
  74. Regex re = new Regex ("\\b(?<year>\\d{2,4})\\-(?<month>\\d{1,2})\\-(?<day>\\d{1,2})(\\s+(?<hour>\\d{1,2}):(?<minute>\\d{1,2}):(?<sec>\\d{1,2})(\\.(?<msec>\\d{1,6}))*)*");
  75. value = value.Trim ('\'');
  76. value = value.Trim ('\"');
  77. Match m = re.Match (value);
  78. string matchedVal = null;
  79. if (m.Success) {
  80. matchedVal = m.Result ("${year}");
  81. if (!matchedVal.Equals (""))
  82. year = Convert.ToInt32 (matchedVal);
  83. matchedVal = m.Result ("${month}");
  84. if (!matchedVal.Equals (""))
  85. month = Convert.ToInt32 (matchedVal);
  86. matchedVal = m.Result ("${day}");
  87. if (!matchedVal.Equals (""))
  88. day = Convert.ToInt32 (matchedVal);
  89. } else {
  90. // Only timespan
  91. re = new Regex("\\b(?<hour>\\d{1,2}):(?<minute>\\d{1,2}):(?<sec>\\d{1,2})(\\.(?<msec>\\d{1,6}))*");
  92. m = re.Match (value);
  93. }
  94. try {
  95. matchedVal = m.Result ("${hour}");
  96. if (!matchedVal.Equals (""))
  97. hour = Convert.ToInt32 (matchedVal);
  98. matchedVal = m.Result ("${minute}");
  99. if (!matchedVal.Equals (""))
  100. min = Convert.ToInt32 (matchedVal);
  101. matchedVal = m.Result ("${sec}");
  102. if (!matchedVal.Equals (""))
  103. sec = Convert.ToInt32 (matchedVal);
  104. matchedVal = m.Result ("${msec}");
  105. if (!matchedVal.Equals (""))
  106. msec = Convert.ToDouble (matchedVal);
  107. if (msec > 1000)
  108. msec = msec / 1000;
  109. if (day == 0 && month == 0 && year == 0) {
  110. // Time span only
  111. TimeSpan ts =TimeSpan.Parse(value);
  112. return ts;
  113. } else {
  114. DateTime dateObj = new DateTime (year, month, day, hour, min, sec);
  115. dateObj = dateObj.AddMilliseconds (msec);
  116. return dateObj;
  117. }
  118. } catch (Exception e) {
  119. errorMsg = "ERROR : " + e.Message;
  120. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  121. return null;
  122. }
  123. }
  124. }
  125. }