Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds / TdsMetaParameter.cs
index e17adbf670a7d1cd0d1ee7457dd85b105e1c4a43..8b704455cb3cb1cd1c77b378c82586f079ebd7a3 100644 (file)
@@ -37,6 +37,11 @@ namespace Mono.Data.Tds {
 
        public class TdsMetaParameter
        {
+               #region Static 
+               public const int maxVarCharCharacters =  2147483647; // According to MS, max size is 2GB, 1 Byte Characters
+               public const int maxNVarCharCharacters = 1073741823; // According to MS, max size is 2GB, 2 Byte Characters
+               #endregion
+
                #region Fields
 
                TdsParameterDirection direction = TdsParameterDirection.Input;
@@ -175,6 +180,78 @@ namespace Mono.Data.Tds {
                        set { isVariableSizeType = value; }
                }
 
+               public bool IsVarNVarCharMax
+               {
+                       get { return (TypeName == "ntext" && size >= maxNVarCharCharacters); }
+               }
+
+               public bool IsVarCharMax
+               {
+                       get { return (TypeName == "text" && size >= maxVarCharCharacters); }
+               }
+
+               public bool IsAnyVarCharMax
+               {
+                       get { return IsVarNVarCharMax || IsVarCharMax; }
+               }
+
+               public bool IsNonUnicodeText
+               {
+                       get {
+                               TdsColumnType colType = GetMetaType();
+                               return (colType == TdsColumnType.VarChar ||
+                                       colType == TdsColumnType.BigVarChar ||
+                                       colType == TdsColumnType.Text ||
+                                       colType == TdsColumnType.Char ||
+                                       colType == TdsColumnType.BigChar);
+                       }
+               }
+
+               public bool IsMoneyType
+               {
+                       get {
+                               TdsColumnType colType = GetMetaType();
+                               return (colType == TdsColumnType.Money ||
+                                       colType == TdsColumnType.MoneyN ||
+                                       colType == TdsColumnType.Money4 ||
+                                       colType == TdsColumnType.SmallMoney);
+                       }
+               }
+
+               public bool IsDateTimeType
+               {
+                       get {
+                               TdsColumnType colType = GetMetaType();
+                               return (colType == TdsColumnType.DateTime ||
+                                       colType == TdsColumnType.DateTime4 ||
+                                       colType == TdsColumnType.DateTimeN);
+                       }
+               }
+
+               public bool IsTextType
+               {
+                       get {
+                               TdsColumnType colType = GetMetaType();
+                               return (colType == TdsColumnType.VarChar ||
+                                       colType == TdsColumnType.BigVarChar ||
+                                       colType == TdsColumnType.BigChar ||
+                                       colType == TdsColumnType.Char ||
+                                       colType == TdsColumnType.BigNVarChar || 
+                                       colType == TdsColumnType.NChar ||
+                                       colType == TdsColumnType.Text ||
+                                       colType == TdsColumnType.NText);
+                       }
+               }
+
+               public bool IsDecimalType
+               {
+                       get {
+                               TdsColumnType colType = GetMetaType();
+                               return (colType == TdsColumnType.Decimal ||
+                                       colType == TdsColumnType.Numeric);
+                       }
+               }
+
                #endregion // Properties
 
                #region Methods
@@ -184,13 +261,15 @@ namespace Mono.Data.Tds {
                        if (newValue == DBNull.Value || newValue == null)
                                return newValue;
 
-                       if (!isSizeSet || size == 0)
+                       if (!isSizeSet || size <= 0)
                                return newValue;
 
                        // if size is set, truncate the value to specified size
                        string text = newValue as string;
                        if (text != null) {
-                               if (TypeName == "nvarchar" || TypeName == "nchar") {
+                               if (TypeName == "nvarchar" || 
+                                   TypeName == "nchar" ||
+                                   TypeName == "xml") {
                                        if (text.Length > size)
                                                return text.Substring (0, size);
                                }
@@ -216,7 +295,7 @@ namespace Mono.Data.Tds {
                                }
                                
                                if (size > 8000) {
-                                       typeName = "image";
+                                       typeName = "varbinary(max)";
                                }
                        }
                        
@@ -227,9 +306,9 @@ namespace Mono.Data.Tds {
                        switch (typeName) {
                        case "decimal":
                        case "numeric":
-                               // msdotnet sends a default precision of 28
+                               // msdotnet sends a default precision of 29
                                result.Append (String.Format ("({0},{1})",
-                                        (Precision == (byte)0 ? (byte)28 : Precision), Scale));
+                                        (Precision == (byte)0 ? (byte)38 : Precision), Scale));
                                break;
                        case "varchar":
                        case "varbinary":
@@ -243,7 +322,9 @@ namespace Mono.Data.Tds {
                                result.Append (size > 8000 ? "(max)" : String.Format ("({0})", size));
                                break;
                        case "nvarchar":
-                               result.Append (Size > 0 ? (Size > 8000 ? "(max)" : String.Format ("({0})", Size)) : "(4000)");
+                       case "xml":
+                               int paramSize = Size < 0 ? GetActualSize () / 2 : Size;
+                               result.Append (paramSize > 0 ? (paramSize > 4000 ? "(max)" : String.Format ("({0})", paramSize)) : "(4000)");
                                break;
                        case "char":
                        case "nchar":
@@ -263,7 +344,9 @@ namespace Mono.Data.Tds {
                        switch (Value.GetType ().ToString ()) {
                        case "System.String":
                                int len = ((string)value).Length;
-                               if (TypeName == "nvarchar" || TypeName == "nchar" || TypeName == "ntext")
+                               if (TypeName == "nvarchar" || TypeName == "nchar" 
+                                   || TypeName == "ntext"
+                                   || TypeName == "xml")
                                        len *= 2;
                                return len ;    
                        case "System.Byte[]":
@@ -315,6 +398,7 @@ namespace Mono.Data.Tds {
                                case "nvarchar" :
                                case "nchar" :
                                case "ntext" :
+                               case "xml" :
                                        return Encoding.Unicode.GetBytes ((string)Value);
                                case "varchar" :
                                case "char" :
@@ -335,9 +419,11 @@ namespace Mono.Data.Tds {
                                        return TdsColumnType.BitN;
                                return TdsColumnType.Bit;
                        case "bigint":
-                               return TdsColumnType.IntN;
+                               if (IsNullable)
+                                       return TdsColumnType.IntN ;
+                               return TdsColumnType.BigInt;
                        case "char":
-                               return TdsColumnType.Char;
+                               return TdsColumnType.BigChar;
                        case "money":
                                if (IsNullable)
                                        return TdsColumnType.MoneyN;
@@ -345,7 +431,7 @@ namespace Mono.Data.Tds {
                        case "smallmoney":
                                if (IsNullable)
                                        return TdsColumnType.MoneyN ;
-                               return TdsColumnType.Money4;
+                               return TdsColumnType.SmallMoney;
                        case "decimal":
                                return TdsColumnType.Decimal;
                        case "datetime":
@@ -372,6 +458,7 @@ namespace Mono.Data.Tds {
                                return TdsColumnType.NChar;
                        case "ntext":
                                return TdsColumnType.NText;
+                       case "xml":
                        case "nvarchar":
                                return TdsColumnType.BigNVarChar;
                        case "real":
@@ -399,6 +486,34 @@ namespace Mono.Data.Tds {
                        }
                }
 
+               public void CalculateIsVariableType()
+               {
+                       switch (GetMetaType ()) {
+                               case TdsColumnType.UniqueIdentifier:
+                               case TdsColumnType.BigVarChar:
+                               case TdsColumnType.BigVarBinary:
+                               case TdsColumnType.IntN:
+                               case TdsColumnType.Text:
+                               case TdsColumnType.FloatN:
+                               case TdsColumnType.BigNVarChar:
+                               case TdsColumnType.NText:
+                               case TdsColumnType.Image:
+                               case TdsColumnType.Decimal:
+                               case TdsColumnType.BigBinary:
+                               case TdsColumnType.DateTimeN:
+                               case TdsColumnType.MoneyN:
+                               case TdsColumnType.BitN:
+                               case TdsColumnType.Char:
+                               case TdsColumnType.BigChar:
+                               case TdsColumnType.NChar:
+                                       IsVariableSizeType = true;
+                                       break;
+                               default:
+                                       IsVariableSizeType = false;
+                               break;
+                       }
+               }
+
                public void Validate (int index)
                {
                        if ((this.direction == TdsParameterDirection.InputOutput || this.direction == TdsParameterDirection.Output) &&