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;
48 // init with a string data
49 public SqlString (string data)
52 lcid = CultureInfo.CurrentCulture.LCID;
54 this.compareOptions = SqlCompareOptions.None;
57 // init with a string data and locale id values.
58 public SqlString (string data, int lcid)
63 this.compareOptions = SqlCompareOptions.None;
66 // init with locale id, compare options,
67 // and an array of bytes data
68 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data)
69 : this (lcid, compareOptions, data, true) { }
71 // init with string data, locale id, and compare options
72 public SqlString (string data, int lcid, SqlCompareOptions compareOptions)
76 this.compareOptions = compareOptions;
80 // init with locale id, compare options, array of bytes data,
81 // and whether unicode is encoded or not
82 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, bool fUnicode)
87 chars = new char [data.Length/2];
89 chars = new char [data.Length];
92 for (int i = 0; i < chars.Length; i++) {
95 chars [i] = (char)(data [j] << 16);
96 chars [i] += (char)data [j + 1];
99 chars [i] = (char)data[i];
103 this.value = new String (chars);
105 this.compareOptions = compareOptions;
109 // init with locale id, compare options, array of bytes data,
110 // starting index in the byte array,
111 // and number of bytes to copy
112 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data,
113 int index, int count)
114 : this (lcid, compareOptions, data, index, count, true) { }
116 // init with locale id, compare options, array of bytes data,
117 // starting index in the byte array, number of byte to copy,
118 // and whether unicode is encoded or not
119 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, int index, int count, bool fUnicode)
124 chars = new char [(count - index) / 2];
126 chars = new char [count - index];
128 if (index >= data.Length)
129 throw new ArgumentOutOfRangeException ("index");
131 if ((index + count) > data.Length)
132 throw new ArgumentOutOfRangeException ("count");
135 for (int i = index; i < chars.Length; i++) {
138 chars [i] = (char)(data[j] << 16);
139 chars [i] += (char)data[j+1];
142 chars [i] = (char)data [j];
147 this.value = new String (chars);
149 this.compareOptions = compareOptions;
153 #endregion // Constructors
156 #region Public Properties
158 public CompareInfo CompareInfo {
160 return new CultureInfo (lcid).CompareInfo;
164 public CultureInfo CultureInfo {
166 return new CultureInfo (lcid);
171 get { return !notNull; }
174 // geographics location and language (locale id)
181 public SqlCompareOptions SqlCompareOptions {
183 return compareOptions;
187 public string Value {
190 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
196 #endregion // Public Properties
198 #region Public Methods
200 public SqlString Clone()
202 return new SqlString (value, lcid, compareOptions);
205 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
207 CompareOptions options = CompareOptions.None;
209 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
210 options |= CompareOptions.IgnoreCase;
211 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
212 options |= CompareOptions.IgnoreKanaType;
213 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
214 options |= CompareOptions.IgnoreNonSpace;
215 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
216 options |= CompareOptions.IgnoreWidth;
217 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
218 // FIXME: Exception string
219 throw new ArgumentOutOfRangeException ();
224 // **********************************
225 // Comparison Methods
226 // **********************************
228 public int CompareTo(object value)
232 else if (!(value is SqlString))
233 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
234 else if (((SqlString)value).IsNull)
237 return this.value.CompareTo (((SqlString)value).Value);
240 public static SqlString Concat(SqlString x, SqlString y)
245 public override bool Equals(object value)
247 if (!(value is SqlString))
249 if (this.IsNull && ((SqlString)value).IsNull)
251 else if (((SqlString)value).IsNull)
254 return (bool) (this == (SqlString)value);
257 public static SqlBoolean Equals(SqlString x, SqlString y)
262 public override int GetHashCode()
265 for (int i = 0; i < value.Length; i++)
266 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
268 result = 91 * result + lcid.GetHashCode ();
269 result = 91 * result + (int)compareOptions;
274 public byte[] GetNonUnicodeBytes()
276 byte [] bytes = new byte [value.Length];
278 for (int i = 0; i < bytes.Length; i++)
279 bytes [i] = (byte)value [i];
284 public byte[] GetUnicodeBytes()
286 byte [] bytes = new byte [value.Length * 2];
289 for (int i = 0; i < value.Length; i++) {
290 bytes [j] = (byte)(value [i] & 0x0000FFFF);
291 bytes [j + 1] = (byte)((value [i] & 0xFFFF0000) >> 16);
298 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
303 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
308 public static SqlBoolean LessThan(SqlString x, SqlString y)
313 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
318 public static SqlBoolean NotEquals(SqlString x, SqlString y)
323 // ****************************************
324 // Type Conversions From SqlString To ...
325 // ****************************************
327 public SqlBoolean ToSqlBoolean()
329 return ((SqlBoolean)this);
332 public SqlByte ToSqlByte()
334 return ((SqlByte)this);
337 public SqlDateTime ToSqlDateTime()
339 return ((SqlDateTime)this);
342 public SqlDecimal ToSqlDecimal()
344 return ((SqlDecimal)this);
347 public SqlDouble ToSqlDouble()
349 return ((SqlDouble)this);
352 public SqlGuid ToSqlGuid()
354 return ((SqlGuid)this);
357 public SqlInt16 ToSqlInt16()
359 return ((SqlInt16)this);
362 public SqlInt32 ToSqlInt32()
364 return ((SqlInt32)this);
367 public SqlInt64 ToSqlInt64()
369 return ((SqlInt64)this);
372 public SqlMoney ToSqlMoney()
374 return ((SqlMoney)this);
377 public SqlSingle ToSqlSingle()
379 return ((SqlSingle)this);
382 public override string ToString()
384 return ((string)this);
387 // ***********************************
389 // ***********************************
392 public static SqlString operator + (SqlString x, SqlString y)
394 if (x.IsNull || y.IsNull)
395 return SqlString.Null;
397 return new SqlString (x.Value + y.Value);
401 public static SqlBoolean operator == (SqlString x, SqlString y)
403 if (x.IsNull || y.IsNull)
404 return SqlBoolean.Null;
406 return new SqlBoolean (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.CompareTo (y) > 0);
418 // Greater Than Or Equal
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 public static SqlBoolean operator != (SqlString x, SqlString y)
429 if (x.IsNull || y.IsNull)
430 return SqlBoolean.Null;
432 return new SqlBoolean (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.CompareTo (y) < 0);
444 // Less Than Or Equal
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 // **************************************
455 // **************************************
457 public static explicit operator SqlString (SqlBoolean x)
462 return new SqlString (x.Value.ToString ());
465 public static explicit operator SqlString (SqlByte x)
470 return new SqlString (x.Value.ToString ());
473 public static explicit operator SqlString (SqlDateTime x)
478 return new SqlString (x.Value.ToString ());
481 public static explicit operator SqlString (SqlDecimal x)
486 return new SqlString (x.Value.ToString ());
489 public static explicit operator SqlString (SqlDouble x)
494 return new SqlString (x.Value.ToString ());
497 public static explicit operator SqlString (SqlGuid x)
502 return new SqlString (x.Value.ToString ());
505 public static explicit operator SqlString (SqlInt16 x)
510 return new SqlString (x.Value.ToString ());
513 public static explicit operator SqlString (SqlInt32 x)
518 return new SqlString (x.Value.ToString ());
521 public static explicit operator SqlString (SqlInt64 x)
526 return new SqlString (x.Value.ToString ());
529 public static explicit operator SqlString (SqlMoney x)
534 return new SqlString (x.Value.ToString ());
537 public static explicit operator SqlString (SqlSingle x)
542 return new SqlString (x.Value.ToString ());
545 public static explicit operator string (SqlString x)
550 public static implicit operator SqlString (string x)
552 return new SqlString (x);
555 #endregion // Public Methods