ConnectedDataProvider.cs 15 KB

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