ConnectedDataProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Data;
  30. using System.Data.OracleClient ;
  31. using System.IO;
  32. using System.Collections;
  33. using Sys = System;
  34. using MonoTests.System.Data.Utils.Data;
  35. // Provide All Data required by the diffderent tests e.g.DataTable, DataRow ...
  36. namespace MonoTests.System.Data.Utils {
  37. /// <summary>
  38. /// Types of Database Servers that tests can be run on.
  39. /// </summary>
  40. public enum DataBaseServer {
  41. SQLServer,
  42. Oracle,
  43. DB2,
  44. Sybase,
  45. PostgreSQL,
  46. Unknown
  47. }
  48. public class ConnectedDataProvider {
  49. #region Private
  50. //A string containing all printable charachters.
  51. private const string SAMPLE_STRING = "abcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()_+-=[]\\|;:,./<>? ";
  52. #endregion
  53. #region Public
  54. /// <summary>
  55. /// Name of the table in the database, that contain columns of simple types.
  56. /// </summary>
  57. public const string SIMPLE_TYPES_TABLE_NAME = "TYPES_SIMPLE";
  58. /// <summary>
  59. /// Name of the table in the database, that contain columns of extended types.
  60. /// </summary>
  61. public const string EXTENDED_TYPES_TABLE_NAME = "TYPES_EXTENDED";
  62. /// <summary>
  63. /// Name of the table in the database, that contain columns of DB specific types.
  64. /// </summary>
  65. public const string SPECIFIC_TYPES_TABLE_NAME = "TYPES_SPECIFIC";
  66. #endregion
  67. public static string ConnectionString {
  68. get {
  69. string connection_string = Sys.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  70. if(connection_string == null)
  71. NUnit.Framework.Assert.Ignore ("Please consult README.tests.");
  72. return connection_string;
  73. }
  74. }
  75. // SQLClient does not allow to use the Provider token
  76. // since Provider is always the first parameter(in GHT framework),
  77. // we trim it.
  78. public static string ConnectionStringSQLClient {
  79. get {
  80. return ConnectionString.Substring(ConnectionString.IndexOf(";"));
  81. }
  82. }
  83. /// <summary>
  84. /// Resolves the type of DB server specified by the "ADOConString.txt" file.
  85. /// </summary>
  86. /// <returns>The type of DB server specified by the "ADOConString.txt" file.</returns>
  87. public static DataBaseServer GetDbType() {
  88. return ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString);
  89. }
  90. /// <summary>
  91. /// Resolves the type of DB server that the specified connection refers.
  92. /// </summary>
  93. /// <param name="OleCon">A valid connection object to a DataBase.</param>
  94. /// <returns>The type of DB server that the specified connection refers to.</returns>
  95. public static DataBaseServer GetDbType(Sys.Data.OracleClient.OracleConnection OleCon) {
  96. return ConnectedDataProvider.GetDbType(OleCon.ConnectionString);
  97. }
  98. /// <summary>
  99. /// Resolves the type of DB server that the specified connection string refers.
  100. /// </summary>
  101. /// <param name="ConnectionString">A valid connection string to a DataBase server.</param>
  102. /// <returns>The type of DB server that the specified connection string refers to.</returns>
  103. public static DataBaseServer GetDbType(string ConnectionString) {
  104. return DataBaseServer.Oracle;
  105. }
  106. /// <summary>
  107. /// Creates a DbTypeParametersCollection with default types and data for the TYPES_SIMPLE table.
  108. /// </summary>
  109. /// <returns>The initialized DbTypeParametersCollection</returns>
  110. public static DbTypeParametersCollection GetSimpleDbTypesParameters() {
  111. DbTypeParametersCollection row = new DbTypeParametersCollection(SIMPLE_TYPES_TABLE_NAME);
  112. switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString)) {
  113. #region SQLServer
  114. case MonoTests.System.Data.Utils.DataBaseServer.SQLServer:
  115. row.Add("bit", true, 1);
  116. row.Add("tinyint", (byte)25, 1);
  117. row.Add("smallint", (Int16)77, 2);
  118. row.Add("int", (Int32)2525, 4);
  119. row.Add("bigint", (Int64)25251414, 8);
  120. row.Add("decimal", 10M, 9); //(Decimal)10
  121. row.Add("numeric", 123123M, 9); //(Decimal)123123
  122. row.Add("float", 17.1414257, 8);
  123. row.Add("real", (float)0.71425, 4);
  124. row.Add("char", "abcdefghij", 10);
  125. row.Add("nchar", "klmnopqrst", 10);
  126. row.Add("varchar", "qwertasdfg", 50);
  127. row.Add("nvarchar", "qwertasdfg", 50);
  128. break;
  129. #endregion
  130. #region Sybase
  131. case MonoTests.System.Data.Utils.DataBaseServer.Sybase:
  132. //row.Add("BIT", true, 1);
  133. row.Add("TINYINT", (byte)25, 1);
  134. row.Add("SMALLINT", (Int16)77, 2);
  135. row.Add("INT", (Int32)2525, 4);
  136. //row.Add("BIGINT", (Int64)25251414, 8);
  137. row.Add("DECIMAL", 10M, 9); //(Decimal)10
  138. row.Add("NUMERIC", 123123M, 9); //(Decimal)123123
  139. row.Add("FLOAT", 17.1414257, 8);
  140. row.Add("REAL", (float)0.71425, 4);
  141. row.Add("CHAR", "abcdefghij", 10);
  142. row.Add("NCHAR", "klmnopqrst", 10);
  143. row.Add("VARCHAR", "qwertasdfg", 50);
  144. row.Add("NVARCHAR", "qwertasdfg", 50);
  145. break;
  146. #endregion
  147. #region ORACLE
  148. case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
  149. row.Add("NUMBER", 21M, 22); //(Decimal)21
  150. row.Add("LONG", SAMPLE_STRING, 2147483647); //Default data type in .NET is system.String.
  151. row.Add("FLOAT", 1.234, 22);
  152. row.Add("VARCHAR", "qwertasdfg", 10);
  153. row.Add("NVARCHAR", "qwertasdfg", 20);
  154. row.Add("CHAR", "abcdefghij", 10);
  155. row.Add("NCHAR", "abcdefghij", 10);
  156. break;
  157. #endregion
  158. #region DB2
  159. case MonoTests.System.Data.Utils.DataBaseServer.DB2:
  160. row.Add("SMALLINT", (Int16)2, 2);
  161. row.Add("INTEGER", 7777, 4);
  162. row.Add("BIGINT", (Int64)21767267, 8);
  163. row.Add("DECIMAL", 123M, 9); //(decimal)123
  164. row.Add("REAL", (float)0.7, 4);
  165. row.Add("DOUBLE", 1.7, 8);
  166. row.Add("CHARACTER", "abcdefghij", 10);
  167. row.Add("VARCHAR", "qwertasdfg", 10);
  168. row.Add("LONGVARCHAR", SAMPLE_STRING, 32000);
  169. break;
  170. #endregion
  171. #region PostgreSQL
  172. case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
  173. // PostgreSQL ODBC Type BOOL returns String with value "1"
  174. // so we don't run it on .NET
  175. // if (!GHTEnvironment.IsJavaRunTime())
  176. // {
  177. // row.Add("BOOL", "1", 1);
  178. // }
  179. // else
  180. // {
  181. row.Add("BOOL", (bool)true, 1);
  182. // }
  183. row.Add("INT2", (Int16)21, 2);
  184. row.Add("INT4", (Int32)30000, 4);
  185. row.Add("INT8", (Int64)30001, 8);
  186. row.Add("NUMERIC", (decimal)100000M, 10); //(decimal)100000
  187. row.Add("FLOAT4", (Single)7.23157, 4);
  188. row.Add("FLOAT8", (Double)7.123456, 8);
  189. row.Add("VARCHAR", "qwertasdfg", 10);
  190. row.Add("CHAR", "abcdefghij", 10);
  191. row.Add("NCHAR", "klmnopqrst", 10);
  192. break;
  193. #endregion
  194. }
  195. return row;
  196. }
  197. /// <summary>
  198. /// Creates a DbTypeParametersCollection with default types and data for the TYPES_EXTENDED table.
  199. /// </summary>
  200. /// <returns>The initialized DbTypeParametersCollection</returns>
  201. public static DbTypeParametersCollection GetExtendedDbTypesParameters() {
  202. DbTypeParametersCollection row = new DbTypeParametersCollection(EXTENDED_TYPES_TABLE_NAME);
  203. switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString)) {
  204. #region SQLServer
  205. case MonoTests.System.Data.Utils.DataBaseServer.SQLServer:
  206. row.Add("text", SAMPLE_STRING, 16);
  207. row.Add("ntext", SAMPLE_STRING, 16);
  208. row.Add("binary", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  209. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  210. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  211. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  212. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  213. row.Add("varbinary", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  214. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  215. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  216. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  217. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  218. row.Add("datetime", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
  219. row.Add("smalldatetime", new DateTime(2004, 8, 9, 20, 30, 00), 4);
  220. break;
  221. #endregion
  222. #region Sybase
  223. case MonoTests.System.Data.Utils.DataBaseServer.Sybase:
  224. row.Add("TEXT", SAMPLE_STRING, 16);
  225. //There is probably a bug in the jdbc driver , we've tried to insert this string using
  226. //sybase command tool and it gave the same result (3850)
  227. row.Add("NTEXT", SAMPLE_STRING.Trim() , 16);
  228. row.Add("BINARY", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  229. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  230. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  231. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  232. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  233. row.Add("VARBINARY", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  234. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  235. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  236. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  237. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  238. row.Add("DATETIME", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
  239. row.Add("SMALLDATETIME", new DateTime(2004, 8, 9, 20, 30, 00), 4);
  240. break;
  241. #endregion
  242. #region ORACLE
  243. case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
  244. row.Add("RAW", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 10);
  245. row.Add("LONGRAW", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  246. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  247. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  248. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  249. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  250. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  251. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  252. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  253. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  254. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  255. }, 100);
  256. row.Add("DATE", new DateTime(2004, 8, 9, 20, 30, 15), 7);
  257. // The .NET Framework provides support for Oracle LOBs in the OracleClient namespace, but not in the Oracle namespace.
  258. // Since Visual MainWin does not support the OracleClient namespace, a partial support for this important feature is provided in the Oracle namespace.
  259. // See ms-help://MS.VSCC.2003/VMW.GH.1033/ghdoc/vmwdoc_ADONET_data_access_limitations_51.htm
  260. #if TARGET_JVM
  261. row.Add("BLOB", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  262. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  263. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  264. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  265. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  266. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  267. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  268. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  269. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  270. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  271. }, 4000);
  272. row.Add("CLOB", SAMPLE_STRING
  273. + SAMPLE_STRING
  274. + SAMPLE_STRING
  275. + SAMPLE_STRING
  276. + SAMPLE_STRING
  277. + SAMPLE_STRING
  278. , 4000);
  279. row.Add("NCLOB", SAMPLE_STRING
  280. + SAMPLE_STRING
  281. + SAMPLE_STRING
  282. + SAMPLE_STRING
  283. + SAMPLE_STRING
  284. + SAMPLE_STRING
  285. , 4000);
  286. #endif
  287. break;
  288. #endregion
  289. #region DB2
  290. case MonoTests.System.Data.Utils.DataBaseServer.DB2:
  291. row.Add("DATE", new DateTime(2004, 8, 9, 20, 30, 15, 500).Date);
  292. row.Add("TIME", new TimeSpan(20, 30, 15));
  293. row.Add("TIMESTAMP", new DateTime(2004, 8, 9, 20, 30, 15, 500));
  294. row.Add("BLOB", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  295. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  296. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  297. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  298. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  299. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  300. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  301. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  302. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  303. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  304. });
  305. row.Add("CLOB", SAMPLE_STRING
  306. + SAMPLE_STRING
  307. + SAMPLE_STRING
  308. + SAMPLE_STRING
  309. + SAMPLE_STRING
  310. + SAMPLE_STRING
  311. );
  312. row.Add("DBCLOB", SAMPLE_STRING
  313. + SAMPLE_STRING
  314. + SAMPLE_STRING
  315. + SAMPLE_STRING
  316. + SAMPLE_STRING
  317. + SAMPLE_STRING
  318. );
  319. break;
  320. #endregion
  321. #region PostgreSQL
  322. case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
  323. row.Add("BYTEA", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  324. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  325. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  326. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  327. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  328. row.Add("DATE", new DateTime(2004, 8, 9));
  329. row.Add("TEXT", "abcdefg", 16);
  330. row.Add("TIME", new Sys.TimeSpan(02,02,02));
  331. row.Add("TIMESTAMP", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
  332. break;
  333. #endregion
  334. }
  335. return row;
  336. }
  337. }
  338. }