(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsClient / TdsParameter.cs
blob30adcf3992e2c7be1ed75a65f66fcf72009810b5
1 //
2 // Mono.Data.TdsClient.TdsParameter.cs
3 //
4 // Author:
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc. 2002
10 // Copyright (C) Tim Coleman, 2002
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using Mono.Data.Tds;
35 using Mono.Data.Tds.Protocol;
36 using System;
37 using System.ComponentModel;
38 using System.Data;
39 using System.Data.Common;
40 using System.Runtime.InteropServices;
41 using System.Text;
43 namespace Mono.Data.TdsClient {
44 public sealed class TdsParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
46 #region Fields
48 TdsMetaParameter metaParameter;
50 TdsParameterCollection container = null;
51 DbType dbType;
52 ParameterDirection direction = ParameterDirection.Input;
53 bool isNullable;
54 bool isSizeSet = false;
55 bool isTypeSet = false;
56 int offset;
57 TdsType sybaseType;
58 string sourceColumn;
59 DataRowVersion sourceVersion;
61 #endregion // Fields
63 #region Constructors
65 public TdsParameter ()
66 : this (String.Empty, TdsType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
70 public TdsParameter (string parameterName, object value)
72 metaParameter = new TdsMetaParameter (parameterName, value);
73 this.sourceVersion = DataRowVersion.Current;
74 InferTdsType (value);
77 public TdsParameter (string parameterName, TdsType dbType)
78 : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
82 public TdsParameter (string parameterName, TdsType dbType, int size)
83 : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
87 public TdsParameter (string parameterName, TdsType dbType, int size, string sourceColumn)
88 : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
92 [EditorBrowsable (EditorBrowsableState.Advanced)]
93 public TdsParameter (string parameterName, TdsType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
95 metaParameter = new TdsMetaParameter (parameterName, size, isNullable, precision, scale, value);
97 TdsType = dbType;
98 Direction = direction;
99 SourceColumn = sourceColumn;
100 SourceVersion = sourceVersion;
103 // This constructor is used internally to construct a
104 // TdsParameter. The value array comes from sp_procedure_params_rowset.
105 // This is in TdsCommand.DeriveParameters.
106 internal TdsParameter (object[] dbValues)
108 Precision = 0;
109 Scale = 0;
110 Direction = ParameterDirection.Input;
112 ParameterName = (string) dbValues[3];
114 switch ((short) dbValues[5]) {
115 case 1:
116 Direction = ParameterDirection.Input;
117 break;
118 case 2:
119 Direction = ParameterDirection.Output;
120 break;
121 case 3:
122 Direction = ParameterDirection.InputOutput;
123 break;
124 case 4:
125 Direction = ParameterDirection.ReturnValue;
126 break;
129 IsNullable = (bool) dbValues[8];
131 if (dbValues[12] != null)
132 Precision = (byte) ((short) dbValues[12]);
133 if (dbValues[13] != null)
134 Scale = (byte) ((short) dbValues[13]);
136 SetDbTypeName ((string) dbValues[16]);
139 #endregion // Constructors
141 #region Properties
143 // Used to ensure that only one collection can contain this
144 // parameter
145 internal TdsParameterCollection Container {
146 get { return container; }
147 set { container = value; }
150 public DbType DbType {
151 get { return dbType; }
152 set {
153 SetDbType (value);
154 isTypeSet = true;
158 public ParameterDirection Direction {
159 get { return direction; }
160 set {
161 direction = value;
162 if (direction == ParameterDirection.Output)
163 MetaParameter.Direction = TdsParameterDirection.Output;
167 internal TdsMetaParameter MetaParameter {
168 get { return metaParameter; }
171 string IDataParameter.ParameterName {
172 get { return metaParameter.ParameterName; }
173 set { metaParameter.ParameterName = value; }
176 public bool IsNullable {
177 get { return metaParameter.IsNullable; }
178 set { metaParameter.IsNullable = value; }
181 public int Offset {
182 get { return offset; }
183 set { offset = value; }
186 public string ParameterName {
187 get { return metaParameter.ParameterName; }
188 set { metaParameter.ParameterName = value; }
191 public byte Precision {
192 get { return metaParameter.Precision; }
193 set { metaParameter.Precision = value; }
196 public byte Scale {
197 get { return metaParameter.Scale; }
198 set { metaParameter.Scale = value; }
201 public int Size {
202 get { return metaParameter.Size; }
203 set { metaParameter.Size = value; }
206 public string SourceColumn {
207 get { return sourceColumn; }
208 set { sourceColumn = value; }
211 public DataRowVersion SourceVersion {
212 get { return sourceVersion; }
213 set { sourceVersion = value; }
216 public TdsType TdsType {
217 get { return sybaseType; }
218 set {
219 SetTdsType (value);
220 isTypeSet = true;
224 public object Value {
225 get { return metaParameter.Value; }
226 set {
227 if (!isTypeSet)
228 InferTdsType (value);
229 metaParameter.Value = value;
233 #endregion // Properties
235 #region Methods
237 object ICloneable.Clone ()
239 return new TdsParameter (ParameterName, TdsType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
242 // If the value is set without the DbType/TdsType being set, then we
243 // infer type information.
244 private void InferTdsType (object value)
246 Type type = value.GetType ();
248 string exception = String.Format ("The parameter data type of {0} is invalid.", type.Name);
250 switch (type.FullName) {
251 case "System.Int64":
252 SetTdsType (TdsType.BigInt);
253 break;
254 case "System.Boolean":
255 SetTdsType (TdsType.Bit);
256 break;
257 case "System.String":
258 SetTdsType (TdsType.NVarChar);
259 break;
260 case "System.DateTime":
261 SetTdsType (TdsType.DateTime);
262 break;
263 case "System.Decimal":
264 SetTdsType (TdsType.Decimal);
265 break;
266 case "System.Double":
267 SetTdsType (TdsType.Float);
268 break;
269 case "System.Byte[]":
270 SetTdsType (TdsType.VarBinary);
271 break;
272 case "System.Byte":
273 SetTdsType (TdsType.TinyInt);
274 break;
275 case "System.Int32":
276 SetTdsType (TdsType.Int);
277 break;
278 case "System.Single":
279 SetTdsType (TdsType.Real);
280 break;
281 case "System.Int16":
282 SetTdsType (TdsType.SmallInt);
283 break;
284 case "System.Guid":
285 SetTdsType (TdsType.UniqueIdentifier);
286 break;
287 case "System.Object":
288 SetTdsType (TdsType.Variant);
289 break;
290 default:
291 throw new ArgumentException (exception);
295 // When the DbType is set, we also set the TdsType, as well as the SQL Server
296 // string representation of the type name. If the DbType is not convertible
297 // to an TdsType, throw an exception.
298 private void SetDbType (DbType type)
300 string exception = String.Format ("No mapping exists from DbType {0} to a known TdsType.", type);
302 switch (type) {
303 case DbType.AnsiString:
304 MetaParameter.TypeName = "varchar";
305 sybaseType = TdsType.VarChar;
306 break;
307 case DbType.AnsiStringFixedLength:
308 MetaParameter.TypeName = "char";
309 sybaseType = TdsType.Char;
310 break;
311 case DbType.Binary:
312 MetaParameter.TypeName = "varbinary";
313 sybaseType = TdsType.VarBinary;
314 break;
315 case DbType.Boolean:
316 MetaParameter.TypeName = "bit";
317 sybaseType = TdsType.Bit;
318 break;
319 case DbType.Byte:
320 MetaParameter.TypeName = "tinyint";
321 sybaseType = TdsType.TinyInt;
322 break;
323 case DbType.Currency:
324 sybaseType = TdsType.Money;
325 MetaParameter.TypeName = "money";
326 break;
327 case DbType.Date:
328 case DbType.DateTime:
329 MetaParameter.TypeName = "datetime";
330 sybaseType = TdsType.DateTime;
331 break;
332 case DbType.Decimal:
333 MetaParameter.TypeName = "decimal";
334 sybaseType = TdsType.Decimal;
335 break;
336 case DbType.Double:
337 MetaParameter.TypeName = "float";
338 sybaseType = TdsType.Float;
339 break;
340 case DbType.Guid:
341 MetaParameter.TypeName = "uniqueidentifier";
342 sybaseType = TdsType.UniqueIdentifier;
343 break;
344 case DbType.Int16:
345 MetaParameter.TypeName = "smallint";
346 sybaseType = TdsType.SmallInt;
347 break;
348 case DbType.Int32:
349 MetaParameter.TypeName = "int";
350 sybaseType = TdsType.Int;
351 break;
352 case DbType.Int64:
353 MetaParameter.TypeName = "bigint";
354 sybaseType = TdsType.BigInt;
355 break;
356 case DbType.Object:
357 MetaParameter.TypeName = "sql_variant";
358 sybaseType = TdsType.Variant;
359 break;
360 case DbType.Single:
361 MetaParameter.TypeName = "real";
362 sybaseType = TdsType.Real;
363 break;
364 case DbType.String:
365 MetaParameter.TypeName = "nvarchar";
366 sybaseType = TdsType.NVarChar;
367 break;
368 case DbType.StringFixedLength:
369 MetaParameter.TypeName = "nchar";
370 sybaseType = TdsType.NChar;
371 break;
372 case DbType.Time:
373 MetaParameter.TypeName = "datetime";
374 sybaseType = TdsType.DateTime;
375 break;
376 default:
377 throw new ArgumentException (exception);
379 dbType = type;
382 // Used by internal constructor which has a SQL Server typename
383 private void SetDbTypeName (string dbTypeName)
385 switch (dbTypeName.ToLower ()) {
386 case "bigint":
387 TdsType = TdsType.BigInt;
388 break;
389 case "binary":
390 TdsType = TdsType.Binary;
391 break;
392 case "bit":
393 TdsType = TdsType.Bit;
394 break;
395 case "char":
396 TdsType = TdsType.Char;
397 break;
398 case "datetime":
399 TdsType = TdsType.DateTime;
400 break;
401 case "decimal":
402 TdsType = TdsType.Decimal;
403 break;
404 case "float":
405 TdsType = TdsType.Float;
406 break;
407 case "image":
408 TdsType = TdsType.Image;
409 break;
410 case "int":
411 TdsType = TdsType.Int;
412 break;
413 case "money":
414 TdsType = TdsType.Money;
415 break;
416 case "nchar":
417 TdsType = TdsType.NChar;
418 break;
419 case "ntext":
420 TdsType = TdsType.NText;
421 break;
422 case "nvarchar":
423 TdsType = TdsType.NVarChar;
424 break;
425 case "real":
426 TdsType = TdsType.Real;
427 break;
428 case "smalldatetime":
429 TdsType = TdsType.SmallDateTime;
430 break;
431 case "smallint":
432 TdsType = TdsType.SmallInt;
433 break;
434 case "smallmoney":
435 TdsType = TdsType.SmallMoney;
436 break;
437 case "text":
438 TdsType = TdsType.Text;
439 break;
440 case "timestamp":
441 TdsType = TdsType.Timestamp;
442 break;
443 case "tinyint":
444 TdsType = TdsType.TinyInt;
445 break;
446 case "uniqueidentifier":
447 TdsType = TdsType.UniqueIdentifier;
448 break;
449 case "varbinary":
450 TdsType = TdsType.VarBinary;
451 break;
452 case "varchar":
453 TdsType = TdsType.VarChar;
454 break;
455 default:
456 TdsType = TdsType.Variant;
457 break;
461 // When the TdsType is set, we also set the DbType, as well as the SQL Server
462 // string representation of the type name. If the TdsType is not convertible
463 // to a DbType, throw an exception.
464 private void SetTdsType (TdsType type)
466 string exception = String.Format ("No mapping exists from TdsType {0} to a known DbType.", type);
468 switch (type) {
469 case TdsType.BigInt:
470 MetaParameter.TypeName = "bigint";
471 dbType = DbType.Int64;
472 break;
473 case TdsType.Binary:
474 MetaParameter.TypeName = "binary";
475 dbType = DbType.Binary;
476 break;
477 case TdsType.Timestamp:
478 MetaParameter.TypeName = "timestamp";
479 dbType = DbType.Binary;
480 break;
481 case TdsType.VarBinary:
482 MetaParameter.TypeName = "varbinary";
483 dbType = DbType.Binary;
484 break;
485 case TdsType.Bit:
486 MetaParameter.TypeName = "bit";
487 dbType = DbType.Boolean;
488 break;
489 case TdsType.Char:
490 MetaParameter.TypeName = "char";
491 dbType = DbType.AnsiStringFixedLength;
492 break;
493 case TdsType.DateTime:
494 MetaParameter.TypeName = "datetime";
495 dbType = DbType.DateTime;
496 break;
497 case TdsType.SmallDateTime:
498 MetaParameter.TypeName = "smalldatetime";
499 dbType = DbType.DateTime;
500 break;
501 case TdsType.Decimal:
502 MetaParameter.TypeName = "decimal";
503 dbType = DbType.Decimal;
504 break;
505 case TdsType.Float:
506 MetaParameter.TypeName = "float";
507 dbType = DbType.Double;
508 break;
509 case TdsType.Image:
510 MetaParameter.TypeName = "image";
511 dbType = DbType.Binary;
512 break;
513 case TdsType.Int:
514 MetaParameter.TypeName = "int";
515 dbType = DbType.Int32;
516 break;
517 case TdsType.Money:
518 MetaParameter.TypeName = "money";
519 dbType = DbType.Currency;
520 break;
521 case TdsType.SmallMoney:
522 MetaParameter.TypeName = "smallmoney";
523 dbType = DbType.Currency;
524 break;
525 case TdsType.NChar:
526 MetaParameter.TypeName = "nchar";
527 dbType = DbType.StringFixedLength;
528 break;
529 case TdsType.NText:
530 MetaParameter.TypeName = "ntext";
531 dbType = DbType.String;
532 break;
533 case TdsType.NVarChar:
534 MetaParameter.TypeName = "nvarchar";
535 dbType = DbType.String;
536 break;
537 case TdsType.Real:
538 MetaParameter.TypeName = "real";
539 dbType = DbType.Single;
540 break;
541 case TdsType.SmallInt:
542 MetaParameter.TypeName = "smallint";
543 dbType = DbType.Int16;
544 break;
545 case TdsType.Text:
546 MetaParameter.TypeName = "text";
547 dbType = DbType.AnsiString;
548 break;
549 case TdsType.VarChar:
550 MetaParameter.TypeName = "varchar";
551 dbType = DbType.AnsiString;
552 break;
553 case TdsType.TinyInt:
554 MetaParameter.TypeName = "tinyint";
555 dbType = DbType.Byte;
556 break;
557 case TdsType.UniqueIdentifier:
558 MetaParameter.TypeName = "uniqueidentifier";
559 dbType = DbType.Guid;
560 break;
561 case TdsType.Variant:
562 MetaParameter.TypeName = "sql_variant";
563 dbType = DbType.Object;
564 break;
565 default:
566 throw new ArgumentException (exception);
568 sybaseType = type;
571 public override string ToString()
573 return ParameterName;
576 #endregion // Methods