1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
namespace SqlUlongType
{
[Serializable]
[SqlUserDefinedType(Format.UserDefined, IsByteOrdered = true, MaxByteSize = sizeof(ulong))]
[CLSCompliant(false)]
public class ULongType : INullable, IComparable, Microsoft.SqlServer.Server.IBinarySerialize
{
private ulong? m_value = null;
public ULongType()
{
}
public ULongType(byte[] bytes)
:this(BitConverter.ToUInt64(bytes, 0))
{
}
public ULongType(ulong? value)
{
this.m_value = value;
}
#region INullable Members
public bool IsNull
{
get
{
return !this.m_value.HasValue;
}
}
#endregion
#region IComparable Members
[SqlMethod(OnNullCall = false)]
public static ULongType Parse(SqlString s)
{
if (s.IsNull)
return ULongType.Null;
return new ULongType(ulong.Parse(s.Value));
}
public static ULongType Null
{
get
{
return new ULongType();
}
}
public override bool Equals(object obj)
{
return this.CompareTo(obj) == 0;
}
public static bool operator ==(ULongType value, ULongType other)
{
if (value == null) throw new ArgumentException(string.Empty);
return value.Equals(other);
}
public static bool operator !=(ULongType value, ULongType other)
{
return !(value == other);
}
public static bool operator <(ULongType value, ULongType other)
{
if (value == null) throw new ArgumentException(string.Empty);
return (value.CompareTo(other) < 0);
}
public static bool operator >(ULongType value, ULongType other)
{
if (value == null) throw new ArgumentException(string.Empty);
return (value.CompareTo(other) > 0);
}
public override int GetHashCode()
{
if (this.IsNull)
return 0;
return this.ToString().GetHashCode();
}
public int CompareTo(object obj)
{
if (obj == null)
return 1;
var value = obj as ULongType;
if (value == null)
throw new ArgumentException("La valeur n'est pas une instance de ULongType");
if (this.IsNull)
{
if (value.IsNull)
return 0;
return -1;
}
if (value.IsNull)
return 1;
return value.CompareTo(this);
}
#endregion
#region IBinarySerialize Members
public void Read(BinaryReader r)
{
if (r == null) throw new ArgumentException(string.Empty);
byte header = r.ReadByte();
if ((header & 1) > 0)
{
this.m_value = null;
return;
}
int length = r.ReadInt32();
this.m_value = Convert.ToUInt64(r.ReadBytes(length));
}
public void Write(BinaryWriter w)
{
if (w == null) throw new ArgumentException(string.Empty);
byte header = (byte)(this.IsNull ? 1 : 0);
w.Write(header);
if (header == 1)
return;
byte[] bytes = this.IsNull ? new byte[0] : BitConverter.GetBytes(this.m_value.Value);
w.Write(bytes.Length);
w.Write(bytes);
}
#endregion
/// <summary>
/// Return a unicode string for this type.
/// </summary>
[Microsoft.SqlServer.Server.SqlMethod(IsDeterministic = true, IsPrecise = true, DataAccess = Microsoft.SqlServer.Server.DataAccessKind.None, SystemDataAccess = Microsoft.SqlServer.Server.SystemDataAccessKind.None)]
public override string ToString()
{
if (this.IsNull)
return null;
return this.m_value.Value.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
/// <summary>
/// Return a SqlStr
/// </summary>
public SqlString ToSqlString()
{
if (this.IsNull)
return SqlString.Null;
return new SqlString(this.ToString());
}
}
} |
Partager