(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Data.SybaseClient / Mono.Data.SybaseTypes / SybaseString.cs
blob4af7c32cef17f3dacb6e2b80a3c798daf22f57fd
1 //
2 // Mono.Data.SybaseTypes.SybaseString
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Based on System.Data.SqlTypes.SqlString
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;
38 using System.Text;
40 namespace Mono.Data.SybaseTypes {
41 public struct SybaseString : INullable, IComparable
43 #region Fields
45 string value;
47 private bool notNull;
49 private CultureInfo cultureInfo;
50 private SybaseCompareOptions compareOptions;
52 public static readonly int BinarySort = 0x8000;
53 public static readonly int IgnoreCase = 0x1;
54 public static readonly int IgnoreKanaType = 0x8;
55 public static readonly int IgnoreNonSpace = 0x2;
56 public static readonly int IgnoreWidth = 0x10;
57 public static readonly SybaseString Null;
59 #endregion // Fields
61 #region Constructors
63 // init with a string data
64 public SybaseString (string data)
66 this.value = data;
67 this.cultureInfo = CultureInfo.CurrentCulture;
68 this.compareOptions = SybaseCompareOptions.None;
69 this.notNull = true;
72 // init with a string data and locale id values.
73 public SybaseString (string data, int lcid)
74 : this (data, lcid, SybaseCompareOptions.None)
78 // init with locale id, compare options,
79 // and an array of bytes data
80 public SybaseString (int lcid, SybaseCompareOptions compareOptions, byte[] data)
81 : this (lcid, compareOptions, data, true)
85 // init with string data, locale id, and compare options
86 public SybaseString (string data, int lcid, SybaseCompareOptions compareOptions)
88 this.value = data;
89 this.cultureInfo = new CultureInfo (lcid);
90 this.compareOptions = compareOptions;
91 this.notNull = true;
94 // init with locale id, compare options, array of bytes data,
95 // and whether unicode is encoded or not
96 public SybaseString (int lcid, SybaseCompareOptions compareOptions, byte[] data, bool fUnicode)
98 Encoding encoding;
99 if (fUnicode)
100 encoding = new UnicodeEncoding ();
101 else
102 encoding = new ASCIIEncoding ();
104 this.value = encoding.GetString (data);
105 this.cultureInfo = new CultureInfo (lcid);
106 this.compareOptions = compareOptions;
107 this.notNull = true;
110 // init with locale id, compare options, array of bytes data,
111 // starting index in the byte array,
112 // and number of bytes to copy
113 public SybaseString (int lcid, SybaseCompareOptions compareOptions, byte[] data, int index, int count)
114 : this (lcid, compareOptions, data, index, count, true)
118 // init with locale id, compare options, array of bytes data,
119 // starting index in the byte array, number of byte to copy,
120 // and whether unicode is encoded or not
121 public SybaseString (int lcid, SybaseCompareOptions compareOptions, byte[] data, int index, int count, bool fUnicode)
123 Encoding encoding;
124 if (fUnicode)
125 encoding = new UnicodeEncoding ();
126 else
127 encoding = new ASCIIEncoding ();
129 this.value = encoding.GetString (data, index, count);
130 this.cultureInfo = new CultureInfo (lcid);
131 this.compareOptions = compareOptions;
132 this.notNull = true;
135 #endregion // Constructors
138 #region Public Properties
140 public CompareInfo CompareInfo {
141 get { return cultureInfo.CompareInfo; }
144 public CultureInfo CultureInfo {
145 get { return cultureInfo; }
148 public bool IsNull {
149 get { return !notNull; }
152 // geographics location and language (locale id)
153 public int LCID {
154 get { return cultureInfo.LCID; }
157 public SybaseCompareOptions SybaseCompareOptions {
158 get { return compareOptions; }
161 public string Value {
162 get {
163 if (this.IsNull)
164 throw new SybaseNullValueException ("The property contains Null.");
165 else
166 return value;
170 #endregion // Public Properties
172 #region Public Methods
174 public SybaseString Clone()
176 return new SybaseString (value, LCID, SybaseCompareOptions);
179 public static CompareOptions CompareOptionsFromSybaseCompareOptions (SybaseCompareOptions compareOptions)
181 CompareOptions options = CompareOptions.None;
182 if ((compareOptions & SybaseCompareOptions.IgnoreCase) != 0)
183 options |= CompareOptions.IgnoreCase;
184 if ((compareOptions & SybaseCompareOptions.IgnoreKanaType) != 0)
185 options |= CompareOptions.IgnoreKanaType;
186 if ((compareOptions & SybaseCompareOptions.IgnoreNonSpace) != 0)
187 options |= CompareOptions.IgnoreNonSpace;
188 if ((compareOptions & SybaseCompareOptions.IgnoreWidth) != 0)
189 options |= CompareOptions.IgnoreWidth;
190 if ((compareOptions & SybaseCompareOptions.BinarySort) != 0)
191 throw new ArgumentOutOfRangeException ();
192 return options;
195 // **********************************
196 // Comparison Methods
197 // **********************************
199 public int CompareTo(object value)
201 if (value == null)
202 return 1;
203 else if (!(value is SybaseString))
204 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SybaseTypes.SybaseString"));
205 else if (((SybaseString)value).IsNull)
206 return 1;
207 else
208 return this.value.CompareTo (((SybaseString)value).Value);
211 public static SybaseString Concat(SybaseString x, SybaseString y)
213 return (x + y);
216 public override bool Equals(object value)
218 if (!(value is SybaseString))
219 return false;
220 else
221 return (bool) (this == (SybaseString)value);
224 public static SybaseBoolean Equals(SybaseString x, SybaseString y)
226 return (x == y);
229 public override int GetHashCode()
231 int result = 10;
232 for (int i = 0; i < value.Length; i += 1)
233 result = 91 * result + (int) (value [i] ^ (value [i] >> 32));
234 result = 91 * result + LCID.GetHashCode ();
235 result = 91 * result + (int) compareOptions;
236 return result;
239 public byte[] GetNonUnicodeBytes()
241 return GetBytes (new ASCIIEncoding ());
244 public byte[] GetUnicodeBytes()
246 return GetBytes (new UnicodeEncoding ());
249 private byte[] GetBytes (Encoding encoding)
251 int blen = encoding.GetByteCount (value);
252 int clen = value.Length;
253 byte[] bytes = new byte [blen];
254 encoding.GetBytes (value, 0, clen, bytes, 0);
255 return bytes;
258 public static SybaseBoolean GreaterThan(SybaseString x, SybaseString y)
260 return (x > y);
263 public static SybaseBoolean GreaterThanOrEqual(SybaseString x, SybaseString y)
265 return (x >= y);
268 public static SybaseBoolean LessThan(SybaseString x, SybaseString y)
270 return (x < y);
273 public static SybaseBoolean LessThanOrEqual(SybaseString x, SybaseString y)
275 return (x <= y);
278 public static SybaseBoolean NotEquals(SybaseString x, SybaseString y)
280 return (x != y);
283 // ****************************************
284 // Type Conversions From SybaseString To ...
285 // ****************************************
287 public SybaseBoolean ToSybaseBoolean()
289 return ((SybaseBoolean)this);
292 public SybaseByte ToSybaseByte()
294 return ((SybaseByte)this);
297 public SybaseDateTime ToSybaseDateTime()
299 return ((SybaseDateTime)this);
302 public SybaseDecimal ToSybaseDecimal()
304 return ((SybaseDecimal)this);
307 public SybaseDouble ToSybaseDouble()
309 return ((SybaseDouble)this);
312 public SybaseGuid ToSybaseGuid()
314 return ((SybaseGuid)this);
317 public SybaseInt16 ToSybaseInt16()
319 return ((SybaseInt16)this);
322 public SybaseInt32 ToSybaseInt32()
324 return ((SybaseInt32)this);
327 public SybaseInt64 ToSybaseInt64()
329 return ((SybaseInt64)this);
332 public SybaseMoney ToSybaseMoney()
334 return ((SybaseMoney)this);
337 public SybaseSingle ToSybaseSingle()
339 return ((SybaseSingle)this);
342 public override string ToString()
344 return ((string)this);
347 // ***********************************
348 // Operators
349 // ***********************************
351 // Concatenates
352 public static SybaseString operator + (SybaseString x, SybaseString y)
354 return new SybaseString (x.Value + y.Value);
357 // Equality
358 public static SybaseBoolean operator == (SybaseString x, SybaseString y)
360 if (x.IsNull || y.IsNull)
361 return SybaseBoolean.Null;
362 else
363 return new SybaseBoolean (x.Value == y.Value);
366 // Greater Than
367 public static SybaseBoolean operator > (SybaseString x, SybaseString y)
369 if (x.IsNull || y.IsNull)
370 return SybaseBoolean.Null;
371 else
372 throw new NotImplementedException ();
375 // Greater Than Or Equal
376 public static SybaseBoolean operator >= (SybaseString x, SybaseString y)
378 if (x.IsNull || y.IsNull)
379 return SybaseBoolean.Null;
380 else
381 throw new NotImplementedException ();
384 public static SybaseBoolean operator != (SybaseString x, SybaseString y)
386 if (x.IsNull || y.IsNull)
387 return SybaseBoolean.Null;
388 else
389 return new SybaseBoolean (x.Value != y.Value);
392 // Less Than
393 public static SybaseBoolean operator < (SybaseString x, SybaseString y)
395 if (x.IsNull || y.IsNull)
396 return SybaseBoolean.Null;
397 else
398 throw new NotImplementedException ();
401 // Less Than Or Equal
402 public static SybaseBoolean operator <= (SybaseString x, SybaseString y)
404 if (x.IsNull || y.IsNull)
405 return SybaseBoolean.Null;
406 else
407 throw new NotImplementedException ();
410 // **************************************
411 // Type Conversions
412 // **************************************
414 public static explicit operator SybaseString (SybaseBoolean x)
416 if (x.IsNull)
417 return Null;
418 else
419 return new SybaseString (x.ByteValue.ToString ());
422 public static explicit operator SybaseString (SybaseByte x)
424 if (x.IsNull)
425 return Null;
426 else
427 return new SybaseString (x.Value.ToString ());
430 public static explicit operator SybaseString (SybaseDateTime x)
432 if (x.IsNull)
433 return Null;
434 else
435 return new SybaseString (x.Value.ToString ());
438 public static explicit operator SybaseString (SybaseDecimal x)
440 if (x.IsNull)
441 return Null;
442 else
443 return new SybaseString (x.Value.ToString ());
446 public static explicit operator SybaseString (SybaseDouble x)
448 if (x.IsNull)
449 return Null;
450 else
451 return new SybaseString (x.Value.ToString ());
454 public static explicit operator SybaseString (SybaseGuid x)
456 if (x.IsNull)
457 return Null;
458 else
459 return new SybaseString (x.Value.ToString ());
462 public static explicit operator SybaseString (SybaseInt16 x)
464 if (x.IsNull)
465 return Null;
466 else
467 return new SybaseString (x.Value.ToString ());
470 public static explicit operator SybaseString (SybaseInt32 x)
472 if (x.IsNull)
473 return Null;
474 else
475 return new SybaseString (x.Value.ToString ());
478 public static explicit operator SybaseString (SybaseInt64 x)
480 if (x.IsNull)
481 return Null;
482 else
483 return new SybaseString (x.Value.ToString ());
486 public static explicit operator SybaseString (SybaseMoney x)
488 if (x.IsNull)
489 return Null;
490 else
491 return new SybaseString (x.Value.ToString ());
494 public static explicit operator SybaseString (SybaseSingle x)
496 if (x.IsNull)
497 return Null;
498 else
499 return new SybaseString (x.Value.ToString ());
502 public static explicit operator string (SybaseString x)
504 return x.Value;
507 public static implicit operator SybaseString (string x)
509 return new SybaseString (x);
512 #endregion // Public Methods