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.
38 using System.Xml.Schema;
39 using System.Globalization;
40 using System.Xml.Serialization;
42 namespace System.Data.SqlTypes
45 /// Represents a variable-length stream of binary data to be stored in or retrieved from a database.
48 [SerializableAttribute]
49 [XmlSchemaProvider ("GetSchema")]
51 public struct SqlBinary : INullable, IComparable
62 public static readonly SqlBinary Null;
68 public SqlBinary (byte[] value)
79 get { return !notNull; }
82 public byte this[int index] {
85 throw new SqlNullValueException ("The property contains Null.");
86 else if (index >= this.Length)
87 throw new IndexOutOfRangeException ("The index parameter indicates a position beyond the length of the byte array.");
96 throw new SqlNullValueException ("The property contains Null.");
106 throw new SqlNullValueException ("The property contains Null.");
115 public int CompareTo (object value)
119 if (!(value is SqlBinary))
120 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBinary"));
122 return CompareTo ((SqlBinary) value);
127 int CompareTo (SqlBinary value)
132 return Compare (this, value);
135 public static SqlBinary Concat (SqlBinary x, SqlBinary y)
140 public override bool Equals (object value)
142 if (!(value is SqlBinary))
144 else if (this.IsNull && ((SqlBinary)value).IsNull)
146 else if (((SqlBinary)value).IsNull)
149 return (bool) (this == (SqlBinary)value);
152 public static SqlBoolean Equals(SqlBinary x, SqlBinary y)
157 public override int GetHashCode ()
159 // FIXME: I'm not sure is this a right way
161 for (int i = 0; i < value.Length; i++) {
163 result = 91 * result + (int)value [i];
173 public static SqlBoolean GreaterThan (SqlBinary x, SqlBinary y)
178 public static SqlBoolean GreaterThanOrEqual (SqlBinary x, SqlBinary y)
183 public static SqlBoolean LessThan (SqlBinary x, SqlBinary y)
188 public static SqlBoolean LessThanOrEqual (SqlBinary x, SqlBinary y)
193 public static SqlBoolean NotEquals (SqlBinary x, SqlBinary y)
198 public SqlGuid ToSqlGuid ()
200 return (SqlGuid)this;
203 public override string ToString ()
207 return "SqlBinary(" + value.Length + ")";
215 public static SqlBinary operator + (SqlBinary x, SqlBinary y)
217 byte [] b = new byte [x.Value.Length + y.Value.Length];
221 for (i = 0; i < x.Value.Length; i++)
225 for (; i < (x.Value.Length + y.Value.Length); i++) {
230 return new SqlBinary (b);
233 public static SqlBoolean operator == (SqlBinary x, SqlBinary y)
235 if (x.IsNull || y.IsNull)
236 return SqlBoolean.Null;
238 return new SqlBoolean (Compare (x, y) == 0);
241 public static SqlBoolean operator > (SqlBinary x, SqlBinary y)
243 if (x.IsNull || y.IsNull)
244 return SqlBoolean.Null;
246 return new SqlBoolean (Compare (x, y) > 0);
249 public static SqlBoolean operator >= (SqlBinary x, SqlBinary y)
251 if (x.IsNull || y.IsNull)
252 return SqlBoolean.Null;
254 return new SqlBoolean (Compare (x, y) >= 0);
257 public static SqlBoolean operator != (SqlBinary x, SqlBinary y)
259 if (x.IsNull || y.IsNull)
260 return SqlBoolean.Null;
262 return new SqlBoolean (Compare (x, y) != 0);
265 public static SqlBoolean operator < (SqlBinary x, SqlBinary y)
267 if (x.IsNull || y.IsNull)
268 return SqlBoolean.Null;
270 return new SqlBoolean (Compare (x, y) < 0);
273 public static SqlBoolean operator <= (SqlBinary x, SqlBinary y)
275 if (x.IsNull || y.IsNull)
276 return SqlBoolean.Null;
278 return new SqlBoolean (Compare (x, y) <= 0);
281 public static explicit operator byte[] (SqlBinary x)
286 public static explicit operator SqlBinary (SqlGuid x)
288 return new SqlBinary (x.ToByteArray ());
291 public static implicit operator SqlBinary (byte[] x)
293 return new SqlBinary (x);
298 // Helper method to Compare methods and operators.
299 // Returns 0 if x == y
302 private static int Compare(SqlBinary x, SqlBinary y)
307 // If they are different size test are bytes something else than 0
308 if (x.Value.Length != y.Value.Length) {
310 LengthDiff = x.Value.Length - y.Value.Length;
312 // If more than zero, x is longer
313 if (LengthDiff > 0) {
315 for (int i = x.Value.Length - 1; i > x.Value.Length - LengthDiff; i--) {
316 // If byte is more than zero the x is bigger
317 if (x.Value [i] != (byte)0)
322 for (int i = y.Value.Length - 1; i > y.Value.Length - LengthDiff; i--) {
323 // If byte is more than zero then y is bigger
324 if (y.Value [i] != (byte)0)
331 int lenght = (LengthDiff > 0) ? y.Value.Length : x.Value.Length;
333 for (int i = lenght - 1 ; i > 0; i--) {
335 byte X = x.Value [i];
336 byte Y = y.Value [i];
344 // If we are here, x and y were same size
349 XmlSchema IXmlSerializable.GetSchema ()
351 throw new NotImplementedException ();
355 void IXmlSerializable.ReadXml (XmlReader reader)
357 throw new NotImplementedException ();
361 void IXmlSerializable.WriteXml (XmlWriter writer)
363 throw new NotImplementedException ();