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.");
116 public static SqlBinary Add (SqlBinary x, SqlBinary y)
122 public int CompareTo (object value)
126 if (!(value is SqlBinary))
127 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlBinary"));
129 return CompareTo ((SqlBinary) value);
134 int CompareTo (SqlBinary value)
139 return Compare (this, value);
142 public static SqlBinary Concat (SqlBinary x, SqlBinary y)
147 public override bool Equals (object value)
149 if (!(value is SqlBinary))
151 else if (this.IsNull && ((SqlBinary)value).IsNull)
153 else if (((SqlBinary)value).IsNull)
156 return (bool) (this == (SqlBinary)value);
159 public static SqlBoolean Equals(SqlBinary x, SqlBinary y)
164 public override int GetHashCode ()
166 // FIXME: I'm not sure is this a right way
168 for (int i = 0; i < value.Length; i++) {
170 result = 91 * result + (int)value [i];
180 public static SqlBoolean GreaterThan (SqlBinary x, SqlBinary y)
185 public static SqlBoolean GreaterThanOrEqual (SqlBinary x, SqlBinary y)
190 public static SqlBoolean LessThan (SqlBinary x, SqlBinary y)
195 public static SqlBoolean LessThanOrEqual (SqlBinary x, SqlBinary y)
200 public static SqlBoolean NotEquals (SqlBinary x, SqlBinary y)
205 public SqlGuid ToSqlGuid ()
207 return (SqlGuid)this;
210 public override string ToString ()
214 return "SqlBinary(" + value.Length + ")";
221 public static SqlBinary operator + (SqlBinary x, SqlBinary y)
223 byte [] b = new byte [x.Value.Length + y.Value.Length];
227 for (i = 0; i < x.Value.Length; i++)
231 for (; i < (x.Value.Length + y.Value.Length); i++) {
236 return new SqlBinary (b);
239 public static SqlBoolean operator == (SqlBinary x, SqlBinary y)
241 if (x.IsNull || y.IsNull)
242 return SqlBoolean.Null;
244 return new SqlBoolean (Compare (x, y) == 0);
247 public static SqlBoolean operator > (SqlBinary x, SqlBinary y)
249 if (x.IsNull || y.IsNull)
250 return SqlBoolean.Null;
252 return new SqlBoolean (Compare (x, y) > 0);
255 public static SqlBoolean operator >= (SqlBinary x, SqlBinary y)
257 if (x.IsNull || y.IsNull)
258 return SqlBoolean.Null;
260 return new SqlBoolean (Compare (x, y) >= 0);
263 public static SqlBoolean operator != (SqlBinary x, SqlBinary y)
265 if (x.IsNull || y.IsNull)
266 return SqlBoolean.Null;
268 return new SqlBoolean (Compare (x, y) != 0);
271 public static SqlBoolean operator < (SqlBinary x, SqlBinary y)
273 if (x.IsNull || y.IsNull)
274 return SqlBoolean.Null;
276 return new SqlBoolean (Compare (x, y) < 0);
279 public static SqlBoolean operator <= (SqlBinary x, SqlBinary y)
281 if (x.IsNull || y.IsNull)
282 return SqlBoolean.Null;
284 return new SqlBoolean (Compare (x, y) <= 0);
287 public static explicit operator byte[] (SqlBinary x)
292 public static explicit operator SqlBinary (SqlGuid x)
294 return new SqlBinary (x.ToByteArray ());
297 public static implicit operator SqlBinary (byte[] x)
299 return new SqlBinary (x);
304 // Helper method to Compare methods and operators.
305 // Returns 0 if x == y
308 private static int Compare(SqlBinary x, SqlBinary y)
313 // If they are different size test are bytes something else than 0
314 if (x.Value.Length != y.Value.Length) {
316 LengthDiff = x.Value.Length - y.Value.Length;
318 // If more than zero, x is longer
319 if (LengthDiff > 0) {
321 for (int i = x.Value.Length - 1; i > x.Value.Length - LengthDiff; i--) {
322 // If byte is more than zero the x is bigger
323 if (x.Value [i] != (byte)0)
328 for (int i = y.Value.Length - 1; i > y.Value.Length - LengthDiff; i--) {
329 // If byte is more than zero then y is bigger
330 if (y.Value [i] != (byte)0)
337 int lenght = (LengthDiff > 0) ? y.Value.Length : x.Value.Length;
339 for (int i = lenght - 1 ; i > 0; i--) {
341 byte X = x.Value [i];
342 byte Y = y.Value [i];
350 // If we are here, x and y were same size
354 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
356 XmlQualifiedName qualifiedName = new XmlQualifiedName ("base64Binary", "http://www.w3.org/2001/XMLSchema");
357 return qualifiedName;
361 XmlSchema IXmlSerializable.GetSchema ()
363 throw new NotImplementedException ();
367 void IXmlSerializable.ReadXml (XmlReader reader)
369 throw new NotImplementedException ();
373 void IXmlSerializable.WriteXml (XmlWriter writer)
375 throw new NotImplementedException ();