MySqlDbAdapterTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // MySqlDbAdapterTest.cs :- Defines a class 'MySqlAdapter' derived from the
  3. // 'BaseAdapter' class
  4. // - Contains code specific to mysql 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.Xml.XPath;
  33. using System.Data;
  34. using System.Data.Common;
  35. using ByteFX.Data.MySqlClient;
  36. using System.Text.RegularExpressions;
  37. namespace MonoTests.System.Data {
  38. public class MySqlAdapter : BaseAdapter {
  39. public MySqlAdapter (string database) : base (database)
  40. {
  41. dataAdapter = new MySqlDataAdapter ();
  42. }
  43. // returns a Open connection
  44. public override void GetConnection ()
  45. {
  46. string connectionString = null;
  47. try {
  48. connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
  49. } catch (XPathException e) {
  50. Console.WriteLine ("Error reading the config file !!");
  51. Console.WriteLine (e.Message);
  52. con = null;
  53. return;
  54. }
  55. con = new MySqlConnection (connectionString);
  56. try {
  57. con.Open ();
  58. } catch (MySqlException e) {
  59. Console.WriteLine ("Cannot establish connection with the database");
  60. Console.WriteLine ("Probably the Database is down ");
  61. con = null;
  62. } catch (InvalidOperationException e) {
  63. Console.WriteLine ("Cannot open connection ");
  64. Console.WriteLine ("Probably the connection is already open");
  65. con = null;
  66. } catch (Exception e) {
  67. Console.WriteLine ("Cannot open connection ");
  68. con = null;
  69. }
  70. }
  71. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  72. {
  73. string dateStr = value.Trim ('\'');
  74. dateStr = dateStr.Trim ('\"');
  75. int year, month, day, hour, min, sec;
  76. year = month = day = hour = min = sec = 0;
  77. Char [] splChars = {'!','@','#','$','%','^','&','*','-','+','.',','};
  78. string [] dateParts = dateStr.Split (' ');
  79. for (int index = 0; index < splChars.Length; index++) {
  80. dateParts [0] = dateParts [0].Replace (splChars [index], '/');
  81. if (dateParts.Length > 1)
  82. dateParts [1] = dateParts [1].Replace (splChars [index], ':');
  83. }
  84. dateStr = String.Join (" ", dateParts);
  85. 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}))*");
  86. Match m = re.Match (dateStr);
  87. if (!m.Success) {
  88. re = new Regex ("\\b(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})((?<hour>\\d{2})(?<min>\\d{2})(?<sec>\\d{2}))*");
  89. m = re.Match (dateStr);
  90. }
  91. string matchedVal = m.Result ("${year}");
  92. if (!matchedVal.Equals (""))
  93. year = Convert.ToInt32 (matchedVal);
  94. matchedVal = m.Result ("${month}");
  95. if (!matchedVal.Equals (""))
  96. month = Convert.ToInt32 (matchedVal);
  97. matchedVal = m.Result ("${day}");
  98. if (!matchedVal.Equals (""))
  99. day = Convert.ToInt32 (matchedVal);
  100. matchedVal = m.Result ("${hour}");
  101. if (!matchedVal.Equals (""))
  102. hour = Convert.ToInt32 (matchedVal);
  103. matchedVal = m.Result ("${min}");
  104. if (!matchedVal.Equals (""))
  105. min = Convert.ToInt32 (matchedVal);
  106. matchedVal = m.Result ("${sec}");
  107. if (!matchedVal.Equals (""))
  108. sec = Convert.ToInt32 (matchedVal);
  109. DateTime dateTime;
  110. try {
  111. dateTime = new DateTime (year, month, day, hour, min, sec);
  112. } catch (Exception e) {
  113. errorMsg = "ERROR : " + e.Message;
  114. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  115. return false;
  116. }
  117. return dateTime;
  118. }
  119. public override object ConvertToTimeSpan (Type type, string value, ref string errorMsg)
  120. {
  121. // Format the input string as 'hh:mm:yy'
  122. string dateStr = value.Trim ('\'');
  123. if (dateStr.IndexOf (':') == -1) {
  124. // String in the form 'hhmmss', insert ':'s in between
  125. dateStr = dateStr.Substring (0, 2) + ":" +
  126. dateStr.Substring (2, 2) + ":" +
  127. dateStr.Substring (4, 2);
  128. }
  129. TimeSpan timespan;
  130. try {
  131. timespan = TimeSpan.Parse (dateStr);
  132. } catch (Exception e) {
  133. errorMsg = "ERROR : " + e.Message;
  134. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  135. return false;
  136. }
  137. return timespan;
  138. }
  139. public override void PopulateDataSetFromTable (string queryStr, string tableName)
  140. {
  141. cmd.CommandText = queryStr;
  142. MySqlDataAdapter da = (MySqlDataAdapter) dataAdapter;
  143. da.SelectCommand = (MySqlCommand) cmd;
  144. dataset = new DataSet ();
  145. da.Fill (dataset, tableName);
  146. }
  147. public override bool ReconcileChanges (string tableName, ref string errorMsg)
  148. {
  149. try {
  150. new MySqlCommandBuilder ((MySqlDataAdapter) dataAdapter);
  151. dataAdapter.Update (dataset, tableName);
  152. } catch (Exception e) {
  153. errorMsg = "ERROR : " + e.Message;
  154. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  155. return false;
  156. }
  157. return true;
  158. }
  159. }
  160. }