OracleDbAdapterTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // OracleDbAdapterTest.cs :- Defines a class 'OraAdapter' derived from the
  3. // 'BaseAdapter' class.
  4. // - Contains code specific to oracle 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.Xml.XPath;
  33. using System.Data;
  34. using System.Data.OracleClient;
  35. using System.Text.RegularExpressions;
  36. namespace MonoTests.System.Data {
  37. public class OraAdapter : BaseAdapter {
  38. public OraAdapter (string database) : base (database)
  39. {
  40. dataAdapter = new OracleDataAdapter ();
  41. }
  42. // returns a Open connection
  43. public override void GetConnection ()
  44. {
  45. string connectionString = null;
  46. try {
  47. connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
  48. } catch (XPathException e) {
  49. Console.WriteLine ("Error reading the config file !!");
  50. Console.WriteLine (e.Message);
  51. con = null;
  52. return;
  53. }
  54. con = new OracleConnection (connectionString);
  55. try {
  56. con.Open ();
  57. } catch (OracleException e) {
  58. Console.WriteLine ("Cannot establish connection with the database");
  59. Console.WriteLine ("Probably the database is down");
  60. con = null;
  61. } catch (InvalidOperationException e) {
  62. Console.WriteLine ("Cannot open connection");
  63. Console.WriteLine("Probably the connection is already open");
  64. con = null;
  65. } catch (Exception e) {
  66. Console.WriteLine ("Cannot open connection ");
  67. con = null;
  68. }
  69. }
  70. public override object GetValue (IDataReader reader, int columnIndex)
  71. {
  72. object value = null;
  73. if (reader.IsDBNull (columnIndex)) {
  74. return null;
  75. }
  76. OracleDataReader rdr = (OracleDataReader) reader;
  77. Type type = rdr.GetFieldType (columnIndex);
  78. string datatypeName = rdr.GetDataTypeName (columnIndex);
  79. if (datatypeName.ToLower ().Equals ("interval year to month")) {
  80. value = rdr.GetOracleMonthSpan (columnIndex);
  81. return value;
  82. }
  83. switch (type.Name.ToLower ()) {
  84. case "int32":
  85. value = rdr.GetInt32 (columnIndex);
  86. break;
  87. case "decimal" :
  88. try {
  89. value = rdr.GetDecimal (columnIndex);
  90. } catch (Exception e) {
  91. value = rdr.GetOracleNumber (columnIndex);
  92. }
  93. break;
  94. case "datetime": value = rdr.GetDateTime (columnIndex);
  95. break;
  96. case "string": value = rdr.GetString (columnIndex);
  97. break;
  98. case "timespan" : value = rdr.GetTimeSpan (columnIndex);
  99. break;
  100. default : value = rdr.GetValue (columnIndex);
  101. break;
  102. }
  103. return value;
  104. }
  105. public override Boolean AreEqual (object obj, string value, ref string errorMsg)
  106. {
  107. if ((obj == null ) || (value.Equals ("null"))) {
  108. if (obj == null && value.Equals ("null"))
  109. return true;
  110. return false;
  111. }
  112. if (obj.Equals (DBNull.Value) || (value.Equals ("null"))) {
  113. if (obj.Equals (DBNull.Value) && value.Equals ("null"))
  114. return true;
  115. return false;
  116. }
  117. Type objType = obj.GetType ();
  118. object valObj = null;
  119. value = value.Trim ('\'');
  120. value = value.Trim ('\"');
  121. switch (objType.ToString ()) {
  122. case "System.Data.OracleClient.OracleNumber" :
  123. return CompareOracleNumber (obj, value, ref errorMsg);
  124. case "System.Data.OracleClient.OracleMonthSpan" :
  125. valObj = ConvertToOracleMonthSpan (objType, value, ref errorMsg);
  126. return valObj.Equals (obj);
  127. case "System.Decimal" :
  128. valObj = ConvertToDecimal (objType, value, ref errorMsg);
  129. return valObj.Equals (obj);
  130. case "System.String" :
  131. return value.Equals (obj);
  132. case "System.TimeSpan" :
  133. valObj = ConvertToTimeSpan (objType, value, ref errorMsg);
  134. return valObj.Equals (obj);
  135. case "System.DateTime" :
  136. valObj = ConvertToDateTime (objType, value, ref errorMsg);
  137. return valObj.Equals (obj);
  138. case "System.Int32" :
  139. valObj = ConvertToInt32 (objType, value, ref errorMsg);
  140. return valObj.Equals (obj);
  141. }
  142. valObj = ConvertValue (objType, value, ref errorMsg);
  143. if (valObj == null) {
  144. Console.WriteLine ("Unable to convert values!!!");
  145. Console.WriteLine (errorMsg);
  146. return false;
  147. }
  148. return valObj.Equals (obj);
  149. }
  150. public override object ConvertToDecimal (Type type, string value, ref string errorMsg)
  151. {
  152. decimal decimalval;
  153. try {
  154. decimalval = Convert.ToDecimal (value);
  155. } catch (FormatException e) {
  156. // This may be bcoz value is of the form 'd.ddEdd'
  157. Double doubleVal = Convert.ToDouble (value);
  158. decimalval = Convert.ToDecimal (doubleVal);
  159. } catch (Exception e) {
  160. errorMsg = "ERROR : " + e.Message;
  161. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  162. return null;
  163. }
  164. return decimalval;
  165. }
  166. public override object ConvertToInt32 (Type type, string value, ref string errorMsg)
  167. {
  168. int intval;
  169. try {
  170. intval = Convert.ToInt32 (value);
  171. } catch (FormatException e) {
  172. // This might be interval year to month
  173. bool isNegative = false;
  174. if (value.StartsWith ("-")) {
  175. isNegative = true;
  176. value = value.Trim ('-');
  177. }
  178. string [] intParts = value.Split ('-');
  179. intval = Convert.ToInt32 (intParts [0]) * 12 + Convert.ToInt32 (intParts [1]);
  180. if (isNegative)
  181. intval *= -1;
  182. } catch (Exception e) {
  183. errorMsg = "ERROR : " + e.Message;
  184. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  185. return null;
  186. }
  187. return intval;
  188. }
  189. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  190. {
  191. value = value.ToLower ().Trim ();
  192. string format = null, date = null;
  193. if (value.StartsWith ("to_date")) {
  194. value = value.Remove (0, 8);
  195. value = value.Remove (value.Length - 1, 1);
  196. string[] dateParts = value.Split(',');
  197. date = dateParts [0].Trim ();
  198. format = dateParts [1].Trim ();
  199. date = date.Trim ('\'');
  200. format = format.Trim ('\'');
  201. // Assuming that date will be in yyyy-mm-dd hh24:mi:ss only
  202. format = format.Replace ("yyyy", "(?<yyyy>\\d{4})");
  203. format = format.Replace ("mm", "(?<mm>\\d{2})");
  204. format = format.Replace ("dd", "(?<dd>\\d{2})");
  205. format = format.Replace ("hh24", "(?<hh24>\\d{2})");
  206. format = format.Replace ("mi", "(?<mi>\\d{2})");
  207. format = format.Replace ("ss", "(?<ss>\\d{2})");
  208. } else if (value.ToLower ().StartsWith ("timestamp")) {
  209. value = value.Remove (0, 9);
  210. value = value.Trim ();
  211. date = value.Trim ('\'');
  212. format = "\\b(?<yyyy>\\d{4})\\-(?<mm>\\d{2})\\-(?<dd>\\d{2}) (?<hh24>\\d{2}):(?<mi>\\d{2}):(?<ss>\\d{2})";
  213. }
  214. Regex re = new Regex (format);
  215. Match m = re.Match (date);
  216. if (!m.Success)
  217. return false;
  218. int year = Convert.ToInt32 (m.Result ("${yyyy}"));
  219. int month = Convert.ToInt32 (m.Result ("${mm}"));
  220. int day = Convert.ToInt32 (m.Result ("${dd}"));
  221. int hour = Convert.ToInt32 (m.Result ("${hh24}"));
  222. int min = Convert.ToInt32 (m.Result ("${mi}"));
  223. int sec = Convert.ToInt32 (m.Result ("${ss}"));
  224. DateTime newDt ;
  225. try {
  226. newDt = new DateTime (year, month, day, hour, min, sec);
  227. } catch (Exception e) {
  228. errorMsg = "ERROR : " + e.Message;
  229. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  230. return false;
  231. }
  232. return newDt;
  233. }
  234. Boolean CompareOracleNumber (object obj, string value, ref string errorMsg)
  235. {
  236. return value.Equals (obj.ToString ());
  237. }
  238. object ConvertToOracleMonthSpan (Type type, string value, ref string errorMsg)
  239. {
  240. int intervalInMonths = 0;
  241. bool isNegative = false;
  242. if (value.StartsWith ("-")) {
  243. isNegative = true;
  244. value = value.Trim ('-');
  245. }
  246. string [] intParts = value.Split ('-');
  247. if (intParts.Length > 0) {
  248. intervalInMonths = Convert.ToInt32 (intParts [0]) * 12;
  249. if (intParts.Length > 1)
  250. intervalInMonths += Convert.ToInt32 (intParts [1]);
  251. } else {// Should not come here
  252. return false;
  253. }
  254. if (isNegative)
  255. intervalInMonths *= -1;
  256. return new OracleMonthSpan (intervalInMonths);
  257. }
  258. public override object ConvertToTimeSpan (Type type, string value, ref string errorMsg)
  259. {
  260. // Input in the form '[-]dd hh:mi:ss'
  261. value = value.Replace (" ", ".");
  262. TimeSpan ts = TimeSpan.Parse (value);
  263. return ts;
  264. }
  265. public override void PopulateDataSetFromTable (string queryStr, string tableName)
  266. {
  267. cmd.CommandText = queryStr;
  268. OracleDataAdapter da = (OracleDataAdapter) dataAdapter;
  269. da.SelectCommand = (OracleCommand) cmd;
  270. dataset = new DataSet ();
  271. da.Fill (dataset, tableName);
  272. }
  273. public override bool ReconcileChanges (string tableName, ref string errorMsg)
  274. {
  275. try {
  276. new OracleCommandBuilder ((OracleDataAdapter)dataAdapter);
  277. dataAdapter.Update (dataset, tableName);
  278. } catch (Exception e) {
  279. errorMsg = "ERROR : " + e.Message;
  280. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  281. return false;
  282. }
  283. return true;
  284. }
  285. }
  286. }