2 // Mono.Data.SybaseTypes.SybaseString
5 // Tim Coleman (tim@timcoleman.com)
7 // Based on System.Data.SqlTypes.SqlString
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
;
40 namespace Mono
.Data
.SybaseTypes
{
41 public struct SybaseString
: INullable
, IComparable
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
;
63 // init with a string data
64 public SybaseString (string data
)
67 this.cultureInfo
= CultureInfo
.CurrentCulture
;
68 this.compareOptions
= SybaseCompareOptions
.None
;
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
)
89 this.cultureInfo
= new CultureInfo (lcid
);
90 this.compareOptions
= compareOptions
;
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
)
100 encoding
= new UnicodeEncoding ();
102 encoding
= new ASCIIEncoding ();
104 this.value = encoding
.GetString (data
);
105 this.cultureInfo
= new CultureInfo (lcid
);
106 this.compareOptions
= compareOptions
;
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
)
125 encoding
= new UnicodeEncoding ();
127 encoding
= new ASCIIEncoding ();
129 this.value = encoding
.GetString (data
, index
, count
);
130 this.cultureInfo
= new CultureInfo (lcid
);
131 this.compareOptions
= compareOptions
;
135 #endregion // Constructors
138 #region Public Properties
140 public CompareInfo CompareInfo
{
141 get { return cultureInfo.CompareInfo; }
144 public CultureInfo CultureInfo
{
145 get { return cultureInfo; }
149 get { return !notNull; }
152 // geographics location and language (locale id)
154 get { return cultureInfo.LCID; }
157 public SybaseCompareOptions SybaseCompareOptions
{
158 get { return compareOptions; }
161 public string Value
{
164 throw new SybaseNullValueException ("The property contains Null.");
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 ();
195 // **********************************
196 // Comparison Methods
197 // **********************************
199 public int CompareTo(object value)
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
)
208 return this.value.CompareTo (((SybaseString
)value).Value
);
211 public static SybaseString
Concat(SybaseString x
, SybaseString y
)
216 public override bool Equals(object value)
218 if (!(value is SybaseString
))
221 return (bool) (this == (SybaseString
)value);
224 public static SybaseBoolean
Equals(SybaseString x
, SybaseString y
)
229 public override int GetHashCode()
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
;
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);
258 public static SybaseBoolean
GreaterThan(SybaseString x
, SybaseString y
)
263 public static SybaseBoolean
GreaterThanOrEqual(SybaseString x
, SybaseString y
)
268 public static SybaseBoolean
LessThan(SybaseString x
, SybaseString y
)
273 public static SybaseBoolean
LessThanOrEqual(SybaseString x
, SybaseString y
)
278 public static SybaseBoolean
NotEquals(SybaseString x
, SybaseString 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 // ***********************************
349 // ***********************************
352 public static SybaseString
operator + (SybaseString x
, SybaseString y
)
354 return new SybaseString (x
.Value
+ y
.Value
);
358 public static SybaseBoolean
operator == (SybaseString x
, SybaseString y
)
360 if (x
.IsNull
|| y
.IsNull
)
361 return SybaseBoolean
.Null
;
363 return new SybaseBoolean (x
.Value
== y
.Value
);
367 public static SybaseBoolean
operator > (SybaseString x
, SybaseString y
)
369 if (x
.IsNull
|| y
.IsNull
)
370 return SybaseBoolean
.Null
;
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
;
381 throw new NotImplementedException ();
384 public static SybaseBoolean
operator != (SybaseString x
, SybaseString y
)
386 if (x
.IsNull
|| y
.IsNull
)
387 return SybaseBoolean
.Null
;
389 return new SybaseBoolean (x
.Value
!= y
.Value
);
393 public static SybaseBoolean
operator < (SybaseString x
, SybaseString y
)
395 if (x
.IsNull
|| y
.IsNull
)
396 return SybaseBoolean
.Null
;
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
;
407 throw new NotImplementedException ();
410 // **************************************
412 // **************************************
414 public static explicit operator SybaseString (SybaseBoolean x
)
419 return new SybaseString (x
.ByteValue
.ToString ());
422 public static explicit operator SybaseString (SybaseByte x
)
427 return new SybaseString (x
.Value
.ToString ());
430 public static explicit operator SybaseString (SybaseDateTime x
)
435 return new SybaseString (x
.Value
.ToString ());
438 public static explicit operator SybaseString (SybaseDecimal x
)
443 return new SybaseString (x
.Value
.ToString ());
446 public static explicit operator SybaseString (SybaseDouble x
)
451 return new SybaseString (x
.Value
.ToString ());
454 public static explicit operator SybaseString (SybaseGuid x
)
459 return new SybaseString (x
.Value
.ToString ());
462 public static explicit operator SybaseString (SybaseInt16 x
)
467 return new SybaseString (x
.Value
.ToString ());
470 public static explicit operator SybaseString (SybaseInt32 x
)
475 return new SybaseString (x
.Value
.ToString ());
478 public static explicit operator SybaseString (SybaseInt64 x
)
483 return new SybaseString (x
.Value
.ToString ());
486 public static explicit operator SybaseString (SybaseMoney x
)
491 return new SybaseString (x
.Value
.ToString ());
494 public static explicit operator SybaseString (SybaseSingle x
)
499 return new SybaseString (x
.Value
.ToString ());
502 public static explicit operator string (SybaseString x
)
507 public static implicit operator SybaseString (string x
)
509 return new SybaseString (x
);
512 #endregion // Public Methods