MsSqlSpecificTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // MsSqlSpecificTest.cs :- A class derived from 'BaseRetrieve' class
  3. // - Contains code specific to ms sql database
  4. // (Retrieves data from the database as sql-specific types)
  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 System.Data.SqlClient;
  34. using System.Data.SqlTypes;
  35. using System.Text.RegularExpressions;
  36. namespace MonoTests.System.Data {
  37. public class SqlRetrieve : BaseRetrieve {
  38. public SqlRetrieve (string dbConfigFile) : base (dbConfigFile)
  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 (Exception e) {
  48. Console.WriteLine ("Error reading the config file");
  49. Console.WriteLine (e.Message);
  50. con = null;
  51. return;
  52. }
  53. con = new SqlConnection (connectionString);
  54. try {
  55. con.Open ();
  56. } catch (SqlException 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 open connection!! 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. SqlDataReader rdr = (SqlDataReader) reader;
  72. if (rdr.IsDBNull (columnIndex))
  73. return null;
  74. if (rdr.GetDataTypeName (columnIndex) == "money") {
  75. value = rdr.GetSqlMoney (columnIndex);
  76. return value;
  77. }
  78. Type type = rdr.GetFieldType (columnIndex);
  79. switch (type.Name.ToLower ()) {
  80. case "byte" : value = rdr.GetSqlByte (columnIndex);
  81. break;
  82. case "sbyte" : value = rdr.GetSqlInt16 (columnIndex);
  83. break;
  84. case "boolean" : value = rdr.GetSqlBoolean (columnIndex);
  85. break;
  86. case "int16" : value = rdr.GetSqlInt16 (columnIndex);
  87. break;
  88. case "uint16" :
  89. case "int32" : value = rdr.GetSqlInt32 (columnIndex);
  90. break;
  91. case "uint32" :
  92. case "int64" : value = rdr.GetSqlInt64 (columnIndex);
  93. break;
  94. case "single" : value = rdr.GetSqlSingle (columnIndex);
  95. break;
  96. case "double" : value = rdr.GetSqlDouble (columnIndex);
  97. break;
  98. case "uint64" :
  99. case "decimal" : value = rdr.GetSqlDecimal (columnIndex);
  100. break;
  101. case "datetime": value = rdr.GetSqlDateTime (columnIndex);
  102. break;
  103. case "string": value = rdr.GetSqlString (columnIndex);
  104. break;
  105. default : value = rdr.GetValue (columnIndex);
  106. break;
  107. }
  108. return value;
  109. }
  110. public override object ConvertToByte (Type type, string value, ref string errorMsg)
  111. {
  112. byte byteval;
  113. try {
  114. byteval = Convert.ToByte (value);
  115. } catch (Exception e) {
  116. errorMsg = "ERROR : " + e.Message;
  117. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  118. return null;
  119. }
  120. return new SqlByte (byteval);
  121. }
  122. public override object ConvertToBoolean (Type type, string value, ref string errorMsg)
  123. {
  124. bool boolval;
  125. try {
  126. boolval = Convert.ToBoolean (Convert.ToInt16 (value));
  127. } catch (Exception e) {
  128. errorMsg = "ERROR : " + e.Message;
  129. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  130. return null;
  131. }
  132. return new SqlBoolean (boolval);
  133. }
  134. public override object ConvertToInt16 (Type type, string value, ref string errorMsg)
  135. {
  136. short shortval;
  137. try {
  138. shortval = Convert.ToInt16 (value);
  139. } catch (Exception e) {
  140. errorMsg = "ERROR : " + e.Message;
  141. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  142. return null;
  143. }
  144. return new SqlInt16 (shortval);
  145. }
  146. public override object ConvertToInt32 (Type type, string value, ref string errorMsg)
  147. {
  148. int intval;
  149. try {
  150. intval = Convert.ToInt32 (value);
  151. } catch (Exception e) {
  152. errorMsg = "ERROR : " + e.Message;
  153. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  154. return null;
  155. }
  156. return new SqlInt32 (intval);
  157. }
  158. public override object ConvertToInt64 (Type type, string value, ref string errorMsg)
  159. {
  160. long longval;
  161. try {
  162. longval = Convert.ToInt64 (value);
  163. } catch (Exception e) {
  164. errorMsg = "ERROR : " + e.Message;
  165. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  166. return null;
  167. }
  168. return new SqlInt64 (longval);
  169. }
  170. public override object ConvertToSingle (Type type, string value, ref string errorMsg)
  171. {
  172. float floatval;
  173. try {
  174. floatval = Convert.ToSingle (value);
  175. } catch (Exception e) {
  176. errorMsg = "ERROR : " + e.Message;
  177. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  178. return null;
  179. }
  180. return new SqlSingle (floatval);
  181. }
  182. public override object ConvertToDouble (Type type, string value, ref string errorMsg)
  183. {
  184. double doubleval;
  185. try {
  186. doubleval = Convert.ToDouble (value);
  187. } catch (Exception e) {
  188. errorMsg = "ERROR : " + e.Message;
  189. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  190. return null;
  191. }
  192. return new SqlDouble (doubleval);
  193. }
  194. public object ConvertToMoney (Type type, string value, ref string errorMsg)
  195. {
  196. decimal decimalval;
  197. try {
  198. decimalval = Convert.ToDecimal (value);
  199. } catch (FormatException e) {
  200. errorMsg = "ERROR : " + e.Message;
  201. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  202. return null;
  203. }
  204. return new SqlMoney (decimalval);
  205. }
  206. public override object ConvertToDecimal (Type type, string value, ref string errorMsg)
  207. {
  208. decimal decimalval;
  209. try {
  210. decimalval = Convert.ToDecimal (value);
  211. } catch (FormatException e) {
  212. // This may be bcoz value is of the form 'd.ddEdd'
  213. Double doubleVal = Convert.ToDouble (value);
  214. decimalval = Convert.ToDecimal (doubleVal);
  215. }
  216. return new SqlDecimal (decimalval);
  217. }
  218. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  219. {
  220. Regex re = new Regex ("\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\s+(?<hour>\\d{1,2}):(?<min>\\d{1,2})(:(?<sec>\\d{1,2})(\\.(?<msec>\\d{1,3}))*)*");
  221. value = value.Trim ('\'');
  222. Match m = re.Match (value);
  223. int month, day, year, hour, min, sec, msec;
  224. month = day = year = hour = min = sec = msec = 0;
  225. month = Convert.ToInt32 (m.Result ("${month}"));
  226. day = Convert.ToInt32 (m.Result ("${day}"));
  227. year = Convert.ToInt32 (m.Result ("${year}"));
  228. string str = m.Result ("${hour}");
  229. if (!str.Equals (""))
  230. hour = Convert.ToInt32 (str);
  231. str = m.Result ("${min}");
  232. if (!str.Equals (""))
  233. min = Convert.ToInt32 (str);
  234. str = m.Result ("${sec}");
  235. if (!str.Equals (""))
  236. sec = Convert.ToInt32 (str);
  237. str = m.Result ("${msec}");
  238. if (!str.Equals (""))
  239. msec = Convert.ToInt32 (str);
  240. SqlDateTime dateObj;
  241. try {
  242. if (hour == 0 && min == 0 && sec == 0)
  243. dateObj = new SqlDateTime (year, month, day);
  244. else {
  245. if (msec != 0) {
  246. dateObj = new SqlDateTime (year, month, day, hour, min, sec, msec);
  247. } else {
  248. dateObj = new SqlDateTime (year, month, day, hour, min, sec);
  249. }
  250. }
  251. } catch (Exception e) {
  252. errorMsg = "Invalid date time\n";
  253. errorMsg += "ERROR : " + e.Message;
  254. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  255. return null;
  256. }
  257. return dateObj;
  258. }
  259. public override Boolean AreEqual (object obj, string value, ref string errorMsg)
  260. {
  261. if ((obj == null ) || (value.Equals ("null"))) {
  262. if (obj == null && value.Equals ("null"))
  263. return true;
  264. return false;
  265. }
  266. object valObj = ConvertToValueType (obj.GetType (), value, ref errorMsg);
  267. return obj.Equals (valObj);
  268. }
  269. public override object ConvertToValueType (Type objType, string value, ref string errorMsg)
  270. {
  271. value = value.Trim ('\'');
  272. value = value.Trim ('\"');
  273. switch (objType.ToString ()) {
  274. case "System.Data.SqlTypes.SqlInt16" :
  275. return ConvertToInt16 (objType, value, ref errorMsg);
  276. case "System.Data.SqlTypes.SqlInt32" :
  277. return ConvertToInt32 (objType, value, ref errorMsg);
  278. case "System.Data.SqlTypes.SqlInt64" :
  279. return ConvertToInt64 (objType, value, ref errorMsg);
  280. case "System.Data.SqlTypes.SqlString" :
  281. return new SqlString (value);
  282. case "System.Data.SqlTypes.SqlBoolean" :
  283. return ConvertToBoolean (objType, value, ref errorMsg);
  284. case "System.Data.SqlTypes.SqlByte" :
  285. return ConvertToByte (objType, value, ref errorMsg);
  286. case "System.Data.SqlTypes.SqlDateTime" :
  287. return ConvertToDateTime (objType, value, ref errorMsg);
  288. case "System.Data.SqlTypes.SqlDecimal" :
  289. return ConvertToDecimal (objType, value, ref errorMsg);
  290. case "System.Data.SqlTypes.SqlDouble" :
  291. return ConvertToDouble (objType, value, ref errorMsg);
  292. case "System.Data.SqlTypes.SqlSingle" :
  293. return ConvertToSingle (objType, value, ref errorMsg);
  294. case "System.Data.SqlTypes.SqlMoney" :
  295. return ConvertToMoney (objType, value, ref errorMsg);
  296. }
  297. if (objType.ToString () == "System.TimeSpan")
  298. return ConvertToTimespan (objType, value, ref errorMsg);
  299. return ConvertValue (objType, value, ref errorMsg);
  300. }
  301. }
  302. }