2 // System.Data.Odbc.OdbcParameter
5 // Brian Ritchie (brianlritchie@hotmail.com)
6 // Sureshkumar T <tsureshkumar@novell.com> 2004.
8 // Copyright (C) Brian Ritchie, 2002
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Data.Common;
38 using System.Data.ProviderBase;
40 using System.ComponentModel;
42 namespace System.Data.Odbc
44 [TypeConverterAttribute (typeof (OdbcParameterConverter))]
46 public sealed class OdbcParameter : DbParameterBase, ICloneable
48 public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
55 ParameterDirection direction;
61 DataRowVersion sourceVersion;
64 OdbcType odbcType = OdbcType.NVarChar;
65 DbType dbType = DbType.String;
66 OdbcParameterCollection container = null;
68 // Buffers for parameter value based on type. Currently I've only optimized
69 // for int parameters and everything else is just converted to a string.
70 private bool bufferIsSet;
78 public OdbcParameter ()
80 ParameterName = String.Empty;
86 SourceColumn = String.Empty;
89 public OdbcParameter (string name, object value)
92 this.ParameterName = name;
95 if (value != null && !value.GetType ().IsValueType) {
96 Type type = value.GetType ();
98 Size = type.GetElementType () == typeof (byte) ?
99 ((Array) value).Length : 0;
101 Size = value.ToString ().Length;
107 public OdbcParameter (string name, OdbcType dataType)
110 this.ParameterName = name;
114 public OdbcParameter (string name, OdbcType dataType, int size)
115 : this (name, dataType)
120 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
121 : this (name, dataType, size)
123 this.SourceColumn = srcColumn;
126 [EditorBrowsable (EditorBrowsableState.Advanced)]
127 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
128 : this (name, dataType, size, srcColumn)
130 this.Direction = direction;
131 this.IsNullable = isNullable;
132 this.Precision = precision;
134 this.SourceVersion = srcVersion;
142 // Used to ensure that only one collection can contain this
144 internal OdbcParameterCollection Container {
145 get { return container; }
146 set { container = value; }
149 [BrowsableAttribute (false)]
150 [OdbcDescriptionAttribute ("The parameter generic type")]
151 [RefreshPropertiesAttribute (RefreshProperties.All)]
152 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
153 [OdbcCategory ("Data")]
159 get { return dbType; }
166 [OdbcCategory ("Data")]
167 [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
168 [DefaultValue (ParameterDirection.Input)]
169 public ParameterDirection Direction {
170 get { return direction; }
171 set { direction = value; }
174 [BrowsableAttribute (false)]
175 [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
176 [DesignOnlyAttribute (true)]
177 [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
178 [DefaultValue (false)]
179 public bool IsNullable {
180 get { return isNullable; }
\r
181 set { isNullable = value; }
\r
186 [DefaultValue (OdbcType.NChar)]
187 [OdbcDescriptionAttribute ("The parameter native type")]
188 [RefreshPropertiesAttribute (RefreshProperties.All)]
189 [OdbcCategory ("Data")]
190 public OdbcType OdbcType {
191 get { return odbcType; }
198 [OdbcDescription ("DataParameter_ParameterName")]
200 public string ParameterName {
202 set { name = value; }
205 [OdbcDescription ("DbDataParameter_Precision")]
206 [OdbcCategory ("DataCategory_Data")]
208 public byte Precision {
209 get { return precision; }
210 set { precision = value; }
213 [OdbcDescription ("DbDataParameter_Scale")]
214 [OdbcCategory ("DataCategory_Data")]
217 get { return scale; }
218 set { scale = value; }
221 [OdbcDescription ("DbDataParameter_Size")]
222 [OdbcCategory ("DataCategory_Data")]
226 set { size = value; }
229 [OdbcDescription ("DataParameter_SourceColumn")]
230 [OdbcCategory ("DataCategory_Data")]
232 public string SourceColumn {
233 get { return sourceColumn; }
234 set { sourceColumn = value; }
237 [OdbcDescription ("DataParameter_SourceVersion")]
238 [OdbcCategory ("DataCategory_Data")]
240 public DataRowVersion SourceVersion {
241 get { return sourceVersion; }
242 set { sourceVersion = value; }
245 [TypeConverter (typeof(StringConverter))]
246 [OdbcDescription ("DataParameter_Value")]
247 [OdbcCategory ("DataCategory_Data")]
248 [DefaultValue (null)]
249 public object Value {
261 [TypeConverter (typeof(StringConverter))]
262 [OdbcDescription ("DataParameter_Value")]
263 [OdbcCategory ("DataCategory_Data")]
264 [DefaultValue (null)]
265 public override object Value {
278 #endregion // Properties
282 internal void Bind(IntPtr hstmt, int ParamNum) {
\r
284 // Set up the buffer if we haven't done so yet
\r
288 // Convert System.Data.ParameterDirection into odbc enum
\r
289 OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.Direction);
291 SQL_C_TYPE ctype = OdbcTypeConverter.ConvertToSqlCType (odbcType);
292 SQL_TYPE sqltype = OdbcTypeConverter.ConvertToSqlType (odbcType);
294 // Bind parameter based on type
296 if (odbcType == OdbcType.Int)
297 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
298 ctype, sqltype, Convert.ToUInt32(Size),
299 0, ref intbuf, 0, ref ind);
301 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
302 ctype, sqltype, Convert.ToUInt32(Size),
303 0, buffer, buffer.Length, ref ind);
304 // Check for error condition
\r
305 if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
\r
306 throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
\r
309 private void setBuffer() {
\r
310 // Load buffer with new value
311 if (odbcType == OdbcType.Int)
312 intbuf = Value == null ? new int () : (int) Value;
313 else if (odbcType == OdbcType.Numeric
314 || odbcType == OdbcType.Decimal) {
315 // for numeric, the buffer is a packed decimal struct.
316 // ref http://www.it-faq.pl/mskb/181/254.HTM
319 int [] bits = Decimal.GetBits (Convert.ToDecimal (Value));
320 buffer = new byte [19]; // ref sqltypes.h
322 buffer [0] = Precision;
323 buffer [1] = (byte) ((bits [3] & 0x00FF0000) >> 16); // scale
324 buffer [2] = (byte) ((bits [3] & 0x80000000) > 0 ? 2 : 1); //sign
325 Buffer.BlockCopy (bits, 0, buffer, 3, 12); // copy data
326 for (int j = 16; j < 19; j++) // pad with 0
329 string paramValueString = Value.ToString();
\r
330 // Treat everything else as a string
\r
331 // Init string buffer
\r
333 paramValueString = "\'"+paramValueString+"\'";
336 minSize = Size > 20 ? Size : 20;
338 minSize += 2; // for enclosing apos
339 if (buffer == null || buffer.Length < minSize)
340 buffer = new byte[minSize];
344 // Convert value into string and store into buffer
345 minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
346 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);
\r
348 bufferIsSet = true;
\r
352 object ICloneable.Clone ()
354 throw new NotImplementedException ();
357 public override string ToString ()
359 return ParameterName;
364 public override void PropertyChanging ()
366 throw new NotImplementedException ();
370 protected override byte ValuePrecision (object value)
372 throw new NotImplementedException ();
376 protected override byte ValueScale (object value)
378 throw new NotImplementedException ();
382 protected override int ValueSize (object value)
384 throw new NotImplementedException ();
388 public override void ResetDbType ()
390 throw new NotImplementedException ();