// created on 18/5/2002 at 01:25 // Npgsql.NpgsqlParameter.cs // // Author: // Francisco Jr. (fxjrlists@yahoo.com.br) // // Copyright (C) 2002 The Npgsql Development Team // npgsql-general@gborg.postgresql.org // http://gborg.postgresql.org/project/npgsql/projdisplay.php // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.Data; using System.ComponentModel; using NpgsqlTypes; #if WITHDESIGN using Npgsql.Design; #endif namespace Npgsql { /// /// This class represents a parameter to a command that will be sent to server /// #if WITHDESIGN [TypeConverter(typeof(NpgsqlParameterConverter))] #endif public sealed class NpgsqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable { // Logging related values private static readonly String CLASSNAME = "NpgsqlParameter"; // Fields to implement IDbDataParameter interface. private byte precision = 0; private byte scale = 0; private Int32 size = 0; // Fields to implement IDataParameter //private NpgsqlDbType npgsqldb_type = NpgsqlDbType.Text; //private DbType db_type = DbType.String; private NpgsqlNativeTypeInfo type_info; private ParameterDirection direction = ParameterDirection.Input; private Boolean is_nullable = false; private String name = String.Empty; private String source_column = String.Empty; private DataRowVersion source_version = DataRowVersion.Current; private Object value = DBNull.Value; private System.Resources.ResourceManager resman; /// /// Initializes a new instance of the NpgsqlParameter class. /// public NpgsqlParameter() { resman = new System.Resources.ResourceManager(this.GetType()); NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME); //type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); } /// /// Initializes a new instance of the NpgsqlParameter /// class with the parameter name and a value of the new NpgsqlParameter. /// /// The name of the parameter to map. /// An Object that is the value of the NpgsqlParameter. /// ///

When you specify an Object /// in the value parameter, the DbType is /// inferred from the .NET Framework type of the Object.

///

When using this constructor, you must be aware of a possible misuse of the constructor which takes a DbType parameter. /// This happens when calling this constructor passing an int 0 and the compiler thinks you are passing a value of DbType. /// Use Convert.ToInt32(value) for example to have compiler calling the correct constructor.

