In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds / TdsMetaParameter.cs
index 51190ac39dc1e7738a177d494a025cbac07e11be..8a59211cff8e2c76207d98505d0288ad33b1fe88 100644 (file)
@@ -105,7 +105,16 @@ namespace Mono.Data.Tds {
                }
 
                public byte Scale {
-                       get { return scale; }
+                       get { 
+                               if (TypeName == "decimal" || TypeName == "numeric") {
+                                       if (scale == 0 && !Convert.IsDBNull(Value)) {
+                                               int[] arr = Decimal.GetBits (
+                                                               Convert.ToDecimal(Value));
+                                               scale = (byte)((arr[3]>>16) & (int)0xFF);
+                                       }
+                               }
+                               return scale;
+                       }
                        set { scale = value; }
                }
 
@@ -123,15 +132,37 @@ namespace Mono.Data.Tds {
 
                internal string Prepare ()
                {
-                       StringBuilder result = new StringBuilder (String.Format ("{0} {1}", ParameterName, TypeName));
-                       switch (TypeName) {
+                       string typeName = TypeName;
+                       
+                       if (typeName == "varbinary") {
+                               int size = Size;
+                               if (size <= 0) {
+                                       size = GetActualSize ();
+                               }
+                               
+                               if (size > 8000) {
+                                       typeName = "image";
+                               }
+                       }
+                       
+                       StringBuilder result = new StringBuilder (String.Format ("{0} {1}", ParameterName, typeName));
+                       switch (typeName) {
                        case "decimal":
                        case "numeric":
-                               result.Append (String.Format ("({0},{1})", Precision, Scale));
+                               // msdotnet sends a default precision of 28
+                               result.Append (String.Format ("({0},{1})",
+                                        (Precision == (byte)0 ? (byte)28 : Precision), Scale));
                                break;
                        case "varchar":
                        case "varbinary":
-                               result.Append (String.Format ("({0})", Size > 0 ? Size : GetActualSize ()));
+                               //A size of 0 is not allowed in declarations.
+                               int size = Size;
+                               if (size <= 0) {
+                                       size = GetActualSize ();
+                                       if (size <= 0)
+                                               size = 1;
+                               }
+                               result.Append (String.Format ("({0})", size));
                                break;
                        case "nvarchar":
                                result.Append (String.Format ("({0})", Size > 0 ? Size : 4000));