(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Data.SybaseClient / Mono.Data.SybaseTypes / SybaseBinary.cs
blobb7f9f61ba4c3da0572429aeb39b7e4deec522aac
1 //
2 // Mono.Data.SybaseTypes.SybaseBinary
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Based on System.Data.SqlTypes.SqlBinary
8 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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;
35 using System;
36 using System.Data.SqlTypes;
37 using System.Globalization;
39 namespace Mono.Data.SybaseTypes {
40 public struct SybaseBinary : INullable, IComparable
42 #region Fields
44 byte[] value;
45 private bool notNull;
47 public static readonly SybaseBinary Null;
49 #endregion
51 #region Constructors
53 public SybaseBinary (byte[] value)
55 this.value = value;
56 notNull = true;
59 #endregion
61 #region Properties
63 public bool IsNull {
64 get { return !notNull; }
67 public byte this[int index] {
68 get {
69 if (this.IsNull)
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.");
73 else
74 return value [index];
78 public int Length {
79 get {
80 if (this.IsNull)
81 throw new SybaseNullValueException ("The property contains Null.");
82 else
83 return value.Length;
87 public byte[] Value
89 get {
90 if (this.IsNull)
91 throw new SybaseNullValueException ("The property contains Null.");
92 else
93 return value;
97 #endregion
99 #region Methods
101 public int CompareTo (object value)
103 if (value == null)
104 return 1;
105 else if (!(value is SybaseBinary))
106 throw new ArgumentException ("Value is not a Mono.Data.SybaseTypes.SybaseBinary.");
107 else if (((SybaseBinary) value).IsNull)
108 return 1;
109 else
110 return Compare (this, (SybaseBinary) value);
113 public static SybaseBinary Concat (SybaseBinary x, SybaseBinary y)
115 return (x + y);
118 public override bool Equals (object value)
120 if (!(value is SybaseBinary))
121 return false;
122 else
123 return (bool) (this == (SybaseBinary)value);
126 public static SybaseBoolean Equals (SybaseBinary x, SybaseBinary y)
128 return (x == y);
131 public override int GetHashCode ()
133 int result = 10;
134 for (int i = 0; i < value.Length; i += 1)
135 result = 91 * result + ((int) value [i]);
136 return result;
139 #endregion
141 #region Operators
143 public static SybaseBoolean GreaterThan (SybaseBinary x, SybaseBinary y)
145 return (x > y);
148 public static SybaseBoolean GreaterThanOrEqual (SybaseBinary x, SybaseBinary y)
150 return (x >= y);
153 public static SybaseBoolean LessThan (SybaseBinary x, SybaseBinary y)
155 return (x < y);
158 public static SybaseBoolean LessThanOrEqual (SybaseBinary x, SybaseBinary y)
160 return (x <= y);
163 public static SybaseBoolean NotEquals (SybaseBinary x, SybaseBinary y)
165 return (x != y);
168 public SybaseGuid ToSybaseGuid ()
170 return new SybaseGuid (value);
173 public override string ToString ()
175 if (IsNull)
176 return "null";
177 return String.Format ("SybaseBinary ({0})", Length);
180 #endregion
182 #region Operators
184 public static SybaseBinary operator + (SybaseBinary x, SybaseBinary y)
186 byte[] b = new byte [x.Length + y.Length];
187 int j = 0;
188 int i;
190 for (i = 0; i < x.Length; i += 1)
191 b [i] = x [i];
193 for (; i < x.Length + y.Length; i += 1) {
194 b [i] = y [j];
195 j += 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;
205 else
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;
213 else
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;
221 else
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;
229 else
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;
237 else
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;
245 else
246 return new SybaseBoolean (Compare (x, y) <= 0);
249 public static explicit operator byte[] (SybaseBinary x)
251 return x.Value;
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)
266 int lengthDiff = 0;
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)
275 return 1;
276 } else {
277 for (int i = y.Length - 1; i > y.Length - lengthDiff; i -= 1)
278 if (y [i] != (byte) 0)
279 return -1;
283 // choose shorter
284 int len = (lengthDiff > 0) ? y.Length : x.Length;
286 for (int i = len - 1; i > 0; i -= 1) {
287 byte bx = x [i];
288 byte by = y [i];
290 if (bx > by)
291 return 1;
292 else if (bx < by)
293 return -1;
296 return 0;
299 #endregion