2005-05-27 Sureshkumar T <tsureshkumar@novell.com>
[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                                 //A size of 0 is not allowed in declarations.
135                                 int size = Size;
136                                 if (size <= 0) {
137                                         size = GetActualSize ();
138                                         if (size <= 0)
139                                                 size = 1;
140                                 }
141                                 result.Append (String.Format ("({0})", size));
142                                 break;
143                         case "nvarchar":
144                                 result.Append (String.Format ("({0})", Size > 0 ? Size : 4000));
145                                 break;
146                         case "char":
147                         case "nchar":
148                         case "binary":
149                                 if (isSizeSet && Size > 0)
150                                         result.Append (String.Format ("({0})", Size));
151                                 break;
152                         }
153                         return result.ToString ();
154                 }
155
156                 internal int GetActualSize ()
157                 {
158                         if (Value == DBNull.Value || Value == null)
159                                 return 0;
160
161                         switch (Value.GetType ().ToString ()) {
162                         case "System.String":
163                                 return ((string) value).Length;
164                         case "System.Byte[]":
165                                 return ((byte[]) value).Length;
166                         }
167                         return GetSize ();
168                 }
169
170                 private int GetSize ()
171                 {
172                         if (IsNullable) {
173                                 switch (TypeName) {
174                                 case "bigint":
175                                         return 8;
176                                 case "datetime":
177                                         return 8;
178                                 case "float":
179                                         return 8;
180                                 case "int":
181                                         return 4;
182                                 case "real":
183                                         return 4;
184                                 case "smalldatetime":
185                                         return 4;
186                                 case "smallint":
187                                         return 2;
188                                 case "tinyint":
189                                         return 1;
190                                 }
191                         }
192                         return size;
193                 }
194
195                 internal TdsColumnType GetMetaType ()
196                 {
197                         switch (TypeName) {
198                         case "binary":
199                                 return TdsColumnType.Binary;
200                         case "bit":
201                                 return TdsColumnType.Bit;
202                         case "char":
203                                 return TdsColumnType.Char;
204                         case "decimal":
205                                 return TdsColumnType.Decimal;
206                         case "datetime":
207                                 if (IsNullable)
208                                         return TdsColumnType.DateTimeN;
209                                 return TdsColumnType.DateTime;
210                         case "float":
211                                 return TdsColumnType.Float8;
212                         case "image":
213                                 return TdsColumnType.Image;
214                         case "int":
215                                 if (IsNullable)
216                                         return TdsColumnType.IntN;
217                                 return TdsColumnType.Int4;
218                         case "numeric":
219                                 return TdsColumnType.Numeric;
220                         case "nchar":
221                                 return TdsColumnType.NChar;
222                         case "ntext":
223                                 return TdsColumnType.NText;
224                         case "nvarchar":
225                                 return TdsColumnType.NVarChar;
226                         case "real":
227                                 return TdsColumnType.Real;
228                         case "smallint":
229                                 if (IsNullable)
230                                         return TdsColumnType.IntN;
231                                 return TdsColumnType.Int2;
232                         case "text":
233                                 return TdsColumnType.Text;
234                         case "tinyint":
235                                 if (IsNullable)
236                                         return TdsColumnType.IntN;
237                                 return TdsColumnType.Int1;
238                         case "uniqueidentifier":
239                                 return TdsColumnType.UniqueIdentifier;
240                         case "varbinary":
241                                 return TdsColumnType.VarBinary;
242                         case "varchar":
243                                 return TdsColumnType.VarChar;
244                         default:
245                                 throw new NotSupportedException ();
246                         }
247                 }
248
249                 #endregion // Methods
250         }
251 }