1a054138f30632cc89da7fe87299282678d48f9f
[mono.git] / mcs / class / System.Data.OracleClient / Test / System.Data.OracleClient.jvm / MonoTests.System.Data.Utils / ConnectedDataProvider.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
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
29 using System;
30 using System.Data;
31 using System.Data.OracleClient ;
32 using System.IO;
33 using System.Collections;
34 using Sys = System;
35 using MonoTests.System.Data.Utils.Data;
36
37 // Provide All Data required by the diffderent tests e.g.DataTable, DataRow ...
38 namespace MonoTests.System.Data.Utils {
39         /// <summary>
40         /// Types of Database Servers that tests can be run on.
41         /// </summary>
42         public enum DataBaseServer {
43                 SQLServer,
44                 Oracle,
45                 DB2,
46                 Sybase,
47                 PostgreSQL,
48                 Unknown
49         }
50
51         public class ConnectedDataProvider {
52
53                 #region Private
54                 //A string containing all printable charachters.
55                 private const string SAMPLE_STRING = "abcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()_+-=[]\\|;:,./<>? ";
56                 #endregion
57
58                 #region Public
59                 /// <summary>
60                 /// Name of the table in the database, that contain columns of simple types.
61                 /// </summary>
62                 public const string SIMPLE_TYPES_TABLE_NAME = "TYPES_SIMPLE";
63                 /// <summary>
64                 /// Name of the table in the database, that contain columns of extended types.
65                 /// </summary>
66                 public const string EXTENDED_TYPES_TABLE_NAME = "TYPES_EXTENDED";
67                 /// <summary>
68                 /// Name of the table in the database, that contain columns of DB specific types.
69                 /// </summary>
70                 public const string SPECIFIC_TYPES_TABLE_NAME = "TYPES_SPECIFIC";
71                 #endregion
72
73                 public static string ConnectionString {
74                         get {
75                                 string connection_string = Sys.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
76                                 if(connection_string == null)
77                                         NUnit.Framework.Assert.Ignore ("Please consult README.tests.");
78                                 return connection_string;
79                         }
80                 }
81
82                 //      SQLClient does not allow to use the Provider token
83                 //      since Provider is always the first parameter(in GHT framework),
84                 //      we trim it.
85                 public static string ConnectionStringSQLClient {
86                         get {
87                                 return ConnectionString.Substring(ConnectionString.IndexOf(";"));
88                         }
89                 }
90         
91
92                 /// <summary>
93                 /// Resolves the type of DB server specified by the "ADOConString.txt" file.
94                 /// </summary>
95                 /// <returns>The type of DB server specified by the "ADOConString.txt" file.</returns>
96                 public static DataBaseServer GetDbType() {
97                         return ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString);
98                 }
99                 
100                 /// <summary>
101                 /// Resolves the type of DB server that the specified connection refers.
102                 /// </summary>
103                 /// <param name="OleCon">A valid connection object to a DataBase.</param>
104                 /// <returns>The type of DB server that the specified connection refers to.</returns>
105                 public static DataBaseServer GetDbType(Sys.Data.OracleClient.OracleConnection OleCon) {
106                         return ConnectedDataProvider.GetDbType(OleCon.ConnectionString);
107                 }
108
109                 /// <summary>
110                 /// Resolves the type of DB server that the specified connection string refers.
111                 /// </summary>
112                 /// <param name="ConnectionString">A valid connection string to a DataBase server.</param>
113                 /// <returns>The type of DB server that the specified connection string refers to.</returns>
114                 public static DataBaseServer GetDbType(string ConnectionString) {
115                         return DataBaseServer.Oracle;
116                 }
117
118                 /// <summary>
119                 /// Creates a DbTypeParametersCollection with default types and data for the TYPES_SIMPLE table.
120                 /// </summary>
121                 /// <returns>The initialized DbTypeParametersCollection</returns>
122                 public static DbTypeParametersCollection GetSimpleDbTypesParameters() {
123                         DbTypeParametersCollection row = new DbTypeParametersCollection(SIMPLE_TYPES_TABLE_NAME);
124                         switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString)) {
125                                         #region SQLServer
126                                 case MonoTests.System.Data.Utils.DataBaseServer.SQLServer:
127                                         row.Add("bit", true, 1);
128                                         row.Add("tinyint", (byte)25, 1);
129                                         row.Add("smallint", (Int16)77, 2);
130                                         row.Add("int", (Int32)2525, 4);
131                                         row.Add("bigint", (Int64)25251414, 8);
132                                         row.Add("decimal", 10M, 9);     //(Decimal)10
133                                         row.Add("numeric", 123123M, 9); //(Decimal)123123
134                                         row.Add("float", 17.1414257, 8);
135                                         row.Add("real", (float)0.71425, 4);
136                                         row.Add("char", "abcdefghij", 10);
137                                         row.Add("nchar", "klmnopqrst", 10);
138                                         row.Add("varchar", "qwertasdfg", 50);
139                                         row.Add("nvarchar", "qwertasdfg", 50);
140                                         break;
141                                         #endregion
142
143                                         #region Sybase
144                                 case MonoTests.System.Data.Utils.DataBaseServer.Sybase:
145                                         //row.Add("BIT", true, 1);
146                                         row.Add("TINYINT", (byte)25, 1);
147                                         row.Add("SMALLINT", (Int16)77, 2);
148                                         row.Add("INT", (Int32)2525, 4);
149                                         //row.Add("BIGINT", (Int64)25251414, 8);
150                                         row.Add("DECIMAL", 10M, 9);     //(Decimal)10
151                                         row.Add("NUMERIC", 123123M, 9); //(Decimal)123123
152                                         row.Add("FLOAT", 17.1414257, 8);
153                                         row.Add("REAL", (float)0.71425, 4);
154                                         row.Add("CHAR", "abcdefghij", 10);
155                                         row.Add("NCHAR", "klmnopqrst", 10);
156                                         row.Add("VARCHAR", "qwertasdfg", 50);
157                                         row.Add("NVARCHAR", "qwertasdfg", 50);
158                                         break;
159                                         #endregion
160
161                                         #region ORACLE
162                                 case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
163                                         row.Add("NUMBER", 21M, 22);     //(Decimal)21
164                                         row.Add("LONG", SAMPLE_STRING, 2147483647);     //Default data type in .NET is system.String.
165                                         row.Add("FLOAT", 1.234, 22);
166                                         row.Add("VARCHAR", "qwertasdfg", 10);
167                                         row.Add("NVARCHAR", "qwertasdfg", 20);
168                                         row.Add("CHAR", "abcdefghij", 10);
169                                         row.Add("NCHAR", "abcdefghij", 10);
170                                         break;
171                                         #endregion
172
173                                         #region DB2
174                                 case MonoTests.System.Data.Utils.DataBaseServer.DB2:
175                                         row.Add("SMALLINT", (Int16)2, 2);
176                                         row.Add("INTEGER", 7777, 4);
177                                         row.Add("BIGINT", (Int64)21767267, 8);
178                                         row.Add("DECIMAL", 123M, 9); //(decimal)123
179                                         row.Add("REAL", (float)0.7, 4);
180                                         row.Add("DOUBLE", 1.7, 8);
181                                         row.Add("CHARACTER", "abcdefghij", 10);
182                                         row.Add("VARCHAR", "qwertasdfg", 10);
183                                         row.Add("LONGVARCHAR", SAMPLE_STRING, 32000);
184                                         break;
185                                         #endregion
186
187                                         #region PostgreSQL
188                                 case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
189                                         
190                                         // PostgreSQL ODBC Type BOOL returns String with value "1" 
191                                         // so we don't run it on .NET
192                                         //                                      if (!GHTEnvironment.IsJavaRunTime())
193                                         //                                      {
194                                         //                                              row.Add("BOOL", "1", 1);
195                                         //                                      }
196                                         //                                      else
197                                         //                                      {
198                                         row.Add("BOOL", (bool)true, 1);
199                                         //                                      }
200
201                                         row.Add("INT2", (Int16)21, 2);
202                                         row.Add("INT4", (Int32)30000, 4);
203                                         row.Add("INT8", (Int64)30001, 8);
204                                         row.Add("NUMERIC", (decimal)100000M, 10); //(decimal)100000
205                                         row.Add("FLOAT4", (Single)7.23157, 4);
206                                         row.Add("FLOAT8", (Double)7.123456, 8);
207                                         row.Add("VARCHAR", "qwertasdfg", 10);
208                                         row.Add("CHAR", "abcdefghij", 10);
209                                         row.Add("NCHAR", "klmnopqrst", 10);
210                                         break;
211                                         #endregion
212                         }
213                         return row;
214                 }
215                 /// <summary>
216                 /// Creates a DbTypeParametersCollection with default types and data for the TYPES_EXTENDED table.
217                 /// </summary>
218                 /// <returns>The initialized DbTypeParametersCollection</returns>
219                 public static DbTypeParametersCollection GetExtendedDbTypesParameters() {
220                         DbTypeParametersCollection row = new DbTypeParametersCollection(EXTENDED_TYPES_TABLE_NAME);
221                         switch (ConnectedDataProvider.GetDbType(ConnectedDataProvider.ConnectionString)) {
222                                         #region SQLServer
223                                 case MonoTests.System.Data.Utils.DataBaseServer.SQLServer:
224                                         row.Add("text", SAMPLE_STRING, 16);
225                                         row.Add("ntext", SAMPLE_STRING, 16);
226                                         row.Add("binary", new byte[]    {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,
230                                                                                                                 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
231                                         row.Add("varbinary", new byte[] {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,
235                                                                                                                 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
236                                         row.Add("datetime", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
237                                         row.Add("smalldatetime", new DateTime(2004, 8, 9, 20, 30, 00), 4);
238                                         break;
239                                         #endregion
240
241                                         #region Sybase
242                                 case MonoTests.System.Data.Utils.DataBaseServer.Sybase:
243                                         row.Add("TEXT", SAMPLE_STRING, 16);
244                                         //There is probably a bug in the jdbc driver , we've tried to insert this string using
245                                         //sybase command tool and it gave the same result (3850)
246                                         row.Add("NTEXT", SAMPLE_STRING.Trim() , 16);
247                                         row.Add("BINARY", 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}, 50);
252                                         row.Add("VARBINARY", new byte[] {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}, 50);
257                                         row.Add("DATETIME", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
258                                         row.Add("SMALLDATETIME", new DateTime(2004, 8, 9, 20, 30, 00), 4);
259                                         break;
260                                         #endregion
261
262                                         #region ORACLE
263                                 case MonoTests.System.Data.Utils.DataBaseServer.Oracle:
264                                         row.Add("RAW", new byte[]       {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 10);
265                                         row.Add("LONGRAW", new byte[]   {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                                                                                                                 ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
274                                                                                                                 ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
275                                                                                                         }, 100);
276                                         row.Add("DATE", new DateTime(2004, 8, 9, 20, 30, 15), 7);
277                                         
278                                         // The .NET Framework provides support for Oracle LOBs in the OracleClient namespace, but not in the Oracle namespace.
279                                         // Since Visual MainWin does not support the OracleClient namespace, a partial support for this important feature is provided in the Oracle namespace.
280                                         // See ms-help://MS.VSCC.2003/VMW.GH.1033/ghdoc/vmwdoc_ADONET_data_access_limitations_51.htm
281 #if TARGET_JVM
282
283                                         row.Add("BLOB", new byte[]      {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
284                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
285                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
286                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
287                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
288                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
289                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
290                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
291                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
292                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
293                                                                                                 }, 4000);
294                                         row.Add("CLOB", SAMPLE_STRING
295                                                 + SAMPLE_STRING
296                                                 + SAMPLE_STRING
297                                                 + SAMPLE_STRING
298                                                 + SAMPLE_STRING
299                                                 + SAMPLE_STRING
300                                                 , 4000);
301                                         row.Add("NCLOB", SAMPLE_STRING
302                                                 + SAMPLE_STRING
303                                                 + SAMPLE_STRING
304                                                 + SAMPLE_STRING
305                                                 + SAMPLE_STRING
306                                                 + SAMPLE_STRING
307                                                 , 4000);
308 #endif
309                                         break;
310                                         #endregion
311
312                                         #region DB2
313                                 case MonoTests.System.Data.Utils.DataBaseServer.DB2:
314                                         row.Add("DATE", new DateTime(2004, 8, 9, 20, 30, 15, 500).Date);
315                                         row.Add("TIME", new TimeSpan(20, 30, 15));
316                                         row.Add("TIMESTAMP", new DateTime(2004, 8, 9, 20, 30, 15, 500));
317                                         row.Add("BLOB", new byte[]      {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
318                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
319                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
320                                                                                                         ,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
325                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
326                                                                                                         ,0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0
327                                                                                                 });
328                                         row.Add("CLOB", SAMPLE_STRING
329                                                 + SAMPLE_STRING
330                                                 + SAMPLE_STRING
331                                                 + SAMPLE_STRING
332                                                 + SAMPLE_STRING
333                                                 + SAMPLE_STRING
334                                                 );
335                                         row.Add("DBCLOB", SAMPLE_STRING
336                                                 + SAMPLE_STRING
337                                                 + SAMPLE_STRING
338                                                 + SAMPLE_STRING
339                                                 + SAMPLE_STRING
340                                                 + SAMPLE_STRING
341                                                 );
342                                         break;
343                                         #endregion
344
345                                         #region PostgreSQL
346                                 case MonoTests.System.Data.Utils.DataBaseServer.PostgreSQL:
347                                         row.Add("BYTEA", new byte[]     {0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
348                                                                                                         0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
349                                                                                                         0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
350                                                                                                         0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0,
351                                                                                                         0x00, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0xF0}, 50);
352                                         row.Add("DATE", new DateTime(2004, 8, 9));
353                                         row.Add("TEXT", "abcdefg", 16); 
354                                         row.Add("TIME", new Sys.TimeSpan(02,02,02));
355                                         row.Add("TIMESTAMP", new DateTime(2004, 8, 9, 20, 30, 15, 500), 8);
356                                         break;
357                                         #endregion
358
359                         }
360                         return row;
361                 }
362         }
363 }