2 // System.Data.SqlTypes.SqlBinary
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
7 // Ville Palo (vi64pa@koti.soon.fi)
10 // (C) Copyright 2002 Tim Coleman
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Globalization;
39 namespace System.Data.SqlTypes
42 /// Represents a variable-length stream of binary data to be stored in or retrieved from a database.
44 public struct SqlBinary : INullable, IComparable
52 public static readonly SqlBinary Null;
58 public SqlBinary (byte[] value)
69 get { return !notNull; }
72 public byte this[int index] {
75 throw new SqlNullValueException ("The property contains Null.");
76 else if (index >= this.Length)
77 throw new IndexOutOfRangeException ("The index parameter indicates a position beyond the length of the byte array.");
86 throw new SqlNullValueException ("The property contains Null.");
96 throw new SqlNullValueException ("The property contains Null.");
106 public int CompareTo (object value)
110 else if (!(value is SqlBinary))
111 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBinary"));
112 else if (((SqlBinary)value).IsNull)
115 return Compare (this, (SqlBinary)value);
118 public static SqlBinary Concat (SqlBinary x, SqlBinary y)
123 public override bool Equals (object value)
125 if (!(value is SqlBinary))
127 else if (this.IsNull && ((SqlBinary)value).IsNull)
129 else if (((SqlBinary)value).IsNull)
132 return (bool) (this == (SqlBinary)value);
135 public static SqlBoolean Equals(SqlBinary x, SqlBinary y)
140 public override int GetHashCode ()
142 // FIXME: I'm not sure is this a right way
144 for (int i = 0; i < value.Length; i++) {
146 result = 91 * result + (int)value [i];
156 public static SqlBoolean GreaterThan (SqlBinary x, SqlBinary y)
161 public static SqlBoolean GreaterThanOrEqual (SqlBinary x, SqlBinary y)
166 public static SqlBoolean LessThan (SqlBinary x, SqlBinary y)
171 public static SqlBoolean LessThanOrEqual (SqlBinary x, SqlBinary y)
176 public static SqlBoolean NotEquals (SqlBinary x, SqlBinary y)
181 public SqlGuid ToSqlGuid ()
183 return (SqlGuid)this;
186 public override string ToString ()
190 return "SqlBinary(" + value.Length + ")";
198 public static SqlBinary operator + (SqlBinary x, SqlBinary y)
200 byte [] b = new byte [x.Value.Length + y.Value.Length];
204 for (i = 0; i < x.Value.Length; i++)
208 for (; i < (x.Value.Length + y.Value.Length); i++) {
213 return new SqlBinary (b);
216 public static SqlBoolean operator == (SqlBinary x, SqlBinary y)
218 if (x.IsNull || y.IsNull)
219 return SqlBoolean.Null;
221 return new SqlBoolean (Compare (x, y) == 0);
224 public static SqlBoolean operator > (SqlBinary x, SqlBinary y)
226 if (x.IsNull || y.IsNull)
227 return SqlBoolean.Null;
229 return new SqlBoolean (Compare (x, y) > 0);
232 public static SqlBoolean operator >= (SqlBinary x, SqlBinary y)
234 if (x.IsNull || y.IsNull)
235 return SqlBoolean.Null;
237 return new SqlBoolean (Compare (x, y) >= 0);
240 public static SqlBoolean operator != (SqlBinary x, SqlBinary y)
242 if (x.IsNull || y.IsNull)
243 return SqlBoolean.Null;
245 return new SqlBoolean (Compare (x, y) != 0);
248 public static SqlBoolean operator < (SqlBinary x, SqlBinary y)
250 if (x.IsNull || y.IsNull)
251 return SqlBoolean.Null;
253 return new SqlBoolean (Compare (x, y) < 0);
256 public static SqlBoolean operator <= (SqlBinary x, SqlBinary y)
258 if (x.IsNull || y.IsNull)
259 return SqlBoolean.Null;
261 return new SqlBoolean (Compare (x, y) <= 0);
264 public static explicit operator byte[] (SqlBinary x)
269 public static explicit operator SqlBinary (SqlGuid x)
271 return new SqlBinary (x.ToByteArray ());
274 public static implicit operator SqlBinary (byte[] x)
276 return new SqlBinary (x);
281 // Helper method to Compare methods and operators.
282 // Returns 0 if x == y
285 private static int Compare(SqlBinary x, SqlBinary y)
290 // If they are different size test are bytes something else than 0
291 if (x.Value.Length != y.Value.Length) {
293 LengthDiff = x.Value.Length - y.Value.Length;
295 // If more than zero, x is longer
296 if (LengthDiff > 0) {
298 for (int i = x.Value.Length - 1; i > x.Value.Length - LengthDiff; i--) {
299 // If byte is more than zero the x is bigger
300 if (x.Value [i] != (byte)0)
305 for (int i = y.Value.Length - 1; i > y.Value.Length - LengthDiff; i--) {
306 // If byte is more than zero then y is bigger
307 if (y.Value [i] != (byte)0)
314 int lenght = (LengthDiff > 0) ? y.Value.Length : x.Value.Length;
316 for (int i = lenght - 1 ; i > 0; i--) {
318 byte X = x.Value [i];
319 byte Y = y.Value [i];
327 // If we are here, x and y were same size