X Tutup
using System; using System.ComponentModel; using System.Data; using System.Data.Common; using System.Threading.Tasks; using JetBrains.Annotations; using Npgsql.PostgresTypes; using Npgsql.TypeHandling; using Npgsql.TypeMapping; using Npgsql.Util; using NpgsqlTypes; namespace Npgsql { /// /// This class represents a parameter to a command that will be sent to server /// public class NpgsqlParameter : DbParameter, IDbDataParameter, ICloneable { #region Fields and Properties byte _precision; byte _scale; int _size; // ReSharper disable InconsistentNaming private protected NpgsqlDbType? _npgsqlDbType; private protected string? _dataTypeName; // ReSharper restore InconsistentNaming DbType? _cachedDbType; string _name = string.Empty; object? _value; internal string TrimmedName { get; private set; } = string.Empty; /// /// Can be used to communicate a value from the validation phase to the writing phase. /// To be used by type handlers only. /// public object? ConvertedValue { get; set; } internal NpgsqlLengthCache? LengthCache { get; set; } internal NpgsqlTypeHandler? Handler { get; set; } internal FormatCode FormatCode { get; private set; } #endregion #region Constructors /// /// Initializes a new instance of the NpgsqlParameter class. /// public NpgsqlParameter() { SourceColumn = string.Empty; Direction = ParameterDirection.Input; SourceVersion = DataRowVersion.Current; } #nullable disable /// /// 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) : this() { ParameterName = parameterName; // ReSharper disable once VirtualMemberCallInConstructor Value = value; } /// /// 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) { } /// /// Initializes a new instance of the NpgsqlParameter. /// /// The name of the parameter to map. /// One of the DbType values. public NpgsqlParameter(string parameterName, DbType parameterType) : this(parameterName, parameterType, 0, string.Empty) { } /// /// Initializes a new instance of the NpgsqlParameter. /// /// The name of the parameter to map. /// One of the NpgsqlDbType values. /// The length of the parameter. public NpgsqlParameter(string parameterName, NpgsqlDbType parameterType, int size) : this(parameterName, parameterType, size, string.Empty) { } /// /// Initializes a new instance of the NpgsqlParameter. /// /// The name of the parameter to map. /// One of the DbType values. /// The length of the parameter. public NpgsqlParameter(string parameterName, DbType parameterType, int size) : this(parameterName, parameterType, size, string.Empty) { } /// /// Initializes a new instance of the NpgsqlParameter /// /// The name of the parameter to map. /// One of the NpgsqlDbType values. /// The length of the parameter. /// The name of the source column. public NpgsqlParameter(string parameterName, NpgsqlDbType parameterType, int size, string sourceColumn) : this() { ParameterName = parameterName; NpgsqlDbType = parameterType; _size = size; SourceColumn = sourceColumn; } /// /// Initializes a new instance of the NpgsqlParameter. /// /// 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, DbType parameterType, int size, string sourceColumn) : this() { ParameterName = parameterName; DbType = parameterType; _size = size; SourceColumn = sourceColumn; } /// /// Initializes a new instance of the NpgsqlParameter. /// /// The name of the parameter to map. /// One of the NpgsqlDbType 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, int size, string sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value) : this() { ParameterName = parameterName; Size = size; SourceColumn = sourceColumn; Direction = direction; IsNullable = isNullable; Precision = precision; Scale = scale; SourceVersion = sourceVersion; // ReSharper disable once VirtualMemberCallInConstructor Value = value; NpgsqlDbType = parameterType; } /// /// Initializes a new instance of the NpgsqlParameter. /// /// 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, DbType parameterType, int size, string sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value) : this() { ParameterName = parameterName; Size = size; SourceColumn = sourceColumn; Direction = direction; IsNullable = isNullable; Precision = precision; Scale = scale; SourceVersion = sourceVersion; // ReSharper disable once VirtualMemberCallInConstructor Value = value; DbType = parameterType; } #nullable restore #endregion #region Name /// /// Gets or sets The name of the NpgsqlParameter. /// /// The name of the NpgsqlParameter. /// The default is an empty string. [DefaultValue("")] #nullable disable public sealed override string ParameterName #nullable restore { get => _name; set { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (value == null) _name = TrimmedName = string.Empty; else if (value.Length > 0 && (value[0] == ':' || value[0] == '@')) TrimmedName = (_name = value).Substring(1); else _name = TrimmedName = value; Collection?.InvalidateHashLookups(); } } #endregion Name #region Value /// [TypeConverter(typeof(StringConverter)), Category("Data")] #nullable disable public override object Value #nullable restore { get => _value; set { if (_value == null || value == null || _value.GetType() != value.GetType()) Handler = null; _value = value; ConvertedValue = null; } } /// /// Gets or sets the value of the parameter. /// /// An Object that is the value of the parameter. /// The default value is null. [Category("Data")] [TypeConverter(typeof(StringConverter))] #nullable disable public object NpgsqlValue #nullable restore { get => Value; set => Value = value; } #endregion Value #region Type /// /// Gets or sets the DbType of the parameter. /// /// One of the DbType values. The default is Object. [DefaultValue(DbType.Object)] [Category("Data"), RefreshProperties(RefreshProperties.All)] public sealed override DbType DbType { get { if (_cachedDbType.HasValue) return _cachedDbType.Value; if (_npgsqlDbType.HasValue) return _cachedDbType ??= GlobalTypeMapper.Instance.ToDbType(_npgsqlDbType.Value); if (_value != null) // Infer from value but don't cache return GlobalTypeMapper.Instance.ToDbType(_value.GetType()); return DbType.Object; } set { Handler = null; if (value == DbType.Object) { _cachedDbType = null; _npgsqlDbType = null; } else { _cachedDbType = value; _npgsqlDbType = GlobalTypeMapper.Instance.ToNpgsqlDbType(value); } } } /// /// Gets or sets the NpgsqlDbType of the parameter. /// /// One of the NpgsqlDbType values. The default is Unknown. [DefaultValue(NpgsqlDbType.Unknown)] [Category("Data"), RefreshProperties(RefreshProperties.All)] [DbProviderSpecificTypeProperty(true)] public NpgsqlDbType NpgsqlDbType { get { if (_npgsqlDbType.HasValue) return _npgsqlDbType.Value; if (_value != null) // Infer from value return GlobalTypeMapper.Instance.ToNpgsqlDbType(_value.GetType()); return NpgsqlDbType.Unknown; } set { if (value == NpgsqlDbType.Array) throw new ArgumentOutOfRangeException(nameof(value), "Cannot set NpgsqlDbType to just Array, Binary-Or with the element type (e.g. Array of Box is NpgsqlDbType.Array | NpgsqlDbType.Box)."); if (value == NpgsqlDbType.Range) throw new ArgumentOutOfRangeException(nameof(value), "Cannot set NpgsqlDbType to just Range, Binary-Or with the element type (e.g. Range of integer is NpgsqlDbType.Range | NpgsqlDbType.Integer)"); Handler = null; _npgsqlDbType = value; _cachedDbType = null; } } /// /// Used to specify which PostgreSQL type will be sent to the database for this parameter. /// [PublicAPI] public string? DataTypeName { get { if (_dataTypeName != null) return _dataTypeName; throw new NotImplementedException("Infer from others"); } set { _dataTypeName = value; Handler = null; } } #endregion Type #region Other Properties /// public sealed override bool IsNullable { get; set; } /// [DefaultValue(ParameterDirection.Input)] [Category("Data")] public sealed override ParameterDirection Direction { get; set; } #pragma warning disable CS0109 /// /// 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. [DefaultValue((byte)0)] [Category("Data")] public new byte Precision { get => _precision; set { _precision = value; Handler = null; } } /// /// 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. [DefaultValue((byte)0)] [Category("Data")] public new byte Scale { get => _scale; set { _scale = value; Handler = null; } } #pragma warning restore CS0109 /// [DefaultValue(0)] [Category("Data")] public sealed override int Size { get => _size; set { if (value < -1) throw new ArgumentException($"Invalid parameter Size value '{value}'. The value must be greater than or equal to 0."); _size = value; Handler = null; } } /// [DefaultValue("")] [Category("Data")] public sealed override string? SourceColumn { get; set; } /// [Category("Data"), DefaultValue(DataRowVersion.Current)] public sealed override DataRowVersion SourceVersion { get; set; } /// public sealed override bool SourceColumnNullMapping { get; set; } #pragma warning disable CA2227 /// /// The collection to which this parameter belongs, if any. /// public NpgsqlParameterCollection? Collection { get; set; } #pragma warning restore CA2227 /// /// The PostgreSQL data type, such as int4 or text, as discovered from pg_type. /// This property is automatically set if parameters have been derived via /// and can be used to /// acquire additional information about the parameters' data type. /// public PostgresType? PostgresType { get; internal set; } #endregion Other Properties #region Internals internal virtual void ResolveHandler(ConnectorTypeMapper typeMapper) { if (Handler != null) return; if (_npgsqlDbType.HasValue) Handler = typeMapper.GetByNpgsqlDbType(_npgsqlDbType.Value); else if (_dataTypeName != null) Handler = typeMapper.GetByDataTypeName(_dataTypeName); else if (_value != null) Handler = typeMapper.GetByClrType(_value.GetType()); else throw new InvalidOperationException($"Parameter '{ParameterName}' must have its value set"); } internal void Bind(ConnectorTypeMapper typeMapper) { ResolveHandler(typeMapper); FormatCode = Handler!.PreferTextWrite ? FormatCode.Text : FormatCode.Binary; } internal virtual int ValidateAndGetLength() { if (_value == null) throw new InvalidCastException($"Parameter {ParameterName} must be set"); if (_value is DBNull) return 0; var lengthCache = LengthCache; var len = Handler!.ValidateObjectAndGetLength(_value, ref lengthCache, this); LengthCache = lengthCache; return len; } internal virtual Task WriteWithLength(NpgsqlWriteBuffer buf, bool async) => Handler!.WriteObjectWithLength(_value!, buf, LengthCache, this, async); /// public override void ResetDbType() { _cachedDbType = null; _npgsqlDbType = null; _dataTypeName = null; Handler = null; } internal bool IsInputDirection => Direction == ParameterDirection.InputOutput || Direction == ParameterDirection.Input; internal bool IsOutputDirection => Direction == ParameterDirection.InputOutput || Direction == ParameterDirection.Output; #endregion #region Clone /// /// Creates a new NpgsqlParameter that /// is a copy of the current instance. /// /// A new NpgsqlParameter that is a copy of this instance. public NpgsqlParameter Clone() { // use fields instead of properties // to avoid auto-initializing something like type_info var clone = new NpgsqlParameter { _precision = _precision, _scale = _scale, _size = _size, _cachedDbType = _cachedDbType, _npgsqlDbType = _npgsqlDbType, Direction = Direction, IsNullable = IsNullable, _name = _name, TrimmedName = TrimmedName, SourceColumn = SourceColumn, SourceVersion = SourceVersion, _value = _value, SourceColumnNullMapping = SourceColumnNullMapping, }; return clone; } object ICloneable.Clone() => Clone(); #endregion } }
X Tutup