using System.Text;
namespace Mono.Data.Tds {
- public delegate object FrameworkValueGetter (object rawValue, out bool updated);
+ public delegate object FrameworkValueGetter (object rawValue, ref bool updated);
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;
public object Value {
get {
if (frameworkValueGetter != null) {
- bool updated;
- object newValue = frameworkValueGetter (rawValue, out updated);
- if (updated) {
+ object newValue = frameworkValueGetter (rawValue, ref isUpdated);
+ if (isUpdated)
value = newValue;
- isUpdated = true;
- }
}
if (isUpdated) {
public object RawValue {
get { return rawValue; }
- set { rawValue = value; }
+ set { Value = value; }
}
public byte Precision {
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
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);
}
}
if (size > 8000) {
- typeName = "image";
+ typeName = "varbinary(max)";
}
}
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":
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":
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[]":
case "nvarchar" :
case "nchar" :
case "ntext" :
+ case "xml" :
return Encoding.Unicode.GetBytes ((string)Value);
case "varchar" :
case "char" :
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;
case "smallmoney":
if (IsNullable)
return TdsColumnType.MoneyN ;
- return TdsColumnType.Money4;
+ return TdsColumnType.SmallMoney;
case "decimal":
return TdsColumnType.Decimal;
case "datetime":
return TdsColumnType.NChar;
case "ntext":
return TdsColumnType.NText;
+ case "xml":
case "nvarchar":
return TdsColumnType.BigNVarChar;
case "real":
}
}
+ 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) &&