MySqlDataReaderTest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // MySqlDataReaderTest.cs :- A class derived from the 'BaseRetrieve' class
  3. // - Contains code specific to mysql database
  4. //
  5. // Author:
  6. // Satya Sudha K ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Xml.XPath;
  32. using System.Data;
  33. using ByteFX.Data.MySqlClient;
  34. using System.Text.RegularExpressions;
  35. namespace MonoTests.System.Data {
  36. public class MySqlRetrieve : BaseRetrieve {
  37. public MySqlRetrieve (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 (XPathException e) {
  47. Console.WriteLine ("Error reading the config file !!");
  48. Console.WriteLine (e.Message);
  49. con = null;
  50. return;
  51. }
  52. con = new MySqlConnection (connectionString);
  53. try {
  54. con.Open ();
  55. } catch (MySqlException 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 open connection ");
  61. Console.WriteLine ("Probably the connection is already open");
  62. con = null;
  63. } catch (Exception e) {
  64. Console.WriteLine ("Cannot open connection ");
  65. con = null;
  66. }
  67. }
  68. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  69. {
  70. string dateStr = value.Trim ('\'');
  71. dateStr = dateStr.Trim ('\"');
  72. int year, month, day, hour, min, sec;
  73. year = month = day = hour = min = sec = 0;
  74. char [] splChars = {'!','@','#','$','%','^','&','*','-','+','.',','};
  75. string [] dateParts = dateStr.Split (' ');
  76. for (int index = 0; index < splChars.Length; index++) {
  77. dateParts [0] = dateParts [0].Replace (splChars [index], '/');
  78. if (dateParts.Length > 1)
  79. dateParts [1] = dateParts [1].Replace (splChars [index], ':');
  80. }
  81. dateStr = String.Join (" ", dateParts);
  82. Regex re = new Regex ("\\b(?<year>\\d{2,4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})(\\s+(?<hour>\\d{1,2}):(?<min>\\d{1,2}):(?<sec>\\d{1,2}))*");
  83. Match m = re.Match (dateStr);
  84. if (!m.Success) {
  85. re = new Regex("\\b(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})((?<hour>\\d{2})(?<min>\\d{2})(?<sec>\\d{2}))*");
  86. m = re.Match (dateStr);
  87. }
  88. string matchedVal = m.Result ("${year}");
  89. if (!matchedVal.Equals (""))
  90. year = Convert.ToInt32 (matchedVal);
  91. matchedVal = m.Result ("${month}");
  92. if (!matchedVal.Equals (""))
  93. month = Convert.ToInt32 (matchedVal);
  94. matchedVal = m.Result ("${day}");
  95. if (!matchedVal.Equals (""))
  96. day = Convert.ToInt32 (matchedVal);
  97. matchedVal = m.Result ("${hour}");
  98. if (!matchedVal.Equals (""))
  99. hour = Convert.ToInt32 (matchedVal);
  100. matchedVal = m.Result ("${min}");
  101. if (!matchedVal.Equals (""))
  102. min = Convert.ToInt32 (matchedVal);
  103. matchedVal = m.Result ("${sec}");
  104. if (!matchedVal.Equals (""))
  105. sec = Convert.ToInt32 (matchedVal);
  106. DateTime dateTime;
  107. try {
  108. dateTime = new DateTime (year, month, day, hour, min, sec);
  109. } catch (Exception e) {
  110. errorMsg = "ERROR : " + e.Message;
  111. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  112. return null;
  113. }
  114. return dateTime;
  115. }
  116. public override object ConvertToTimespan (Type type, string value, ref string errorMsg)
  117. {
  118. // Format the input string as 'hh:mm:yy'
  119. string dateStr = value.Trim ('\'');
  120. if (dateStr.IndexOf (':') == -1) {
  121. // String in the form 'hhmmss', insert ':'s in between
  122. dateStr = dateStr.Substring (0,2) + ":" +
  123. dateStr.Substring (2,2)+ ":" +
  124. dateStr.Substring (4,2);
  125. }
  126. TimeSpan timespan;
  127. try {
  128. timespan = TimeSpan.Parse (dateStr);
  129. } catch (Exception e) {
  130. errorMsg = "ERROR : " + e.Message;
  131. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  132. return null;
  133. }
  134. return timespan;
  135. }
  136. }
  137. }