Merge branch 'master' of github.com:tgiphil/mono
[mono.git] / mcs / class / System.Data / System.Data.ProviderBase.jvm / AbstractDBParameter.cs
1 //\r
2 // System.Data.ProviderBase.AbstractDbParameter\r
3 //
4 // Authors:
5 //      Konstantin Triger <kostat@mainsoft.com>
6 //      Boris Kirzner <borisk@mainsoft.com>
7 //      
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //\r
31 \r
32 \r
33 using System;\r
34 using System.Data;\r
35 using System.Data.Common;\r
36 \r
37 using java.sql;\r
38 \r
39 namespace System.Data.ProviderBase\r
40 {\r
41     public abstract class AbstractDbParameter : DbParameter, IDbDataParameter, ICloneable\r
42     {\r
43                 #region Fields\r
44 \r
45                 byte _precision;\r
46                 byte _scale;\r
47                 protected DataRowVersion _sourceVersion;\r
48                 private int _jdbcType;\r
49                 bool _isDbTypeSet;\r
50                 bool _isJdbcTypeSet;\r
51                 object _convertedValue;\r
52 \r
53                 string _parameterName;
54                 ParameterDirection _direction = ParameterDirection.Input;
55                 int _size;\r
56                 object _value;
57                 bool _isNullable;
58                 int _offset;
59                 string _sourceColumn;
60                 DbParameterCollection _parent = null;\r
61 \r
62                 #endregion // Fields\r
63 \r
64                 #region Constructors
65
66                 [MonoTODO]
67                 protected AbstractDbParameter ()
68                 {
69                 }
70
71                 #endregion // Constructors\r
72 \r
73                 #region Properties
74
75                 public override ParameterDirection Direction {
76                         get { return _direction; }
77                         set {
78                                 if (_direction != value) {
79                                         switch (value) {
80                                                         case ParameterDirection.Input:
81                                                         case ParameterDirection.Output:
82                                                         case ParameterDirection.InputOutput:
83                                                         case ParameterDirection.ReturnValue:
84                                                         {
85                                                                 _direction = value;
86                                                                 return;
87                                                         }
88                                         }
89                                         throw ExceptionHelper.InvalidParameterDirection (value);
90                                 }
91                         }
92                 }
93
94                 public override bool IsNullable {
95                         get { return _isNullable; }
96                         set { _isNullable = value; }
97                 }
98
99                 
100                 public virtual int Offset {
101                         get { return _offset; }
102                         set { _offset = value; }                        
103                 }
104
105                 public override string ParameterName {
106                         get {
107                                 if (_parameterName == null)
108                                                 return String.Empty;
109
110                                 return _parameterName;
111                         }
112                         set {
113                                 if (_parameterName != value) {
114                                         _parameterName = value;
115                                 }
116                         }
117                 }
118
119                 public override int Size {
120                         get { return _size; }
121
122                         set {
123                                 if (_size != value) {
124                                         if (value < -1)
125                                                 throw ExceptionHelper.InvalidSizeValue (value);
126
127                                         _size = value;
128                                 }
129                         }
130                 }
131
132                 
133                 public override string SourceColumn {
134                         get { 
135                                 if (_sourceColumn == null)
136                                         return String.Empty;
137
138                                 return _sourceColumn;
139                         }
140
141                         set     { _sourceColumn = value; }
142                 }
143
144                 internal DbParameterCollection Parent
145                 {
146                         get { return _parent; }
147                         set { _parent = value; }
148                 }
149                 
150                 public byte Precision 
151                 { 
152                         get { return _precision; }\r
153                         set { _precision = value; } 
154                 }
155
156                 public byte Scale 
157                 { 
158                         get { return _scale; }\r
159                         set { _scale = value; } 
160                 }
161 \r
162                 public override DataRowVersion SourceVersion\r
163                 {\r
164                         get { return _sourceVersion; }\r
165                         set { _sourceVersion = value; }\r
166                 }
167
168                 protected internal int JdbcType
169                 {
170                         get { 
171                                 if (!IsJdbcTypeSet) {
172                                         return JdbcTypeFromProviderType();
173                                 }
174                                 return _jdbcType; 
175                         }
176                         set { 
177                                 _jdbcType = value; 
178                                 IsJdbcTypeSet = true;
179                         }
180                 }
181                 
182                 protected internal bool IsJdbcTypeSet
183                 {
184                         get { 
185                                 return _isJdbcTypeSet; 
186                         }
187
188                         set {
189                                 _isJdbcTypeSet = value;
190                         }
191                 }
192
193                 protected internal bool IsDbTypeSet
194                 {
195                         get { return _isDbTypeSet; }
196                         set { _isDbTypeSet = value; }
197                 }
198
199                 protected internal virtual bool IsSpecial {
200                         get {
201                                 return false;
202                         }
203                 }
204
205                 private bool IsFixedLength
206                 {
207                         get {
208                                 return ((DbType != DbType.AnsiString) && (DbType != DbType.Binary) && 
209                                                 (DbType != DbType.String) && (DbType != DbType.VarNumeric));
210                         }
211                 }
212
213                 protected internal virtual string Placeholder {
214                         get {
215                                 return "?";
216                         }
217                 }
218
219                 internal object ConvertedValue\r
220                 {\r
221                         get { \r
222                                 if (_convertedValue == null) {\r
223                                         object value = Value;\r
224                                         _convertedValue = ((value != null) && (value != DBNull.Value)) ? ConvertValue(value) : value;\r
225                                 }\r
226                                 return _convertedValue;\r
227                         }\r
228                 }
229
230                 public override object Value {
231                         get { return _value; }
232                         set { 
233                                 _convertedValue = null;
234                                 _value = value;
235                         }
236                 }
237
238                 //DbParameter overrides
239
240                 public override bool SourceColumnNullMapping {\r
241                         get {\r
242                                 throw new NotImplementedException();\r
243                         }\r
244                         set {\r
245                                 throw new NotImplementedException();\r
246                         }\r
247                 }\r
248
249
250                 #endregion // Properties
251
252                 #region Methods
253
254                 public override String ToString()\r
255         {\r
256             return ParameterName;\r
257         }
258
259                 protected internal abstract void SetParameterName(ResultSet res);\r
260 \r
261                 protected internal abstract void SetParameterDbType(ResultSet res);\r
262 \r
263                 protected internal abstract void SetSpecialFeatures(ResultSet res);
264                 
265                 public virtual object Clone()
266                 {\r
267                         AbstractDbParameter other = (AbstractDbParameter) MemberwiseClone ();\r
268                         other._parent = null;\r
269                         return other;
270                 }
271
272                 protected internal abstract int JdbcTypeFromProviderType();
273
274                 protected internal abstract object ConvertValue(object value);
275
276                 internal void SetParameterPrecisionAndScale(ResultSet res)
277                 {
278                         int jdbcType = res.getInt("DATA_TYPE");\r
279                         if(jdbcType == java.sql.Types.DECIMAL || jdbcType == java.sql.Types.NUMERIC) {\r
280                                 Precision = (byte)res.getInt("PRECISION");\r
281                                 Scale = (byte)res.getInt("SCALE");\r
282                         }
283                 }
284
285                 internal void SetParameterSize(ResultSet res)
286                 {
287                         Size = res.getInt("LENGTH");
288                 }
289
290                 internal void SetParameterIsNullable(ResultSet res)
291                 {
292                         IsNullable = (res.getInt("NULLABLE") == 1);
293                 }
294
295                 internal void Validate()
296                 {
297                         if (!IsFixedLength && ((Direction & ParameterDirection.Output) != 0) && (Size == 0)) {
298                                 throw ExceptionHelper.ParameterSizeNotInitialized(Offset,ParameterName,DbType.ToString(),Size); 
299                         }
300                 }
301
302                 //DbParameter overrides
303
304                 public override void ResetDbType() {\r
305                         throw new NotImplementedException();\r
306                 }
307
308                 #endregion // Methods\r
309         }\r
310 }\r
311 \r