Merge pull request #463 from strawd/concurrent-requests
[mono.git] / mcs / class / System.Data / Test / DataProviderTests / datareadertests / MySqlDataReaderTest.cs
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 (ksathyasudha@novell.com)
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
31 using System;
32 using System.Xml.XPath;
33 using System.Data;
34 using ByteFX.Data.MySqlClient;
35 using System.Text.RegularExpressions;
36
37 namespace MonoTests.System.Data {
38
39         public class MySqlRetrieve : BaseRetrieve {
40         
41                 public MySqlRetrieve (string database) : base (database) 
42                 {
43                 }
44                 
45                 // returns a Open connection 
46                 public override void GetConnection () 
47                 {
48                         
49                         string connectionString = null;
50                         try {
51                                 connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
52                         } catch (XPathException e) {
53                                 Console.WriteLine ("Error reading the config file !!");
54                                 Console.WriteLine (e.Message);
55                                 con = null;
56                                 return;
57                         }
58                         
59                         con = new MySqlConnection (connectionString);
60                         try {
61                                 con.Open ();
62                         } catch (MySqlException e) {
63                                 Console.WriteLine ("Cannot establish connection with the database");
64                                 Console.WriteLine ("Probably the Database is down ");
65                                 con = null;
66                         } catch (InvalidOperationException e) {
67                                 Console.WriteLine ("Cannot open connection ");
68                                 Console.WriteLine ("Probably the connection is already open");
69                                 con = null;
70                         } catch (Exception e) {
71                                 Console.WriteLine ("Cannot open connection ");
72                                 con = null;
73                         }
74                 }
75                 
76                 public override object ConvertToDateTime (Type type, string value, ref string errorMsg) 
77                 {
78                 
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 (' ');
85
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], ':');
90                         }
91
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}))*");
94                         
95                         Match m = re.Match (dateStr);
96                         if (!m.Success) {
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);
99                         }
100                         
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);
119                         
120                         DateTime dateTime;
121                         try {
122                                 dateTime = new DateTime (year, month, day, hour, min, sec);
123                         } catch (Exception e) {
124                                 errorMsg = "ERROR : " + e.Message;
125                                 errorMsg += "\nSTACKTRACE : " + e.StackTrace;
126                                 return null;
127                         }
128                         
129                         return dateTime;
130                 }
131                 
132                 public override object ConvertToTimespan (Type type, string value, ref string errorMsg) 
133                 {
134                 
135                         // Format the input string as 'hh:mm:yy'
136                         string dateStr = value.Trim ('\'');
137                         
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);
143                         }
144                 
145                         TimeSpan timespan;
146                         try {
147                                 timespan = TimeSpan.Parse (dateStr);
148                         } catch (Exception e) {
149                                 errorMsg = "ERROR : " + e.Message;
150                                 errorMsg += "\nSTACKTRACE : " + e.StackTrace;
151                                 return null;
152                         }
153                         
154                         return timespan;
155                 }
156         }
157 }