2 // PostgresDbAdapterTest.cs :- Defines a class 'PostgresAdapter' derived from the
4 // - Contains code specific to postgres database
7 // Satya Sudha K (ksathyasudha@novell.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
35 using System.Text.RegularExpressions;
37 namespace MonoTests.System.Data {
39 public class PostgresAdapter : BaseAdapter {
41 public PostgresAdapter (string database) : base (database)
43 dataAdapter = new NpgsqlDataAdapter ();
46 // returns a Open connection
47 public override void GetConnection ()
49 string connectionString = null;
51 connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
52 } catch (Exception e) {
53 Console.WriteLine ("Error reading the config file");
54 Console.WriteLine (e.Message);
59 con = new NpgsqlConnection (connectionString);
63 } catch (NpgsqlException e) {
64 Console.WriteLine ("Cannot establish connection with the database");
65 Console.WriteLine ("Probably the Database is down!!");
67 } catch (InvalidOperationException e) {
68 Console.WriteLine ("Cannot establish connection with the database");
69 Console.WriteLine ("Probably connection is already open");
71 } catch (Exception e) {
72 Console.WriteLine ("Cannot open connection ");
77 public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
80 int month, day, year, hour, min, sec;
81 month = day = year = hour = min = sec = 0;
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}))*)*");
87 value = value.Trim ('\'');
88 value = value.Trim ('\"');
89 Match m = re.Match (value);
90 string matchedVal = null;
93 matchedVal = m.Result ("${year}");
94 if (!matchedVal.Equals (""))
95 year = Convert.ToInt32 (matchedVal);
97 matchedVal = m.Result ("${month}");
98 if (!matchedVal.Equals (""))
99 month = Convert.ToInt32 (matchedVal);
101 matchedVal = m.Result ("${day}");
102 if (!matchedVal.Equals (""))
103 day = Convert.ToInt32 (matchedVal);
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);
112 matchedVal = m.Result ("${hour}");
113 if (!matchedVal.Equals (""))
114 hour = Convert.ToInt32 (matchedVal);
116 matchedVal = m.Result ("${minute}");
117 if (!matchedVal.Equals (""))
118 min = Convert.ToInt32 (matchedVal);
120 matchedVal = m.Result ("${sec}");
121 if (!matchedVal.Equals (""))
122 sec = Convert.ToInt32 (matchedVal);
124 matchedVal = m.Result ("${msec}");
125 if (!matchedVal.Equals ("")) {
126 //matchedVal = matchedVal.PadRight(6, '0');
127 msec = Convert.ToDouble (matchedVal);
132 if (day == 0 && month == 0 && year == 0) {
134 TimeSpan ts =TimeSpan.Parse(value);
138 DateTime dateObj = new DateTime (year, month, day, hour, min, sec);
139 dateObj = dateObj.AddMilliseconds (msec);
142 } catch (Exception e) {
143 errorMsg = "ERROR : " + e.Message;
144 errorMsg += "\nSTACKTRACE : " + e.StackTrace;
149 public override void PopulateDataSetFromTable (string queryStr, string tableName)
151 cmd.CommandText = queryStr;
152 NpgsqlDataAdapter da = (NpgsqlDataAdapter) dataAdapter;
153 da.SelectCommand = (NpgsqlCommand) cmd;
154 dataset = new DataSet ();
155 da.Fill (dataset, tableName);
158 public override bool ReconcileChanges (string tableName, ref string errorMsg)
161 new NpgsqlCommandBuilder ((NpgsqlDataAdapter) dataAdapter);
162 dataAdapter.Update (dataset, tableName);
163 } catch (Exception e) {
164 errorMsg = "ERROR : " + e.Message;
165 errorMsg += "\nSTACKTRACE : " + e.StackTrace;