copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / TypeHelper.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and     Mono 
3  * 
4  *         The contents of this file are subject to the Initial 
5  *         Developer's Public License Version 1.0 (the "License"); 
6  *         you may not use this file except in compliance with the 
7  *         License. You may obtain a copy of the License at 
8  *         http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *         Software distributed under the License is distributed on 
11  *         an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *         express or implied. See the License for the specific 
13  *         language governing rights and limitations under the License.
14  * 
15  *      Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *      All Rights Reserved.
17  */
18
19 using System;
20 using System.Data;
21
22 namespace FirebirdSql.Data.Common
23 {
24         internal sealed class TypeHelper
25         {
26                 #region Constructors
27
28                 private TypeHelper()
29                 {
30                 }
31
32                 #endregion
33
34                 #region Static Methods
35
36                 public static short GetSize(DbDataType dataType)
37                 {
38                         switch (dataType)
39                         {
40                                 case DbDataType.Array:
41                                 case DbDataType.Binary:
42                                 case DbDataType.Text:
43                                         return 8;
44
45                                 case DbDataType.SmallInt:
46                                         return 2;
47
48                                 case DbDataType.Integer:
49                                 case DbDataType.Float:
50                                 case DbDataType.Date:
51                                 case DbDataType.Time:
52                                         return 4;
53
54                                 case DbDataType.BigInt:
55                                 case DbDataType.Double:
56                                 case DbDataType.TimeStamp:
57                                         return 8;
58
59                                 case DbDataType.Guid:
60                                         return 16;
61
62                                 default:
63                                         return 0;
64                         }
65                 }
66
67                 public static int GetFbType(DbDataType dataType, bool isNullable)
68                 {
69                         int sqltype = 0;
70
71                         switch (dataType)
72                         {
73                                 case DbDataType.Array:
74                                         sqltype = IscCodes.SQL_ARRAY;
75                                         break;
76
77                                 case DbDataType.Binary:
78                                 case DbDataType.Text:
79                                         sqltype = IscCodes.SQL_BLOB;
80                                         break;
81
82                                 case DbDataType.Char:
83                                         sqltype = IscCodes.SQL_TEXT;
84                                         break;
85
86                                 case DbDataType.VarChar:
87                                         sqltype = IscCodes.SQL_VARYING;
88                                         break;
89
90                                 case DbDataType.SmallInt:
91                                         sqltype = IscCodes.SQL_SHORT;
92                                         break;
93
94                                 case DbDataType.Integer:
95                                         sqltype = IscCodes.SQL_LONG;
96                                         break;
97
98                                 case DbDataType.BigInt:
99                                         sqltype = IscCodes.SQL_INT64;
100                                         break;
101
102                                 case DbDataType.Float:
103                                         sqltype = IscCodes.SQL_FLOAT;
104                                         break;
105
106                                 case DbDataType.Guid:
107                                         sqltype = IscCodes.SQL_TEXT;
108                                         break;
109
110                                 case DbDataType.Double:
111                                         sqltype = IscCodes.SQL_DOUBLE;
112                                         break;
113
114                                 case DbDataType.Date:
115                                         sqltype = IscCodes.SQL_TYPE_DATE;
116                                         break;
117
118                                 case DbDataType.Time:
119                                         sqltype = IscCodes.SQL_TYPE_TIME;
120                                         break;
121
122                                 case DbDataType.TimeStamp:
123                                         sqltype = IscCodes.SQL_TIMESTAMP;
124                                         break;
125
126                                 default:
127                                         throw new ArgumentException("Invalid data type");
128                         }
129
130                         if (isNullable)
131                         {
132                                 sqltype++;
133                         }
134
135                         return sqltype;
136                 }
137
138                 public static int GetFbType(int blrType)
139                 {
140                         switch (blrType)
141                         {
142                                 case IscCodes.blr_varying:
143                                 case IscCodes.blr_varying2:
144                                         return IscCodes.SQL_VARYING;
145
146                                 case IscCodes.blr_text:
147                                 case IscCodes.blr_text2:
148                                 case IscCodes.blr_cstring:
149                                 case IscCodes.blr_cstring2:
150                                         return IscCodes.SQL_TEXT;
151
152                                 case IscCodes.blr_short:
153                                         return IscCodes.SQL_SHORT;
154
155                                 case IscCodes.blr_long:
156                                         return IscCodes.SQL_LONG;
157
158                                 case IscCodes.blr_quad:
159                                         return IscCodes.SQL_QUAD;
160
161                                 case IscCodes.blr_int64:
162                                 case IscCodes.blr_blob_id:
163                                         return IscCodes.SQL_INT64;
164
165                                 case IscCodes.blr_double:
166                                         return IscCodes.SQL_DOUBLE;
167
168                                 case IscCodes.blr_d_float:
169                                         return IscCodes.SQL_D_FLOAT;
170
171                                 case IscCodes.blr_float:
172                                         return IscCodes.SQL_FLOAT;
173
174                                 case IscCodes.blr_sql_date:
175                                         return IscCodes.SQL_TYPE_DATE;
176
177                                 case IscCodes.blr_sql_time:
178                                         return IscCodes.SQL_TYPE_TIME;
179
180                                 case IscCodes.blr_timestamp:
181                                         return IscCodes.SQL_TIMESTAMP;
182
183                                 case IscCodes.blr_blob:
184                                         return IscCodes.SQL_BLOB;
185
186                                 default:
187                                         throw new ArgumentException("Invalid data type");
188                         }
189                 }
190
191                 public static DbDataType GetDbDataType(int blrType, int subType, int scale)
192                 {
193                         switch (blrType)
194                         {
195                                 case IscCodes.blr_varying:
196                                 case IscCodes.blr_varying2:
197                                         return DbDataType.VarChar;
198
199                                 case IscCodes.blr_text:
200                                 case IscCodes.blr_text2:
201                                         return DbDataType.Char;
202
203                                 case IscCodes.blr_cstring:
204                                 case IscCodes.blr_cstring2:
205                                         return DbDataType.Text;
206
207                                 case IscCodes.blr_short:
208                                         if (scale < 0)
209                                         {
210                                                 return DbDataType.Decimal;
211                                         }
212                                         else
213                                         {
214                                                 return DbDataType.SmallInt;
215                                         }
216
217                                 case IscCodes.blr_long:
218                                         if (scale < 0)
219                                         {
220                                                 return DbDataType.Decimal;
221                                         }
222                                         else
223                                         {
224                                                 return DbDataType.Integer;
225                                         }
226
227                                 case IscCodes.blr_quad:
228                                 case IscCodes.blr_int64:
229                                 case IscCodes.blr_blob_id:
230                                         if (scale < 0)
231                                         {
232                                                 return DbDataType.Decimal;
233                                         }
234                                         else
235                                         {
236                                                 return DbDataType.BigInt;
237                                         }
238
239                                 case IscCodes.blr_double:
240                                 case IscCodes.blr_d_float:
241                                         return DbDataType.Double;
242
243                                 case IscCodes.blr_float:
244                                         return DbDataType.Float;
245
246                                 case IscCodes.blr_sql_date:
247                                         return DbDataType.Date;
248
249                                 case IscCodes.blr_sql_time:
250                                         return DbDataType.Time;
251
252                                 case IscCodes.blr_timestamp:
253                                         return DbDataType.TimeStamp;
254
255                                 case IscCodes.blr_blob:
256                                         if (subType == 1)
257                                         {
258                                                 return DbDataType.Text;
259                                         }
260                                         else
261                                         {
262                                                 return DbDataType.Binary;
263                                         }
264
265                                 default:
266                                         throw new ArgumentException("Invalid data type");
267                         }
268                 }
269
270                 public static string GetDataTypeName(DbDataType dataType)
271                 {
272                         switch (dataType)
273                         {
274                                 case DbDataType.Array:
275                                         return "ARRAY";
276
277                                 case DbDataType.Binary:
278                                         return "BLOB";
279
280                                 case DbDataType.Text:
281                                         return "BLOB SUB_TYPE 1";
282
283                                 case DbDataType.Char:
284                                 case DbDataType.Guid:
285                                         return "CHAR";
286
287                                 case DbDataType.VarChar:
288                                         return "VARCHAR";
289
290                                 case DbDataType.SmallInt:
291                                         return "SMALLINT";
292
293                                 case DbDataType.Integer:
294                                         return "INTEGER";
295
296                                 case DbDataType.Float:
297                                         return "FLOAT";
298
299                                 case DbDataType.Double:
300                                         return "DOUBLE PRECISION";
301
302                                 case DbDataType.BigInt:
303                                         return "BIGINT";
304
305                                 case DbDataType.Numeric:
306                                         return "NUMERIC";
307
308                                 case DbDataType.Decimal:
309                                         return "DECIMAL";
310
311                                 case DbDataType.Date:
312                                         return "DATE";
313
314                                 case DbDataType.Time:
315                                         return "TIME";
316
317                                 case DbDataType.TimeStamp:
318                                         return "TIMESTAMP";
319
320                                 default:
321                                         return null;
322                         }
323                 }
324
325                 public static DbType GetDbType(DbDataType type)
326                 {
327                         switch (type)
328                         {
329                                 case DbDataType.Array:
330                                 case DbDataType.Binary:
331                                         return DbType.Binary;
332
333                                 case DbDataType.Text:
334                                 case DbDataType.VarChar:
335                                 case DbDataType.Char:
336                                         return DbType.String;
337
338                                 case DbDataType.SmallInt:
339                                         return DbType.Int16;
340
341                                 case DbDataType.Integer:
342                                         return DbType.Int32;
343
344                                 case DbDataType.BigInt:
345                                         return DbType.Int64;
346
347                                 case DbDataType.Date:
348                                         return DbType.Date;
349
350                                 case DbDataType.Time:
351                                         return DbType.Time;
352
353                                 case DbDataType.TimeStamp:
354                                         return DbType.DateTime;
355
356                                 case DbDataType.Numeric:
357                                 case DbDataType.Decimal:
358                                         return DbType.Decimal;
359
360                                 case DbDataType.Float:
361                                         return DbType.Single;
362
363                                 case DbDataType.Double:
364                                         return DbType.Double;
365
366                                 case DbDataType.Guid:
367                                         return DbType.Guid;
368
369                                 default:
370                                         throw new ArgumentException("Invalid data type");
371                         }
372                 }
373
374                 public static DbDataType GetDbDataType(DbType dbType)
375                 {
376                         switch (dbType)
377                         {
378                                 case DbType.String:
379                                 case DbType.AnsiString:
380                                         return DbDataType.VarChar;
381
382                                 case DbType.StringFixedLength:
383                                 case DbType.AnsiStringFixedLength:
384                                         return DbDataType.Char;
385
386                                 case DbType.Boolean:
387                                 case DbType.Byte:
388                                 case DbType.SByte:
389                                 case DbType.Int16:
390                                 case DbType.UInt16:
391                                         return DbDataType.SmallInt;
392
393                                 case DbType.Int32:
394                                 case DbType.UInt32:
395                                         return DbDataType.Integer;
396
397                                 case DbType.Int64:
398                                 case DbType.UInt64:
399                                         return DbDataType.BigInt;
400
401                                 case DbType.Date:
402                                         return DbDataType.Date;
403
404                                 case DbType.Time:
405                                         return DbDataType.Time;
406
407                                 case DbType.DateTime:
408                                         return DbDataType.TimeStamp;
409
410                                 case DbType.Object:
411                                 case DbType.Binary:
412                                         return DbDataType.Binary;
413
414                                 case DbType.Decimal:
415                                         return DbDataType.Decimal;
416
417                                 case DbType.Double:
418                                         return DbDataType.Double;
419
420                                 case DbType.Single:
421                                         return DbDataType.Float;
422
423                                 case DbType.Guid:
424                                         return DbDataType.Guid;
425
426                                 default:
427                                         throw new ArgumentException("Invalid data type");
428                         }
429                 }
430
431                 #endregion
432         }
433 }