2 // Mono.Data.Tds.TdsMetaParameter.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2002
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using Mono.Data.Tds.Protocol;
35 namespace Mono.Data.Tds {
36 public class TdsMetaParameter
40 TdsParameterDirection direction = TdsParameterDirection.Input;
46 bool isSizeSet = false;
49 bool isVariableSizeType;
53 public TdsMetaParameter (string name, object value)
54 : this (name, String.Empty, value)
58 public TdsMetaParameter (string name, string typeName, object value)
66 public TdsMetaParameter (string name, int size, bool isNullable, byte precision, byte scale, object value)
70 IsNullable = isNullable;
71 Precision = precision;
78 public TdsParameterDirection Direction {
79 get { return direction; }
80 set { direction = value; }
83 public string TypeName {
84 get { return typeName; }
85 set { typeName = value; }
88 public string ParameterName {
93 public bool IsNullable {
94 get { return isNullable; }
95 set { isNullable = value; }
100 set { this.value = value; }
103 public byte Precision {
104 get { return precision; }
105 set { precision = value; }
110 if (TypeName == "decimal" || TypeName == "numeric") {
111 if (scale == 0 && !Convert.IsDBNull(Value)) {
112 int[] arr = Decimal.GetBits (
113 Convert.ToDecimal(Value));
114 scale = (byte)((arr[3]>>16) & (int)0xFF);
119 set { scale = value; }
123 get { return GetSize (); }
130 public bool IsVariableSizeType
132 get { return isVariableSizeType; }
133 set { isVariableSizeType = value; }
136 #endregion // Properties
140 internal string Prepare ()
142 string typeName = TypeName;
144 if (typeName == "varbinary") {
147 size = GetActualSize ();
155 string includeAt = "@";
156 if (ParameterName [0] == '@')
158 StringBuilder result = new StringBuilder (String.Format ("{0}{1} {2}", includeAt, ParameterName, typeName));
162 // msdotnet sends a default precision of 28
163 result.Append (String.Format ("({0},{1})",
164 (Precision == (byte)0 ? (byte)28 : Precision), Scale));
168 //A size of 0 is not allowed in declarations.
171 size = GetActualSize ();
175 result.Append (size > 8000 ? "(max)" : String.Format ("({0})", size));
178 result.Append (Size > 0 ? (Size > 8000 ? "(max)" : String.Format ("({0})", Size)) : "(4000)");
183 if (isSizeSet && Size > 0)
184 result.Append (String.Format ("({0})", Size));
187 return result.ToString ();
190 internal int GetActualSize ()
192 if (Value == DBNull.Value || Value == null)
195 switch (Value.GetType ().ToString ()) {
196 case "System.String":
197 int len = ((string)value).Length;
198 if (TypeName == "nvarchar" || TypeName == "nchar" || TypeName == "ntext")
201 case "System.Byte[]":
202 return ((byte[]) value).Length;
207 private int GetSize ()
212 case "uniqueidentifier":
221 case "smalldatetime":
239 internal byte[] GetBytes ()
242 if (Value == DBNull.Value || Value == null)
250 return Encoding.Unicode.GetBytes ((string)Value);
254 return Encoding.Default.GetBytes ((string)Value);
256 return ((byte[]) Value);
260 internal TdsColumnType GetMetaType ()
264 return TdsColumnType.BigBinary;
267 return TdsColumnType.BitN;
268 return TdsColumnType.Bit;
270 return TdsColumnType.IntN;
272 return TdsColumnType.Char;
275 return TdsColumnType.MoneyN;
276 return TdsColumnType.Money;
279 return TdsColumnType.MoneyN ;
280 return TdsColumnType.Money4;
282 return TdsColumnType.Decimal;
285 return TdsColumnType.DateTimeN;
286 return TdsColumnType.DateTime;
287 case "smalldatetime":
289 return TdsColumnType.DateTimeN;
290 return TdsColumnType.DateTime4;
293 return TdsColumnType.FloatN ;
294 return TdsColumnType.Float8;
296 return TdsColumnType.Image;
299 return TdsColumnType.IntN;
300 return TdsColumnType.Int4;
302 return TdsColumnType.Numeric;
304 return TdsColumnType.NChar;
306 return TdsColumnType.NText;
308 return TdsColumnType.BigNVarChar;
311 return TdsColumnType.FloatN ;
312 return TdsColumnType.Real;
315 return TdsColumnType.IntN;
316 return TdsColumnType.Int2;
318 return TdsColumnType.Text;
321 return TdsColumnType.IntN;
322 return TdsColumnType.Int1;
323 case "uniqueidentifier":
324 return TdsColumnType.UniqueIdentifier;
326 return TdsColumnType.BigVarBinary;
328 return TdsColumnType.BigVarChar;
330 throw new NotSupportedException ("Unknown Type : " + TypeName);
334 public void Validate (int index)
336 if ((this.direction == TdsParameterDirection.InputOutput || this.direction == TdsParameterDirection.Output) &&
337 this.isVariableSizeType && (Value == DBNull.Value || Value == null) && Size == 0
340 throw new InvalidOperationException (String.Format ("{0}[{1}]: the Size property should " +
347 #endregion // Methods