[corlib] Let XI and XM define their own default encoding [#29928]
[mono.git] / mcs / class / System.Data / Microsoft.SqlServer.Server / SqlMetaData.cs
1 //
2 // Microsoft.SqlServer.Server.SqlMetaData
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2003
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33
34 using System;
35 using System.Data;
36 using System.Threading;
37 using System.Data.SqlTypes;
38
39 namespace Microsoft.SqlServer.Server {
40         public sealed class SqlMetaData
41         {
42                 #region Fields
43
44                 SqlCompareOptions compareOptions = SqlCompareOptions.None;
45                 string databaseName = null;
46                 long _localeId = 0L;
47                 long maxLength = 0L;
48                 string name;
49                 byte precision = 10;
50                 byte scale = 0;
51                 string owningSchema = null;
52                 string objectName = null;
53                 SqlDbType _sqlDbType = SqlDbType.NVarChar;
54                 DbType _dbType = DbType.String;
55                 Type type = typeof (string);
56
57                 #endregion // Fields
58
59                 #region Constructors
60
61                 public SqlMetaData (string name, SqlDbType dbType)
62                 {
63                         if (name == null)
64                                 throw new ArgumentNullException ("name can not be null");
65                         switch (dbType) {
66                         case SqlDbType.Bit:
67                                 maxLength = 1;
68                                 precision = 1;
69                                 scale = 0;
70                                 _localeId = 0;
71                                 compareOptions = SqlCompareOptions.None;
72                                 _dbType = DbType.Boolean;
73                                 type = typeof (bool);
74                                 break;
75                         case SqlDbType.BigInt:
76                                 maxLength = 8;
77                                 precision = 19;
78                                 scale = 0;
79                                 _localeId = 0;
80                                 compareOptions = SqlCompareOptions.None;
81                                 _dbType = DbType.Int64;
82                                 type = typeof (long);
83                                 break;
84                         case SqlDbType.DateTime:
85                                 maxLength = 8;
86                                 precision = 23;
87                                 scale = 3;
88                                 _localeId = 0;
89                                 compareOptions = SqlCompareOptions.None;
90                                 _dbType = DbType.DateTime;
91                                 type = typeof (DateTime);
92                                 break;
93                         case SqlDbType.Decimal:
94                                 maxLength = 9;
95                                 precision = 18;
96                                 scale = 0;
97                                 _localeId = 0;
98                                 compareOptions = SqlCompareOptions.None;
99                                 _dbType = DbType.Decimal;
100                                 type = typeof (decimal);
101                                 break;
102                         case SqlDbType.Float:
103                                 maxLength = 8;
104                                 precision = 53;
105                                 scale = 0;
106                                 _localeId = 0;
107                                 compareOptions = SqlCompareOptions.None;
108                                 _dbType = DbType.Double;
109                                 type = typeof (float);
110                                 break;
111                         case SqlDbType.Int:
112                                 maxLength = 4;
113                                 precision = 10;
114                                 scale = 0;
115                                 _localeId = 0;
116                                 compareOptions = SqlCompareOptions.None;
117                                 _dbType = DbType.Int32;
118                                 type = typeof (int);
119                                 break;
120                         case SqlDbType.Money:
121                                 maxLength = 8;
122                                 precision = 19;
123                                 scale = 4;
124                                 _localeId = 0;
125                                 compareOptions = SqlCompareOptions.None;
126                                 _dbType = DbType.Currency;
127                                 type = typeof (double);
128                                 break;
129                           /*
130                         case SqlDbType.Numeric:
131                                 maxLength = ;
132                                 precision = ;
133                                 scale = ;
134                                 localeId = 0;
135                                 compareOptions = SqlCompareOptions.None;
136                                 break;
137                           */
138                         case SqlDbType.SmallDateTime:
139                                 maxLength = 4;
140                                 precision = 16;
141                                 scale = 0;
142                                 _localeId = 0;
143                                 compareOptions = SqlCompareOptions.None;
144                                 _dbType = DbType.DateTime;
145                                 type = typeof (DateTime);
146                                 break;
147                         case SqlDbType.SmallInt:
148                                 maxLength = 2;
149                                 precision = 5;
150                                 scale = 0;
151                                 _localeId = 0;
152                                 compareOptions = SqlCompareOptions.None;
153                                 _dbType = DbType.Int16;
154                                 type = typeof (short);
155                                 break;
156                         case SqlDbType.SmallMoney:
157                                 maxLength = 4;
158                                 precision = 10;
159                                 scale = 4;
160                                 _localeId = 0;
161                                 compareOptions = SqlCompareOptions.None;
162                                 _dbType = DbType.Currency;
163                                 type = typeof (double);
164                                 break;
165                         case SqlDbType.Timestamp:
166                                 maxLength = 8;
167                                 precision = 0;
168                                 scale = 0;
169                                 _localeId = 0;
170                                 compareOptions = SqlCompareOptions.None;
171                                 _dbType = DbType.DateTime;
172                                 type = typeof (DateTime);
173                                 break;
174                         case SqlDbType.TinyInt:
175                                 maxLength = 1;
176                                 precision = 3;
177                                 scale = 0;
178                                 _localeId = 0;
179                                 compareOptions = SqlCompareOptions.None;
180                                 _dbType = DbType.Int16;
181                                 type = typeof (short);
182                                 break;
183                         case SqlDbType.UniqueIdentifier:
184                                 maxLength = 16;
185                                 precision = 0;
186                                 scale = 0;
187                                 _localeId = 0;
188                                 compareOptions = SqlCompareOptions.None;
189                                 _dbType = DbType.Guid;
190                                 type = typeof (Guid);
191                                 break;
192                         case SqlDbType.Xml:
193                                 maxLength = -1;
194                                 precision = 0;
195                                 scale = 0;
196                                 _localeId = 0;
197                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
198                                 _dbType = DbType.Xml;
199                                 type = typeof (string);
200                                 break;
201                         default:
202                                 throw new ArgumentException ("SqlDbType not supported");
203                         }
204                         this.name = name;
205                         this._sqlDbType = dbType;
206                 }
207
208                 public SqlMetaData (string name, SqlDbType dbType, long maxLength)
209                 {
210                         if (name == null)
211                                 throw new ArgumentNullException ("name can not be null");
212                         switch (dbType) {
213                         case SqlDbType.Binary:
214                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
215                                 _dbType = DbType.Binary;
216                                 type = typeof (byte []);
217                                 break;
218                         case SqlDbType.Char:
219                                 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
220                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
221                                 _dbType = DbType.AnsiStringFixedLength;
222                                 type = typeof (string);
223                                 break;
224                         case SqlDbType.Image:
225                                 maxLength = -1;
226                                 precision = 0;
227                                 scale = 0;
228                                 _localeId = 0;
229                                 compareOptions = SqlCompareOptions.None;
230                                 _dbType = DbType.Binary;
231                                 type = typeof (byte []);
232                                 break;
233                         case SqlDbType.NChar:
234                                 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
235                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
236                                 _dbType = DbType.String;
237                                 type = typeof (string);
238                                 break;
239                         case SqlDbType.NText:
240                                 maxLength = -1;
241                                 precision = 0;
242                                 scale = 0;
243                                 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
244                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
245                                 _dbType = DbType.String;
246                                 type = typeof (string);
247                                 break;
248                         case SqlDbType.NVarChar:
249                                 maxLength = -1;
250                                 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
251                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
252                                 _dbType = DbType.String;
253                                 type = typeof (string);
254                                 break;
255                         case SqlDbType.Text:
256                                 maxLength = -1;
257                                 precision = 0;
258                                 scale = 0;
259                                 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
260                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
261                                 _dbType = DbType.String;
262                                 type = typeof (char []);
263                                 break;
264                         case SqlDbType.VarBinary:
265                                 maxLength = -1;
266                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
267                                 _dbType = DbType.Binary;
268                                 type = typeof (byte []);
269                                 break;
270                         case SqlDbType.VarChar:
271                                 maxLength = -1;
272                                 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
273                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
274                                 _dbType = DbType.String;
275                                 type = typeof (char []);
276                                 break;
277                         default:
278                                 throw new ArgumentException ("SqlDbType not supported");
279                         }
280                         this.maxLength = maxLength;
281                         this.name = name;
282                         this._sqlDbType = dbType;
283                 }
284
285                 [MonoTODO]
286                 public SqlMetaData (string name, SqlDbType dbType, Type userDefinedType)
287                 {
288                         if (name == null)
289                                 throw new ArgumentNullException ("name can not be null");
290                         switch (dbType) {
291                         case SqlDbType.Udt:
292                                 maxLength = -1;
293                                 precision = 0;
294                                 scale = 0;
295                                 _localeId = 0;
296                                 compareOptions = SqlCompareOptions.None;
297                                 _dbType = DbType.Guid;
298                                 type = typeof (Guid);
299                                 break;
300                         default:
301                                 throw new ArgumentException ("SqlDbType not supported");
302                         }
303                         this.name = name;
304                         // FIXME:
305                         //this.sqlDbType = userDefinedType;
306                         throw new NotImplementedException ();
307                 }
308
309                 public SqlMetaData (string name, SqlDbType dbType, byte precision, byte scale)
310                 {
311                         if (name == null)
312                                 throw new ArgumentNullException ("name can not be null");
313                         switch (dbType) {
314                         case SqlDbType.Decimal:
315                                 maxLength = 9;
316                                 this.precision = precision;
317                                 this.scale = scale;
318                                 _localeId = 0;
319                                 compareOptions = SqlCompareOptions.None;
320                                 _dbType = DbType.Decimal;
321                                 type = typeof (decimal);
322                                 break;
323                         default:
324                                 throw new ArgumentException ("SqlDbType not supported");
325                         }
326                         this.name = name;
327                         this._sqlDbType = dbType;
328                 }
329
330                 public SqlMetaData (string name, SqlDbType dbType, long maxLength, long locale, SqlCompareOptions compareOptions)
331                 {
332                         if (name == null)
333                                 throw new ArgumentNullException ("name can not be null");
334                         switch (dbType) {
335                         case SqlDbType.Char:
336                                 _dbType = DbType.AnsiStringFixedLength;
337                                 type = typeof (char []);
338                                 break;
339                         case SqlDbType.NChar:
340                                 _dbType = DbType.StringFixedLength;
341                                 type = typeof (char []);
342                                 break;
343                         case SqlDbType.NText:
344                         case SqlDbType.NVarChar:
345                                 _dbType = DbType.String;
346                                 type = typeof (string);
347                                 break;
348                         case SqlDbType.Text:
349                         case SqlDbType.VarChar:
350                                 _dbType = DbType.AnsiString;
351                                 type = typeof (char []);
352                                 break;
353                         default:
354                                 throw new ArgumentException ("SqlDbType not supported");
355                         }
356                         this.compareOptions = compareOptions;
357                         this._localeId = locale;
358                         this.maxLength = maxLength;
359                         this.name = name;
360                         this._sqlDbType = dbType;
361                 }
362
363                 public SqlMetaData (string name, SqlDbType dbType, string database, string owningSchema, string objectName)
364                 {
365                         if ((name == null || objectName == null) && database != null && owningSchema != null)
366                                 throw new ArgumentNullException ("name can not be null");
367                         switch (dbType) {
368                         case SqlDbType.Xml:
369                                 maxLength = -1;
370                                 precision = 0;
371                                 scale = 0;
372                                 _localeId = 0;
373                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
374                                 _dbType = DbType.String;
375                                 type = typeof (string);
376                                 break;
377                         default:
378                                 throw new ArgumentException ("SqlDbType not supported");
379                         }
380                         this.name = name;
381                         this._sqlDbType = dbType;
382                         databaseName = database;
383                         this.owningSchema = owningSchema;
384                         this.objectName = objectName;
385                 }
386
387                 public SqlMetaData (string name, SqlDbType dbType, long maxLength, byte precision,
388                                     byte scale, long locale, SqlCompareOptions compareOptions,
389                                     Type userDefinedType)
390                 {
391                         if (name == null)
392                                 throw new ArgumentNullException ("name can not be null");
393                         this.compareOptions = compareOptions;
394                         this._localeId = locale;
395                         this.maxLength = maxLength;
396                         this.precision = precision;
397                         this.scale = scale;
398                         switch (dbType) {
399                         case SqlDbType.Bit:
400                                 maxLength = 1;
401                                 precision = 1;
402                                 scale = 0;
403                                 locale = 0;
404                                 compareOptions = SqlCompareOptions.None;
405                                 _dbType = DbType.Boolean;
406                                 type = typeof (bool);
407                                 break;
408                         case SqlDbType.BigInt:
409                                 maxLength = 8;
410                                 precision = 19;
411                                 scale = 0;
412                                 locale = 0;
413                                 compareOptions = SqlCompareOptions.None;
414                                 _dbType = DbType.Int64;
415                                 type = typeof (long);
416                                 break;
417                         case SqlDbType.DateTime:
418                                 maxLength = 8;
419                                 precision = 23;
420                                 scale = 3;
421                                 locale = 0;
422                                 compareOptions = SqlCompareOptions.None;
423                                 _dbType = DbType.DateTime;
424                                 type = typeof (DateTime);
425                                 break;
426                         case SqlDbType.Decimal:
427                                 maxLength = 9;
428                                 precision = 18;
429                                 scale = 0;
430                                 locale = 0;
431                                 compareOptions = SqlCompareOptions.None;
432                                 _dbType = DbType.Decimal;
433                                 type = typeof (decimal);
434                                 break;
435                         case SqlDbType.Float:
436                                 maxLength = 8;
437                                 precision = 53;
438                                 scale = 0;
439                                 locale = 0;
440                                 compareOptions = SqlCompareOptions.None;
441                                 _dbType = DbType.Decimal;
442                                 type = typeof (float);
443                                 break;
444                         case SqlDbType.Image:
445                                 maxLength = -1;
446                                 precision = 0;
447                                 scale = 0;
448                                 locale = 0;
449                                 compareOptions = SqlCompareOptions.None;
450                                 _dbType = DbType.Binary;
451                                 type = typeof (byte []);
452                                 break;
453                         case SqlDbType.Int:
454                                 maxLength = 4;
455                                 precision = 10;
456                                 scale = 0;
457                                 locale = 0;
458                                 compareOptions = SqlCompareOptions.None;
459                                 _dbType = DbType.Int32;
460                                 type = typeof (int);
461                                 break;
462                         case SqlDbType.Money:
463                                 maxLength = 8;
464                                 precision = 19;
465                                 scale = 4;
466                                 locale = 0;
467                                 compareOptions = SqlCompareOptions.None;
468                                 _dbType = DbType.Currency;
469                                 type = typeof (decimal);
470                                 break;
471                         case SqlDbType.NText:
472                                 maxLength = -1;
473                                 precision = 0;
474                                 scale = 0;
475                                 locale = Thread.CurrentThread.CurrentCulture.LCID;
476                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
477                                 _dbType = DbType.String;
478                                 type = typeof (string);
479                                 break;
480                           /*
481                         case SqlDbType.Numeric:
482                                 maxLength = ;
483                                 precision = ;
484                                 scale = ;
485                                 localeId = 0;
486                                 compareOptions = SqlCompareOptions.None;
487                                 break;
488                           */
489                         case SqlDbType.Real:
490                                 maxLength = 4;
491                                 precision = 24;
492                                 scale = 0;
493                                 locale = 0;
494                                 compareOptions = SqlCompareOptions.None;
495                                 _dbType = DbType.Single;
496                                 type = typeof (Single);
497                                 break;
498                         case SqlDbType.SmallDateTime:
499                                 maxLength = 4;
500                                 precision = 16;
501                                 scale = 0;
502                                 locale = 0;
503                                 compareOptions = SqlCompareOptions.None;
504                                 _dbType = DbType.DateTime;
505                                 type = typeof (DateTime);
506                                 break;
507                         case SqlDbType.SmallInt:
508                                 maxLength = 2;
509                                 precision = 5;
510                                 scale = 0;
511                                 locale = 0;
512                                 compareOptions = SqlCompareOptions.None;
513                                 _dbType = DbType.Int16;
514                                 type = typeof (short);
515                                 break;
516                         case SqlDbType.SmallMoney:
517                                 maxLength = 4;
518                                 precision = 10;
519                                 scale = 4;
520                                 locale = 0;
521                                 compareOptions = SqlCompareOptions.None;
522                                 _dbType = DbType.Currency;
523                                 type = typeof (decimal);
524                                 break;
525                         case SqlDbType.Text:
526                                 maxLength = -1;
527                                 precision = 0;
528                                 scale = 0;
529                                 locale = Thread.CurrentThread.CurrentCulture.LCID;
530                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
531                                 _dbType = DbType.AnsiString;
532                                 type = typeof (char []);
533                                 break;
534                         case SqlDbType.Timestamp:
535                                 maxLength = 8;
536                                 precision = 0;
537                                 scale = 0;
538                                 locale = 0;
539                                 compareOptions = SqlCompareOptions.None;
540                                 _dbType = DbType.Byte;
541                                 type = typeof (byte []);
542                                 break;
543                         case SqlDbType.TinyInt:
544                                 maxLength = 1;
545                                 precision = 3;
546                                 scale = 0;
547                                 locale = 0;
548                                 compareOptions = SqlCompareOptions.None;
549                                 _dbType = DbType.Int16;
550                                 type = typeof (short);
551                                 break;
552                         case SqlDbType.UniqueIdentifier:
553                                 maxLength = 16;
554                                 precision = 0;
555                                 scale = 0;
556                                 locale = 0;
557                                 compareOptions = SqlCompareOptions.None;
558                                 _dbType = DbType.Guid;
559                                 type = typeof (Guid);
560                                 break;
561                         case SqlDbType.Udt:
562                                 maxLength = -1;
563                                 precision = 0;
564                                 scale = 0;
565                                 locale = 0;
566                                 compareOptions = SqlCompareOptions.None;
567                                 _dbType = DbType.Object;
568                                 type = typeof (object);
569                                 break;
570                         case SqlDbType.Variant:
571                                 maxLength = 8016;
572                                 precision = 0;
573                                 scale = 0;
574                                 locale = 0;
575                                 compareOptions = SqlCompareOptions.None;
576                                 _dbType = DbType.Object;
577                                 type = typeof (object);
578                                 break;
579                         case SqlDbType.Xml:
580                                 maxLength = -1;
581                                 precision = 0;
582                                 scale = 0;
583                                 locale = 0;
584                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
585                                 _dbType = DbType.Xml;
586                                 type = typeof (string);
587                                 break;
588                         default:
589                           /*
590                                 if (typeof (DbType.Row) == typeof (userDefinedType)) {
591                                         // FIXME:
592                                         // maxLength = Number of columns;
593                                         precision = 0;
594                                         scale = 0;
595                                         localeId = 0;
596                                         compareOptions = SqlCompareOptions.None;
597                                 } else
598                           */
599                                         throw new ArgumentException ("SqlDbType not supported");
600                         }
601                         this.name = name;
602                         this._sqlDbType = dbType;
603                 }
604
605                 #endregion // Constructors
606
607                 #region Properties
608
609                 public SqlCompareOptions CompareOptions {
610                         get { return compareOptions; }
611                 }
612
613                 public DbType DbType {
614                         get { return _dbType; }
615                 }
616
617                 public long LocaleId {
618                         get { return _localeId; }
619                 }
620
621                 public static long Max {
622                         get { return -1; }
623                 }
624
625                 public long MaxLength {
626                         get { return maxLength; }
627                 }
628
629                 public string Name {
630                         get { return name; }
631                 }
632
633                 public byte Precision { 
634                         get { return precision; }
635                 }
636
637                 public byte Scale { 
638                         get { return scale; }
639                 }
640
641                 public SqlDbType SqlDbType {
642                         get { return _sqlDbType; }
643                 }
644
645                 public string XmlSchemaCollectionDatabase {
646                         get { return databaseName; }
647                 }
648
649                 public string XmlSchemaCollectionName {
650                         get { return objectName; }
651                 }
652
653                 public string XmlSchemaCollectionOwningSchema {
654                         get { return owningSchema; }
655                 }
656
657                 [MonoTODO]
658                 public string TypeName {
659                         get { throw new NotImplementedException (); }
660                 }
661
662                 #endregion // Properties
663
664                 #region Methods
665
666                 public bool Adjust (bool value)
667                 {
668                         if (type != typeof (bool))
669                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
670                         return value;
671                 }
672
673                 public byte Adjust (byte value)
674                 {
675                         if (type != typeof (byte))
676                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
677                         return value;
678                 }
679
680                 public byte[] Adjust (byte[] value)
681                 {
682                         if (type != typeof (byte []))
683                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
684                         return value;
685                 }
686
687                 public char Adjust (char value)
688                 {
689                         if (type != typeof (char))
690                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
691                         return value;
692                 }
693
694                 public char[] Adjust (char[] value)
695                 {
696                         if (type != typeof (char []))
697                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
698                         return value;
699                 }
700
701                 public DateTime Adjust (DateTime value)
702                 {
703                         if (type != typeof (DateTime))
704                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
705                         return value;
706                 }
707
708                 public decimal Adjust (decimal value)
709                 {
710                         if (type != typeof (decimal))
711                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
712                         return value;
713                 }
714
715                 public double Adjust (double value)
716                 {
717                         if (type != typeof (double))
718                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
719                         return value;
720                 }
721
722                 public Guid Adjust (Guid value)
723                 {
724                         if (type != typeof (Guid))
725                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
726                         return value;
727                 }
728
729                 public short Adjust (short value)
730                 {
731                         if (type != typeof (short))
732                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
733                         return value;
734                 }
735
736                 public int Adjust (int value)
737                 {
738                         if (type != typeof (int))
739                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
740                         return value;
741                 }
742
743                 public long Adjust (long value)
744                 {
745                         if (type != typeof (long))
746                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
747                         return value;
748                 }
749
750                 public object Adjust (object value)
751                 {
752                         if (type != typeof (object))
753                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
754                         return value;
755                 }
756
757                 public float Adjust (float value)
758                 {
759                         if (type != typeof (float))
760                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
761                         return value;
762                 }
763
764                 public SqlBinary Adjust (SqlBinary value)
765                 {
766                         if (type != typeof (byte []))
767                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
768                         return value;
769                 }
770
771                 public SqlBoolean Adjust (SqlBoolean value)
772                 {
773                         if (type != typeof (bool))
774                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
775                         return value;
776                 }
777
778                 public SqlByte Adjust (SqlByte value)
779                 {
780                         if (type != typeof (byte))
781                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
782                         return value;
783                 }
784
785                 public SqlBytes Adjust (SqlBytes value)
786                 {
787                         if (type != typeof (byte []))
788                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
789                         return value;
790                 }
791
792                 public SqlChars Adjust (SqlChars value)
793                 {
794                         if (type != typeof (char []))
795                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
796                         return value;
797                 }
798
799                 public SqlDateTime Adjust (SqlDateTime value)
800                 {
801                         if (type != typeof (DateTime))
802                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
803                         return value;
804                 }
805
806                 public SqlDecimal Adjust (SqlDecimal value)
807                 {
808                         if (type != typeof (decimal))
809                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
810                         return value;
811                 }
812
813                 public SqlDouble Adjust (SqlDouble value)
814                 {
815                         if (type != typeof (double))
816                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
817                         return value;
818                 }
819
820                 public SqlGuid Adjust (SqlGuid value)
821                 {
822                         if (type != typeof (Guid))
823                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
824                         return value;
825                 }
826
827                 public SqlInt16 Adjust (SqlInt16 value)
828                 {
829                         if (type != typeof (short))
830                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
831                         return value;
832                 }
833
834                 public SqlInt32 Adjust (SqlInt32 value)
835                 {
836                         if (type != typeof (int))
837                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
838                         return value;
839                 }
840
841                 public SqlInt64 Adjust (SqlInt64 value)
842                 {
843                         if (type != typeof (long))
844                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
845                         return value;
846                 }
847
848                 public SqlMoney Adjust (SqlMoney value)
849                 {
850                         if (type != typeof (decimal))
851                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
852                         return value;
853                 }
854
855                 public SqlSingle Adjust (SqlSingle value)
856                 {
857                         if (type != typeof (Single))
858                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
859                         return value;
860                 }
861
862                 public SqlString Adjust (SqlString value)
863                 {
864                         if (type != typeof (string))
865                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
866                         return value;
867                 }
868
869                 public string Adjust (string value)
870                 {
871                         if (type != typeof (string))
872                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
873                         return value;
874                 }
875
876                 public static SqlMetaData InferFromValue (object value, string name)
877                 {
878                         if (name == null)
879                                 throw new ArgumentNullException ("name can not be null");
880                         if (value == null)
881                                 throw new ArgumentException ("value can not be null");
882                         SqlMetaData sqlMetaData = null;
883                         switch (value.GetType ().ToString ()) {
884                         case "System.Boolean":
885                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Bit);
886                                 break;
887                         case "System.Byte":
888                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Binary);
889                                 break;
890                         case "System.Byte[]":
891                                 sqlMetaData = new SqlMetaData (name, SqlDbType.VarBinary);
892                                 break;
893                         case "System.Char":
894                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Char);
895                                 break;
896                         case "System.Char[]":
897                                 sqlMetaData = new SqlMetaData (name, SqlDbType.VarChar);
898                                 break;
899                         case "System.DateTime":
900                                 sqlMetaData = new SqlMetaData (name, SqlDbType.DateTime);
901                                 break;
902                         case "System.Decimal":
903                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Decimal);
904                                 break;
905                         case "System.Double":
906                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Float);
907                                 break;
908                         case "System.Guid":
909                                 sqlMetaData = new SqlMetaData (name, SqlDbType.UniqueIdentifier);
910                                 break;
911                         case "System.Int16":
912                                 sqlMetaData = new SqlMetaData (name, SqlDbType.SmallInt);
913                                 break;
914                         case "System.Int32":
915                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Int);
916                                 break;
917                         case "System.Int64":
918                                 sqlMetaData = new SqlMetaData (name, SqlDbType.BigInt);
919                                 break;
920                         case "System.Single":
921                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Real);
922                                 break;
923                         case "System.String":
924                                 sqlMetaData = new SqlMetaData (name, SqlDbType.NVarChar);
925                                 break;
926                         case "System.Object":
927                         default:
928                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Variant);
929                                 break;
930                         }
931                         return sqlMetaData;
932                 }
933
934                 #endregion // Methods
935         }
936 }
937