2 // Mono.Data.SybaseTypes.SybaseBinary
5 // Tim Coleman (tim@timcoleman.com)
7 // Based on System.Data.SqlTypes.SqlBinary
9 // (C) Ximian, Inc. 2002-2003
10 // (C) Copyright Tim Coleman, 2002-2003
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using Mono
.Data
.SybaseClient
;
36 using System
.Data
.SqlTypes
;
37 using System
.Globalization
;
39 namespace Mono
.Data
.SybaseTypes
{
40 public struct SybaseBinary
: INullable
, IComparable
47 public static readonly SybaseBinary Null
;
53 public SybaseBinary (byte[] value)
64 get { return !notNull; }
67 public byte this[int index
] {
70 throw new SybaseNullValueException ("The property contains Null.");
71 else if (index
>= this.Length
)
72 throw new SybaseNullValueException ("The index parameter indicates a position beyond the length of the byte array.");
81 throw new SybaseNullValueException ("The property contains Null.");
91 throw new SybaseNullValueException ("The property contains Null.");
101 public int CompareTo (object value)
105 else if (!(value is SybaseBinary
))
106 throw new ArgumentException ("Value is not a Mono.Data.SybaseTypes.SybaseBinary.");
107 else if (((SybaseBinary
) value).IsNull
)
110 return Compare (this, (SybaseBinary
) value);
113 public static SybaseBinary
Concat (SybaseBinary x
, SybaseBinary y
)
118 public override bool Equals (object value)
120 if (!(value is SybaseBinary
))
123 return (bool) (this == (SybaseBinary
)value);
126 public static SybaseBoolean
Equals (SybaseBinary x
, SybaseBinary y
)
131 public override int GetHashCode ()
134 for (int i
= 0; i
< value.Length
; i
+= 1)
135 result
= 91 * result
+ ((int) value [i
]);
143 public static SybaseBoolean
GreaterThan (SybaseBinary x
, SybaseBinary y
)
148 public static SybaseBoolean
GreaterThanOrEqual (SybaseBinary x
, SybaseBinary y
)
153 public static SybaseBoolean
LessThan (SybaseBinary x
, SybaseBinary y
)
158 public static SybaseBoolean
LessThanOrEqual (SybaseBinary x
, SybaseBinary y
)
163 public static SybaseBoolean
NotEquals (SybaseBinary x
, SybaseBinary y
)
168 public SybaseGuid
ToSybaseGuid ()
170 return new SybaseGuid (value);
173 public override string ToString ()
177 return String
.Format ("SybaseBinary ({0})", Length
);
184 public static SybaseBinary
operator + (SybaseBinary x
, SybaseBinary y
)
186 byte[] b
= new byte [x
.Length
+ y
.Length
];
190 for (i
= 0; i
< x
.Length
; i
+= 1)
193 for (; i
< x
.Length
+ y
.Length
; i
+= 1) {
198 return new SybaseBinary (b
);
201 public static SybaseBoolean
operator == (SybaseBinary x
, SybaseBinary y
)
203 if (x
.IsNull
|| y
.IsNull
)
204 return SybaseBoolean
.Null
;
206 return new SybaseBoolean (Compare (x
, y
) == 0);
209 public static SybaseBoolean
operator > (SybaseBinary x
, SybaseBinary y
)
211 if (x
.IsNull
|| y
.IsNull
)
212 return SybaseBoolean
.Null
;
214 return new SybaseBoolean (Compare (x
, y
) > 0);
217 public static SybaseBoolean
operator >= (SybaseBinary x
, SybaseBinary y
)
219 if (x
.IsNull
|| y
.IsNull
)
220 return SybaseBoolean
.Null
;
222 return new SybaseBoolean (Compare (x
, y
) >= 0);
225 public static SybaseBoolean
operator != (SybaseBinary x
, SybaseBinary y
)
227 if (x
.IsNull
|| y
.IsNull
)
228 return SybaseBoolean
.Null
;
230 return new SybaseBoolean (Compare (x
, y
) != 0);
233 public static SybaseBoolean
operator < (SybaseBinary x
, SybaseBinary y
)
235 if (x
.IsNull
|| y
.IsNull
)
236 return SybaseBoolean
.Null
;
238 return new SybaseBoolean (Compare (x
, y
) < 0);
241 public static SybaseBoolean
operator <= (SybaseBinary x
, SybaseBinary y
)
243 if (x
.IsNull
|| y
.IsNull
)
244 return SybaseBoolean
.Null
;
246 return new SybaseBoolean (Compare (x
, y
) <= 0);
249 public static explicit operator byte[] (SybaseBinary x
)
254 public static explicit operator SybaseBinary (SybaseGuid x
)
256 return new SybaseBinary (x
.ToByteArray ());
259 public static implicit operator SybaseBinary (byte[] x
)
261 return new SybaseBinary (x
);
264 private static int Compare (SybaseBinary x
, SybaseBinary y
)
268 if (x
.Length
!= y
.Length
) {
269 lengthDiff
= x
.Length
- y
.Length
;
271 // if diff more than 0, x is longer
272 if (lengthDiff
> 0) {
273 for (int i
= x
.Length
- 1; i
> x
.Length
- lengthDiff
; i
-= 1)
274 if (x
[i
] != (byte) 0)
277 for (int i
= y
.Length
- 1; i
> y
.Length
- lengthDiff
; i
-= 1)
278 if (y
[i
] != (byte) 0)
284 int len
= (lengthDiff
> 0) ? y
.Length
: x
.Length
;
286 for (int i
= len
- 1; i
> 0; i
-= 1) {