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;
40 /// This class represents a parameter to a command that will be sent to server
43 [TypeConverter(typeof(NpgsqlParameterConverter))]
46 public sealed class NpgsqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
49 // Logging related values
50 private static readonly String CLASSNAME = "NpgsqlParameter";
52 // Fields to implement IDbDataParameter interface.
53 private byte precision = 0;
54 private byte scale = 0;
55 private Int32 size = 0;
57 // Fields to implement IDataParameter
58 //private NpgsqlDbType npgsqldb_type = NpgsqlDbType.Text;
59 //private DbType db_type = DbType.String;
60 private NpgsqlNativeTypeInfo type_info;
61 private ParameterDirection direction = ParameterDirection.Input;
62 private Boolean is_nullable = false;
64 private String source_column = String.Empty;
65 private DataRowVersion source_version = DataRowVersion.Current;
66 private Object value = DBNull.Value;
67 private System.Resources.ResourceManager resman;
71 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> class.
73 public NpgsqlParameter()
75 resman = new System.Resources.ResourceManager(this.GetType());
76 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
80 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
81 /// class with the parameter name and a value of the new <b>NpgsqlParameter</b>.
83 /// <param name="parameterName">The name of the parameter to map.</param>
84 /// <param name="value">An <see cref="System.Object">Object</see> that is the value of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
86 /// <p>When you specify an <see cref="System.Object">Object</see>
87 /// in the value parameter, the <see cref="System.Data.DbType">DbType</see> is
88 /// inferred from the .NET Framework type of the <b>Object</b>.</p>
89 /// <p>When using this constructor, you must be aware of a possible misuse of the constructor which takes a DbType parameter.
90 /// This happens when calling this constructor passing an int 0 and the compiler thinks you are passing a value of DbType.
91 /// Use <code> Convert.ToInt32(value) </code> for example to have compiler calling the correct constructor.</p>
93 public NpgsqlParameter(String parameterName, object value)
95 resman = new System.Resources.ResourceManager(this.GetType());
96 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, value);
98 this.ParameterName = parameterName;
101 if ((this.value == null) || (this.value == DBNull.Value) )
103 // don't really know what to do - leave default and do further exploration
104 // Default type for null values is String.
105 this.value = DBNull.Value;
106 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String));
111 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value.GetType());
112 if (type_info == null)
114 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value.GetType()));
121 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
122 /// class with the parameter name and the data type.
124 /// <param name="parameterName">The name of the parameter to map.</param>
125 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
126 public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType) : this(parameterName, parameterType, 0, String.Empty)
130 public NpgsqlParameter(String parameterName, DbType parameterType) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, 0, String.Empty)
134 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
135 /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, and the size.
137 /// <param name="parameterName">The name of the parameter to map.</param>
138 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
139 /// <param name="size">The length of the parameter.</param>
140 public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size) : this(parameterName, parameterType, size, String.Empty)
143 public NpgsqlParameter(String parameterName, DbType parameterType, Int32 size) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, String.Empty)
148 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
149 /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, the size,
150 /// and the source column name.
152 /// <param name="parameterName">The name of the parameter to map.</param>
153 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
154 /// <param name="size">The length of the parameter.</param>
155 /// <param name="sourceColumn">The name of the source column.</param>
156 public NpgsqlParameter(String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn)
159 resman = new System.Resources.ResourceManager(this.GetType());
161 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, parameterName, parameterType, size, source_column);
163 this.ParameterName = parameterName;
165 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType);
166 if (type_info == null)
167 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType));
170 source_column = sourceColumn;
175 public NpgsqlParameter(String parameterName, DbType parameterType, Int32 size, String sourceColumn) : this(parameterName, NpgsqlTypesHelper.GetNativeTypeInfo(parameterType).NpgsqlDbType, size, sourceColumn)
181 /// Initializes a new instance of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>
182 /// class with the parameter name, the <see cref="System.Data.DbType">DbType</see>, the size,
183 /// the source column name, a <see cref="System.Data.ParameterDirection">ParameterDirection</see>,
184 /// the precision of the parameter, the scale of the parameter, a
185 /// <see cref="System.Data.DataRowVersion">DataRowVersion</see> to use, and the
186 /// value of the parameter.
188 /// <param name="parameterName">The name of the parameter to map.</param>
189 /// <param name="parameterType">One of the <see cref="System.Data.DbType">DbType</see> values.</param>
190 /// <param name="size">The length of the parameter.</param>
191 /// <param name="sourceColumn">The name of the source column.</param>
192 /// <param name="direction">One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see> values.</param>
193 /// <param name="isNullable"><b>true</b> if the value of the field can be null, otherwise <b>false</b>.</param>
194 /// <param name="precision">The total number of digits to the left and right of the decimal point to which
195 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param>
196 /// <param name="scale">The total number of decimal places to which
197 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.</param>
198 /// <param name="sourceVersion">One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.</param>
199 /// <param name="value">An <see cref="System.Object">Object</see> that is the value
200 /// of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.</param>
201 public NpgsqlParameter (String parameterName, NpgsqlDbType parameterType, Int32 size, String sourceColumn, ParameterDirection direction, bool isNullable, byte precision, byte scale, DataRowVersion sourceVersion, object value)
204 resman = new System.Resources.ResourceManager(this.GetType());
206 this.ParameterName = parameterName;
208 this.SourceColumn = sourceColumn;
209 this.Direction = direction;
210 this.IsNullable = isNullable;
211 this.Precision = precision;
213 this.SourceVersion = sourceVersion;
216 if (this.value == null)
218 this.value = DBNull.Value;
219 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String));
223 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(parameterType);
224 if (type_info == null)
225 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), parameterType));
230 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)
233 // Implementation of IDbDataParameter
235 /// Gets or sets the maximum number of digits used to represent the
236 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> property.
238 /// <value>The maximum number of digits used to represent the
239 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> property.
240 /// The default value is 0, which indicates that the data provider
241 /// sets the precision for <b>Value</b>.</value>
242 [Category("Data"), DefaultValue((Byte)0)]
243 public Byte Precision
247 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Precision");
253 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Precision", value);
259 /// Gets or sets the number of decimal places to which
260 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved.
262 /// <value>The number of decimal places to which
263 /// <see cref="Npgsql.NpgsqlParameter.Value">Value</see> is resolved. The default is 0.</value>
264 [Category("Data"), DefaultValue((Byte)0)]
269 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Scale");
275 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Scale", value);
281 /// Gets or sets the maximum size, in bytes, of the data within the column.
283 /// <value>The maximum size, in bytes, of the data within the column.
284 /// The default value is inferred from the parameter value.</value>
285 [Category("Data"), DefaultValue(0)]
290 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Size");
296 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Size", value);
302 /// Gets or sets the <see cref="System.Data.DbType">DbType</see> of the parameter.
304 /// <value>One of the <see cref="System.Data.DbType">DbType</see> values. The default is <b>String</b>.</value>
305 [Category("Data"), RefreshProperties(RefreshProperties.All), DefaultValue(DbType.String)]
310 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "DbType");
311 return TypeInfo.DbType;
314 // [TODO] Validate data type.
317 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "DbType", value);
318 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value);
319 if (type_info == null)
320 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value));
326 /// Gets or sets the <see cref="System.Data.DbType">DbType</see> of the parameter.
328 /// <value>One of the <see cref="System.Data.DbType">DbType</see> values. The default is <b>String</b>.</value>
329 [Category("Data"), RefreshProperties(RefreshProperties.All), DefaultValue(NpgsqlDbType.Text)]
330 public NpgsqlDbType NpgsqlDbType
334 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "DbType");
336 return TypeInfo.NpgsqlDbType;
339 // [TODO] Validate data type.
342 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "DbType", value);
343 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value);
344 if (type_info == null)
345 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value));
352 internal NpgsqlNativeTypeInfo TypeInfo
361 /// Gets or sets a value indicating whether the parameter is input-only,
362 /// output-only, bidirectional, or a stored procedure return value parameter.
364 /// <value>One of the <see cref="System.Data.ParameterDirection">ParameterDirection</see>
365 /// values. The default is <b>Input</b>.</value>
366 [Category("Data"), DefaultValue(ParameterDirection.Input)]
367 public ParameterDirection Direction
371 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "Direction");
377 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Direction", value);
383 /// Gets or sets a value indicating whether the parameter accepts null values.
385 /// <value><b>true</b> if null values are accepted; otherwise, <b>false</b>. The default is <b>false</b>.</value>
388 [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false), DefaultValue(false), DesignOnly(true)]
391 public Boolean IsNullable
395 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsNullable");
401 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "IsNullable", value);
407 /// Gets or sets the name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.
409 /// <value>The name of the <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see>.
410 /// The default is an empty string.</value>
412 public String ParameterName
416 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "ParameterName");
423 if ( (name.Equals(String.Empty)) || ((name[0] != ':') && (name[0] != '@')) )
426 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "ParameterName", value);
431 /// Gets or sets the name of the source column that is mapped to the
432 /// <see cref="System.Data.DataSet">DataSet</see> and used for loading or
433 /// returning the <see cref="Npgsql.NpgsqlParameter.Value">Value</see>.
435 /// <value>The name of the source column that is mapped to the
436 /// <see cref="System.Data.DataSet">DataSet</see>. The default is an empty string.</value>
437 [Category("Data"), DefaultValue("")]
438 public String SourceColumn
442 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "SourceColumn");
443 return source_column;
448 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "SourceColumn", value);
449 source_column = value;
454 /// Gets or sets the <see cref="System.Data.DataRowVersion">DataRowVersion</see>
455 /// to use when loading <see cref="Npgsql.NpgsqlParameter.Value">Value</see>.
457 /// <value>One of the <see cref="System.Data.DataRowVersion">DataRowVersion</see> values.
458 /// The default is <b>Current</b>.</value>
459 [Category("Data"), DefaultValue(DataRowVersion.Current)]
460 public DataRowVersion SourceVersion
464 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "SourceVersion");
465 return source_version;
470 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "SourceVersion", value);
471 source_version = value;
476 /// Gets or sets the value of the parameter.
478 /// <value>An <see cref="System.Object">Object</see> that is the value of the parameter.
479 /// The default value is null.</value>
480 [TypeConverter(typeof(StringConverter)), Category("Data")]
485 NpgsqlEventLog.LogPropertyGet(LogLevel.Normal, CLASSNAME, "Value");
489 // [TODO] Check and validate data type.
492 NpgsqlEventLog.LogPropertySet(LogLevel.Normal, CLASSNAME, "Value", value);
495 if ((this.value == null) || (this.value == DBNull.Value) )
497 // don't really know what to do - leave default and do further exploration
498 // Default type for null values is String.
499 this.value = DBNull.Value;
500 if (type_info == null)
501 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String));
506 if (type_info == null)
508 type_info = NpgsqlTypesHelper.GetNativeTypeInfo(value.GetType());
509 if (type_info == null)
510 throw new InvalidCastException(String.Format(resman.GetString("Exception_ImpossibleToCast"), value.GetType()));
519 /// Creates a new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> that
520 /// is a copy of the current instance.
522 /// <returns>A new <see cref="Npgsql.NpgsqlParameter">NpgsqlParameter</see> that is a copy of this instance.</returns>
523 object System.ICloneable.Clone()
525 return new NpgsqlParameter(this.ParameterName, this.NpgsqlDbType, this.Size, this.SourceColumn, this.Direction, this.IsNullable, this.Precision, this.Scale, this.SourceVersion, this.Value);