**** Merged r36954 from MCS ****
[mono.git] / mcs / class / System.Data / System.Data.Odbc / OdbcParameter.cs
1 //
2 // System.Data.Odbc.OdbcParameter
3 //
4 // Authors:
5 //   Brian Ritchie (brianlritchie@hotmail.com)
6 //
7 // Copyright (C) Brian Ritchie, 2002
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Data;
35 using System.Data.Common;
36 using System.ComponentModel;
37
38 namespace System.Data.Odbc
39 {
40         [TypeConverterAttribute (typeof (OdbcParameterConverter))]      
41         public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
42         {
43                 #region Fields
44
45                 string name;
46                 object ParamValue;
47                 int size;
48                 bool isNullable;
49                 byte precision;
50                 byte scale;
51                 DataRowVersion sourceVersion;
52                 string sourceColumn;
53                 ParameterDirection direction;
54                 OdbcType odbcType;
55                 DbType dbType;
56                 OdbcParameterCollection container = null;       
57                 
58                 // Buffers for parameter value based on type. Currently I've only optimized 
59                 // for int parameters and everything else is just converted to a string.
60                 private bool bufferIsSet;
61                 int intbuf;
62                 byte[] buffer;
63
64                 #endregion
65
66                 #region Constructors
67                 
68                 public OdbcParameter ()
69                 {
70                         name = String.Empty;
71                         ParamValue = null;
72                         size = 0;
73                         isNullable = true;
74                         precision = 0;
75                         scale = 0;
76                         sourceColumn = String.Empty;
77                 }
78
79                 public OdbcParameter (string name, object value) 
80                         : this ()
81                 {
82                         this.name = name;
83                         this.ParamValue = value;
84                         
85                         if (value != null && !value.GetType ().IsValueType) {
86                                 Type type = value.GetType ();
87                                 if (type.IsArray)
88                                         size = type.GetElementType () == typeof (byte) ? 
89                                                 ((Array) value).Length : 0;
90                                 else
91                                         size = value.ToString ().Length;
92                         }
93
94
95                 }
96
97                 public OdbcParameter (string name, OdbcType dataType) 
98                         : this ()
99                 {
100                         this.name = name;
101                         OdbcType = dataType;
102                 }
103
104                 public OdbcParameter (string name, OdbcType dataType, int size)
105                         : this (name, dataType)
106                 {
107                         this.size = size;
108                 }
109
110                 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
111                         : this (name, dataType, size)
112                 {
113                         this.sourceColumn = srcColumn;
114                 }
115
116                 [EditorBrowsable (EditorBrowsableState.Advanced)]
117                 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
118                         : this (name, dataType, size, srcColumn)
119                 {
120                         this.direction = direction;
121                         this.isNullable = isNullable;
122                         this.precision = precision;
123                         this.scale = scale;
124                         this.sourceVersion = srcVersion;
125                         this.ParamValue = value;
126                 }
127
128                 #endregion
129
130                 #region Properties
131
132                 // Used to ensure that only one collection can contain this
133                 // parameter
134                 internal OdbcParameterCollection Container {
135                         get { return container; }
136                         set { container = value; }
137                 }
138                 
139                 [BrowsableAttribute (false)]
140                 [OdbcDescriptionAttribute ("The parameter generic type")]
141                 [RefreshPropertiesAttribute (RefreshProperties.All)]
142                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
143                 [OdbcCategory ("Data")]
144                 public DbType DbType {
145                         get { return dbType; }
146                         set { 
147                                 dbType = value;
148                         }
149                 }
150                 
151                 [OdbcCategory ("Data")]
152                 [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]  
153                 [DefaultValue (ParameterDirection.Input)]
154                 public ParameterDirection Direction {
155                         get { return direction; }
156                         set { direction = value; }
157                 }
158                 
159                 [BrowsableAttribute (false)]
160                 [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
161                 [DesignOnlyAttribute (true)]
162                 [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
163                 [DefaultValue (false)]
164                 public bool IsNullable {
165                         get { return isNullable; }\r
166                         set { isNullable = value; }\r
167                 }
168
169                 [DefaultValue (OdbcType.NChar)]
170                 [OdbcDescriptionAttribute ("The parameter native type")]
171                 [RefreshPropertiesAttribute (RefreshProperties.All)]
172                 [OdbcCategory ("Data")]
173                 public OdbcType OdbcType {
174                         get { return odbcType; }
175                         set {
176                                 odbcType = value;
177                         }
178                 }
179                 
180                 [OdbcDescription ("DataParameter_ParameterName")]
181                 [DefaultValue ("")]     
182                 public string ParameterName {
183                         get { return name; }
184                         set { name = value; }
185                 }
186
187                 [OdbcDescription ("DbDataParameter_Precision")]
188                 [OdbcCategory ("DataCategory_Data")]
189                 [DefaultValue (0)]
190                 public byte Precision {
191                         get { return precision; }
192                         set { precision = value; }
193                 }
194                 
195                 [OdbcDescription ("DbDataParameter_Scale")]
196                 [OdbcCategory ("DataCategory_Data")]
197                 [DefaultValue (0)]
198                 public byte Scale {
199                         get { return scale; }
200                         set { scale = value; }
201                 }
202                 
203                 [OdbcDescription ("DbDataParameter_Size")]
204                 [OdbcCategory ("DataCategory_Data")]
205                 [DefaultValue (0)]
206                 public int Size {
207                         get { return size; }
208                         set { size = value; }
209                 }
210
211                 [OdbcDescription ("DataParameter_SourceColumn")]
212                 [OdbcCategory ("DataCategory_Data")]
213                 [DefaultValue ("")]
214                 public string SourceColumn {
215                         get { return sourceColumn; }
216                         set { sourceColumn = value; }
217                 }
218                 
219                 [OdbcDescription ("DataParameter_SourceVersion")]
220                 [OdbcCategory ("DataCategory_Data")]
221                 [DefaultValue (512)]                    
222                 public DataRowVersion SourceVersion {
223                         get { return sourceVersion; }
224                         set { sourceVersion = value; }
225                 }
226
227                 [TypeConverter (typeof(StringConverter))]
228                 [OdbcDescription ("DataParameter_Value")]
229                 [OdbcCategory ("DataCategory_Data")]
230                 [DefaultValue (null)]           
231                 public object Value {
232                         get { 
233                                 return ParamValue;
234                         }
235                         set { 
236                                 this.ParamValue = value;
237                                 bufferIsSet = false;
238                         }
239                 }
240
241                 #endregion // Properties
242
243                 #region Methods\r
244 \r
245                 internal void Bind(IntPtr hstmt, int ParamNum) {\r
246                         OdbcReturn ret;\r
247                         // Set up the buffer if we haven't done so yet\r
248                         if (!bufferIsSet)\r
249                                 setBuffer();\r
250 \r
251                         // Convert System.Data.ParameterDirection into odbc enum\r
252                         OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);\r
253                         // Bind parameter based on type\r
254                         if (odbcType == OdbcType.Int)\r
255                                 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,\r
256                                         (short)odbcType, (short)odbcType, Convert.ToUInt32(size),\r
257                                         0, ref intbuf, 0, 0);\r
258                         else\r
259                                 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,\r
260                                         (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),\r
261                                         0, buffer, 0, 0);\r
262                         // Check for error condition\r
263                         if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))\r
264                                 throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));\r
265                 }\r
266 \r
267                 private void setBuffer() {\r
268                         // Load buffer with new value
269                         if (odbcType == OdbcType.Int)
270                                 intbuf = ParamValue == null ? new int () : (int) ParamValue;
271                         else {\r
272                                 string paramValueString = ParamValue.ToString();\r
273                                 // Treat everything else as a string\r
274                                 // Init string buffer\r
275                                  if (ParamValue is String)
276                                         paramValueString = "\'"+paramValueString+"\'";
277
278                                  int minSize = size;
279                                  minSize = size > 20 ? size : 20;
280                                  if (buffer == null || buffer.Length < minSize)
281                                          buffer = new byte[minSize];
282                                  else
283                                          buffer.Initialize();
284                                  
285                                  // Convert value into string and store into buffer
286                                  minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
287                                  System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);\r
288                         }\r
289                         bufferIsSet = true;\r
290                 }\r
291 \r
292                 [MonoTODO]
293                 object ICloneable.Clone ()
294                 {
295                         throw new NotImplementedException ();
296                 }
297
298                 public override string ToString ()
299                 {
300                         return ParameterName;
301                 }
302                 #endregion
303         }
304 }