1 // created on 18/5/2002 at 01:25
3 // Npgsql.NpgsqlParameter.cs
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 using System.ComponentModel;
37 /// This class represents a parameter to a command that will be sent to server
39 [TypeConverter(typeof(NpgsqlParameterConverter))]
40 public sealed class NpgsqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
43 // Logging related values
44 private static readonly String CLASSNAME = "NpgsqlParameter";
46 // Fields to implement IDbDataParameter interface.
47 private byte precision = 0;
48 private byte scale = 0;
49 private Int32 size = 0;
51 // Fields to implement IDataParameter
52 //private NpgsqlDbType npgsqldb_type = NpgsqlDbType.Text;
53 //private DbType db_type = DbType.String;
54 private NpgsqlNativeTypeInfo type_info;
55 private ParameterDirection direction = ParameterDirection.Input;
56 private Boolean is_nullable = false;
58 private String source_column = String.Empty;
59 private DataRowVersion source_version = DataRowVersion.Current;
60 private Object value = DBNull.Value;
61 private System.Resources.ResourceManager resman;
65 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> class.
67 public NpgsqlParameter()
69 resman = new System.Resources.ResourceManager(this.GetType());
70 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
74 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
75 /// class with the parameter name and a value of the new <b>NpgsqlParameter</b>.
77 /// <param name="parameterName">The name of the parameter to map.</param>
78 /// <param name="value">An <see cref="System.Object">Object</see> that is the value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
80 /// <p>When you specify an <see cref="System.Object">Object</see>
81 /// in the value parameter, the <see cref="System.Data.DbType">DbType</see> is
82 /// inferred from the .NET Framework type of the <b>Object</b>.</p>
83 /// <p>When using this constructor, you must be aware of a possible misuse of the constructor which takes a DbType parameter.
84 /// This happens when calling this constructor passing an int 0 and the compiler thinks you are passing a value of DbType.
85 /// Use <code> Convert.ToInt32(value) </code> for example to have compiler calling the correct constructor.</p>
87 public NpgsqlParameter(String parameterName, object value)
89 resman = new System.Resources.ResourceManager(this.GetType());
90 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, value);
92 this.ParameterName = parameterName;
95 if ((this.value == null) || (this.value == DBNull.Value) )
97 // don't really know what to do - leave default and do further exploration
98 // Default type for null values is String.
99 this.value = DBNull.Value;
100 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String));
103 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value.GetType());
104 if (type_info == null) {
105 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value.GetType()));
112 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
113 /// class with the parameter name and the data type.
115 /// <param name="parameterName">The name of the parameter to map.</param>
116 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
117 public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType) : this(parameterName, parameterType, 0, String.Empty)
121 public NpgsqlParameter(String parameterName, DbType parameterType) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, 0, String.Empty)
125 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
126 /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, and the size.
128 /// <param name="parameterName">The name of the parameter to map.</param>
129 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
130 /// <param name="size">The length of the parameter.</param>
131 public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size) : this(parameterName, parameterType, size, String.Empty)
134 public NpgsqlParameter(String parameterName, DbType parameterType, Int32 size) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, String.Empty)
139 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
140 /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, the size,
141 /// and the source column name.
143 /// <param name="parameterName">The name of the parameter to map.</param>
144 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
145 /// <param name="size">The length of the parameter.</param>
146 /// <param name="sourceColumn">The name of the source column.</param>
147 public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn)
150 resman = new System.Resources.ResourceManager(this.GetType());
152 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, parameterType, size, source_column);
154 this.ParameterName = parameterName;
156 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType);
157 if (type_info == null)
158 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType));
161 source_column = sourceColumn;
166 public NpgsqlParameter(String parameterName, DbType parameterType, Int32 size, String sourceColumn) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, sourceColumn)
172 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
173 /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, the size,
174 /// the source column name, a <see cref="System.Data.ParameterDirection">ParameterDirection</see>,
175 /// the precision of the parameter, the scale of the parameter, a
176 /// <see cref="System.Data.DataRowVersion">DataRowVersion</see> to use, and the
177 /// value of the parameter.
179 /// <param name="parameterName">The name of the parameter to map.</param>
180 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
181 /// <param name="size">The length of the parameter.</param>
182 /// <param name="sourceColumn">The name of the source column.</param>
183 /// <param name="direction">One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see> values.</param>
184 /// <param name="isNullable"><b>true</b> if the value of the field can be null, otherwise <b>false</b>.</param>
185 /// <param name="precision">The total number of digits to the left and right of the decimal point to which
186 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param>
187 /// <param name="scale">The total number of decimal places to which
188 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param>
189 /// <param name="sourceVersion">One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.</param>
190 /// <param name="value">An <see cref="System.Object">Object</see> that is the value
191 /// of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
192 public NpgsqlParameter (String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value)
195 resman = new System.Resources.ResourceManager(this.GetType());
197 this.ParameterName = parameterName;
199 this.SourceColumn = sourceColumn;
200 this.Direction = direction;
201 this.IsNullable = isNullable;
202 this.Precision = precision;
204 this.SourceVersion = sourceVersion;
207 if (this.value == null) {
208 this.value = DBNull.Value;
209 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String));
213 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType);
214 if (type_info == null)
215 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType));
220 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)
223 // Implementation of IDbDataParameter
225 /// Gets or sets the maximum number of digits used to represent the
226 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> property.
228 /// <value>The maximum number of digits used to represent the
229 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> property.
230 /// The default value is 0, which indicates that the data provider
231 /// sets the precision for <b>Value</b>.</value>
232 [Category("Data"), DefaultValue((Byte)0)]
233 public Byte Precision
237 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Precision");
243 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Precision", value);
249 /// Gets or sets the number of decimal places to which
250 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.
252 /// <value>The number of decimal places to which
253 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved. The default is 0.</value>
254 [Category("Data"), DefaultValue((Byte)0)]
259 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Scale");
265 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Scale", value);
271 /// Gets or sets the maximum size, in bytes, of the data within the column.
273 /// <value>The maximum size, in bytes, of the data within the column.
274 /// The default value is inferred from the parameter value.</value>
275 [Category("Data"), DefaultValue(0)]
280 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Size");
286 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Size", value);
292 /// Gets or sets the <see cref="System.Data.DbType">DbType</see> of the parameter.
294 /// <value>One of the <see cref="System.Data.DbType">DbType</see> values. The default is <b>String</b>.</value>
295 [Category("Data"), RefreshProperties(RefreshProperties.All), DefaultValue(DbType.String)]
300 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "DbType");
301 return TypeInfo.DbType;
304 // [TODO] Validate data type.
307 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "DbType", value);
308 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value);
309 if (type_info == null)
310 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value));
316 /// Gets or sets the <see cref="System.Data.DbType">DbType</see> of the parameter.
318 /// <value>One of the <see cref="System.Data.DbType">DbType</see> values. The default is <b>String</b>.</value>
319 [Category("Data"), RefreshProperties(RefreshProperties.All), DefaultValue(NpgsqlDbType.Text)]
320 public NpgsqlDbType NpgsqlDbType
324 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "DbType");
326 return TypeInfo.NpgsqlDbType;
329 // [TODO] Validate data type.
332 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "DbType", value);
333 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value);
334 if (type_info == null)
335 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value));
342 internal NpgsqlNativeTypeInfo TypeInfo
351 /// Gets or sets a value indicating whether the parameter is input-only,
352 /// output-only, bidirectional, or a stored procedure return value parameter.
354 /// <value>One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see>
355 /// values. The default is <b>Input</b>.</value>
356 [Category("Data"), DefaultValue(ParameterDirection.Input)]
357 public ParameterDirection Direction
361 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "Direction");
367 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Direction", value);
373 /// Gets or sets a value indicating whether the parameter accepts null values.
375 /// <value><b>true</b> if null values are accepted; otherwise, <b>false</b>. The default is <b>false</b>.</value>
376 [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false), DefaultValue(false), DesignOnly(true)]
377 public Boolean IsNullable
381 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsNullable");
387 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "IsNullable", value);
393 /// Gets or sets the name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.
395 /// <value>The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.
396 /// The default is an empty string.</value>
398 public String ParameterName
402 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "ParameterName");
409 if ( (name.Equals(String.Empty)) || ((name[0] != ':') && (name[0] != '@')) )
412 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "ParameterName", value);
417 /// Gets or sets the name of the source column that is mapped to the
418 /// <see cref="System.Data.DataSet">DataSet</see> and used for loading or
419 /// returning the <see cref="Npgsql.NpgsqlParameter.Value">Value</see>.
421 /// <value>The name of the source column that is mapped to the
422 /// <see cref="System.Data.DataSet">DataSet</see>. The default is an empty string.</value>
423 [Category("Data"), DefaultValue("")]
424 public String SourceColumn
428 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "SourceColumn");
429 return source_column;
434 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "SourceColumn", value);
435 source_column = value;
440 /// Gets or sets the <see cref="System.Data.DataRowVersion">DataRowVersion</see>
441 /// to use when loading <see cref="Npgsql.NpgsqlParameter.Value">Value</see>.
443 /// <value>One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.
444 /// The default is <b>Current</b>.</value>
445 [Category("Data"), DefaultValue(DataRowVersion.Current)]
446 public DataRowVersion SourceVersion
450 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "SourceVersion");
451 return source_version;
456 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "SourceVersion", value);
457 source_version = value;
462 /// Gets or sets the value of the parameter.
464 /// <value>An <see cref="System.Object">Object</see> that is the value of the parameter.
465 /// The default value is null.</value>
466 [TypeConverter(typeof(StringConverter)), Category("Data")]
471 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "Value");
475 // [TODO] Check and validate data type.
478 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Value", value);
480 if (this.value == null) {
481 this.value = DBNull.Value;
487 /// Creates a new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> that
488 /// is a copy of the current instance.
490 /// <returns>A new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> that is a copy of this instance.</returns>
491 object System.ICloneable.Clone()
493 return new NpgsqlParameter(this.ParameterName, this.NpgsqlDbType, this.Size, this.SourceColumn, this.Direction, this.IsNullable, this.Precision, this.Scale, this.SourceVersion, this.Value);