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);
187 public CompareOptions CompareOptions {
190 (this.compareOptions & SqlCompareOptions.BinarySort) != 0 ?
191 CompareOptions.Ordinal :
192 // 27 == all SqlCompareOptions - BinarySort
193 // (1,2,8,24 are common to CompareOptions)
194 (CompareOptions) ((int) this.compareOptions & 27);
199 get { return !notNull; }
202 // geographics location and language (locale id)
209 public SqlCompareOptions SqlCompareOptions {
211 return compareOptions;
215 public string Value {
218 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
224 #endregion // Public Properties
226 #region Public Methods
228 public SqlString Clone()
230 return new SqlString (value, lcid, compareOptions);
233 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
235 CompareOptions options = CompareOptions.None;
237 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
238 options |= CompareOptions.IgnoreCase;
239 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
240 options |= CompareOptions.IgnoreKanaType;
241 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
242 options |= CompareOptions.IgnoreNonSpace;
243 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
244 options |= CompareOptions.IgnoreWidth;
245 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
246 // FIXME: Exception string
247 throw new ArgumentOutOfRangeException ();
252 // **********************************
253 // Comparison Methods
254 // **********************************
256 public int CompareTo (object value)
260 else if (!(value is SqlString))
261 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
262 else if (((SqlString)value).IsNull)
265 // return String.Compare (this.value, ((SqlString)value).Value, (this.SqlCompareOptions & SqlCompareOptions.IgnoreCase) != 0, this.CultureInfo);
266 return CultureInfo.CompareInfo.Compare (this.value, ((SqlString)value).Value, this.CompareOptions);
269 public static SqlString Concat(SqlString x, SqlString y)
274 public override bool Equals(object value)
276 if (!(value is SqlString))
278 if (this.IsNull && ((SqlString)value).IsNull)
280 else if (((SqlString)value).IsNull)
283 return (bool) (this == (SqlString)value);
286 public static SqlBoolean Equals(SqlString x, SqlString y)
291 public override int GetHashCode()
294 for (int i = 0; i < value.Length; i++)
295 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
297 result = 91 * result + lcid.GetHashCode ();
298 result = 91 * result + (int)compareOptions;
303 public byte[] GetNonUnicodeBytes()
305 byte [] bytes = new byte [value.Length];
307 for (int i = 0; i < bytes.Length; i++)
308 bytes [i] = (byte)value [i];
313 public byte[] GetUnicodeBytes()
315 byte [] bytes = new byte [value.Length * 2];
318 for (int i = 0; i < value.Length; i++) {
319 bytes [j] = (byte)(value [i] & 0x0000FFFF);
320 bytes [j + 1] = (byte)((value [i] & 0xFFFF0000) >> 16);
327 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
332 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
337 public static SqlBoolean LessThan(SqlString x, SqlString y)
342 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
347 public static SqlBoolean NotEquals(SqlString x, SqlString y)
352 // ****************************************
353 // Type Conversions From SqlString To ...
354 // ****************************************
356 public SqlBoolean ToSqlBoolean()
358 return ((SqlBoolean)this);
361 public SqlByte ToSqlByte()
363 return ((SqlByte)this);
366 public SqlDateTime ToSqlDateTime()
368 return ((SqlDateTime)this);
371 public SqlDecimal ToSqlDecimal()
373 return ((SqlDecimal)this);
376 public SqlDouble ToSqlDouble()
378 return ((SqlDouble)this);
381 public SqlGuid ToSqlGuid()
383 return ((SqlGuid)this);
386 public SqlInt16 ToSqlInt16()
388 return ((SqlInt16)this);
391 public SqlInt32 ToSqlInt32()
393 return ((SqlInt32)this);
396 public SqlInt64 ToSqlInt64()
398 return ((SqlInt64)this);
401 public SqlMoney ToSqlMoney()
403 return ((SqlMoney)this);
406 public SqlSingle ToSqlSingle()
408 return ((SqlSingle)this);
411 public override string ToString()
413 return ((string)this);
416 // ***********************************
418 // ***********************************
421 public static SqlString operator + (SqlString x, SqlString y)
423 if (x.IsNull || y.IsNull)
424 return SqlString.Null;
426 return new SqlString (x.Value + y.Value);
430 public static SqlBoolean operator == (SqlString x, SqlString y)
432 if (x.IsNull || y.IsNull)
433 return SqlBoolean.Null;
435 return new SqlBoolean (x.Value == y.Value);
439 public static SqlBoolean operator > (SqlString x, SqlString y)
441 if (x.IsNull || y.IsNull)
442 return SqlBoolean.Null;
444 return new SqlBoolean (x.CompareTo (y) > 0);
447 // Greater Than Or Equal
448 public static SqlBoolean operator >= (SqlString x, SqlString y)
450 if (x.IsNull || y.IsNull)
451 return SqlBoolean.Null;
453 return new SqlBoolean (x.CompareTo (y) >= 0);
456 public static SqlBoolean operator != (SqlString x, SqlString y)
458 if (x.IsNull || y.IsNull)
459 return SqlBoolean.Null;
461 return new SqlBoolean (x.Value != y.Value);
465 public static SqlBoolean operator < (SqlString x, SqlString y)
467 if (x.IsNull || y.IsNull)
468 return SqlBoolean.Null;
470 return new SqlBoolean (x.CompareTo (y) < 0);
473 // Less Than Or Equal
474 public static SqlBoolean operator <= (SqlString x, SqlString y)
476 if (x.IsNull || y.IsNull)
477 return SqlBoolean.Null;
479 return new SqlBoolean (x.CompareTo (y) <= 0);
482 // **************************************
484 // **************************************
486 public static explicit operator SqlString (SqlBoolean x)
491 return new SqlString (x.Value.ToString ());
494 public static explicit operator SqlString (SqlByte x)
499 return new SqlString (x.Value.ToString ());
502 public static explicit operator SqlString (SqlDateTime x)
507 return new SqlString (x.Value.ToString ());
510 public static explicit operator SqlString (SqlDecimal x)
515 return new SqlString (x.Value.ToString ());
516 return new SqlString (x.Value.ToString ("N", DecimalFormat));
519 public static explicit operator SqlString (SqlDouble x)
524 return new SqlString (x.Value.ToString ());
527 public static explicit operator SqlString (SqlGuid x)
532 return new SqlString (x.Value.ToString ());
535 public static explicit operator SqlString (SqlInt16 x)
540 return new SqlString (x.Value.ToString ());
543 public static explicit operator SqlString (SqlInt32 x)
548 return new SqlString (x.Value.ToString ());
551 public static explicit operator SqlString (SqlInt64 x)
556 return new SqlString (x.Value.ToString ());
559 public static explicit operator SqlString (SqlMoney x)
564 return new SqlString (x.Value.ToString ());
565 return new SqlString (x.Value.ToString ("N", MoneyFormat));
568 public static explicit operator SqlString (SqlSingle x)
573 return new SqlString (x.Value.ToString ());
576 public static explicit operator string (SqlString x)
581 public static implicit operator SqlString (string x)
583 return new SqlString (x);
586 #endregion // Public Methods