ConnectedDataProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. return Sys.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  70. }
  71. }
  72. // SQLClient does not allow to use the Provider token
  73. // since Provider is always the first parameter(in GHT framework),
  74. // we trim it.
  75. public static string ConnectionStringSQLClient {
  76. get {
  77. return ConnectionString.Substring(ConnectionString.IndexOf(";"));
  78. }
  79. }
  80. /// <summary>
  81. /// Resolves the type of DB server specified by the "ADOConString.txt" file.
  82. /// </summary>
  83. /// <returns>The type of DB server specified by the "ADOConString.txt" file.</returns>
  84. public static DataBaseServer GetDbType() {
  85. return ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString);
  86. }
  87. /// <summary>
  88. /// Resolves the type of DB server that the specified connection refers.
  89. /// </summary>
  90. /// <param name="OleCon">A valid connection object to a DataBase.</param>
  91. /// <returns>The type of DB server that the specified connection refers to.</returns>
  92. public static DataBaseServer GetDbType(Sys.Data.OracleClient.OracleConnection OleCon) {
  93. return ConnectedDataProvider.GetDbType(OleCon.ConnectionString);
  94. }
  95. /// <summary>
  96. /// Resolves the type of DB server that the specified connection string refers.
  97. /// </summary>
  98. /// <param name="ConnectionString">A valid connection string to a DataBase server.</param>
  99. /// <returns>The type of DB server that the specified connection string refers to.</returns>
  100. public static DataBaseServer GetDbType(string ConnectionString) {
  101. return DataBaseServer.Oracle;
  102. }
  103. /// <summary>
  104. /// Creates a DbTypeParametersCollection with default types and data for the TYPES_SIMPLE table.
  105. /// </summary>
  106. /// <returns>The initialized DbTypeParametersCollection</returns>
  107. public static DbTypeParametersCollection GetSimpleDbTypesParameters() {
  108. DbTypeParametersCollection row = new DbTypeParametersCollection(SIMPLE_TYPES_TABLE_NAME);
  109. switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString)) {
  110. #region SQLServer
  111. case MonoTests.System.Data.Utils.DataBaseServer.SQLServer:
  112. row.Add("bit", true, 1);
  113. row.Add("tinyint", (byte)25, 1);
  114. row.Add("smallint", (Int16)77, 2);
  115. row.Add("int", (Int32)2525, 4);
  116. row.Add("bigint", (Int64)25251414, 8);
  117. row.Add("decimal", 10M, 9); //(Decimal)10
  118. row.Add("numeric", 123123M, 9); //(Decimal)123123
  119. row.Add("float", 17.1414257, 8);
  120. row.Add("real", (float)0.71425, 4);
  121. row.Add("char", "abcdefghij", 10);
  122. row.Add("nchar", "klmnopqrst", 10);
  123. row.Add("varchar", "qwertasdfg", 50);
  124. row.Add("nvarchar", "qwertasdfg", 50);
  125. break;
  126. #endregion
  127. #region Sybase
  128. case MonoTests.System.Data.Utils.DataBaseServer.Sybase:
  129. //row.Add("BIT", true, 1);
  130. row.Add("TINYINT", (byte)25, 1);
  131. row.Add("SMALLINT", (Int16)77, 2);
  132. row.Add("INT", (Int32)2525, 4);
  133. //row.Add("BIGINT", (Int64)25251414, 8);
  134. row.Add("DECIMAL", 10M, 9); //(Decimal)10
  135. row.Add("NUMERIC", 123123M, 9); //(Decimal)123123
  136. row.Add("FLOAT", 17.1414257, 8);
  137. row.Add("REAL", (float)0.71425, 4);
  138. row.Add("CHAR", "abcdefghij", 10);
  139. row.Add("NCHAR", "klmnopqrst", 10);
  140. row.Add("VARCHAR", "qwertasdfg", 50);
  141. row.Add("NVARCHAR", "qwertasdfg", 50);
  142. break;
  143. #endregion
  144. #region ORACLE
  145. case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
  146. row.Add("NUMBER", 21M, 22); //(Decimal)21
  147. row.Add("LONG", SAMPLE_STRING, 2147483647); //Default data type in .NET is system.String.
  148. row.Add("FLOAT", 1.234, 22);
  149. row.Add("VARCHAR", "qwertasdfg", 10);
  150. row.Add("NVARCHAR", "qwertasdfg", 20);
  151. row.Add("CHAR", "abcdefghij", 10);
  152. row.Add("NCHAR", "abcdefghij", 10);
  153. break;
  154. #endregion
  155. #region DB2
  156. case MonoTests.System.Data.Utils.DataBaseServer.DB2:
  157. row.Add("SMALLINT", (Int16)2, 2);
  158. row.Add("INTEGER", 7777, 4);
  159. row.Add("BIGINT", (Int64)21767267, 8);
  160. row.Add("DECIMAL", 123M, 9); //(decimal)123
  161. row.Add("REAL", (float)0.7, 4);
  162. row.Add("DOUBLE", 1.7, 8);
  163. row.Add("CHARACTER", "abcdefghij", 10);
  164. row.Add("VARCHAR", "qwertasdfg", 10);
  165. row.Add("LONGVARCHAR", SAMPLE_STRING, 32000);
  166. break;
  167. #endregion
  168. #region PostgreSQL
  169. case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
  170. // PostgreSQL ODBC Type BOOL returns String with value "1"
  171. // so we don't run it on .NET
  172. // if (!GHTEnvironment.IsJavaRunTime())
  173. // {
  174. // row.Add("BOOL", "1", 1);
  175. // }
  176. // else
  177. // {
  178. row.Add("BOOL", (bool)true, 1);
  179. // }
  180. row.Add("INT2", (Int16)21, 2);
  181. row.Add("INT4", (Int32)30000, 4);
  182. row.Add("INT8", (Int64)30001, 8);
  183. row.Add("NUMERIC", (decimal)100000M, 10); //(decimal)100000
  184. row.Add("FLOAT4", (Single)7.23157, 4);
  185. row.Add("FLOAT8", (Double)7.123456, 8);
  186. row.Add("VARCHAR", "qwertasdfg", 10);
  187. row.Add("CHAR", "abcdefghij", 10);
  188. row.Add("NCHAR", "klmnopqrst", 10);
  189. break;
  190. #endregion
  191. }
  192. return row;
  193. }
  194. /// <summary>
  195. /// Creates a DbTypeParametersCollection with default types and data for the TYPES_EXTENDED table.
  196. /// </summary>
  197. /// <returns>The initialized DbTypeParametersCollection</returns>
  198. public static DbTypeParametersCollection GetExtendedDbTypesParameters() {
  199. DbTypeParametersCollection row = new DbTypeParametersCollection(EXTENDED_TYPES_TABLE_NAME);
  200. switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString)) {
  201. #region SQLServer
  202. case MonoTests.System.Data.Utils.DataBaseServer.SQLServer:
  203. row.Add("text", SAMPLE_STRING, 16);
  204. row.Add("ntext", SAMPLE_STRING, 16);
  205. row.Add("binary", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  206. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  207. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  208. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  209. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  210. row.Add("varbinary", new byte[] {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,
  213. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  214. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  215. row.Add("datetime", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
  216. row.Add("smalldatetime", new DateTime(2004, 8, 9, 20, 30, 00), 4);
  217. break;
  218. #endregion
  219. #region Sybase
  220. case MonoTests.System.Data.Utils.DataBaseServer.Sybase:
  221. row.Add("TEXT", SAMPLE_STRING, 16);
  222. //There is probably a bug in the jdbc driver , we've tried to insert this string using
  223. //sybase command tool and it gave the same result (3850)
  224. row.Add("NTEXT", SAMPLE_STRING.Trim() , 16);
  225. row.Add("BINARY", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  226. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  227. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  228. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  229. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  230. row.Add("VARBINARY", new byte[] {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,
  233. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  234. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  235. row.Add("DATETIME", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
  236. row.Add("SMALLDATETIME", new DateTime(2004, 8, 9, 20, 30, 00), 4);
  237. break;
  238. #endregion
  239. #region ORACLE
  240. case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
  241. row.Add("RAW", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 10);
  242. row.Add("LONGRAW", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  243. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  244. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  245. ,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. }, 100);
  253. row.Add("DATE", new DateTime(2004, 8, 9, 20, 30, 15), 7);
  254. // The .NET Framework provides support for Oracle LOBs in the OracleClient namespace, but not in the Oracle namespace.
  255. // Since Visual MainWin does not support the OracleClient namespace, a partial support for this important feature is provided in the Oracle namespace.
  256. // See ms-help://MS.VSCC.2003/VMW.GH.1033/ghdoc/vmwdoc_ADONET_data_access_limitations_51.htm
  257. #if TARGET_JVM
  258. row.Add("BLOB", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  259. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  260. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  261. ,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. }, 4000);
  269. row.Add("CLOB", SAMPLE_STRING
  270. + SAMPLE_STRING
  271. + SAMPLE_STRING
  272. + SAMPLE_STRING
  273. + SAMPLE_STRING
  274. + SAMPLE_STRING
  275. , 4000);
  276. row.Add("NCLOB", SAMPLE_STRING
  277. + SAMPLE_STRING
  278. + SAMPLE_STRING
  279. + SAMPLE_STRING
  280. + SAMPLE_STRING
  281. + SAMPLE_STRING
  282. , 4000);
  283. #endif
  284. break;
  285. #endregion
  286. #region DB2
  287. case MonoTests.System.Data.Utils.DataBaseServer.DB2:
  288. row.Add("DATE", new DateTime(2004, 8, 9, 20, 30, 15, 500).Date);
  289. row.Add("TIME", new TimeSpan(20, 30, 15));
  290. row.Add("TIMESTAMP", new DateTime(2004, 8, 9, 20, 30, 15, 500));
  291. row.Add("BLOB", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  292. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  293. ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
  294. ,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. });
  302. row.Add("CLOB", SAMPLE_STRING
  303. + SAMPLE_STRING
  304. + SAMPLE_STRING
  305. + SAMPLE_STRING
  306. + SAMPLE_STRING
  307. + SAMPLE_STRING
  308. );
  309. row.Add("DBCLOB", SAMPLE_STRING
  310. + SAMPLE_STRING
  311. + SAMPLE_STRING
  312. + SAMPLE_STRING
  313. + SAMPLE_STRING
  314. + SAMPLE_STRING
  315. );
  316. break;
  317. #endregion
  318. #region PostgreSQL
  319. case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
  320. row.Add("BYTEA", new byte[] {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  321. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  322. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  323. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
  324. 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
  325. row.Add("DATE", new DateTime(2004, 8, 9));
  326. row.Add("TEXT", "abcdefg", 16);
  327. row.Add("TIME", new Sys.TimeSpan(02,02,02));
  328. row.Add("TIMESTAMP", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
  329. break;
  330. #endregion
  331. }
  332. return row;
  333. }
  334. }
  335. }