2009-03-27 Jonathan Chambers <joncham@gmail.com>
[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 (subType == 2)
209                     {
210                         return DbDataType.Decimal;
211                     }
212                     else if (subType == 1)
213                     {
214                         return DbDataType.Numeric;
215                     }
216                                         else
217                                         {
218                                                 return DbDataType.SmallInt;
219                                         }
220
221                                 case IscCodes.blr_long:
222                     if (subType == 2)
223                     {
224                         return DbDataType.Decimal;
225                     }
226                     else if (subType == 1)
227                     {
228                         return DbDataType.Numeric;
229                     }
230                                         else
231                                         {
232                                                 return DbDataType.Integer;
233                                         }
234
235                                 case IscCodes.blr_quad:
236                                 case IscCodes.blr_int64:
237                                 case IscCodes.blr_blob_id:
238                     if (subType == 2)
239                     {
240                         return DbDataType.Decimal;
241                     }
242                     else if (subType == 1)
243                     {
244                         return DbDataType.Numeric;
245                     }
246                                         else
247                                         {
248                                                 return DbDataType.BigInt;
249                                         }
250
251                                 case IscCodes.blr_double:
252                                 case IscCodes.blr_d_float:
253                                         return DbDataType.Double;
254
255                                 case IscCodes.blr_float:
256                                         return DbDataType.Float;
257
258                                 case IscCodes.blr_sql_date:
259                                         return DbDataType.Date;
260
261                                 case IscCodes.blr_sql_time:
262                                         return DbDataType.Time;
263
264                                 case IscCodes.blr_timestamp:
265                                         return DbDataType.TimeStamp;
266
267                                 case IscCodes.blr_blob:
268                                         if (subType == 1)
269                                         {
270                                                 return DbDataType.Text;
271                                         }
272                                         else
273                                         {
274                                                 return DbDataType.Binary;
275                                         }
276
277                                 default:
278                                         throw new ArgumentException("Invalid data type");
279                         }
280                 }
281
282                 public static string GetDataTypeName(DbDataType dataType)
283                 {
284                         switch (dataType)
285                         {
286                                 case DbDataType.Array:
287                                         return "ARRAY";
288
289                                 case DbDataType.Binary:
290                                         return "BLOB";
291
292                                 case DbDataType.Text:
293                                         return "BLOB SUB_TYPE 1";
294
295                                 case DbDataType.Char:
296                                 case DbDataType.Guid:
297                                         return "CHAR";
298
299                                 case DbDataType.VarChar:
300                                         return "VARCHAR";
301
302                                 case DbDataType.SmallInt:
303                                         return "SMALLINT";
304
305                                 case DbDataType.Integer:
306                                         return "INTEGER";
307
308                                 case DbDataType.Float:
309                                         return "FLOAT";
310
311                                 case DbDataType.Double:
312                                         return "DOUBLE PRECISION";
313
314                                 case DbDataType.BigInt:
315                                         return "BIGINT";
316
317                                 case DbDataType.Numeric:
318                                         return "NUMERIC";
319
320                                 case DbDataType.Decimal:
321                                         return "DECIMAL";
322
323                                 case DbDataType.Date:
324                                         return "DATE";
325
326                                 case DbDataType.Time:
327                                         return "TIME";
328
329                                 case DbDataType.TimeStamp:
330                                         return "TIMESTAMP";
331
332                                 default:
333                                         return null;
334                         }
335                 }
336
337                 public static DbType GetDbType(DbDataType type)
338                 {
339                         switch (type)
340                         {
341                                 case DbDataType.Array:
342                                 case DbDataType.Binary:
343                                         return DbType.Binary;
344
345                                 case DbDataType.Text:
346                                 case DbDataType.VarChar:
347                                 case DbDataType.Char:
348                                         return DbType.String;
349
350                                 case DbDataType.SmallInt:
351                                         return DbType.Int16;
352
353                                 case DbDataType.Integer:
354                                         return DbType.Int32;
355
356                                 case DbDataType.BigInt:
357                                         return DbType.Int64;
358
359                                 case DbDataType.Date:
360                                         return DbType.Date;
361
362                                 case DbDataType.Time:
363                                         return DbType.Time;
364
365                                 case DbDataType.TimeStamp:
366                                         return DbType.DateTime;
367
368                                 case DbDataType.Numeric:
369                                 case DbDataType.Decimal:
370                                         return DbType.Decimal;
371
372                                 case DbDataType.Float:
373                                         return DbType.Single;
374
375                                 case DbDataType.Double:
376                                         return DbType.Double;
377
378                                 case DbDataType.Guid:
379                                         return DbType.Guid;
380
381                                 default:
382                                         throw new ArgumentException("Invalid data type");
383                         }
384                 }
385
386                 public static DbDataType GetDbDataType(DbType dbType)
387                 {
388                         switch (dbType)
389                         {
390                                 case DbType.String:
391                                 case DbType.AnsiString:
392                                         return DbDataType.VarChar;
393
394                                 case DbType.StringFixedLength:
395                                 case DbType.AnsiStringFixedLength:
396                                         return DbDataType.Char;
397
398                                 case DbType.Boolean:
399                                 case DbType.Byte:
400                                 case DbType.SByte:
401                                 case DbType.Int16:
402                                 case DbType.UInt16:
403                                         return DbDataType.SmallInt;
404
405                                 case DbType.Int32:
406                                 case DbType.UInt32:
407                                         return DbDataType.Integer;
408
409                                 case DbType.Int64:
410                                 case DbType.UInt64:
411                                         return DbDataType.BigInt;
412
413                                 case DbType.Date:
414                                         return DbDataType.Date;
415
416                                 case DbType.Time:
417                                         return DbDataType.Time;
418
419                                 case DbType.DateTime:
420                                         return DbDataType.TimeStamp;
421
422                                 case DbType.Object:
423                                 case DbType.Binary:
424                                         return DbDataType.Binary;
425
426                                 case DbType.Decimal:
427                                         return DbDataType.Decimal;
428
429                                 case DbType.Double:
430                                         return DbDataType.Double;
431
432                                 case DbType.Single:
433                                         return DbDataType.Float;
434
435                                 case DbType.Guid:
436                                         return DbDataType.Guid;
437
438                                 default:
439                                         throw new ArgumentException("Invalid data type");
440                         }
441                 }
442
443                 #endregion
444         }
445 }