///
public NpgsqlParameter(String parameterName, object value) { resman = new System.Resources.ResourceManager(this.GetType()); NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, value); this.ParameterName = parameterName; this.value = value; if ((this.value == null) || (this.value == DBNull.Value) ) { // don't really know what to do - leave default and do further exploration // Default type for null values is String. this.value = DBNull.Value; type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); return; } else { type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value.GetType()); if (type_info == null) { throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value.GetType())); } } } /// /// Internal constructor to handle parameter creation from CommandBuilder passing a NpgsqlNativeTypeInfo directly. /// internal NpgsqlParameter(String parameterName, object value, NpgsqlNativeTypeInfo type_info) { resman = new System.Resources.ResourceManager(this.GetType()); NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, value, type_info); this.ParameterName = parameterName; this.value = (value == null) ? DBNull.Value : value; this.type_info = (type_info == null) ? NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)) : type_info; } /// /// Initializes a new instance of the NpgsqlParameter /// class with the parameter name and the data type. /// /// The name of the parameter to map. /// One of the DbType values. public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType) : this(parameterName, parameterType, 0, String.Empty) {} public NpgsqlParameter(String parameterName, DbType parameterType) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, 0, String.Empty) {} /// /// Initializes a new instance of the NpgsqlParameter /// class with the parameter name, the DbType, and the size. /// /// The name of the parameter to map. /// One of the DbType values. /// The length of the parameter. public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size) : this(parameterName, parameterType, size, String.Empty) {} public NpgsqlParameter(String parameterName, DbType parameterType, Int32 size) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, String.Empty) {} /// /// Initializes a new instance of the NpgsqlParameter /// class with the parameter name, the DbType, the size, /// and the source column name. /// /// The name of the parameter to map. /// One of the DbType values. /// The length of the parameter. /// The name of the source column. public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn) { resman = new System.Resources.ResourceManager(this.GetType()); NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, parameterType, size, source_column); this.ParameterName = parameterName; type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType); if (type_info == null) throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType)); this.size = size; source_column = sourceColumn; } public NpgsqlParameter(String parameterName, DbType parameterType, Int32 size, String sourceColumn) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, sourceColumn) {} /// /// Initializes a new instance of the NpgsqlParameter /// class with the parameter name, the DbType, the size, /// the source column name, a ParameterDirection, /// the precision of the parameter, the scale of the parameter, a /// DataRowVersion to use, and the /// value of the parameter. /// /// The name of the parameter to map. /// One of the DbType values. /// The length of the parameter. /// The name of the source column. /// One of the ParameterDirection values. /// true if the value of the field can be null, otherwise false. /// The total number of digits to the left and right of the decimal point to which /// Value is resolved. /// The total number of decimal places to which /// Value is resolved. /// One of the DataRowVersion values. /// An Object that is the value /// of the NpgsqlParameter. public NpgsqlParameter (String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value) { resman = new System.Resources.ResourceManager(this.GetType()); this.ParameterName = parameterName; this.Size = size; this.SourceColumn = sourceColumn; this.Direction = direction; this.IsNullable = isNullable; this.Precision = precision; this.Scale = scale; this.SourceVersion = sourceVersion; this.Value = value; if (this.value == null) { this.value = DBNull.Value; type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); } else { type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType); if (type_info == null) throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType)); } } public NpgsqlParameter (String parameterName, DbType parameterType, Int32 size, String sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, sourceColumn, direction, isNullable, precision, scale, sourceVersion, value) {} // Implementation of IDbDataParameter /// /// Gets or sets the maximum number of digits used to represent the /// Value property. /// /// The maximum number of digits used to represent the /// Value property. /// The default value is 0, which indicates that the data provider /// sets the precision for Value. [Category("Data"), DefaultValue((Byte)0)] public Byte Precision { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Precision"); return precision; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Precision", value); precision = value; } } /// /// Gets or sets the number of decimal places to which /// Value is resolved. /// /// The number of decimal places to which /// Value is resolved. The default is 0. [Category("Data"), DefaultValue((Byte)0)] public Byte Scale { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Scale"); return scale; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Scale", value); scale = value; } } /// /// Gets or sets the maximum size, in bytes, of the data within the column. /// /// The maximum size, in bytes, of the data within the column. /// The default value is inferred from the parameter value. [Category("Data"), DefaultValue(0)] public Int32 Size { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Size"); return size; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Size", value); size = value; } } /// /// Gets or sets the DbType of the parameter. /// /// One of the DbType values. The default is String. [Category("Data"), RefreshProperties(RefreshProperties.All), DefaultValue(DbType.String)] public DbType DbType { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "DbType"); return TypeInfo.DbType; } // [TODO] Validate data type. set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "DbType", value); type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value); if (type_info == null) throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value)); } } /// /// Gets or sets the DbType of the parameter. /// /// One of the DbType values. The default is String. [Category("Data"), RefreshProperties(RefreshProperties.All), DefaultValue(NpgsqlDbType.Text)] public NpgsqlDbType NpgsqlDbType { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "DbType"); return TypeInfo.NpgsqlDbType; } // [TODO] Validate data type. set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "DbType", value); type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value); if (type_info == null) throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value)); } } internal NpgsqlNativeTypeInfo TypeInfo { get { if (type_info == null) type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); return type_info; } } /// /// Gets or sets a value indicating whether the parameter is input-only, /// output-only, bidirectional, or a stored procedure return value parameter. /// /// One of the ParameterDirection /// values. The default is Input. [Category("Data"), DefaultValue(ParameterDirection.Input)] public ParameterDirection Direction { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "Direction"); return direction; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Direction", value); direction = value; } } /// /// Gets or sets a value indicating whether the parameter accepts null values. /// /// true if null values are accepted; otherwise, false. The default is false. #if WITHDESIGN [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false), DefaultValue(false), DesignOnly(true)] #endif public Boolean IsNullable { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsNullable"); return is_nullable; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "IsNullable", value); is_nullable = value; } } /// /// Gets or sets the name of the NpgsqlParameter. /// /// The name of the NpgsqlParameter. /// The default is an empty string. [DefaultValue("")] public String ParameterName { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "ParameterName"); return name; } set { name = value; if (value == null) name = String.Empty; if ( (name.Equals(String.Empty)) || ((name[0] != ':') && (name[0] != '@')) ) name = ':' + name; NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "ParameterName", value); } } /// /// Gets or sets the name of the source column that is mapped to the /// DataSet and used for loading or /// returning the Value. /// /// The name of the source column that is mapped to the /// DataSet. The default is an empty string. [Category("Data"), DefaultValue("")] public String SourceColumn { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "SourceColumn"); return source_column; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "SourceColumn", value); source_column = value; } } /// /// Gets or sets the DataRowVersion /// to use when loading Value. /// /// One of the DataRowVersion values. /// The default is Current. [Category("Data"), DefaultValue(DataRowVersion.Current)] public DataRowVersion SourceVersion { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "SourceVersion"); return source_version; } set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "SourceVersion", value); source_version = value; } } /// /// Gets or sets the value of the parameter. /// /// An Object that is the value of the parameter. /// The default value is null. [TypeConverter(typeof(StringConverter)), Category("Data")] public Object Value { get { NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "Value"); return value; } // [TODO] Check and validate data type. set { NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Value", value); this.value = value; if ((this.value == null) || (this.value == DBNull.Value) ) { // don't really know what to do - leave default and do further exploration // Default type for null values is String. this.value = DBNull.Value; if (type_info == null) type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String)); } else { if (type_info == null) { type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value.GetType()); if (type_info == null) throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value.GetType())); } } } } /// /// Creates a new NpgsqlParameter that /// is a copy of the current instance. /// /// A new NpgsqlParameter that is a copy of this instance. object System.ICloneable.Clone() { return new NpgsqlParameter(this.ParameterName, this.NpgsqlDbType, this.Size, this.SourceColumn, this.Direction, this.IsNullable, this.Precision, this.Scale, this.SourceVersion, this.Value); } } }