2004-03-16 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 using System;
11 using System.Data;
12 using System.Data.Common;
13
14 namespace System.Data.Odbc
15 {
16         public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
17         {
18                 #region Fields
19
20                 string name;
21                 object ParamValue;
22                 int size;
23                 bool isNullable;
24                 byte precision;
25                 byte scale;
26                 DataRowVersion sourceVersion;
27                 string sourceColumn;
28                 ParameterDirection direction;
29                 OdbcType odbcType;
30                 DbType dbType;
31                 OdbcParameterCollection container = null;       
32                 
33                 // Buffers for parameter value based on type. Currently I've only optimized 
34                 // for int parameters and everything else is just converted to a string.
35                 private bool bufferIsSet;
36                 int intbuf;
37                 byte[] buffer;
38
39                 #endregion
40
41                 #region Constructors
42                 
43                 public OdbcParameter ()
44                 {
45                         name = String.Empty;
46                         ParamValue = null;
47                         size = 0;
48                         isNullable = true;
49                         precision = 0;
50                         scale = 0;
51                         sourceColumn = String.Empty;
52                 }
53
54                 public OdbcParameter (string name, object value) 
55                         : this ()
56                 {
57                         this.name = name;
58                         this.ParamValue = value;
59                 }
60
61                 public OdbcParameter (string name, OdbcType dataType) 
62                         : this ()
63                 {
64                         this.name = name;
65                         OdbcType = dataType;
66                 }
67
68                 public OdbcParameter (string name, OdbcType dataType, int size)
69                         : this (name, dataType)
70                 {
71                         this.size = size;
72                 }
73
74                 public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
75                         : this (name, dataType, size)
76                 {
77                         this.sourceColumn = srcColumn;
78                 }
79
80                 public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
81                         : this (name, dataType, size, srcColumn)
82                 {
83                         this.direction = direction;
84                         this.isNullable = isNullable;
85                         this.precision = precision;
86                         this.scale = scale;
87                         this.sourceVersion = srcVersion;
88                         this.ParamValue = value;
89                 }
90
91                 #endregion
92
93                 #region Properties
94
95                 // Used to ensure that only one collection can contain this
96                 // parameter
97                 internal OdbcParameterCollection Container {
98                         get { return container; }
99                         set { container = value; }
100                 }
101
102                 public DbType DbType {
103                         get { return dbType; }
104                         set { 
105                                 dbType = value;
106                         }
107                 }
108                 
109                 public ParameterDirection Direction {
110                         get { return direction; }
111                         set { direction = value; }
112                 }
113                 
114                 public bool IsNullable {
115                         get { return isNullable; }
116                 }
117
118                 public OdbcType OdbcType {
119                         get { return odbcType; }
120                         set {
121                                 odbcType = value;
122                         }
123                 }
124                 
125                 public string ParameterName {
126                         get { return name; }
127                         set { name = value; }
128                 }
129
130                 public byte Precision {
131                         get { return precision; }
132                         set { precision = value; }
133                 }
134                 
135                 public byte Scale {
136                         get { return scale; }
137                         set { scale = value; }
138                 }
139                 
140                 public int Size {
141                         get { return size; }
142                         set { size = value; }
143                 }
144
145                 public string SourceColumn {
146                         get { return sourceColumn; }
147                         set { sourceColumn = value; }
148                 }
149                 
150                 public DataRowVersion SourceVersion {
151                         get { return sourceVersion; }
152                         set { sourceVersion = value; }
153                 }
154                 
155                 public object Value {
156                         get { 
157                                 return ParamValue;
158                         }
159                         set { 
160                                 this.ParamValue = value;
161                                 bufferIsSet = false;
162                         }
163                 }
164
165                 #endregion // Properties
166
167                 #region public Properties
168
169                 public void Bind(IntPtr hstmt,int ParamNum)
170                 {
171                         OdbcReturn ret;
172                         // Set up the buffer if we haven't done so yet
173                         if(!bufferIsSet)
174                                 setBuffer();
175                                 
176                         // Convert System.Data.ParameterDirection into odbc enum
177                         OdbcInputOutputDirection paramdir=libodbc.ConvertParameterDirection(this.direction);
178                         // Bind parameter based on type
179                         if (odbcType==OdbcType.Int)
180                                 ret=libodbc.SQLBindParameter(hstmt, (ushort) ParamNum, (short) paramdir, 
181                                         (short) odbcType, (short) odbcType, Convert.ToUInt32(size), 
182                                         0, ref intbuf, 0, 0);
183                         else
184                                 ret=libodbc.SQLBindParameter(hstmt, (ushort) ParamNum,  (short) paramdir,
185                                         (short) OdbcType.Char, (short) odbcType, Convert.ToUInt32(size), 
186                                         0,      buffer, 0, 0);
187                         // Check for error condition
188                         if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo)) 
189                                 throw new OdbcException(new OdbcError("SQLBindParam",OdbcHandleType.Stmt,hstmt));
190                 }
191
192                 private void setBuffer() {
193                         // Load buffer with new value
194                         if (odbcType==OdbcType.Int)
195                                 intbuf=(int) ParamValue;
196                         else
197                         {
198                                 string paramValueString = ParamValue.ToString();
199                                 // Treat everything else as a string
200                                 // Init string buffer
201                                 if (buffer==null || buffer.Length< ((size>20)?size:20) )
202                                         buffer=new byte[(size>20)?size:20];
203                                 else
204                                         buffer.Initialize();
205                                 // Convert value into string and store into buffer
206                                 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length , buffer, 0);
207                         }
208                         bufferIsSet = true;
209                 }
210
211                 #endregion // public Properties
212
213                 #region Methods
214
215                 [MonoTODO]
216                 object ICloneable.Clone ()
217                 {
218                         throw new NotImplementedException ();
219                 }
220
221                 public override string ToString ()
222                 {
223                         return ParameterName;
224                 }
225                 #endregion
226         }
227 }