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.
36 using System.Threading;
37 using System.Data.SqlTypes;
39 namespace Microsoft.SqlServer.Server {
40 public sealed class SqlMetaData
44 SqlCompareOptions compareOptions = SqlCompareOptions.None;
45 string databaseName = null;
51 string owningSchema = null;
52 string objectName = null;
53 SqlDbType _sqlDbType = SqlDbType.NVarChar;
54 DbType _dbType = DbType.String;
55 Type type = typeof (string);
61 public SqlMetaData (string name, SqlDbType dbType)
64 throw new ArgumentNullException ("name can not be null");
71 compareOptions = SqlCompareOptions.None;
72 _dbType = DbType.Boolean;
75 case SqlDbType.BigInt:
80 compareOptions = SqlCompareOptions.None;
81 _dbType = DbType.Int64;
84 case SqlDbType.DateTime:
89 compareOptions = SqlCompareOptions.None;
90 _dbType = DbType.DateTime;
91 type = typeof (DateTime);
93 case SqlDbType.Decimal:
98 compareOptions = SqlCompareOptions.None;
99 _dbType = DbType.Decimal;
100 type = typeof (decimal);
102 case SqlDbType.Float:
107 compareOptions = SqlCompareOptions.None;
108 _dbType = DbType.Double;
109 type = typeof (float);
116 compareOptions = SqlCompareOptions.None;
117 _dbType = DbType.Int32;
120 case SqlDbType.Money:
125 compareOptions = SqlCompareOptions.None;
126 _dbType = DbType.Currency;
127 type = typeof (double);
130 case SqlDbType.Numeric:
135 compareOptions = SqlCompareOptions.None;
138 case SqlDbType.SmallDateTime:
143 compareOptions = SqlCompareOptions.None;
144 _dbType = DbType.DateTime;
145 type = typeof (DateTime);
147 case SqlDbType.SmallInt:
152 compareOptions = SqlCompareOptions.None;
153 _dbType = DbType.Int16;
154 type = typeof (short);
156 case SqlDbType.SmallMoney:
161 compareOptions = SqlCompareOptions.None;
162 _dbType = DbType.Currency;
163 type = typeof (double);
165 case SqlDbType.Timestamp:
170 compareOptions = SqlCompareOptions.None;
171 _dbType = DbType.DateTime;
172 type = typeof (DateTime);
174 case SqlDbType.TinyInt:
179 compareOptions = SqlCompareOptions.None;
180 _dbType = DbType.Int16;
181 type = typeof (short);
183 case SqlDbType.UniqueIdentifier:
188 compareOptions = SqlCompareOptions.None;
189 _dbType = DbType.Guid;
190 type = typeof (Guid);
197 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
198 _dbType = DbType.Xml;
199 type = typeof (string);
202 throw new ArgumentException ("SqlDbType not supported");
205 this._sqlDbType = dbType;
208 public SqlMetaData (string name, SqlDbType dbType, long maxLength)
211 throw new ArgumentNullException ("name can not be null");
213 case SqlDbType.Binary:
214 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
215 _dbType = DbType.Binary;
216 type = typeof (byte []);
219 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
220 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
221 _dbType = DbType.AnsiStringFixedLength;
222 type = typeof (string);
224 case SqlDbType.Image:
229 compareOptions = SqlCompareOptions.None;
230 _dbType = DbType.Binary;
231 type = typeof (byte []);
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);
239 case SqlDbType.NText:
243 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
244 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
245 _dbType = DbType.String;
246 type = typeof (string);
248 case SqlDbType.NVarChar:
250 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
251 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
252 _dbType = DbType.String;
253 type = typeof (string);
259 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
260 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
261 _dbType = DbType.String;
262 type = typeof (char []);
264 case SqlDbType.VarBinary:
266 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
267 _dbType = DbType.Binary;
268 type = typeof (byte []);
270 case SqlDbType.VarChar:
272 _localeId = Thread.CurrentThread.CurrentCulture.LCID;
273 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
274 _dbType = DbType.String;
275 type = typeof (char []);
278 throw new ArgumentException ("SqlDbType not supported");
280 this.maxLength = maxLength;
282 this._sqlDbType = dbType;
286 public SqlMetaData (string name, SqlDbType dbType, Type userDefinedType)
289 throw new ArgumentNullException ("name can not be null");
296 compareOptions = SqlCompareOptions.None;
297 _dbType = DbType.Guid;
298 type = typeof (Guid);
301 throw new ArgumentException ("SqlDbType not supported");
305 //this.sqlDbType = userDefinedType;
306 throw new NotImplementedException ();
309 public SqlMetaData (string name, SqlDbType dbType, byte precision, byte scale)
312 throw new ArgumentNullException ("name can not be null");
314 case SqlDbType.Decimal:
316 this.precision = precision;
319 compareOptions = SqlCompareOptions.None;
320 _dbType = DbType.Decimal;
321 type = typeof (decimal);
324 throw new ArgumentException ("SqlDbType not supported");
327 this._sqlDbType = dbType;
330 public SqlMetaData (string name, SqlDbType dbType, long maxLength, long locale, SqlCompareOptions compareOptions)
333 throw new ArgumentNullException ("name can not be null");
336 _dbType = DbType.AnsiStringFixedLength;
337 type = typeof (char []);
339 case SqlDbType.NChar:
340 _dbType = DbType.StringFixedLength;
341 type = typeof (char []);
343 case SqlDbType.NText:
344 case SqlDbType.NVarChar:
345 _dbType = DbType.String;
346 type = typeof (string);
349 case SqlDbType.VarChar:
350 _dbType = DbType.AnsiString;
351 type = typeof (char []);
354 throw new ArgumentException ("SqlDbType not supported");
356 this.compareOptions = compareOptions;
357 this._localeId = locale;
358 this.maxLength = maxLength;
360 this._sqlDbType = dbType;
363 public SqlMetaData (string name, SqlDbType dbType, string database, string owningSchema, string objectName)
365 if ((name == null || objectName == null) && database != null && owningSchema != null)
366 throw new ArgumentNullException ("name can not be null");
373 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
374 _dbType = DbType.String;
375 type = typeof (string);
378 throw new ArgumentException ("SqlDbType not supported");
381 this._sqlDbType = dbType;
382 databaseName = database;
383 this.owningSchema = owningSchema;
384 this.objectName = objectName;
387 public SqlMetaData (string name, SqlDbType dbType, long maxLength, byte precision,
388 byte scale, long locale, SqlCompareOptions compareOptions,
389 Type userDefinedType)
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;
404 compareOptions = SqlCompareOptions.None;
405 _dbType = DbType.Boolean;
406 type = typeof (bool);
408 case SqlDbType.BigInt:
413 compareOptions = SqlCompareOptions.None;
414 _dbType = DbType.Int64;
415 type = typeof (long);
417 case SqlDbType.DateTime:
422 compareOptions = SqlCompareOptions.None;
423 _dbType = DbType.DateTime;
424 type = typeof (DateTime);
426 case SqlDbType.Decimal:
431 compareOptions = SqlCompareOptions.None;
432 _dbType = DbType.Decimal;
433 type = typeof (decimal);
435 case SqlDbType.Float:
440 compareOptions = SqlCompareOptions.None;
441 _dbType = DbType.Decimal;
442 type = typeof (float);
444 case SqlDbType.Image:
449 compareOptions = SqlCompareOptions.None;
450 _dbType = DbType.Binary;
451 type = typeof (byte []);
458 compareOptions = SqlCompareOptions.None;
459 _dbType = DbType.Int32;
462 case SqlDbType.Money:
467 compareOptions = SqlCompareOptions.None;
468 _dbType = DbType.Currency;
469 type = typeof (decimal);
471 case SqlDbType.NText:
475 locale = Thread.CurrentThread.CurrentCulture.LCID;
476 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
477 _dbType = DbType.String;
478 type = typeof (string);
481 case SqlDbType.Numeric:
486 compareOptions = SqlCompareOptions.None;
494 compareOptions = SqlCompareOptions.None;
495 _dbType = DbType.Single;
496 type = typeof (Single);
498 case SqlDbType.SmallDateTime:
503 compareOptions = SqlCompareOptions.None;
504 _dbType = DbType.DateTime;
505 type = typeof (DateTime);
507 case SqlDbType.SmallInt:
512 compareOptions = SqlCompareOptions.None;
513 _dbType = DbType.Int16;
514 type = typeof (short);
516 case SqlDbType.SmallMoney:
521 compareOptions = SqlCompareOptions.None;
522 _dbType = DbType.Currency;
523 type = typeof (decimal);
529 locale = Thread.CurrentThread.CurrentCulture.LCID;
530 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
531 _dbType = DbType.AnsiString;
532 type = typeof (char []);
534 case SqlDbType.Timestamp:
539 compareOptions = SqlCompareOptions.None;
540 _dbType = DbType.Byte;
541 type = typeof (byte []);
543 case SqlDbType.TinyInt:
548 compareOptions = SqlCompareOptions.None;
549 _dbType = DbType.Int16;
550 type = typeof (short);
552 case SqlDbType.UniqueIdentifier:
557 compareOptions = SqlCompareOptions.None;
558 _dbType = DbType.Guid;
559 type = typeof (Guid);
566 compareOptions = SqlCompareOptions.None;
567 _dbType = DbType.Object;
568 type = typeof (object);
570 case SqlDbType.Variant:
575 compareOptions = SqlCompareOptions.None;
576 _dbType = DbType.Object;
577 type = typeof (object);
584 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
585 _dbType = DbType.Xml;
586 type = typeof (string);
590 if (typeof (DbType.Row) == typeof (userDefinedType)) {
592 // maxLength = Number of columns;
596 compareOptions = SqlCompareOptions.None;
599 throw new ArgumentException ("SqlDbType not supported");
602 this._sqlDbType = dbType;
605 #endregion // Constructors
609 public SqlCompareOptions CompareOptions {
610 get { return compareOptions; }
613 public DbType DbType {
614 get { return _dbType; }
617 public long LocaleId {
618 get { return _localeId; }
621 public static long Max {
625 public long MaxLength {
626 get { return maxLength; }
633 public byte Precision {
634 get { return precision; }
638 get { return scale; }
641 public SqlDbType SqlDbType {
642 get { return _sqlDbType; }
645 public string XmlSchemaCollectionDatabase {
646 get { return databaseName; }
649 public string XmlSchemaCollectionName {
650 get { return objectName; }
653 public string XmlSchemaCollectionOwningSchema {
654 get { return owningSchema; }
658 public string TypeName {
659 get { throw new NotImplementedException (); }
662 #endregion // Properties
666 public bool Adjust (bool value)
668 if (type != typeof (bool))
669 throw new ArgumentException ("Value does not match the SqlMetaData type");
673 public byte Adjust (byte value)
675 if (type != typeof (byte))
676 throw new ArgumentException ("Value does not match the SqlMetaData type");
680 public byte[] Adjust (byte[] value)
682 if (type != typeof (byte []))
683 throw new ArgumentException ("Value does not match the SqlMetaData type");
687 public char Adjust (char value)
689 if (type != typeof (char))
690 throw new ArgumentException ("Value does not match the SqlMetaData type");
694 public char[] Adjust (char[] value)
696 if (type != typeof (char []))
697 throw new ArgumentException ("Value does not match the SqlMetaData type");
701 public DateTime Adjust (DateTime value)
703 if (type != typeof (DateTime))
704 throw new ArgumentException ("Value does not match the SqlMetaData type");
708 public decimal Adjust (decimal value)
710 if (type != typeof (decimal))
711 throw new ArgumentException ("Value does not match the SqlMetaData type");
715 public double Adjust (double value)
717 if (type != typeof (double))
718 throw new ArgumentException ("Value does not match the SqlMetaData type");
722 public Guid Adjust (Guid value)
724 if (type != typeof (Guid))
725 throw new ArgumentException ("Value does not match the SqlMetaData type");
729 public short Adjust (short value)
731 if (type != typeof (short))
732 throw new ArgumentException ("Value does not match the SqlMetaData type");
736 public int Adjust (int value)
738 if (type != typeof (int))
739 throw new ArgumentException ("Value does not match the SqlMetaData type");
743 public long Adjust (long value)
745 if (type != typeof (long))
746 throw new ArgumentException ("Value does not match the SqlMetaData type");
750 public object Adjust (object value)
752 if (type != typeof (object))
753 throw new ArgumentException ("Value does not match the SqlMetaData type");
757 public float Adjust (float value)
759 if (type != typeof (float))
760 throw new ArgumentException ("Value does not match the SqlMetaData type");
764 public SqlBinary Adjust (SqlBinary value)
766 if (type != typeof (byte []))
767 throw new ArgumentException ("Value does not match the SqlMetaData type");
771 public SqlBoolean Adjust (SqlBoolean value)
773 if (type != typeof (bool))
774 throw new ArgumentException ("Value does not match the SqlMetaData type");
778 public SqlByte Adjust (SqlByte value)
780 if (type != typeof (byte))
781 throw new ArgumentException ("Value does not match the SqlMetaData type");
785 public SqlBytes Adjust (SqlBytes value)
787 if (type != typeof (byte []))
788 throw new ArgumentException ("Value does not match the SqlMetaData type");
792 public SqlChars Adjust (SqlChars value)
794 if (type != typeof (char []))
795 throw new ArgumentException ("Value does not match the SqlMetaData type");
799 public SqlDateTime Adjust (SqlDateTime value)
801 if (type != typeof (DateTime))
802 throw new ArgumentException ("Value does not match the SqlMetaData type");
806 public SqlDecimal Adjust (SqlDecimal value)
808 if (type != typeof (decimal))
809 throw new ArgumentException ("Value does not match the SqlMetaData type");
813 public SqlDouble Adjust (SqlDouble value)
815 if (type != typeof (double))
816 throw new ArgumentException ("Value does not match the SqlMetaData type");
820 public SqlGuid Adjust (SqlGuid value)
822 if (type != typeof (Guid))
823 throw new ArgumentException ("Value does not match the SqlMetaData type");
827 public SqlInt16 Adjust (SqlInt16 value)
829 if (type != typeof (short))
830 throw new ArgumentException ("Value does not match the SqlMetaData type");
834 public SqlInt32 Adjust (SqlInt32 value)
836 if (type != typeof (int))
837 throw new ArgumentException ("Value does not match the SqlMetaData type");
841 public SqlInt64 Adjust (SqlInt64 value)
843 if (type != typeof (long))
844 throw new ArgumentException ("Value does not match the SqlMetaData type");
848 public SqlMoney Adjust (SqlMoney value)
850 if (type != typeof (decimal))
851 throw new ArgumentException ("Value does not match the SqlMetaData type");
855 public SqlSingle Adjust (SqlSingle value)
857 if (type != typeof (Single))
858 throw new ArgumentException ("Value does not match the SqlMetaData type");
862 public SqlString Adjust (SqlString value)
864 if (type != typeof (string))
865 throw new ArgumentException ("Value does not match the SqlMetaData type");
869 public string Adjust (string value)
871 if (type != typeof (string))
872 throw new ArgumentException ("Value does not match the SqlMetaData type");
876 public static SqlMetaData InferFromValue (object value, string name)
879 throw new ArgumentNullException ("name can not be 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);
888 sqlMetaData = new SqlMetaData (name, SqlDbType.Binary);
890 case "System.Byte[]":
891 sqlMetaData = new SqlMetaData (name, SqlDbType.VarBinary);
894 sqlMetaData = new SqlMetaData (name, SqlDbType.Char);
896 case "System.Char[]":
897 sqlMetaData = new SqlMetaData (name, SqlDbType.VarChar);
899 case "System.DateTime":
900 sqlMetaData = new SqlMetaData (name, SqlDbType.DateTime);
902 case "System.Decimal":
903 sqlMetaData = new SqlMetaData (name, SqlDbType.Decimal);
905 case "System.Double":
906 sqlMetaData = new SqlMetaData (name, SqlDbType.Float);
909 sqlMetaData = new SqlMetaData (name, SqlDbType.UniqueIdentifier);
912 sqlMetaData = new SqlMetaData (name, SqlDbType.SmallInt);
915 sqlMetaData = new SqlMetaData (name, SqlDbType.Int);
918 sqlMetaData = new SqlMetaData (name, SqlDbType.BigInt);
920 case "System.Single":
921 sqlMetaData = new SqlMetaData (name, SqlDbType.Real);
923 case "System.String":
924 sqlMetaData = new SqlMetaData (name, SqlDbType.NVarChar);
926 case "System.Object":
928 sqlMetaData = new SqlMetaData (name, SqlDbType.Variant);
934 #endregion // Methods