2 // Microsoft.SqlServer.Server.SqlMetaData
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2003
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
37 using System.Threading;
38 using System.Data.SqlTypes;
40 namespace Microsoft.SqlServer.Server {
41 public sealed class SqlMetaData
45 SqlCompareOptions compareOptions = SqlCompareOptions.None;
46 string databaseName = null;
52 string owningSchema = null;
53 string objectName = null;
54 SqlDbType sqlDbType = SqlDbType.NVarChar;
55 DbType dbType = DbType.String;
56 Type type = typeof (string);
62 public SqlMetaData (string name, SqlDbType sqlDbType)
65 throw new ArgumentNullException ("name can not be null");
72 compareOptions = SqlCompareOptions.None;
73 dbType = DbType.Boolean;
76 case SqlDbType.BigInt:
81 compareOptions = SqlCompareOptions.None;
82 dbType = DbType.Int64;
85 case SqlDbType.DateTime:
90 compareOptions = SqlCompareOptions.None;
91 dbType = DbType.DateTime;
92 type = typeof (DateTime);
94 case SqlDbType.Decimal:
99 compareOptions = SqlCompareOptions.None;
100 dbType = DbType.Decimal;
101 type = typeof (decimal);
103 case SqlDbType.Float:
108 compareOptions = SqlCompareOptions.None;
109 dbType = DbType.Double;
110 type = typeof (float);
117 compareOptions = SqlCompareOptions.None;
118 dbType = DbType.Int32;
121 case SqlDbType.Money:
126 compareOptions = SqlCompareOptions.None;
127 dbType = DbType.Currency;
128 type = typeof (double);
131 case SqlDbType.Numeric:
136 compareOptions = SqlCompareOptions.None;
139 case SqlDbType.SmallDateTime:
144 compareOptions = SqlCompareOptions.None;
145 dbType = DbType.DateTime;
146 type = typeof (DateTime);
148 case SqlDbType.SmallInt:
153 compareOptions = SqlCompareOptions.None;
154 dbType = DbType.Int16;
155 type = typeof (short);
157 case SqlDbType.SmallMoney:
162 compareOptions = SqlCompareOptions.None;
163 dbType = DbType.Currency;
164 type = typeof (double);
166 case SqlDbType.Timestamp:
171 compareOptions = SqlCompareOptions.None;
172 dbType = DbType.DateTime;
173 type = typeof (DateTime);
175 case SqlDbType.TinyInt:
180 compareOptions = SqlCompareOptions.None;
181 dbType = DbType.Int16;
182 type = typeof (short);
184 case SqlDbType.UniqueIdentifier:
189 compareOptions = SqlCompareOptions.None;
190 dbType = DbType.Guid;
191 type = typeof (Guid);
198 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
200 type = typeof (string);
203 throw new ArgumentException ("SqlDbType not supported");
206 this.sqlDbType = sqlDbType;
209 public SqlMetaData (string name, SqlDbType sqlDbType, long maxLength)
212 throw new ArgumentNullException ("name can not be null");
214 case SqlDbType.Binary:
215 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
216 dbType = DbType.Binary;
217 type = typeof (byte []);
220 localeId = Thread.CurrentThread.CurrentCulture.LCID;
221 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
222 dbType = DbType.AnsiStringFixedLength;
223 type = typeof (string);
225 case SqlDbType.Image:
230 compareOptions = SqlCompareOptions.None;
231 dbType = DbType.Binary;
232 type = typeof (byte []);
234 case SqlDbType.NChar:
235 localeId = Thread.CurrentThread.CurrentCulture.LCID;
236 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
237 dbType = DbType.String;
238 type = typeof (string);
240 case SqlDbType.NText:
244 localeId = Thread.CurrentThread.CurrentCulture.LCID;
245 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
246 dbType = DbType.String;
247 type = typeof (string);
249 case SqlDbType.NVarChar:
251 localeId = Thread.CurrentThread.CurrentCulture.LCID;
252 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
253 dbType = DbType.String;
254 type = typeof (string);
260 localeId = Thread.CurrentThread.CurrentCulture.LCID;
261 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
262 dbType = DbType.String;
263 type = typeof (char []);
265 case SqlDbType.VarBinary:
267 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
268 dbType = DbType.Binary;
269 type = typeof (byte []);
271 case SqlDbType.VarChar:
273 localeId = Thread.CurrentThread.CurrentCulture.LCID;
274 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
275 dbType = DbType.String;
276 type = typeof (char []);
279 throw new ArgumentException ("SqlDbType not supported");
281 this.maxLength = maxLength;
283 this.sqlDbType = sqlDbType;
287 public SqlMetaData (string name, SqlDbType sqlDbType, Type userDefinedType)
290 throw new ArgumentNullException ("name can not be null");
297 compareOptions = SqlCompareOptions.None;
298 dbType = DbType.Guid;
299 type = typeof (Guid);
302 throw new ArgumentException ("SqlDbType not supported");
306 //this.sqlDbType = userDefinedType;
307 throw new NotImplementedException ();
310 public SqlMetaData (string name, SqlDbType sqlDbType, byte precision, byte scale)
313 throw new ArgumentNullException ("name can not be null");
315 case SqlDbType.Decimal:
317 this.precision = precision;
320 compareOptions = SqlCompareOptions.None;
321 dbType = DbType.Decimal;
322 type = typeof (decimal);
325 throw new ArgumentException ("SqlDbType not supported");
328 this.sqlDbType = sqlDbType;
331 public SqlMetaData (string name, SqlDbType sqlDbType, long maxLength, long locale, SqlCompareOptions compareOptions)
334 throw new ArgumentNullException ("name can not be null");
337 dbType = DbType.AnsiStringFixedLength;
338 type = typeof (char []);
340 case SqlDbType.NChar:
341 dbType = DbType.StringFixedLength;
342 type = typeof (char []);
344 case SqlDbType.NText:
345 case SqlDbType.NVarChar:
346 dbType = DbType.String;
347 type = typeof (string);
350 case SqlDbType.VarChar:
351 dbType = DbType.AnsiString;
352 type = typeof (char []);
355 throw new ArgumentException ("SqlDbType not supported");
357 this.compareOptions = compareOptions;
358 this.localeId = locale;
359 this.maxLength = maxLength;
361 this.sqlDbType = sqlDbType;
364 public SqlMetaData (string name, SqlDbType sqlDbType, string database, string owningSchema, string objectName)
366 if ((name == null || objectName == null) && database != null && owningSchema != null)
367 throw new ArgumentNullException ("name can not be null");
374 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
375 dbType = DbType.String;
376 type = typeof (string);
379 throw new ArgumentException ("SqlDbType not supported");
382 this.sqlDbType = sqlDbType;
383 databaseName = database;
384 this.owningSchema = owningSchema;
385 this.objectName = objectName;
388 public SqlMetaData (string name, SqlDbType sqlDbType, long maxLength, byte precision,
389 byte scale, long localeId, SqlCompareOptions compareOptions,
390 Type userDefinedType)
393 throw new ArgumentNullException ("name can not be null");
394 this.compareOptions = compareOptions;
395 this.localeId = localeId;
396 this.maxLength = maxLength;
397 this.precision = precision;
405 compareOptions = SqlCompareOptions.None;
406 dbType = DbType.Boolean;
407 type = typeof (bool);
409 case SqlDbType.BigInt:
414 compareOptions = SqlCompareOptions.None;
415 dbType = DbType.Int64;
416 type = typeof (long);
418 case SqlDbType.DateTime:
423 compareOptions = SqlCompareOptions.None;
424 dbType = DbType.DateTime;
425 type = typeof (DateTime);
427 case SqlDbType.Decimal:
432 compareOptions = SqlCompareOptions.None;
433 dbType = DbType.Decimal;
434 type = typeof (decimal);
436 case SqlDbType.Float:
441 compareOptions = SqlCompareOptions.None;
442 dbType = DbType.Decimal;
443 type = typeof (float);
445 case SqlDbType.Image:
450 compareOptions = SqlCompareOptions.None;
451 dbType = DbType.Binary;
452 type = typeof (byte []);
459 compareOptions = SqlCompareOptions.None;
460 dbType = DbType.Int32;
463 case SqlDbType.Money:
468 compareOptions = SqlCompareOptions.None;
469 dbType = DbType.Currency;
470 type = typeof (decimal);
472 case SqlDbType.NText:
476 localeId = Thread.CurrentThread.CurrentCulture.LCID;
477 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
478 dbType = DbType.String;
479 type = typeof (string);
482 case SqlDbType.Numeric:
487 compareOptions = SqlCompareOptions.None;
495 compareOptions = SqlCompareOptions.None;
496 dbType = DbType.Single;
497 type = typeof (Single);
499 case SqlDbType.SmallDateTime:
504 compareOptions = SqlCompareOptions.None;
505 dbType = DbType.DateTime;
506 type = typeof (DateTime);
508 case SqlDbType.SmallInt:
513 compareOptions = SqlCompareOptions.None;
514 dbType = DbType.Int16;
515 type = typeof (short);
517 case SqlDbType.SmallMoney:
522 compareOptions = SqlCompareOptions.None;
523 dbType = DbType.Currency;
524 type = typeof (decimal);
530 localeId = Thread.CurrentThread.CurrentCulture.LCID;
531 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
532 dbType = DbType.AnsiString;
533 type = typeof (char []);
535 case SqlDbType.Timestamp:
540 compareOptions = SqlCompareOptions.None;
541 dbType = DbType.Byte;
542 type = typeof (byte []);
544 case SqlDbType.TinyInt:
549 compareOptions = SqlCompareOptions.None;
550 dbType = DbType.Int16;
551 type = typeof (short);
553 case SqlDbType.UniqueIdentifier:
558 compareOptions = SqlCompareOptions.None;
559 dbType = DbType.Guid;
560 type = typeof (Guid);
567 compareOptions = SqlCompareOptions.None;
568 dbType = DbType.Object;
569 type = typeof (object);
571 case SqlDbType.Variant:
576 compareOptions = SqlCompareOptions.None;
577 dbType = DbType.Object;
578 type = typeof (object);
585 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
587 type = typeof (string);
591 if (typeof (DbType.Row) == typeof (userDefinedType)) {
593 // maxLength = Number of columns;
597 compareOptions = SqlCompareOptions.None;
600 throw new ArgumentException ("SqlDbType not supported");
603 this.sqlDbType = sqlDbType;
606 #endregion // Constructors
610 public SqlCompareOptions CompareOptions {
611 get { return compareOptions; }
614 public DbType DbType {
615 get { return dbType; }
618 public long LocaleId {
619 get { return localeId; }
622 public static long Max {
626 public long MaxLength {
627 get { return maxLength; }
634 public byte Precision {
635 get { return precision; }
639 get { return scale; }
642 public SqlDbType SqlDbType {
643 get { return sqlDbType; }
646 public string XmlSchemaCollectionDatabase {
647 get { return databaseName; }
650 public string XmlSchemaCollectionName {
651 get { return objectName; }
654 public string XmlSchemaCollectionOwningSchema {
655 get { return owningSchema; }
659 public string TypeName {
660 get { throw new NotImplementedException (); }
663 #endregion // Properties
667 public bool Adjust (bool value)
669 if (type != typeof (bool))
670 throw new ArgumentException ("Value does not match the SqlMetaData type");
674 public byte Adjust (byte value)
676 if (type != typeof (byte))
677 throw new ArgumentException ("Value does not match the SqlMetaData type");
681 public byte[] Adjust (byte[] value)
683 if (type != typeof (byte []))
684 throw new ArgumentException ("Value does not match the SqlMetaData type");
688 public char Adjust (char value)
690 if (type != typeof (char))
691 throw new ArgumentException ("Value does not match the SqlMetaData type");
695 public char[] Adjust (char[] value)
697 if (type != typeof (char []))
698 throw new ArgumentException ("Value does not match the SqlMetaData type");
702 public DateTime Adjust (DateTime value)
704 if (type != typeof (DateTime))
705 throw new ArgumentException ("Value does not match the SqlMetaData type");
709 public decimal Adjust (decimal value)
711 if (type != typeof (decimal))
712 throw new ArgumentException ("Value does not match the SqlMetaData type");
716 public double Adjust (double value)
718 if (type != typeof (double))
719 throw new ArgumentException ("Value does not match the SqlMetaData type");
723 public Guid Adjust (Guid value)
725 if (type != typeof (Guid))
726 throw new ArgumentException ("Value does not match the SqlMetaData type");
730 public short Adjust (short value)
732 if (type != typeof (short))
733 throw new ArgumentException ("Value does not match the SqlMetaData type");
737 public int Adjust (int value)
739 if (type != typeof (int))
740 throw new ArgumentException ("Value does not match the SqlMetaData type");
744 public long Adjust (long value)
746 if (type != typeof (long))
747 throw new ArgumentException ("Value does not match the SqlMetaData type");
751 public object Adjust (object value)
753 if (type != typeof (object))
754 throw new ArgumentException ("Value does not match the SqlMetaData type");
758 public float Adjust (float value)
760 if (type != typeof (float))
761 throw new ArgumentException ("Value does not match the SqlMetaData type");
765 public SqlBinary Adjust (SqlBinary value)
767 if (type != typeof (byte []))
768 throw new ArgumentException ("Value does not match the SqlMetaData type");
772 public SqlBoolean Adjust (SqlBoolean value)
774 if (type != typeof (bool))
775 throw new ArgumentException ("Value does not match the SqlMetaData type");
779 public SqlByte Adjust (SqlByte value)
781 if (type != typeof (byte))
782 throw new ArgumentException ("Value does not match the SqlMetaData type");
786 public SqlBytes Adjust (SqlBytes value)
788 if (type != typeof (byte []))
789 throw new ArgumentException ("Value does not match the SqlMetaData type");
793 public SqlChars Adjust (SqlChars value)
795 if (type != typeof (char []))
796 throw new ArgumentException ("Value does not match the SqlMetaData type");
800 public SqlDateTime Adjust (SqlDateTime value)
802 if (type != typeof (DateTime))
803 throw new ArgumentException ("Value does not match the SqlMetaData type");
807 public SqlDecimal Adjust (SqlDecimal value)
809 if (type != typeof (decimal))
810 throw new ArgumentException ("Value does not match the SqlMetaData type");
814 public SqlDouble Adjust (SqlDouble value)
816 if (type != typeof (double))
817 throw new ArgumentException ("Value does not match the SqlMetaData type");
821 public SqlGuid Adjust (SqlGuid value)
823 if (type != typeof (Guid))
824 throw new ArgumentException ("Value does not match the SqlMetaData type");
828 public SqlInt16 Adjust (SqlInt16 value)
830 if (type != typeof (short))
831 throw new ArgumentException ("Value does not match the SqlMetaData type");
835 public SqlInt32 Adjust (SqlInt32 value)
837 if (type != typeof (int))
838 throw new ArgumentException ("Value does not match the SqlMetaData type");
842 public SqlInt64 Adjust (SqlInt64 value)
844 if (type != typeof (long))
845 throw new ArgumentException ("Value does not match the SqlMetaData type");
849 public SqlMoney Adjust (SqlMoney value)
851 if (type != typeof (decimal))
852 throw new ArgumentException ("Value does not match the SqlMetaData type");
856 public SqlSingle Adjust (SqlSingle value)
858 if (type != typeof (Single))
859 throw new ArgumentException ("Value does not match the SqlMetaData type");
863 public SqlString Adjust (SqlString value)
865 if (type != typeof (string))
866 throw new ArgumentException ("Value does not match the SqlMetaData type");
870 public string Adjust (string value)
872 if (type != typeof (string))
873 throw new ArgumentException ("Value does not match the SqlMetaData type");
877 public static SqlMetaData InferFromValue (object value, string name)
880 throw new ArgumentNullException ("name can not be null");
882 throw new ArgumentException ("value can not be null");
883 SqlMetaData sqlMetaData = null;
884 switch (value.GetType ().ToString ()) {
885 case "System.Boolean":
886 sqlMetaData = new SqlMetaData (name, SqlDbType.Bit);
889 sqlMetaData = new SqlMetaData (name, SqlDbType.Binary);
891 case "System.Byte[]":
892 sqlMetaData = new SqlMetaData (name, SqlDbType.VarBinary);
895 sqlMetaData = new SqlMetaData (name, SqlDbType.Char);
897 case "System.Char[]":
898 sqlMetaData = new SqlMetaData (name, SqlDbType.VarChar);
900 case "System.DateTime":
901 sqlMetaData = new SqlMetaData (name, SqlDbType.DateTime);
903 case "System.Decimal":
904 sqlMetaData = new SqlMetaData (name, SqlDbType.Decimal);
906 case "System.Double":
907 sqlMetaData = new SqlMetaData (name, SqlDbType.Float);
910 sqlMetaData = new SqlMetaData (name, SqlDbType.UniqueIdentifier);
913 sqlMetaData = new SqlMetaData (name, SqlDbType.SmallInt);
916 sqlMetaData = new SqlMetaData (name, SqlDbType.Int);
919 sqlMetaData = new SqlMetaData (name, SqlDbType.BigInt);
921 case "System.Single":
922 sqlMetaData = new SqlMetaData (name, SqlDbType.Real);
924 case "System.String":
925 sqlMetaData = new SqlMetaData (name, SqlDbType.NVarChar);
927 case "System.Object":
929 sqlMetaData = new SqlMetaData (name, SqlDbType.Variant);
935 #endregion // Methods