context: //
  // System.Data.SqlClient.SqlParameter.cs
  //
  // Author:
  //   Rodrigo Moya (rodrigo@ximian.com)
  //   Daniel Morgan (danmorg@sc.rr.com)
  //   Tim Coleman (tim@timcoleman.com)
  //   Diego Caravana (diego@toth.it)
  //   Umadevi S (sumadevi@novell.com)
  //   Amit Biswas (amit@amitbiswas.com)
  //   Veerapuram Varadhan (vvaradhan@novell.com)
  //
  // (C) Ximian, Inc. 2002
  // Copyright (C) Tim Coleman, 2002
  //
  
  //
  // Copyright (C) 2004, 2008, 2009 Novell, Inc (http://www.novell.com)
  //
  // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  // 
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  // 
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  
  using Mono.Data.Tds;
  using Mono.Data.Tds.Protocol;
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Data.Common;
  using System.Data.SqlTypes;
  using System.Globalization;
  using System.Runtime.InteropServices;
  using System.Text;
  using System.Xml;
  
  namespace System.Data.SqlClient {
  	[TypeConverterAttribute ("System.Data.SqlClient.SqlParameter+SqlParameterConverter, " + Consts.AssemblySystem_Data)]
  	public sealed class SqlParameter : DbParameter, IDbDataParameter, IDataParameter, ICloneable
  	{
  #region Import from old DbParameter
  		static Hashtable dbTypeMapping = new Hashtable ();
                  internal static Hashtable DbTypeMapping {
                          get { return dbTypeMapping;}
                          set { dbTypeMapping = value;}
                  }
  
                  // LAMESPEC: Implementors should populate the dbTypeMapping accordingly
                  internal Type SystemType {
                          get {
                                  return (Type) dbTypeMapping [SqlDbType];
                          }
                  }
  #endregion
  
  		#region Fields
  
  		TdsMetaParameter metaParameter;
  
  		SqlParameterCollection container;
  		DbType dbType;
  		ParameterDirection direction = ParameterDirection.Input;
  		bool isTypeSet;
  		int offset;
  		SqlDbType sqlDbType;
  		string sourceColumn;
  		DataRowVersion sourceVersion;
  		SqlCompareOptions compareInfo;
  		int localeId;
  		Type sqlType;
  		bool typeChanged;
  		bool sourceColumnNullMapping;
  		string xmlSchemaCollectionDatabase = String.Empty;
  		string xmlSchemaCollectionOwningSchema = String.Empty;
  		string xmlSchemaCollectionName = String.Empty;
  
  		static Hashtable type_mapping;
  
  		#endregion // Fields
  
  		#region Constructors
  
  		
  		static SqlParameter ()
  		{
  			if (DbTypeMapping == null)
  				DbTypeMapping = new Hashtable ();
  			
  			DbTypeMapping.Add (SqlDbType.BigInt, typeof (long));
  			DbTypeMapping.Add (SqlDbType.Bit, typeof (bool));
  			DbTypeMapping.Add (SqlDbType.Char, typeof (string));
  			DbTypeMapping.Add (SqlDbType.NChar, typeof (string));
  			DbTypeMapping.Add (SqlDbType.Text, typeof (string));
  			DbTypeMapping.Add (SqlDbType.NText, typeof (string));
  			DbTypeMapping.Add (SqlDbType.VarChar, typeof (string));
  			DbTypeMapping.Add (SqlDbType.NVarChar, typeof (string));
  			DbTypeMapping.Add (SqlDbType.SmallDateTime, typeof (DateTime));
  			DbTypeMapping.Add (SqlDbType.DateTime, typeof (DateTime));
  			DbTypeMapping.Add (SqlDbType.DateTime2, typeof (DateTime));
  			DbTypeMapping.Add (SqlDbType.DateTimeOffset, typeof (DateTimeOffset));
  			DbTypeMapping.Add (SqlDbType.Decimal, typeof (decimal));
  			DbTypeMapping.Add (SqlDbType.Float, typeof (double));
  			DbTypeMapping.Add (SqlDbType.Binary, typeof (byte []));
  			DbTypeMapping.Add (SqlDbType.Image, typeof (byte []));
  			DbTypeMapping.Add (SqlDbType.Money, typeof (decimal));
  			DbTypeMapping.Add (SqlDbType.SmallMoney, typeof (decimal));
  			DbTypeMapping.Add (SqlDbType.VarBinary, typeof (byte []));
  			DbTypeMapping.Add (SqlDbType.TinyInt, typeof (byte));
  			DbTypeMapping.Add (SqlDbType.Int, typeof (int));
  			DbTypeMapping.Add (SqlDbType.Real, typeof (float));
  			DbTypeMapping.Add (SqlDbType.SmallInt, typeof (short));
  			DbTypeMapping.Add (SqlDbType.UniqueIdentifier, typeof (Guid));
  			DbTypeMapping.Add (SqlDbType.Variant, typeof (object));
  			DbTypeMapping.Add (SqlDbType.Xml, typeof (string));
  
  			type_mapping = new Hashtable ();
  
  			type_mapping.Add (typeof (long), SqlDbType.BigInt);
  			type_mapping.Add (typeof (SqlTypes.SqlInt64), SqlDbType.BigInt);
  
  			type_mapping.Add (typeof (bool), SqlDbType.Bit);
  			type_mapping.Add (typeof (SqlTypes.SqlBoolean), SqlDbType.Bit);
  
  			type_mapping.Add (typeof (char), SqlDbType.NVarChar);
  			type_mapping.Add (typeof (char []), SqlDbType.NVarChar);
  			type_mapping.Add (typeof (SqlTypes.SqlChars), SqlDbType.NVarChar);
  
  			type_mapping.Add (typeof (string), SqlDbType.NVarChar);
  			type_mapping.Add (typeof (SqlTypes.SqlString), SqlDbType.NVarChar);
  
  			type_mapping.Add (typeof (DateTime), SqlDbType.DateTime);
  			type_mapping.Add (typeof (SqlTypes.SqlDateTime), SqlDbType.DateTime);
  
  			type_mapping.Add (typeof (decimal), SqlDbType.Decimal);
  			type_mapping.Add (typeof (SqlTypes.SqlDecimal), SqlDbType.Decimal);
  
  			type_mapping.Add (typeof (double), SqlDbType.Float);
  			type_mapping.Add (typeof (SqlTypes.SqlDouble), SqlDbType.Float);
  
  			type_mapping.Add (typeof (byte []), SqlDbType.VarBinary);
  			type_mapping.Add (typeof (SqlTypes.SqlBinary), SqlDbType.VarBinary);
  
  			type_mapping.Add (typeof (SqlTypes.SqlBytes), SqlDbType.VarBinary);
  
  			type_mapping.Add (typeof (byte), SqlDbType.TinyInt);
  			type_mapping.Add (typeof (SqlTypes.SqlByte), SqlDbType.TinyInt);
  
  			type_mapping.Add (typeof (int), SqlDbType.Int);
  			type_mapping.Add (typeof (SqlTypes.SqlInt32), SqlDbType.Int);
  
  			type_mapping.Add (typeof (float), SqlDbType.Real);
  			type_mapping.Add (typeof (SqlTypes.SqlSingle), SqlDbType.Real);
  
  			type_mapping.Add (typeof (short), SqlDbType.SmallInt);
  			type_mapping.Add (typeof (SqlTypes.SqlInt16), SqlDbType.SmallInt);
  
  			type_mapping.Add (typeof (Guid), SqlDbType.UniqueIdentifier);
  			type_mapping.Add (typeof (SqlTypes.SqlGuid), SqlDbType.UniqueIdentifier);
  
  			type_mapping.Add (typeof (SqlTypes.SqlMoney), SqlDbType.Money);
  
  			type_mapping.Add (typeof (XmlReader), SqlDbType.Xml);
  			type_mapping.Add (typeof (SqlTypes.SqlXml), SqlDbType.Xml);
  
  			type_mapping.Add (typeof (object), SqlDbType.Variant);
  			type_mapping.Add (typeof (DateTimeOffset), SqlDbType.DateTimeOffset);
  		}
  		
  		public SqlParameter () 
  			: this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  		{
  			isTypeSet = false;
  		}
  
  		public SqlParameter (string parameterName, object value)
  		{
  			if (parameterName == null)
  				parameterName = string.Empty;
  			metaParameter = new TdsMetaParameter (parameterName, GetFrameworkValue);
  			metaParameter.RawValue = value;
  			InferSqlType (value);
  			sourceVersion = DataRowVersion.Current;
  		}
  		
  		public SqlParameter (string parameterName, SqlDbType dbType) 
  			: this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, null)
  		{
  		}
  
  		public SqlParameter (string parameterName, SqlDbType dbType, int size) 
  			: this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, null)
  		{
  		}
  		
  		public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn) 
  			: this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  		{
  		}
  		
  		[EditorBrowsable (EditorBrowsableState.Advanced)]
  		public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value) 
  		{
  			if (parameterName == null)
  				parameterName = string.Empty;
  
  			metaParameter = new TdsMetaParameter (parameterName, size, 
  							      isNullable, precision, 
  							      scale,
  							      GetFrameworkValue);
  			metaParameter.RawValue =  value;
  			SqlDbType = dbType;
  			Direction = direction;
  			SourceColumn = sourceColumn;
  			SourceVersion = sourceVersion;
  		}
  
  		public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, bool sourceColumnNullMapping, Object value, string xmlSchemaCollectionDatabase, string xmlSchemaCollectionOwningSchema, string xmlSchemaCollectionName)
  			: this (parameterName, dbType, size, direction, false, precision, scale, sourceColumn, sourceVersion, value)
  		{
  			XmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase;
  			XmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema;
  			XmlSchemaCollectionName = xmlSchemaCollectionName;
  			SourceColumnNullMapping = sourceColumnNullMapping;
  		}
  
  		// This constructor is used internally to construct a
  		// SqlParameter.  The value array comes from sp_procedure_params_rowset.
  		// This is in SqlCommand.DeriveParameters.
  		//
  		// http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/900756fd-3980-48e3-ae59-a15d7fc15b4c/
  		internal SqlParameter (object[] dbValues) 
  			: this (dbValues [3].ToString (), (object) null)
  		{
  			ParameterName = (string) dbValues [3];
  
  			switch ((short) dbValues [5]) {
  			case 1:
  				Direction = ParameterDirection.Input;
  				break;
  			case 2:
  				Direction = ParameterDirection.InputOutput;
  				break;
  			case 3:
  				Direction = ParameterDirection.Output;
  				break;
  			case 4:
  				Direction = ParameterDirection.ReturnValue;
  				break;
  			default:
  				Direction = ParameterDirection.Input;
  				break;
  			}
  
  			SqlDbType = (SqlDbType) FrameworkDbTypeFromName ((string) dbValues [16]);
  
  			if (MetaParameter.IsVariableSizeType) {
  				if (dbValues [10] != DBNull.Value)
  					Size = (int) dbValues [10];
  			}
  
  			if (SqlDbType == SqlDbType.Decimal) {
  				if (dbValues [12] != null && dbValues [12] != DBNull.Value)
  					Precision = (byte) ((short) dbValues [12]);
  				if (dbValues [13] != null && dbValues [13] != DBNull.Value)
  					Scale = (byte) ((short) dbValues [13]);
  			}
  		}
  
  		#endregion // Constructors
  
  		#region Properties
  
  		// Used to ensure that only one collection can contain this
  		// parameter
  		internal SqlParameterCollection Container {
  			get { return container; }
  			set { container = value; }
  		}
  
  		internal void CheckIfInitialized ()
  		{
  			if (!isTypeSet)
  				throw new Exception ("all parameters to have an explicity set type");
  
  			if (MetaParameter.IsVariableSizeType) {
  				if (SqlDbType == SqlDbType.Decimal && Precision == 0)
  					throw new Exception ("Parameter of type 'Decimal' have an explicitly set Precision and Scale");
  				else if (Size == 0)
  					throw new Exception ("all variable length parameters to have an explicitly set non-zero Size");
  			}
  		}
  	
  		public override DbType DbType {
  			get { return dbType; }
  			set {
  				SetDbType (value);
  				typeChanged = true;
  				isTypeSet = true;
  			}
  		}
  
  		[RefreshProperties (RefreshProperties.All)]
  		public override
  		ParameterDirection Direction {
  			get { return direction; }
  			set { 
  				direction = value; 
  				switch( direction ) {
  					case ParameterDirection.Output:
  						MetaParameter.Direction = TdsParameterDirection.Output;
  						break;
  					case ParameterDirection.InputOutput:
  						MetaParameter.Direction = TdsParameterDirection.InputOutput;
  						break;
  					case ParameterDirection.ReturnValue:
  						MetaParameter.Direction = TdsParameterDirection.ReturnValue;
  						break;
  				}
  			}
  		}
  
  		internal TdsMetaParameter MetaParameter {
  			get { return metaParameter; }
  		}
  
  		public override bool IsNullable {
  			get { return metaParameter.IsNullable; }
  			set { metaParameter.IsNullable = value; }
  		}
  
  		[Browsable (false)]
  		[EditorBrowsable (EditorBrowsableState.Advanced)]
  		public int Offset {
  			get { return offset; }
  			set { offset = value; }
  		}
  	
  		public override string ParameterName {
  			get { return metaParameter.ParameterName; }
  			set {
  				if (value == null)
  					value = string.Empty;
  				metaParameter.ParameterName = value;
  			}
  		}
  
  		[DefaultValue (0)]
  		public byte Precision {
  			get { return metaParameter.Precision; }
  			set { metaParameter.Precision = value; }
  		}
  
  		[DefaultValue (0)]
  		public byte Scale {
  			get { return metaParameter.Scale; }
  			set { metaParameter.Scale = value; }
  		}
  
  		public override int Size {
  			get { return metaParameter.Size; }
  			set { metaParameter.Size = value; }
  		}
  
  		public override string SourceColumn {
  			get {
  				if (sourceColumn == null)
  					return string.Empty;
  				return sourceColumn;
  			}
  			set { sourceColumn = value; }
  		}
  
  		public override DataRowVersion SourceVersion {
  			get { return sourceVersion; }
  			set { sourceVersion = value; }
  		}
  		
  		[RefreshProperties (RefreshProperties.All)]
  		[DbProviderSpecificTypeProperty(true)]
  		public SqlDbType SqlDbType {
  			get { return sqlDbType; }
  			set {
  				SetSqlDbType (value);
  				typeChanged = true;
  				isTypeSet = true;
  			}
  		}
  
  
gt: 		[TypeConverterAttribute (typeof (StringConverter))]