fixed tests
[mono.git] / mcs / class / System.Data / System.Data.SqlClient.jvm / SqlParameter.cs
1 //\r
2 // System.Data.SqlClient.SqlParameter\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 //
31
32 using System;
33 using System.Collections;
34 using System.Data;
35 using System.Data.ProviderBase;
36 using System.Data.Common;
37
38 using java.sql;
39
40 namespace System.Data.SqlClient
41 {
42         public sealed class SqlParameter : AbstractDbParameter
43         {
44                 #region Fields
45
46                 private SqlDbType _sqlDbType;
47
48                 #endregion // Fields
49
50                 #region Constructors
51
52                 public SqlParameter()
53                 {
54                 }
55
56                 public SqlParameter(String parameterName, Object value)
57                         : this(parameterName, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, value,false)
58                 {
59                 }
60
61                 public SqlParameter(String parameterName, SqlDbType dbType)
62                         : this(parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null, true)
63                 {
64                 }
65
66         
67                 public SqlParameter(String parameterName, SqlDbType dbType, int size)
68                         : this(parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null, true)
69                 {
70                 }
71
72
73                 public SqlParameter(String parameterName, SqlDbType dbType, int size, String sourceColumn)
74                         : this(parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null, true)
75                 {
76                 }
77
78         
79                 public SqlParameter(
80                         String parameterName,
81                         SqlDbType dbType,
82                         int size,
83                         ParameterDirection direction,
84                         bool isNullable,
85                         byte precision,
86                         byte scale,
87                         String sourceColumn,
88                         DataRowVersion sourceVersion,
89                         Object value) : this(parameterName,dbType,size,direction,isNullable,precision,scale,sourceColumn,sourceVersion,value,true)
90                 {
91                 }
92
93 #if NET_2_0\r
94                 public SqlParameter (\r
95                         string parameterName,\r
96                         SqlDbType dbType,\r
97                         int size,\r
98                         ParameterDirection direction,\r
99                         byte precision,\r
100                         byte scale,\r
101                         string sourceColumn,\r
102                         DataRowVersion sourceVersion,\r
103                         bool sourceColumnNullMapping,\r
104                         Object value,\r
105                         string xmlSchemaCollectionDatabase,\r
106                         string xmlSchemaCollectionOwningSchema,\r
107                         string xmlSchemaCollectionName\r
108                 ) : this (parameterName, dbType, size, direction, sourceColumnNullMapping, precision, scale, sourceColumn, sourceVersion, value, true)\r
109                 {\r
110                 }
111 #endif
112
113                 SqlParameter(
114                         String parameterName,
115                         SqlDbType dbType,
116                         int size,
117                         ParameterDirection direction,
118                         bool isNullable,
119                         byte precision,
120                         byte scale,
121                         String sourceColumn,
122                         DataRowVersion sourceVersion,
123                         Object value,
124                         bool dbTypeExplicit)
125                 {
126                         ParameterName = parameterName;
127                         SqlDbType = dbType;
128                         Size = size;
129                         Direction = direction;
130                         IsNullable = isNullable;
131                         Precision = precision;
132                         Scale = scale;
133                         SourceColumn = sourceColumn;
134                         SourceVersion = sourceVersion;
135                         if (!dbTypeExplicit) {
136                                 IsDbTypeSet = false;
137                         }
138                         Value = value;
139                 }
140
141                 #endregion // Constructors
142
143                 #region Properties
144
145                 public override DbType DbType\r
146         {\r
147             get { return SqlConvert.SqlDbTypeToDbType(_sqlDbType); }           \r
148                         set { SqlDbType = SqlConvert.DbTypeToSqlDbType(value); }\r
149         }                \r
150         \r
151         public SqlDbType SqlDbType\r
152         {\r
153             get { return _sqlDbType; }            \r
154                         set {\r
155                 _sqlDbType = value;\r
156                                 IsDbTypeSet = true;\r
157             }\r
158         }                 
159
160                 public override int Size
161                 {
162                         get {
163                                 int retVal = base.Size;
164                                 return retVal;
165                         }
166                         set {
167                                 if (value < 0) {
168                                         throw ExceptionHelper.InvalidSizeValue(value);
169                                 }
170
171                                 if (value != 0) {
172                                         base.Size = value;
173                                 }
174                                 else {
175                                         base.Size = -1;
176                                 }
177                         }
178                 }
179
180 #if NET_2_0\r
181                 public new byte Precision 
182                 { 
183                         get { return base.Precision; }\r
184                         set { base.Precision = value; } 
185                 }
186
187                 public new byte Scale 
188                 { 
189                         get { return base.Scale; }\r
190                         set { base.Scale = value; } 
191                 }\r
192 #endif
193
194                 protected internal override string Placeholder {\r
195                         get {\r
196                                 if (ParameterName.Length == 0 || ParameterName[0] == '@')
197                                         return ParameterName;
198
199                                 return String.Concat("@", ParameterName);       
200                         }\r
201                 }\r
202
203         
204                 public override Object Value
205                 {
206                         get { return base.Value; }
207                         set { 
208                                 if (!IsDbTypeSet && (value != null) && (value != DBNull.Value)) {\r
209                     _sqlDbType = SqlConvert.ValueTypeToSqlDbType(value.GetType());\r
210                                 }
211                                 base.Value = value; 
212                         }
213                 }
214
215                 #endregion // Properties
216
217                 #region Methods
218
219                 protected internal sealed override object ConvertValue(object value)\r
220                 {\r
221                         // can not convert null or DbNull to other types\r
222                         if (value == null || value == DBNull.Value) {\r
223                                 return value;\r
224                         }\r
225                         // .NET throws an exception to the user.\r
226                         object convertedValue = Convert.ChangeType(value,SqlConvert.SqlDbTypeToValueType(SqlDbType));\r
227                         return convertedValue;\r
228                 }
229
230                 protected internal sealed override void SetParameterName(ResultSet res)\r
231                 {\r
232                         string name = res.getString("COLUMN_NAME");\r
233                         if (name != null && name.Length > 0 && name[0] != '@')\r
234                                 name = String.Concat("@", name);\r
235                         ParameterName = name;\r
236                 }\r
237 \r
238                 protected internal sealed override void SetParameterDbType(ResultSet res)\r
239                 {\r
240                         int dataType = res.getInt("DATA_TYPE");\r
241                         SqlDbType = SqlConvert.JdbcTypeToSqlDbType(dataType);\r
242                         JdbcType = dataType;\r
243                 }\r
244 \r
245                 protected internal sealed override void SetSpecialFeatures(ResultSet res)\r
246                 {\r
247                         // do nothing\r
248                 }
249
250                 protected internal sealed override int JdbcTypeFromProviderType()\r
251                 {\r
252                         return SqlConvert.SqlDbTypeToJdbcType(SqlDbType);\r
253                 }
254
255                 #endregion // Methods  \r
256 \r
257         }
258 }