PostgresDbAdapterTest.cs 5.3 KB

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