Fix a few cases of mixed line-endings
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes.jvm / SqlBinary.cs
1 // System.Data.SqlTypes.SqlBinary
2 //
3 // Authors:
4 //      Konstantin Triger <kostat@mainsoft.com>
5 //      Boris Kirzner <borisk@mainsoft.com>
6 //      
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 namespace System.Data.SqlTypes
32 {
33
34     using System;
35     //using clr.exceptions;
36     //using clr.compiler.BitConstants;
37
38     /**
39      *
40      */
41     public struct SqlBinary : INullable, IComparable
42     {
43         public static readonly SqlBinary Null = new SqlBinary(true);
44         private bool _isNull;
45         private byte[] _value;
46         
47         private SqlBinary(bool isNull)
48         {
49             _isNull = isNull;
50             _value = null;
51         }
52         
53         /**
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.
57          */
58         public SqlBinary(byte[] value)
59         {
60             if (value != null && value.Length > 0)
61             {
62                 _value = new byte[value.Length];
63                 Array.Copy (value, 0, _value, 0, value.Length);
64             }
65             else
66                 _value = new byte[0];
67             
68             _isNull = false;
69         }
70
71         
72         /**
73          * Indicates whether or not Value is null.
74          * @return true if Value is null, otherwise false.
75          */
76         public bool IsNull
77         {
78             get
79             {
80                 return _isNull;
81             }
82         }
83
84         /**
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.
89          */
90         public int this[int index]
91         {
92             get
93             {
94                 if (IsNull)
95                 {
96                     throw new SqlNullValueException();
97                 }
98                 if (index >= _value.Length)
99                 {
100                     throw new ArgumentOutOfRangeException("index");
101                 }
102                 return _value[index];// & BitConstants.ALL_BYTE;
103             }
104         }
105
106         /**
107          * Gets the length in bytes of the Value property.
108          * @return The length of the binary data in the Value property.
109          */
110         public int Length
111         {
112             get
113             {
114                 if (IsNull)
115                 {
116                     throw new SqlNullValueException();
117                 }
118                 return _value.Length;
119             }
120         }
121
122         /**
123          * Gets the value of the SqlBinary instance.
124          * @return the value of this instance
125          */
126         public byte[] Value
127         {
128             get
129             {
130                 if (IsNull)
131                 {
132                     throw new SqlNullValueException();
133                 }
134                 return _value;
135             }
136         }
137
138         /**
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.
146          */
147         public int CompareTo(Object value)
148         {
149             if (value == null)
150                 return 1;
151
152             if ((value is SqlBinary) == false)
153             {
154                 throw new ArgumentException("Wrong value type " + 
155                     value.GetType().Name + "in SqlBinary.CompareTo");
156             }
157
158             SqlBinary obj = (SqlBinary)value;
159
160             if (this.IsNull)
161             {
162                 if (obj.IsNull)
163                     return 0;
164                 return -1;
165             }
166             else if (obj.IsNull)
167                 return 1;
168
169             int length = _value.Length > obj._value.Length ? _value.Length : obj._value.Length;
170
171             for (int i = 0; i < length; i++)
172             {
173                 if (_value[i] > obj._value[i])
174                     return 1;
175                 if (_value[i] < obj._value[i])
176                     return -1;
177             }
178
179             if (_value.Length > obj._value.Length)
180                 return 1;
181             if (_value.Length < obj._value.Length)
182                 return -1;
183
184             return 0;
185
186         }
187
188         /**
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.
193          */
194         public static SqlBinary Concat(SqlBinary x, SqlBinary y)
195         {
196             if (x.IsNull)
197             {
198                 if (y.IsNull)
199                 {
200                     return SqlBinary.Null;
201                 }
202                 else
203                     return new SqlBinary((byte[])y._value);
204             }
205             else if (y.IsNull)
206                 return new SqlBinary((byte[])x._value);
207
208             byte[] newVal = new byte[x.Length + y.Length];
209
210             java.lang.System.arraycopy(x, 0, newVal, 0, x.Length);
211             java.lang.System.arraycopy(y, 0, newVal, x.Length, y.Length);
212
213             return new SqlBinary(newVal);
214
215         }
216
217         /**
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.
223          */
224         public static SqlBoolean Equals(SqlBinary x, SqlBinary y)
225         {
226             if (x.IsNull || y.IsNull)
227                 return SqlBoolean.Null;
228
229             if (x.Equals(y))
230                 return SqlBoolean.True;
231
232             return SqlBoolean.False;
233
234         }
235
236         public bool equals(Object obj)
237         {
238             if (obj == null)
239                 return false;
240
241             if (obj is SqlBinary)
242             {
243                 SqlBinary o = (SqlBinary)obj;
244
245                 if (IsNull && o.IsNull)
246                     return true;
247
248                 if (IsNull || o.IsNull)
249                     return false;
250
251                 if (_value.Length != o._value.Length)
252                     return false;
253
254                 for (int i = 0; i < _value.Length; i++)
255                 {
256                     if (_value[i] != o._value[i])
257                         return false;
258                 }
259
260                 return true;
261             }
262
263             return false;
264         }
265
266         /**
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.
272          */
273         public static SqlBoolean GreaterThan(SqlBinary x, SqlBinary y)
274         {
275             if (x.IsNull || y.IsNull)
276                 return SqlBoolean.Null;
277
278             int i = x.CompareTo(y);
279
280             if ( i > 0)
281                 return SqlBoolean.True;
282
283             return SqlBoolean.False;
284         }
285
286         /**
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.
292          */
293         public static SqlBoolean GreaterThanOrEqual(SqlBinary x, SqlBinary y)
294         {
295             if (x.IsNull || y.IsNull)
296                 return SqlBoolean.Null;
297
298             int i = x.CompareTo(y);
299
300             if ( i < 0)
301                 return SqlBoolean.False;
302
303             return SqlBoolean.True;
304         }
305
306         /**
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.
312          */
313         public static SqlBoolean LessThan(SqlBinary x, SqlBinary y)
314         {
315             if (x.IsNull || y.IsNull)
316                 return SqlBoolean.Null;
317
318             int i = x.CompareTo(y);
319
320             if ( i < 0)
321                 return SqlBoolean.True;
322
323             return SqlBoolean.False;
324         }
325
326         /**
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.
332          */
333         public static SqlBoolean LessThanOrEqual(SqlBinary x, SqlBinary y)
334         {
335             if (x.IsNull || y.IsNull)
336                 return SqlBoolean.Null;
337
338             int i = x.CompareTo(y);
339
340             if ( i > 0)
341                 return SqlBoolean.False;
342
343             return SqlBoolean.True;
344         }
345
346         /**
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.
352          */
353         public static SqlBoolean NotEquals(SqlBinary x, SqlBinary y)
354         {
355             if (x.IsNull || y.IsNull)
356                 return SqlBoolean.Null;
357
358             if (x.Equals(y))
359                 return SqlBoolean.False;
360
361             return SqlBoolean.True;
362         }
363
364         public String toString()
365         {
366             if (IsNull)
367                 return "null";
368
369             return "SqlBinary(" + _value.ToString() + ")";
370         }
371
372         public static SqlBinary op_Implicit(byte[] x)
373         {
374             return new SqlBinary(x);
375         }
376
377         public static byte[] op_Explicit(SqlBinary x)
378         {
379             return x.Value;
380         }
381
382         public static SqlBinary op_Addition(SqlBinary x, SqlBinary y)
383         {
384             throw new NotImplementedException("The method op_Addition in class SqlBinary is not supported");
385         }
386
387         public static SqlBinary op_Explicit(SqlGuid x)
388         {
389             if(x.IsNull)
390             {
391                 return SqlBinary.Null;
392             }
393
394             return new SqlBinary(x.ToByteArray());
395         }
396
397         public static SqlBoolean op_Equality(SqlBinary x, SqlBinary y)
398         {
399             return Equals(x, y);
400         }
401
402         public static SqlBoolean op_Inequality(SqlBinary x, SqlBinary y)
403         {
404             return NotEquals(x, y);
405         }
406
407         public static SqlBoolean op_LessThan(SqlBinary x, SqlBinary y)
408         {
409             return LessThan(x, y);
410         }
411
412         public static SqlBoolean op_GreaterThan(SqlBinary x, SqlBinary y)
413         {
414             return GreaterThan(x, y);
415         }
416
417         public static SqlBoolean op_LessThanOrEqual(SqlBinary x, SqlBinary y)
418         {
419             return LessThanOrEqual(x, y);
420         }
421
422         public static SqlBoolean op_GreaterThanOrEqual(SqlBinary x, SqlBinary y)
423         {
424             return GreaterThanOrEqual(x, y);
425         }
426
427         public SqlGuid ToSqlGuid()
428         {
429             return SqlGuid.op_Explicit(this);
430         }
431     }}