2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / Test / DataProviderTests / dataadaptertests / PostgresDbAdapterTest.cs
1 //
2 // PostgresDbAdapterTest.cs :- Defines a class 'PostgresAdapter' derived from the 
3 //                             'BaseAdapter' class
4 //                               - Contains code specific to postgres database
5 //
6 // Author:
7 //   Satya Sudha K (ksathyasudha@novell.com)
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
32 using System;
33 using System.Data;
34 using Npgsql;
35 using System.Text.RegularExpressions;
36
37 namespace MonoTests.System.Data {
38         
39         public class PostgresAdapter : BaseAdapter {
40                 
41                 public PostgresAdapter (string database) : base (database) 
42                 {
43                         dataAdapter = new NpgsqlDataAdapter ();
44                 }
45                 
46                 // returns a Open connection 
47                 public override void GetConnection () 
48                 {
49                         string connectionString = null;
50                         try {
51                                 connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
52                         } catch (Exception e) {
53                                 Console.WriteLine ("Error reading the config file");
54                                 Console.WriteLine (e.Message);
55                                 con = null;
56                                 return;
57                         }
58
59                         con = new NpgsqlConnection (connectionString);
60
61                         try {
62                                 con.Open ();
63                         } catch (NpgsqlException e) {
64                                 Console.WriteLine ("Cannot establish connection with the database");
65                                 Console.WriteLine ("Probably the Database is down!!");
66                                 con = null;
67                         } catch (InvalidOperationException e) {
68                                 Console.WriteLine ("Cannot establish connection with the database");
69                                 Console.WriteLine ("Probably connection is already open");
70                                 con = null;
71                         } catch (Exception e) {
72                                 Console.WriteLine ("Cannot open connection ");
73                                 con = null;
74                         }
75                 }
76                 
77                 public override object ConvertToDateTime (Type type, string value, ref string errorMsg) 
78                 {
79                 
80                         int month, day, year, hour, min, sec;
81                         month = day = year = hour = min = sec = 0;
82                         double msec = 0;
83                         msec = 0;
84                         
85                         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}))*)*");
86                         
87                         value = value.Trim ('\'');
88                         value = value.Trim ('\"');
89                         Match m = re.Match (value);
90                         string matchedVal = null;
91
92                         if (m.Success) {
93                                 matchedVal = m.Result ("${year}");
94                                 if (!matchedVal.Equals (""))
95                                         year = Convert.ToInt32 (matchedVal);
96                         
97                                 matchedVal = m.Result ("${month}");
98                                 if (!matchedVal.Equals (""))
99                                         month = Convert.ToInt32 (matchedVal);
100                         
101                                 matchedVal = m.Result ("${day}");
102                                 if (!matchedVal.Equals (""))
103                                         day = Convert.ToInt32 (matchedVal);
104                         } else {
105                                 // Only timespan
106                                 re = new Regex ("\\b(?<hour>\\d{1,2}):(?<minute>\\d{1,2}):(?<sec>\\d{1,2})(\\.(?<msec>\\d{1,6}))*");
107                                 m = re.Match (value);
108                         }
109                         
110                         try {
111                 
112                                 matchedVal = m.Result ("${hour}");
113                                 if (!matchedVal.Equals (""))
114                                         hour = Convert.ToInt32 (matchedVal);
115                         
116                                 matchedVal = m.Result ("${minute}");
117                                 if (!matchedVal.Equals (""))
118                                         min = Convert.ToInt32 (matchedVal);
119                                 
120                                 matchedVal = m.Result ("${sec}");
121                                 if (!matchedVal.Equals (""))
122                                         sec = Convert.ToInt32 (matchedVal);
123                 
124                                 matchedVal = m.Result ("${msec}");
125                                 if (!matchedVal.Equals ("")) {
126                                 //matchedVal = matchedVal.PadRight(6, '0'); 
127                                         msec = Convert.ToDouble (matchedVal);
128                                         if (msec > 1000) 
129                                                 msec = msec / 1000;
130                                 }
131                                 
132                                 if (day == 0 && month == 0 && year == 0) {
133                                         // Time span only
134                                         TimeSpan ts =TimeSpan.Parse(value);
135                                         return ts;
136                                 
137                                 } else {
138                                         DateTime dateObj = new DateTime (year, month, day, hour, min, sec);
139                                         dateObj = dateObj.AddMilliseconds (msec);
140                                         return dateObj;
141                                 }                       
142                         } catch (Exception e) {
143                                 errorMsg = "ERROR : " + e.Message;
144                                 errorMsg += "\nSTACKTRACE : " + e.StackTrace;
145                                 return null;
146                         }
147                 }
148                 
149                 public override void PopulateDataSetFromTable (string queryStr, string tableName) 
150                 {
151                         cmd.CommandText = queryStr;
152                         NpgsqlDataAdapter da = (NpgsqlDataAdapter) dataAdapter;
153                         da.SelectCommand = (NpgsqlCommand) cmd;
154                         dataset = new DataSet ();
155                         da.Fill (dataset, tableName);
156                 }
157                 
158                 public override bool ReconcileChanges (string tableName, ref string errorMsg) 
159                 {
160                         try {
161                                 new NpgsqlCommandBuilder ((NpgsqlDataAdapter) dataAdapter);
162                                 dataAdapter.Update (dataset, tableName);
163                         } catch (Exception e) {
164                                 errorMsg = "ERROR : " + e.Message;
165                                 errorMsg += "\nSTACKTRACE : " + e.StackTrace;
166                                 return false;
167                         }
168
169                         return true;
170                 }
171         }
172 }