2004-07-28 Umadevi S (sumadevi@novell.com)
[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
86                 public OdbcParameter (string name, OdbcType dataType) 
87                         : this ()
88                 {
89                         this.name = name;
90                         OdbcType = dataType;
91                 }
92
93                 public OdbcParameter (string name, OdbcType dataType, int size)
94                         : this (name, dataType)
95                 {
96                         this.size = size;
97                 }
98
99                 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
100                         : this (name, dataType, size)
101                 {
102                         this.sourceColumn = srcColumn;
103                 }
104
105                 [EditorBrowsable (EditorBrowsableState.Advanced)]
106                 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
107                         : this (name, dataType, size, srcColumn)
108                 {
109                         this.direction = direction;
110                         this.isNullable = isNullable;
111                         this.precision = precision;
112                         this.scale = scale;
113                         this.sourceVersion = srcVersion;
114                         this.ParamValue = value;
115                 }
116
117                 #endregion
118
119                 #region Properties
120
121                 // Used to ensure that only one collection can contain this
122                 // parameter
123                 internal OdbcParameterCollection Container {
124                         get { return container; }
125                         set { container = value; }
126                 }
127                 
128                 [BrowsableAttribute (false)]
129                 [OdbcDescriptionAttribute ("The parameter generic type")]
130                 [RefreshPropertiesAttribute (RefreshProperties.All)]
131                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
132                 [OdbcCategory ("Data")]
133                 public DbType DbType {
134                         get { return dbType; }
135                         set { 
136                                 dbType = value;
137                         }
138                 }
139                 
140                 [OdbcCategory ("Data")]
141                 [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]  
142                 [DefaultValue (ParameterDirection.Input)]
143                 public ParameterDirection Direction {
144                         get { return direction; }
145                         set { direction = value; }
146                 }
147                 
148                 [BrowsableAttribute (false)]
149                 [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
150                 [DesignOnlyAttribute (true)]
151                 [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
152                 [DefaultValue (false)]
153                 public bool IsNullable {
154                         get { return isNullable; }\r
155                         set { isNullable = value; }\r
156                 }
157
158                 [DefaultValue (OdbcType.NChar)]
159                 [OdbcDescriptionAttribute ("The parameter native type")]
160                 [RefreshPropertiesAttribute (RefreshProperties.All)]
161                 [OdbcCategory ("Data")]
162                 public OdbcType OdbcType {
163                         get { return odbcType; }
164                         set {
165                                 odbcType = value;
166                         }
167                 }
168                 
169                 [OdbcDescription ("DataParameter_ParameterName")]
170                 [DefaultValue ("")]     
171                 public string ParameterName {
172                         get { return name; }
173                         set { name = value; }
174                 }
175
176                 [OdbcDescription ("DbDataParameter_Precision")]
177                 [OdbcCategory ("DataCategory_Data")]
178                 [DefaultValue (0)]
179                 public byte Precision {
180                         get { return precision; }
181                         set { precision = value; }
182                 }
183                 
184                 [OdbcDescription ("DbDataParameter_Scale")]
185                 [OdbcCategory ("DataCategory_Data")]
186                 [DefaultValue (0)]
187                 public byte Scale {
188                         get { return scale; }
189                         set { scale = value; }
190                 }
191                 
192                 [OdbcDescription ("DbDataParameter_Size")]
193                 [OdbcCategory ("DataCategory_Data")]
194                 [DefaultValue (0)]
195                 public int Size {
196                         get { return size; }
197                         set { size = value; }
198                 }
199
200                 [OdbcDescription ("DataParameter_SourceColumn")]
201                 [OdbcCategory ("DataCategory_Data")]
202                 [DefaultValue ("")]
203                 public string SourceColumn {
204                         get { return sourceColumn; }
205                         set { sourceColumn = value; }
206                 }
207                 
208                 [OdbcDescription ("DataParameter_SourceVersion")]
209                 [OdbcCategory ("DataCategory_Data")]
210                 [DefaultValue (512)]                    
211                 public DataRowVersion SourceVersion {
212                         get { return sourceVersion; }
213                         set { sourceVersion = value; }
214                 }
215
216                 [TypeConverter (typeof(StringConverter))]
217                 [OdbcDescription ("DataParameter_Value")]
218                 [OdbcCategory ("DataCategory_Data")]
219                 [DefaultValue (null)]           
220                 public object Value {
221                         get { 
222                                 return ParamValue;
223                         }
224                         set { 
225                                 this.ParamValue = value;
226                                 bufferIsSet = false;
227                         }
228                 }
229
230                 #endregion // Properties
231
232                 #region Methods\r
233 \r
234                 internal void Bind(IntPtr hstmt, int ParamNum) {\r
235                         OdbcReturn ret;\r
236                         // Set up the buffer if we haven't done so yet\r
237                         if (!bufferIsSet)\r
238                                 setBuffer();\r
239 \r
240                         // Convert System.Data.ParameterDirection into odbc enum\r
241                         OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);\r
242                         // Bind parameter based on type\r
243                         if (odbcType == OdbcType.Int)\r
244                                 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,\r
245                                         (short)odbcType, (short)odbcType, Convert.ToUInt32(size),\r
246                                         0, ref intbuf, 0, 0);\r
247                         else\r
248                                 ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,\r
249                                         (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),\r
250                                         0, buffer, 0, 0);\r
251                         // Check for error condition\r
252                         if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))\r
253                                 throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));\r
254                 }\r
255 \r
256                 private void setBuffer() {\r
257                         // Load buffer with new value\r
258                         if (odbcType == OdbcType.Int)\r
259                                 intbuf = (int)ParamValue;\r
260                         else {\r
261                                 string paramValueString = ParamValue.ToString();\r
262                                 // Treat everything else as a string\r
263                                 // Init string buffer\r
264                                  if (ParamValue is String)
265                                         paramValueString = "\'"+paramValueString+"\'";
266                                  
267                                  if (buffer == null || buffer.Length < ((size > 20) ? size : 20))\r
268                                         buffer = new byte[(size > 20) ? size : 20];\r
269                                 else\r
270                                         buffer.Initialize();\r
271                                 // Convert value into string and store into buffer\r
272                                 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);\r
273                         }\r
274                         bufferIsSet = true;\r
275                 }\r
276 \r
277                 [MonoTODO]
278                 object ICloneable.Clone ()
279                 {
280                         throw new NotImplementedException ();
281                 }
282
283                 public override string ToString ()
284                 {
285                         return ParameterName;
286                 }
287                 #endregion
288         }
289 }