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.
38 using System.Globalization;
39 using System.Threading;
41 namespace System.Data.SqlTypes
44 /// A variable-length stream of characters
45 /// to be stored in or retrieved from the database
47 public struct SqlString : INullable, IComparable
56 // FIXME: locale id is not working yet
58 private SqlCompareOptions compareOptions;
60 public static readonly int BinarySort = 0x8000;
61 public static readonly int IgnoreCase = 0x1;
62 public static readonly int IgnoreKanaType = 0x8;
63 public static readonly int IgnoreNonSpace = 0x2;
64 public static readonly int IgnoreWidth = 0x10;
65 public static readonly SqlString Null;
67 internal static readonly NumberFormatInfo MoneyFormat;
68 internal static NumberFormatInfo DecimalFormat;
75 MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
76 MoneyFormat.NumberDecimalDigits = 4;
77 MoneyFormat.NumberGroupSeparator = String.Empty;
79 DecimalFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
80 DecimalFormat.NumberDecimalDigits = 13;
81 DecimalFormat.NumberGroupSeparator = String.Empty;
84 // init with a string data
85 public SqlString (string data)
88 lcid = CultureInfo.CurrentCulture.LCID;
90 this.compareOptions = SqlCompareOptions.IgnoreCase |
91 SqlCompareOptions.IgnoreKanaType |
92 SqlCompareOptions.IgnoreWidth;
95 // init with a string data and locale id values.
96 public SqlString (string data, int lcid)
101 this.compareOptions = SqlCompareOptions.IgnoreCase |
102 SqlCompareOptions.IgnoreKanaType |
103 SqlCompareOptions.IgnoreWidth;
106 // init with locale id, compare options,
107 // and an array of bytes data
108 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data)
109 : this (lcid, compareOptions, data, true) { }
111 // init with string data, locale id, and compare options
112 public SqlString (string data, int lcid, SqlCompareOptions compareOptions)
116 this.compareOptions = compareOptions;
120 // init with locale id, compare options, array of bytes data,
121 // and whether unicode is encoded or not
122 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, bool fUnicode)
127 chars = new char [data.Length/2];
129 chars = new char [data.Length];
132 for (int i = 0; i < chars.Length; i++) {
135 chars [i] = (char)(data [j] << 16);
136 chars [i] += (char)data [j + 1];
139 chars [i] = (char)data[i];
143 this.value = new String (chars);
145 this.compareOptions = compareOptions;
149 // init with locale id, compare options, array of bytes data,
150 // starting index in the byte array,
151 // and number of bytes to copy
152 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data,
153 int index, int count)
154 : this (lcid, compareOptions, data, index, count, true) { }
156 // init with locale id, compare options, array of bytes data,
157 // starting index in the byte array, number of byte to copy,
158 // and whether unicode is encoded or not
159 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, int index, int count, bool fUnicode)
164 chars = new char [(count - index) / 2];
166 chars = new char [count - index];
168 if (index >= data.Length)
169 throw new ArgumentOutOfRangeException ("index");
171 if ((index + count) > data.Length)
172 throw new ArgumentOutOfRangeException ("count");
175 for (int i = index; i < chars.Length; i++) {
178 chars [i] = (char)(data[j] << 16);
179 chars [i] += (char)data[j+1];
182 chars [i] = (char)data [j];
187 this.value = new String (chars);
189 this.compareOptions = compareOptions;
193 #endregion // Constructors
196 #region Public Properties
198 public CompareInfo CompareInfo {
200 return new CultureInfo (lcid).CompareInfo;
204 public CultureInfo CultureInfo {
206 return new CultureInfo (lcid);
211 get { return !notNull; }
214 // geographics location and language (locale id)
221 public SqlCompareOptions SqlCompareOptions {
223 return compareOptions;
227 public string Value {
230 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
236 #endregion // Public Properties
\r
238 #region Private Properties
\r
240 private CompareOptions CompareOptions {
\r
243 (this.compareOptions & SqlCompareOptions.BinarySort) != 0 ?
\r
244 CompareOptions.Ordinal :
\r
245 // 27 == all SqlCompareOptions - BinarySort
\r
246 // (1,2,8,24 are common to CompareOptions)
\r
247 (CompareOptions)((int)this.compareOptions & 27);
\r
251 #endregion Private Properties
253 #region Public Methods
255 public SqlString Clone()
257 return new SqlString (value, lcid, compareOptions);
260 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
262 CompareOptions options = CompareOptions.None;
264 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
265 options |= CompareOptions.IgnoreCase;
266 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
267 options |= CompareOptions.IgnoreKanaType;
268 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
269 options |= CompareOptions.IgnoreNonSpace;
270 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
271 options |= CompareOptions.IgnoreWidth;
272 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
273 // FIXME: Exception string
274 throw new ArgumentOutOfRangeException ();
279 // **********************************
280 // Comparison Methods
281 // **********************************
283 public int CompareTo (object value)
287 else if (!(value is SqlString))
288 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
289 else if (((SqlString)value).IsNull)
292 // return String.Compare (this.value, ((SqlString)value).Value, (this.SqlCompareOptions & SqlCompareOptions.IgnoreCase) != 0, this.CultureInfo);
293 return CultureInfo.CompareInfo.Compare (this.value, ((SqlString)value).Value, this.CompareOptions);
296 public static SqlString Concat(SqlString x, SqlString y)
301 public override bool Equals(object value)
303 if (!(value is SqlString))
305 if (this.IsNull && ((SqlString)value).IsNull)
307 else if (((SqlString)value).IsNull)
310 return (bool) (this == (SqlString)value);
313 public static SqlBoolean Equals(SqlString x, SqlString y)
318 public override int GetHashCode()
321 for (int i = 0; i < value.Length; i++)
322 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
324 result = 91 * result + lcid.GetHashCode ();
325 result = 91 * result + (int)compareOptions;
330 public byte[] GetNonUnicodeBytes()
332 byte [] bytes = new byte [value.Length];
334 for (int i = 0; i < bytes.Length; i++)
335 bytes [i] = (byte)value [i];
340 public byte[] GetUnicodeBytes()
342 byte [] bytes = new byte [value.Length * 2];
345 for (int i = 0; i < value.Length; i++) {
346 bytes [j] = (byte)(value [i] & 0x0000FFFF);
347 bytes [j + 1] = (byte)((value [i] & 0xFFFF0000) >> 16);
354 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
359 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
364 public static SqlBoolean LessThan(SqlString x, SqlString y)
369 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
374 public static SqlBoolean NotEquals(SqlString x, SqlString y)
379 // ****************************************
380 // Type Conversions From SqlString To ...
381 // ****************************************
383 public SqlBoolean ToSqlBoolean()
385 return ((SqlBoolean)this);
388 public SqlByte ToSqlByte()
390 return ((SqlByte)this);
393 public SqlDateTime ToSqlDateTime()
395 return ((SqlDateTime)this);
398 public SqlDecimal ToSqlDecimal()
400 return ((SqlDecimal)this);
403 public SqlDouble ToSqlDouble()
405 return ((SqlDouble)this);
408 public SqlGuid ToSqlGuid()
410 return ((SqlGuid)this);
413 public SqlInt16 ToSqlInt16()
415 return ((SqlInt16)this);
418 public SqlInt32 ToSqlInt32()
420 return ((SqlInt32)this);
423 public SqlInt64 ToSqlInt64()
425 return ((SqlInt64)this);
428 public SqlMoney ToSqlMoney()
430 return ((SqlMoney)this);
433 public SqlSingle ToSqlSingle()
435 return ((SqlSingle)this);
438 public override string ToString()
442 return ((string)this);
445 // ***********************************
447 // ***********************************
450 public static SqlString operator + (SqlString x, SqlString y)
452 if (x.IsNull || y.IsNull)
453 return SqlString.Null;
455 return new SqlString (x.Value + y.Value);
459 public static SqlBoolean operator == (SqlString x, SqlString y)
461 if (x.IsNull || y.IsNull)
462 return SqlBoolean.Null;
464 return new SqlBoolean (x.Value == y.Value);
468 public static SqlBoolean operator > (SqlString x, SqlString y)
470 if (x.IsNull || y.IsNull)
471 return SqlBoolean.Null;
473 return new SqlBoolean (x.CompareTo (y) > 0);
476 // Greater Than Or Equal
477 public static SqlBoolean operator >= (SqlString x, SqlString y)
479 if (x.IsNull || y.IsNull)
480 return SqlBoolean.Null;
482 return new SqlBoolean (x.CompareTo (y) >= 0);
485 public static SqlBoolean operator != (SqlString x, SqlString y)
487 if (x.IsNull || y.IsNull)
488 return SqlBoolean.Null;
490 return new SqlBoolean (x.Value != y.Value);
494 public static SqlBoolean operator < (SqlString x, SqlString y)
496 if (x.IsNull || y.IsNull)
497 return SqlBoolean.Null;
499 return new SqlBoolean (x.CompareTo (y) < 0);
502 // Less Than Or Equal
503 public static SqlBoolean operator <= (SqlString x, SqlString y)
505 if (x.IsNull || y.IsNull)
506 return SqlBoolean.Null;
508 return new SqlBoolean (x.CompareTo (y) <= 0);
511 // **************************************
513 // **************************************
515 public static explicit operator SqlString (SqlBoolean x)
520 return new SqlString (x.Value.ToString ());
523 public static explicit operator SqlString (SqlByte x)
528 return new SqlString (x.Value.ToString ());
531 public static explicit operator SqlString (SqlDateTime x)
536 return new SqlString (x.Value.ToString ());
539 public static explicit operator SqlString (SqlDecimal x)
544 return new SqlString (x.Value.ToString ());
545 // return new SqlString (x.Value.ToString ("N", DecimalFormat));
548 public static explicit operator SqlString (SqlDouble x)
553 return new SqlString (x.Value.ToString ());
556 public static explicit operator SqlString (SqlGuid x)
561 return new SqlString (x.Value.ToString ());
564 public static explicit operator SqlString (SqlInt16 x)
569 return new SqlString (x.Value.ToString ());
572 public static explicit operator SqlString (SqlInt32 x)
577 return new SqlString (x.Value.ToString ());
580 public static explicit operator SqlString (SqlInt64 x)
585 return new SqlString (x.Value.ToString ());
588 public static explicit operator SqlString (SqlMoney x)
593 return new SqlString (x.Value.ToString ());
594 // return new SqlString (x.Value.ToString ("N", MoneyFormat));
597 public static explicit operator SqlString (SqlSingle x)
602 return new SqlString (x.Value.ToString ());
605 public static explicit operator string (SqlString x)
610 public static implicit operator SqlString (string x)
612 return new SqlString (x);
615 #endregion // Public Methods