OracleDataReaderTest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // OracleDataReaderTest.cs :- Defines a class OraRetrieve' derived from the
  3. // 'BaseRetrieve' 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 OraRetrieve : BaseRetrieve {
  38. public OraRetrieve (string database) : base (database)
  39. {
  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 (XPathException e) {
  48. Console.WriteLine ("Error reading the config file !!");
  49. Console.WriteLine (e.Message);
  50. return;
  51. }
  52. con = new OracleConnection (connectionString);
  53. try {
  54. con.Open ();
  55. } catch (OracleException e) {
  56. Console.WriteLine ("Cannot establish connection with the database");
  57. Console.WriteLine ("Probably the database is down");
  58. con = null;
  59. } catch (InvalidOperationException e) {
  60. Console.WriteLine ("Cannot open connection");
  61. Console.WriteLine ("Probably the connection is already open");
  62. con = null;
  63. } catch (Exception e) {
  64. Console.WriteLine ("Cannot open connection");
  65. con = null;
  66. }
  67. }
  68. public override object GetValue (IDataReader reader, int columnIndex)
  69. {
  70. object value = null;
  71. if (reader.IsDBNull (columnIndex))
  72. return null;
  73. OracleDataReader rdr = (OracleDataReader) reader;
  74. Type type = rdr.GetFieldType (columnIndex);
  75. string datatypeName = rdr.GetDataTypeName (columnIndex);
  76. if (datatypeName.ToLower ().Equals ("interval year to month")) {
  77. value = rdr.GetOracleMonthSpan (columnIndex);
  78. return value;
  79. }
  80. switch (type.Name.ToLower ()) {
  81. case "int32": value = rdr.GetInt32 (columnIndex);
  82. break;
  83. case "decimal" :
  84. try {
  85. value = rdr.GetDecimal (columnIndex);
  86. } catch (Exception e) {
  87. value = rdr.GetOracleNumber (columnIndex);
  88. }
  89. break;
  90. case "datetime": value = rdr.GetDateTime (columnIndex);
  91. break;
  92. case "string": value = rdr.GetString (columnIndex);
  93. break;
  94. case "timespan" : value = rdr.GetTimeSpan (columnIndex);
  95. break;
  96. default : value = rdr.GetValue (columnIndex);
  97. break;
  98. }
  99. return value;
  100. }
  101. public override Boolean AreEqual (object obj, string value, ref string errorMsg)
  102. {
  103. if ((obj == null ) || (value.Equals ("null"))) {
  104. if (obj == null && value.Equals ("null"))
  105. return true;
  106. return false;
  107. }
  108. object valObj = ConvertToValueType (obj.GetType (), value, ref errorMsg);
  109. return valObj.Equals (obj);
  110. }
  111. public override object ConvertToValueType (Type objType, string value, ref string errorMsg)
  112. {
  113. value = value.Trim ('\'');
  114. value = value.Trim ('\"');
  115. switch (objType.ToString ()) {
  116. case "System.Data.OracleClient.OracleNumber" :
  117. return ConvertToOracleNumber (objType, value, ref errorMsg);
  118. case "System.Data.OracleClient.OracleMonthSpan" :
  119. return ConvertToOracleMonthSpan (objType, value, ref errorMsg);
  120. case "System.Decimal" :
  121. return ConvertToDecimal (objType, value, ref errorMsg);
  122. case "System.String" :
  123. return value;
  124. case "System.TimeSpan" :
  125. return ConvertToTimespan (objType, value, ref errorMsg);
  126. case "System.DateTime" :
  127. return ConvertToDateTime (objType, value, ref errorMsg);
  128. }
  129. return ConvertValue (objType, value, ref errorMsg);
  130. }
  131. public override object ConvertToDecimal (Type type, string value, ref string errorMsg)
  132. {
  133. decimal decimalval;
  134. try {
  135. decimalval = Convert.ToDecimal (value);
  136. } catch (FormatException e) {
  137. // This may be bcoz value is of the form 'd.ddEdd'
  138. Double doubleVal = Convert.ToDouble (value);
  139. decimalval = Convert.ToDecimal (doubleVal);
  140. } catch (Exception e) {
  141. errorMsg = "ERROR : " + e.Message;
  142. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  143. return false;
  144. }
  145. return decimalval;
  146. }
  147. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  148. {
  149. value = value.ToLower ().Trim ();
  150. string format = null, date = null;
  151. if (value.StartsWith ("to_date")) {
  152. value = value.Remove (0, 8);
  153. value = value.Remove (value.Length - 1, 1);
  154. string [] dateParts = value.Split (',');
  155. date = dateParts [0].Trim ();
  156. format = dateParts [1].Trim ();
  157. date = date.Trim ('\'');
  158. format = format.Trim ('\'');
  159. // Assuming that date will be in yyyy-mm-dd hh24:mi:ss only
  160. format = format.Replace ("yyyy", "(?<yyyy>\\d{4})");
  161. format = format.Replace ("mm", "(?<mm>\\d{2})");
  162. format = format.Replace ("dd", "(?<dd>\\d{2})");
  163. format = format.Replace ("hh24", "(?<hh24>\\d{2})");
  164. format = format.Replace ("mi", "(?<mi>\\d{2})");
  165. format = format.Replace ("ss", "(?<ss>\\d{2})");
  166. } else if (value.ToLower().StartsWith ("timestamp")) {
  167. value = value.Remove (0, 9);
  168. value = value.Trim ();
  169. date = value.Trim ('\'');
  170. format = "\\b(?<yyyy>\\d{4})\\-(?<mm>\\d{2})\\-(?<dd>\\d{2}) (?<hh24>\\d{2}):(?<mi>\\d{2}):(?<ss>\\d{2})";
  171. }
  172. Regex re = new Regex (format);
  173. Match m = re.Match (date);
  174. if (!m.Success)
  175. return false;
  176. int year = Convert.ToInt32 (m.Result ("${yyyy}"));
  177. int month = Convert.ToInt32 (m.Result ("${mm}"));
  178. int day = Convert.ToInt32 (m.Result ("${dd}"));
  179. int hour = Convert.ToInt32 (m.Result ("${hh24}"));
  180. int min = Convert.ToInt32 (m.Result ("${mi}"));
  181. int sec = Convert.ToInt32 (m.Result ("${ss}"));
  182. DateTime newDt ;
  183. try {
  184. newDt = new DateTime (year, month, day, hour, min, sec);
  185. } catch (Exception e) {
  186. errorMsg = "ERROR : " + e.Message;
  187. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  188. return false;
  189. }
  190. return newDt;
  191. }
  192. object ConvertToOracleNumber (Type type, string value, ref string errorMsg)
  193. {
  194. return OracleNumber.Parse (value);
  195. }
  196. object ConvertToOracleMonthSpan (Type type, string value, ref string errorMsg)
  197. {
  198. int intervalInMonths = 0;
  199. bool isNegative = false;
  200. if (value.StartsWith ("-")) {
  201. isNegative = true;
  202. value = value.Trim ('-');
  203. }
  204. string [] intParts = value.Split ('-');
  205. if (intParts.Length > 0) {
  206. intervalInMonths = Convert.ToInt32 (intParts [0]) * 12;
  207. if (intParts.Length > 1)
  208. intervalInMonths += Convert.ToInt32 (intParts [1]);
  209. } else {// Should not come here
  210. return false;
  211. }
  212. if (isNegative) {
  213. intervalInMonths *= -1;
  214. }
  215. return new OracleMonthSpan (intervalInMonths);
  216. }
  217. public override object ConvertToTimespan (Type type, string value, ref string errorMsg)
  218. {
  219. // Input in the form '[-]dd hh:mi:ss'
  220. value = value.Replace (" ", ".");
  221. return TimeSpan.Parse (value);
  222. }
  223. public override IDataReader QueryUsingStoredProc (IDbCommand cmd,
  224. string storedProcName,
  225. string paramName)
  226. {
  227. IDataReader reader = null;
  228. OracleCommand command = (OracleCommand) cmd;
  229. command.CommandText = storedProcName;
  230. command.CommandType = CommandType.StoredProcedure;
  231. OracleParameter pOutput = command.Parameters.Add ("ref_cur", OracleType.Cursor);
  232. pOutput.Direction = ParameterDirection.Output;
  233. try {
  234. reader = cmd.ExecuteReader ();
  235. } catch (Exception e) {
  236. Console.WriteLine (e.Message);
  237. Console.WriteLine (e.StackTrace);
  238. reader = null;
  239. }
  240. command.Parameters.Remove (pOutput);
  241. return reader;
  242. }
  243. }
  244. }