2 // System.Data.SqlTypes.SqlString
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 // Ville Palo (vi64pa@koti.soon.fi)
10 // (C) Ximian, Inc. 2002
11 // (C) Copyright 2002 Tim Coleman
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Globalization;
40 using System.Threading;
42 namespace System.Data.SqlTypes
45 /// A variable-length stream of characters
46 /// to be stored in or retrieved from the database
48 public struct SqlString : INullable, IComparable
57 // FIXME: locale id is not working yet
59 private SqlCompareOptions compareOptions;
61 public static readonly int BinarySort = 0x8000;
62 public static readonly int IgnoreCase = 0x1;
63 public static readonly int IgnoreKanaType = 0x8;
64 public static readonly int IgnoreNonSpace = 0x2;
65 public static readonly int IgnoreWidth = 0x10;
66 public static readonly SqlString Null;
68 internal static NumberFormatInfo DecimalFormat;
75 DecimalFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
76 DecimalFormat.NumberDecimalDigits = 13;
77 DecimalFormat.NumberGroupSeparator = String.Empty;
80 // init with a string data
81 public SqlString (string data)
84 lcid = CultureInfo.CurrentCulture.LCID;
86 this.compareOptions = SqlCompareOptions.IgnoreCase |
87 SqlCompareOptions.IgnoreKanaType |
88 SqlCompareOptions.IgnoreWidth;
91 // init with a string data and locale id values.
92 public SqlString (string data, int lcid)
97 this.compareOptions = SqlCompareOptions.IgnoreCase |
98 SqlCompareOptions.IgnoreKanaType |
99 SqlCompareOptions.IgnoreWidth;
102 // init with locale id, compare options,
103 // and an array of bytes data
104 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data)
105 : this (lcid, compareOptions, data, true) { }
107 // init with string data, locale id, and compare options
108 public SqlString (string data, int lcid, SqlCompareOptions compareOptions)
112 this.compareOptions = compareOptions;
116 // init with locale id, compare options, array of bytes data,
117 // and whether unicode is encoded or not
118 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, bool fUnicode)
120 Encoding encoding = (fUnicode ? Encoding.Unicode : Encoding.ASCII);
121 this.value = encoding.GetString (data);
123 this.compareOptions = compareOptions;
127 // init with locale id, compare options, array of bytes data,
128 // starting index in the byte array,
129 // and number of bytes to copy
130 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data,
131 int index, int count)
132 : this (lcid, compareOptions, data, index, count, true) { }
134 // init with locale id, compare options, array of bytes data,
135 // starting index in the byte array, number of byte to copy,
136 // and whether unicode is encoded or not
137 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, int index, int count, bool fUnicode)
139 Encoding encoding = (fUnicode ? Encoding.Unicode : Encoding.ASCII);
140 this.value = encoding.GetString (data, index, count);
142 this.compareOptions = compareOptions;
146 #endregion // Constructors
149 #region Public Properties
151 public CompareInfo CompareInfo {
153 return new CultureInfo (lcid).CompareInfo;
157 public CultureInfo CultureInfo {
159 return new CultureInfo (lcid);
164 get { return !notNull; }
167 // geographics location and language (locale id)
174 public SqlCompareOptions SqlCompareOptions {
176 return compareOptions;
180 public string Value {
183 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
189 #endregion // Public Properties
191 #region Private Properties
193 private CompareOptions CompareOptions {
196 (this.compareOptions & SqlCompareOptions.BinarySort) != 0 ?
197 CompareOptions.Ordinal :
198 // 27 == all SqlCompareOptions - BinarySort
199 // (1,2,8,24 are common to CompareOptions)
200 (CompareOptions)((int)this.compareOptions & 27);
204 #endregion Private Properties
206 #region Public Methods
208 public SqlString Clone()
210 return new SqlString (value, lcid, compareOptions);
213 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
215 CompareOptions options = CompareOptions.None;
217 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
218 options |= CompareOptions.IgnoreCase;
219 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
220 options |= CompareOptions.IgnoreKanaType;
221 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
222 options |= CompareOptions.IgnoreNonSpace;
223 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
224 options |= CompareOptions.IgnoreWidth;
225 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
226 // FIXME: Exception string
227 throw new ArgumentOutOfRangeException ();
232 // **********************************
233 // Comparison Methods
234 // **********************************
236 public int CompareTo (object value)
240 else if (!(value is SqlString))
241 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
243 return CompareSqlString ((SqlString)value);
247 private int CompareSqlString (SqlString value)
251 else if (value.CompareOptions != this.CompareOptions)
252 throw new SqlTypeException (Locale.GetText ("Two strings to be compared have different collation"));
254 // return String.Compare (this.value, ((SqlString)value).Value, (this.SqlCompareOptions & SqlCompareOptions.IgnoreCase) != 0, this.CultureInfo);
255 return CultureInfo.CompareInfo.Compare (this.value, value.Value, this.CompareOptions);
258 public static SqlString Concat(SqlString x, SqlString y)
263 public override bool Equals(object value)
265 if (!(value is SqlString))
267 if (this.IsNull && ((SqlString)value).IsNull)
269 else if (((SqlString)value).IsNull)
272 return (bool) (this == (SqlString)value);
275 public static SqlBoolean Equals(SqlString x, SqlString y)
280 public override int GetHashCode()
283 for (int i = 0; i < value.Length; i++)
284 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
286 result = 91 * result + lcid.GetHashCode ();
287 result = 91 * result + (int)compareOptions;
292 public byte[] GetNonUnicodeBytes()
294 return Encoding.ASCII.GetBytes (value);
297 public byte[] GetUnicodeBytes()
299 return Encoding.Unicode.GetBytes (value);
302 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
307 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
312 public static SqlBoolean LessThan(SqlString x, SqlString y)
317 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
322 public static SqlBoolean NotEquals(SqlString x, SqlString y)
327 // ****************************************
328 // Type Conversions From SqlString To ...
329 // ****************************************
331 public SqlBoolean ToSqlBoolean()
333 return ((SqlBoolean)this);
336 public SqlByte ToSqlByte()
338 return ((SqlByte)this);
341 public SqlDateTime ToSqlDateTime()
343 return ((SqlDateTime)this);
346 public SqlDecimal ToSqlDecimal()
348 return ((SqlDecimal)this);
351 public SqlDouble ToSqlDouble()
353 return ((SqlDouble)this);
356 public SqlGuid ToSqlGuid()
358 return ((SqlGuid)this);
361 public SqlInt16 ToSqlInt16()
363 return ((SqlInt16)this);
366 public SqlInt32 ToSqlInt32()
368 return ((SqlInt32)this);
371 public SqlInt64 ToSqlInt64()
373 return ((SqlInt64)this);
376 public SqlMoney ToSqlMoney()
378 return ((SqlMoney)this);
381 public SqlSingle ToSqlSingle()
383 return ((SqlSingle)this);
386 public override string ToString()
390 return ((string)this);
393 // ***********************************
395 // ***********************************
398 public static SqlString operator + (SqlString x, SqlString y)
400 if (x.IsNull || y.IsNull)
401 return SqlString.Null;
403 if (( x == null) || (y == null))
404 return SqlString.Null;
406 return new SqlString (x.Value + y.Value);
410 public static SqlBoolean operator == (SqlString x, SqlString y)
412 if (x.IsNull || y.IsNull)
413 return SqlBoolean.Null;
415 return new SqlBoolean (x.Value == y.Value);
419 public static SqlBoolean operator > (SqlString x, SqlString y)
421 if (x.IsNull || y.IsNull)
422 return SqlBoolean.Null;
424 return new SqlBoolean (x.CompareTo (y) > 0);
427 // Greater Than Or Equal
428 public static SqlBoolean operator >= (SqlString x, SqlString y)
430 if (x.IsNull || y.IsNull)
431 return SqlBoolean.Null;
433 return new SqlBoolean (x.CompareTo (y) >= 0);
436 public static SqlBoolean operator != (SqlString x, SqlString y)
438 if (x.IsNull || y.IsNull)
439 return SqlBoolean.Null;
441 return new SqlBoolean (x.Value != y.Value);
445 public static SqlBoolean operator < (SqlString x, SqlString y)
447 if (x.IsNull || y.IsNull)
448 return SqlBoolean.Null;
450 return new SqlBoolean (x.CompareTo (y) < 0);
453 // Less Than Or Equal
454 public static SqlBoolean operator <= (SqlString x, SqlString y)
456 if (x.IsNull || y.IsNull)
457 return SqlBoolean.Null;
459 return new SqlBoolean (x.CompareTo (y) <= 0);
462 // **************************************
464 // **************************************
466 public static explicit operator SqlString (SqlBoolean x)
471 return new SqlString (x.Value.ToString ());
474 public static explicit operator SqlString (SqlByte x)
479 return new SqlString (x.Value.ToString ());
482 public static explicit operator SqlString (SqlDateTime x)
487 return new SqlString (x.Value.ToString ());
490 public static explicit operator SqlString (SqlDecimal x)
495 return new SqlString (x.Value.ToString ());
496 // return new SqlString (x.Value.ToString ("N", DecimalFormat));
499 public static explicit operator SqlString (SqlDouble x)
504 return new SqlString (x.Value.ToString ());
507 public static explicit operator SqlString (SqlGuid x)
512 return new SqlString (x.Value.ToString ());
515 public static explicit operator SqlString (SqlInt16 x)
520 return new SqlString (x.Value.ToString ());
523 public static explicit operator SqlString (SqlInt32 x)
528 return new SqlString (x.Value.ToString ());
531 public static explicit operator SqlString (SqlInt64 x)
536 return new SqlString (x.Value.ToString ());
539 public static explicit operator SqlString (SqlMoney x)
544 return new SqlString (x.ToString ());
547 public static explicit operator SqlString (SqlSingle x)
552 return new SqlString (x.Value.ToString ());
555 public static explicit operator string (SqlString x)
560 public static implicit operator SqlString (string x)
562 return new SqlString (x);
566 public static SqlString Add (SqlString x, SqlString y)
572 public int CompareTo (SqlString value)
574 return CompareSqlString (value);
580 #endregion // Public Methods