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 using System.Globalization;
16 using System.Threading;
18 namespace System.Data.SqlTypes
21 /// A variable-length stream of characters
22 /// to be stored in or retrieved from the database
24 public struct SqlString : INullable, IComparable
33 // FIXME: locale id is not working yet
35 private SqlCompareOptions compareOptions;
37 public static readonly int BinarySort = 0x8000;
38 public static readonly int IgnoreCase = 0x1;
39 public static readonly int IgnoreKanaType = 0x8;
40 public static readonly int IgnoreNonSpace = 0x2;
41 public static readonly int IgnoreWidth = 0x10;
42 public static readonly SqlString Null;
44 internal static readonly NumberFormatInfo MoneyFormat;
45 internal static NumberFormatInfo DecimalFormat;
52 MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
53 MoneyFormat.NumberDecimalDigits = 4;
54 MoneyFormat.NumberGroupSeparator = String.Empty;
56 DecimalFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
57 DecimalFormat.NumberDecimalDigits = 13;
58 DecimalFormat.NumberGroupSeparator = String.Empty;
61 // init with a string data
62 public SqlString (string data)
65 lcid = CultureInfo.CurrentCulture.LCID;
67 this.compareOptions = SqlCompareOptions.IgnoreCase |
68 SqlCompareOptions.IgnoreKanaType |
69 SqlCompareOptions.IgnoreWidth;
72 // init with a string data and locale id values.
73 public SqlString (string data, int lcid)
78 this.compareOptions = SqlCompareOptions.IgnoreCase |
79 SqlCompareOptions.IgnoreKanaType |
80 SqlCompareOptions.IgnoreWidth;
83 // init with locale id, compare options,
84 // and an array of bytes data
85 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data)
86 : this (lcid, compareOptions, data, true) { }
88 // init with string data, locale id, and compare options
89 public SqlString (string data, int lcid, SqlCompareOptions compareOptions)
93 this.compareOptions = compareOptions;
97 // init with locale id, compare options, array of bytes data,
98 // and whether unicode is encoded or not
99 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, bool fUnicode)
104 chars = new char [data.Length/2];
106 chars = new char [data.Length];
109 for (int i = 0; i < chars.Length; i++) {
112 chars [i] = (char)(data [j] << 16);
113 chars [i] += (char)data [j + 1];
116 chars [i] = (char)data[i];
120 this.value = new String (chars);
122 this.compareOptions = compareOptions;
126 // init with locale id, compare options, array of bytes data,
127 // starting index in the byte array,
128 // and number of bytes to copy
129 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data,
130 int index, int count)
131 : this (lcid, compareOptions, data, index, count, true) { }
133 // init with locale id, compare options, array of bytes data,
134 // starting index in the byte array, number of byte to copy,
135 // and whether unicode is encoded or not
136 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, int index, int count, bool fUnicode)
141 chars = new char [(count - index) / 2];
143 chars = new char [count - index];
145 if (index >= data.Length)
146 throw new ArgumentOutOfRangeException ("index");
148 if ((index + count) > data.Length)
149 throw new ArgumentOutOfRangeException ("count");
152 for (int i = index; i < chars.Length; i++) {
155 chars [i] = (char)(data[j] << 16);
156 chars [i] += (char)data[j+1];
159 chars [i] = (char)data [j];
164 this.value = new String (chars);
166 this.compareOptions = compareOptions;
170 #endregion // Constructors
173 #region Public Properties
175 public CompareInfo CompareInfo {
177 return new CultureInfo (lcid).CompareInfo;
181 public CultureInfo CultureInfo {
183 return new CultureInfo (lcid);
188 get { return !notNull; }
191 // geographics location and language (locale id)
198 public SqlCompareOptions SqlCompareOptions {
200 return compareOptions;
204 public string Value {
207 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
213 #endregion // Public Properties
\r
215 #region Private Properties
\r
217 private CompareOptions CompareOptions {
\r
220 (this.compareOptions & SqlCompareOptions.BinarySort) != 0 ?
\r
221 CompareOptions.Ordinal :
\r
222 // 27 == all SqlCompareOptions - BinarySort
\r
223 // (1,2,8,24 are common to CompareOptions)
\r
224 (CompareOptions)((int)this.compareOptions & 27);
\r
228 #endregion Private Properties
230 #region Public Methods
232 public SqlString Clone()
234 return new SqlString (value, lcid, compareOptions);
237 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
239 CompareOptions options = CompareOptions.None;
241 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
242 options |= CompareOptions.IgnoreCase;
243 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
244 options |= CompareOptions.IgnoreKanaType;
245 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
246 options |= CompareOptions.IgnoreNonSpace;
247 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
248 options |= CompareOptions.IgnoreWidth;
249 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
250 // FIXME: Exception string
251 throw new ArgumentOutOfRangeException ();
256 // **********************************
257 // Comparison Methods
258 // **********************************
260 public int CompareTo (object value)
264 else if (!(value is SqlString))
265 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
266 else if (((SqlString)value).IsNull)
269 // return String.Compare (this.value, ((SqlString)value).Value, (this.SqlCompareOptions & SqlCompareOptions.IgnoreCase) != 0, this.CultureInfo);
270 return CultureInfo.CompareInfo.Compare (this.value, ((SqlString)value).Value, this.CompareOptions);
273 public static SqlString Concat(SqlString x, SqlString y)
278 public override bool Equals(object value)
280 if (!(value is SqlString))
282 if (this.IsNull && ((SqlString)value).IsNull)
284 else if (((SqlString)value).IsNull)
287 return (bool) (this == (SqlString)value);
290 public static SqlBoolean Equals(SqlString x, SqlString y)
295 public override int GetHashCode()
298 for (int i = 0; i < value.Length; i++)
299 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
301 result = 91 * result + lcid.GetHashCode ();
302 result = 91 * result + (int)compareOptions;
307 public byte[] GetNonUnicodeBytes()
309 byte [] bytes = new byte [value.Length];
311 for (int i = 0; i < bytes.Length; i++)
312 bytes [i] = (byte)value [i];
317 public byte[] GetUnicodeBytes()
319 byte [] bytes = new byte [value.Length * 2];
322 for (int i = 0; i < value.Length; i++) {
323 bytes [j] = (byte)(value [i] & 0x0000FFFF);
324 bytes [j + 1] = (byte)((value [i] & 0xFFFF0000) >> 16);
331 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
336 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
341 public static SqlBoolean LessThan(SqlString x, SqlString y)
346 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
351 public static SqlBoolean NotEquals(SqlString x, SqlString y)
356 // ****************************************
357 // Type Conversions From SqlString To ...
358 // ****************************************
360 public SqlBoolean ToSqlBoolean()
362 return ((SqlBoolean)this);
365 public SqlByte ToSqlByte()
367 return ((SqlByte)this);
370 public SqlDateTime ToSqlDateTime()
372 return ((SqlDateTime)this);
375 public SqlDecimal ToSqlDecimal()
377 return ((SqlDecimal)this);
380 public SqlDouble ToSqlDouble()
382 return ((SqlDouble)this);
385 public SqlGuid ToSqlGuid()
387 return ((SqlGuid)this);
390 public SqlInt16 ToSqlInt16()
392 return ((SqlInt16)this);
395 public SqlInt32 ToSqlInt32()
397 return ((SqlInt32)this);
400 public SqlInt64 ToSqlInt64()
402 return ((SqlInt64)this);
405 public SqlMoney ToSqlMoney()
407 return ((SqlMoney)this);
410 public SqlSingle ToSqlSingle()
412 return ((SqlSingle)this);
415 public override string ToString()
419 return ((string)this);
422 // ***********************************
424 // ***********************************
427 public static SqlString operator + (SqlString x, SqlString y)
429 if (x.IsNull || y.IsNull)
430 return SqlString.Null;
432 return new SqlString (x.Value + y.Value);
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 // Greater 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 public static SqlBoolean operator != (SqlString x, SqlString y)
464 if (x.IsNull || y.IsNull)
465 return SqlBoolean.Null;
467 return new SqlBoolean (x.Value != y.Value);
471 public static SqlBoolean operator < (SqlString x, SqlString y)
473 if (x.IsNull || y.IsNull)
474 return SqlBoolean.Null;
476 return new SqlBoolean (x.CompareTo (y) < 0);
479 // Less Than Or Equal
480 public static SqlBoolean operator <= (SqlString x, SqlString y)
482 if (x.IsNull || y.IsNull)
483 return SqlBoolean.Null;
485 return new SqlBoolean (x.CompareTo (y) <= 0);
488 // **************************************
490 // **************************************
492 public static explicit operator SqlString (SqlBoolean x)
497 return new SqlString (x.Value.ToString ());
500 public static explicit operator SqlString (SqlByte x)
505 return new SqlString (x.Value.ToString ());
508 public static explicit operator SqlString (SqlDateTime x)
513 return new SqlString (x.Value.ToString ());
516 public static explicit operator SqlString (SqlDecimal x)
521 return new SqlString (x.Value.ToString ());
522 // return new SqlString (x.Value.ToString ("N", DecimalFormat));
525 public static explicit operator SqlString (SqlDouble x)
530 return new SqlString (x.Value.ToString ());
533 public static explicit operator SqlString (SqlGuid x)
538 return new SqlString (x.Value.ToString ());
541 public static explicit operator SqlString (SqlInt16 x)
546 return new SqlString (x.Value.ToString ());
549 public static explicit operator SqlString (SqlInt32 x)
554 return new SqlString (x.Value.ToString ());
557 public static explicit operator SqlString (SqlInt64 x)
562 return new SqlString (x.Value.ToString ());
565 public static explicit operator SqlString (SqlMoney x)
570 return new SqlString (x.Value.ToString ());
571 // return new SqlString (x.Value.ToString ("N", MoneyFormat));
574 public static explicit operator SqlString (SqlSingle x)
579 return new SqlString (x.Value.ToString ());
582 public static explicit operator string (SqlString x)
587 public static implicit operator SqlString (string x)
589 return new SqlString (x);
592 #endregion // Public Methods