2 // MySqlDataReaderTest.cs :- A class derived from the 'BaseRetrieve' class
3 // - Contains code specific to mysql database
6 // Satya Sudha K (ksathyasudha@novell.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Xml.XPath;
34 using ByteFX.Data.MySqlClient;
35 using System.Text.RegularExpressions;
37 namespace MonoTests.System.Data {
39 public class MySqlRetrieve : BaseRetrieve {
41 public MySqlRetrieve (string database) : base (database)
45 // returns a Open connection
46 public override void GetConnection ()
49 string connectionString = null;
51 connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
52 } catch (XPathException e) {
53 Console.WriteLine ("Error reading the config file !!");
54 Console.WriteLine (e.Message);
59 con = new MySqlConnection (connectionString);
62 } catch (MySqlException e) {
63 Console.WriteLine ("Cannot establish connection with the database");
64 Console.WriteLine ("Probably the Database is down ");
66 } catch (InvalidOperationException e) {
67 Console.WriteLine ("Cannot open connection ");
68 Console.WriteLine ("Probably the connection is already open");
70 } catch (Exception e) {
71 Console.WriteLine ("Cannot open connection ");
76 public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
79 string dateStr = value.Trim ('\'');
80 dateStr = dateStr.Trim ('\"');
81 int year, month, day, hour, min, sec;
82 year = month = day = hour = min = sec = 0;
83 char [] splChars = {'!','@','#','$','%','^','&','*','-','+','.',','};
84 string [] dateParts = dateStr.Split (' ');
86 for (int index = 0; index < splChars.Length; index++) {
87 dateParts [0] = dateParts [0].Replace (splChars [index], '/');
88 if (dateParts.Length > 1)
89 dateParts [1] = dateParts [1].Replace (splChars [index], ':');
92 dateStr = String.Join (" ", dateParts);
93 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}))*");
95 Match m = re.Match (dateStr);
97 re = new Regex("\\b(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})((?<hour>\\d{2})(?<min>\\d{2})(?<sec>\\d{2}))*");
98 m = re.Match (dateStr);
101 string matchedVal = m.Result ("${year}");
102 if (!matchedVal.Equals (""))
103 year = Convert.ToInt32 (matchedVal);
104 matchedVal = m.Result ("${month}");
105 if (!matchedVal.Equals (""))
106 month = Convert.ToInt32 (matchedVal);
107 matchedVal = m.Result ("${day}");
108 if (!matchedVal.Equals (""))
109 day = Convert.ToInt32 (matchedVal);
110 matchedVal = m.Result ("${hour}");
111 if (!matchedVal.Equals (""))
112 hour = Convert.ToInt32 (matchedVal);
113 matchedVal = m.Result ("${min}");
114 if (!matchedVal.Equals (""))
115 min = Convert.ToInt32 (matchedVal);
116 matchedVal = m.Result ("${sec}");
117 if (!matchedVal.Equals (""))
118 sec = Convert.ToInt32 (matchedVal);
122 dateTime = new DateTime (year, month, day, hour, min, sec);
123 } catch (Exception e) {
124 errorMsg = "ERROR : " + e.Message;
125 errorMsg += "\nSTACKTRACE : " + e.StackTrace;
132 public override object ConvertToTimespan (Type type, string value, ref string errorMsg)
135 // Format the input string as 'hh:mm:yy'
136 string dateStr = value.Trim ('\'');
138 if (dateStr.IndexOf (':') == -1) {
139 // String in the form 'hhmmss', insert ':'s in between
140 dateStr = dateStr.Substring (0,2) + ":" +
141 dateStr.Substring (2,2)+ ":" +
142 dateStr.Substring (4,2);
147 timespan = TimeSpan.Parse (dateStr);
148 } catch (Exception e) {
149 errorMsg = "ERROR : " + e.Message;
150 errorMsg += "\nSTACKTRACE : " + e.StackTrace;