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;
52 public TdsMetaParameter (string name, object value)
53 : this (name, String.Empty, value)
57 public TdsMetaParameter (string name, string typeName, object value)
65 public TdsMetaParameter (string name, int size, bool isNullable, byte precision, byte scale, object value)
69 IsNullable = isNullable;
70 Precision = precision;
77 public TdsParameterDirection Direction {
78 get { return direction; }
79 set { direction = value; }
82 public string TypeName {
83 get { return typeName; }
84 set { typeName = value; }
87 public string ParameterName {
92 public bool IsNullable {
93 get { return isNullable; }
94 set { isNullable = value; }
99 set { this.value = value; }
102 public byte Precision {
103 get { return precision; }
104 set { precision = value; }
109 if (TypeName == "decimal" || TypeName == "numeric") {
110 if (scale == 0 && !Convert.IsDBNull(Value)) {
111 int[] arr = Decimal.GetBits (
112 Convert.ToDecimal(Value));
113 scale = (byte)((arr[3]>>16) & (int)0xFF);
118 set { scale = value; }
122 get { return GetSize (); }
129 #endregion // Properties
133 internal string Prepare ()
135 string typeName = TypeName;
137 if (typeName == "varbinary") {
140 size = GetActualSize ();
148 StringBuilder result = new StringBuilder (String.Format ("{0} {1}", ParameterName, typeName));
152 // msdotnet sends a default precision of 28
153 result.Append (String.Format ("({0},{1})",
154 (Precision == (byte)0 ? (byte)28 : Precision), Scale));
158 //A size of 0 is not allowed in declarations.
161 size = GetActualSize ();
165 result.Append (String.Format ("({0})", size));
168 result.Append (String.Format ("({0})", Size > 0 ? Size : 4000));
173 if (isSizeSet && Size > 0)
174 result.Append (String.Format ("({0})", Size));
177 return result.ToString ();
180 internal int GetActualSize ()
182 if (Value == DBNull.Value || Value == null)
185 switch (Value.GetType ().ToString ()) {
186 case "System.String":
187 return ((string) value).Length;
188 case "System.Byte[]":
189 return ((byte[]) value).Length;
194 private int GetSize ()
208 case "smalldatetime":
219 internal TdsColumnType GetMetaType ()
223 return TdsColumnType.Binary;
225 return TdsColumnType.Bit;
227 return TdsColumnType.Char;
229 return TdsColumnType.Decimal;
232 return TdsColumnType.DateTimeN;
233 return TdsColumnType.DateTime;
235 return TdsColumnType.Float8;
237 return TdsColumnType.Image;
240 return TdsColumnType.IntN;
241 return TdsColumnType.Int4;
243 return TdsColumnType.Numeric;
245 return TdsColumnType.NChar;
247 return TdsColumnType.NText;
249 return TdsColumnType.NVarChar;
251 return TdsColumnType.Real;
254 return TdsColumnType.IntN;
255 return TdsColumnType.Int2;
257 return TdsColumnType.Text;
260 return TdsColumnType.IntN;
261 return TdsColumnType.Int1;
262 case "uniqueidentifier":
263 return TdsColumnType.UniqueIdentifier;
265 return TdsColumnType.VarBinary;
267 return TdsColumnType.VarChar;
269 throw new NotSupportedException ();
273 #endregion // Methods