copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds / TdsMetaParameter.cs
1 //
2 // Mono.Data.Tds.TdsMetaParameter.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using Mono.Data.Tds.Protocol;
32 using System;
33 using System.Text;
34
35 namespace Mono.Data.Tds {
36         public class TdsMetaParameter
37         {
38                 #region Fields
39
40                 TdsParameterDirection direction = TdsParameterDirection.Input;
41                 byte precision;
42                 byte scale;
43                 int size;
44                 string typeName;
45                 string name;
46                 bool isSizeSet = false;
47                 bool isNullable;
48                 object value;
49
50                 #endregion // Fields
51
52                 public TdsMetaParameter (string name, object value)
53                         : this (name, String.Empty, value)
54                 {
55                 }
56
57                 public TdsMetaParameter (string name, string typeName, object value)
58                 {
59                         ParameterName = name;
60                         Value = value;
61                         TypeName = typeName;
62                         IsNullable = false;
63                 }
64
65                 public TdsMetaParameter (string name, int size, bool isNullable, byte precision, byte scale, object value)
66                 {
67                         ParameterName = name;
68                         Size = size;
69                         IsNullable = isNullable;
70                         Precision = precision;
71                         Scale = scale;
72                         Value = value;
73                 }
74
75                 #region Properties
76
77                 public TdsParameterDirection Direction {
78                         get { return direction; }
79                         set { direction = value; }
80                 }
81
82                 public string TypeName {
83                         get { return typeName; }
84                         set { typeName = value; }
85                 }
86
87                 public string ParameterName {
88                         get { return name; }
89                         set { name = value; }
90                 }
91
92                 public bool IsNullable {
93                         get { return isNullable; }
94                         set { isNullable = value; }
95                 }
96
97                 public object Value {
98                         get { return value; }
99                         set { this.value = value; }
100                 }
101
102                 public byte Precision {
103                         get { return precision; }
104                         set { precision = value; }
105                 }
106
107                 public byte Scale {
108                         get { return scale; }
109                         set { scale = value; }
110                 }
111
112                 public int Size {
113                         get { return GetSize (); }
114                         set {
115                                 size = value; 
116                                 isSizeSet = true;
117                         }
118                 }
119
120                 #endregion // Properties
121
122                 #region Methods
123
124                 internal string Prepare ()
125                 {
126                         StringBuilder result = new StringBuilder (String.Format ("{0} {1}", ParameterName, TypeName));
127                         switch (TypeName) {
128                         case "decimal":
129                         case "numeric":
130                                 result.Append (String.Format ("({0},{1})", Precision, Scale));
131                                 break;
132                         case "varchar":
133                         case "varbinary":
134                                 result.Append (String.Format ("({0})", Size > 0 ? Size : GetActualSize ()));
135                                 break;
136                         case "nvarchar":
137                                 result.Append (String.Format ("({0})", Size > 0 ? Size : 4000));
138                                 break;
139                         case "char":
140                         case "nchar":
141                         case "binary":
142                                 if (isSizeSet && Size > 0)
143                                         result.Append (String.Format ("({0})", Size));
144                                 break;
145                         }
146                         return result.ToString ();
147                 }
148
149                 internal int GetActualSize ()
150                 {
151                         if (Value == DBNull.Value || Value == null)
152                                 return 0;
153
154                         switch (Value.GetType ().ToString ()) {
155                         case "System.String":
156                                 return ((string) value).Length;
157                         case "System.Byte[]":
158                                 return ((byte[]) value).Length;
159                         }
160                         return GetSize ();
161                 }
162
163                 private int GetSize ()
164                 {
165                         if (IsNullable) {
166                                 switch (TypeName) {
167                                 case "bigint":
168                                         return 8;
169                                 case "datetime":
170                                         return 8;
171                                 case "float":
172                                         return 8;
173                                 case "int":
174                                         return 4;
175                                 case "real":
176                                         return 4;
177                                 case "smalldatetime":
178                                         return 4;
179                                 case "smallint":
180                                         return 2;
181                                 case "tinyint":
182                                         return 1;
183                                 }
184                         }
185                         return size;
186                 }
187
188                 internal TdsColumnType GetMetaType ()
189                 {
190                         switch (TypeName) {
191                         case "binary":
192                                 return TdsColumnType.Binary;
193                         case "bit":
194                                 return TdsColumnType.Bit;
195                         case "char":
196                                 return TdsColumnType.Char;
197                         case "decimal":
198                                 return TdsColumnType.Decimal;
199                         case "datetime":
200                                 if (IsNullable)
201                                         return TdsColumnType.DateTimeN;
202                                 return TdsColumnType.DateTime;
203                         case "float":
204                                 return TdsColumnType.Float8;
205                         case "image":
206                                 return TdsColumnType.Image;
207                         case "int":
208                                 if (IsNullable)
209                                         return TdsColumnType.IntN;
210                                 return TdsColumnType.Int4;
211                         case "numeric":
212                                 return TdsColumnType.Numeric;
213                         case "nchar":
214                                 return TdsColumnType.NChar;
215                         case "ntext":
216                                 return TdsColumnType.NText;
217                         case "nvarchar":
218                                 return TdsColumnType.NVarChar;
219                         case "real":
220                                 return TdsColumnType.Real;
221                         case "smallint":
222                                 if (IsNullable)
223                                         return TdsColumnType.IntN;
224                                 return TdsColumnType.Int2;
225                         case "text":
226                                 return TdsColumnType.Text;
227                         case "tinyint":
228                                 if (IsNullable)
229                                         return TdsColumnType.IntN;
230                                 return TdsColumnType.Int1;
231                         case "uniqueidentifier":
232                                 return TdsColumnType.UniqueIdentifier;
233                         case "varbinary":
234                                 return TdsColumnType.VarBinary;
235                         case "varchar":
236                                 return TdsColumnType.VarChar;
237                         default:
238                                 throw new NotSupportedException ();
239                         }
240                 }
241
242                 #endregion // Methods
243         }
244 }