1 // System.Data.SqlTypes.SqlBinary
4 // Konstantin Triger <kostat@mainsoft.com>
5 // Boris Kirzner <borisk@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Data.SqlTypes
35 //using clr.exceptions;
36 //using clr.compiler.BitConstants;
41 public struct SqlBinary : INullable, IComparable
43 public static readonly SqlBinary Null = new SqlBinary(true);
45 private byte[] _value;
47 private SqlBinary(bool isNull)
54 * Initializes a new instance of the SqlBinary instance,
55 * setting the Value property to the contents of the supplied byte array.
56 * @param value The byte array to be stored or retrieved.
58 public SqlBinary(byte[] value)
60 if (value != null && value.Length > 0)
62 _value = new byte[value.Length];
63 Array.Copy (value, 0, _value, 0, value.Length);
73 * Indicates whether or not Value is null.
74 * @return true if Value is null, otherwise false.
85 * Gets the single byte from the Value property located at the position indicated by the integer parameter, index.
86 * If index indicates a position beyond the end of the byte array, a SqlNullValueException will be raised.
87 * @param index The position of the byte to be retrieved.
88 * @return The byte located at the position indicated by the integer parameter.
90 public int this[int index]
96 throw new SqlNullValueException();
98 if (index >= _value.Length)
100 throw new ArgumentOutOfRangeException("index");
102 return _value[index];// & BitConstants.ALL_BYTE;
107 * Gets the length in bytes of the Value property.
108 * @return The length of the binary data in the Value property.
116 throw new SqlNullValueException();
118 return _value.Length;
123 * Gets the value of the SqlBinary instance.
124 * @return the value of this instance
132 throw new SqlNullValueException();
139 * Compares this instance to the supplied object and returns an indication of their relative values.
140 * @param obj The object to compare.
141 * @return A signed number indicating the relative values of the instance and the object.
142 * Less than zero This instance is less than object.
143 * Zero This instance is the same as object.
144 * Greater than zero This instance is greater than object -or-
145 * object is a null reference.
147 public int CompareTo(Object value)
152 if ((value is SqlBinary) == false)
154 throw new ArgumentException("Wrong value type " +
155 value.GetType().Name + "in SqlBinary.CompareTo");
158 SqlBinary obj = (SqlBinary)value;
169 int length = _value.Length > obj._value.Length ? _value.Length : obj._value.Length;
171 for (int i = 0; i < length; i++)
173 if (_value[i] > obj._value[i])
175 if (_value[i] < obj._value[i])
179 if (_value.Length > obj._value.Length)
181 if (_value.Length < obj._value.Length)
189 * Concatenates two SqlBinary instances to create a new SqlBinary instance.
190 * @param x A SqlBinary instance.
191 * @param y A SqlBinary instance.
192 * @return The concatenated values of the x and y parameters.
194 public static SqlBinary Concat(SqlBinary x, SqlBinary y)
200 return SqlBinary.Null;
203 return new SqlBinary((byte[])y._value);
206 return new SqlBinary((byte[])x._value);
208 byte[] newVal = new byte[x.Length + y.Length];
210 java.lang.System.arraycopy(x, 0, newVal, 0, x.Length);
211 java.lang.System.arraycopy(y, 0, newVal, x.Length, y.Length);
213 return new SqlBinary(newVal);
218 * Performs a logical comparison on two instances of SqlBinary to determine if they are equal.
219 * @param x A SqlBinary instance.
220 * @param y A SqlBinary instance.
221 * @return true if the two values are equal, otherwise false.
222 * If one of the parameters is null or null value return SqlBoolean.Null.
224 public static SqlBoolean Equals(SqlBinary x, SqlBinary y)
226 if (x.IsNull || y.IsNull)
227 return SqlBoolean.Null;
230 return SqlBoolean.True;
232 return SqlBoolean.False;
236 public bool equals(Object obj)
241 if (obj is SqlBinary)
243 SqlBinary o = (SqlBinary)obj;
245 if (IsNull && o.IsNull)
248 if (IsNull || o.IsNull)
251 if (_value.Length != o._value.Length)
254 for (int i = 0; i < _value.Length; i++)
256 if (_value[i] != o._value[i])
267 * Compares two instances of SqlBinary to determine if the first is greater than the second.
268 * @param x A SqlBinary instance
269 * @param y A SqlBinary instance
270 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
271 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
273 public static SqlBoolean GreaterThan(SqlBinary x, SqlBinary y)
275 if (x.IsNull || y.IsNull)
276 return SqlBoolean.Null;
278 int i = x.CompareTo(y);
281 return SqlBoolean.True;
283 return SqlBoolean.False;
287 * Compares two instances of SqlBinary to determine if the first is greater than or equal to the second.
288 * @param x A SqlBinary instance
289 * @param y A SqlBinary instance
290 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
291 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
293 public static SqlBoolean GreaterThanOrEqual(SqlBinary x, SqlBinary y)
295 if (x.IsNull || y.IsNull)
296 return SqlBoolean.Null;
298 int i = x.CompareTo(y);
301 return SqlBoolean.False;
303 return SqlBoolean.True;
307 * Compares two instances of SqlBinary to determine if the first is less than the second.
308 * @param x A SqlBinary instance
309 * @param y A SqlBinary instance
310 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
311 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
313 public static SqlBoolean LessThan(SqlBinary x, SqlBinary y)
315 if (x.IsNull || y.IsNull)
316 return SqlBoolean.Null;
318 int i = x.CompareTo(y);
321 return SqlBoolean.True;
323 return SqlBoolean.False;
327 * Compares two instances of SqlBinary to determine if the first is less than or equal to the second.
328 * @param x A SqlBinary instance
329 * @param y A SqlBinary instance
330 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
331 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
333 public static SqlBoolean LessThanOrEqual(SqlBinary x, SqlBinary y)
335 if (x.IsNull || y.IsNull)
336 return SqlBoolean.Null;
338 int i = x.CompareTo(y);
341 return SqlBoolean.False;
343 return SqlBoolean.True;
347 * Compares two instances of SqlBinary to determine if they are equal.
348 * @param x A SqlBinary instance
349 * @param y A SqlBinary instance
350 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
351 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
353 public static SqlBoolean NotEquals(SqlBinary x, SqlBinary y)
355 if (x.IsNull || y.IsNull)
356 return SqlBoolean.Null;
359 return SqlBoolean.False;
361 return SqlBoolean.True;
364 public String toString()
369 return "SqlBinary(" + _value.ToString() + ")";
372 public static SqlBinary op_Implicit(byte[] x)
374 return new SqlBinary(x);
377 public static byte[] op_Explicit(SqlBinary x)
382 public static SqlBinary op_Addition(SqlBinary x, SqlBinary y)
384 throw new NotImplementedException("The method op_Addition in class SqlBinary is not supported");
387 public static SqlBinary op_Explicit(SqlGuid x)
391 return SqlBinary.Null;
394 return new SqlBinary(x.ToByteArray());
397 public static SqlBoolean op_Equality(SqlBinary x, SqlBinary y)
402 public static SqlBoolean op_Inequality(SqlBinary x, SqlBinary y)
404 return NotEquals(x, y);
407 public static SqlBoolean op_LessThan(SqlBinary x, SqlBinary y)
409 return LessThan(x, y);
412 public static SqlBoolean op_GreaterThan(SqlBinary x, SqlBinary y)
414 return GreaterThan(x, y);
417 public static SqlBoolean op_LessThanOrEqual(SqlBinary x, SqlBinary y)
419 return LessThanOrEqual(x, y);
422 public static SqlBoolean op_GreaterThanOrEqual(SqlBinary x, SqlBinary y)
424 return GreaterThanOrEqual(x, y);
427 public SqlGuid ToSqlGuid()
429 return SqlGuid.op_Explicit(this);