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];
129 for (int i = index; i < chars.Length; i++) {
132 chars [i] = (char)(data[j] << 16);
133 chars [i] += (char)data[j+1];
136 chars [i] = (char)data [j];
141 this.value = new String (chars);
143 this.compareOptions = compareOptions;
147 #endregion // Constructors
150 #region Public Properties
152 public CompareInfo CompareInfo {
154 return new CultureInfo (lcid).CompareInfo;
158 public CultureInfo CultureInfo {
160 return new CultureInfo (lcid);
165 get { return !notNull; }
168 // geographics location and language (locale id)
175 public SqlCompareOptions SqlCompareOptions {
177 return compareOptions;
181 public string Value {
184 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
190 #endregion // Public Properties
192 #region Public Methods
194 public SqlString Clone()
196 return new SqlString (value, lcid, compareOptions);
199 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
201 CompareOptions options = CompareOptions.None;
203 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
204 options |= CompareOptions.IgnoreCase;
205 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
206 options |= CompareOptions.IgnoreKanaType;
207 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
208 options |= CompareOptions.IgnoreNonSpace;
209 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
210 options |= CompareOptions.IgnoreWidth;
211 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
212 // FIXME: Exception string
213 throw new ArgumentOutOfRangeException ();
218 // **********************************
219 // Comparison Methods
220 // **********************************
222 public int CompareTo(object value)
226 else if (!(value is SqlString))
227 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
228 else if (((SqlString)value).IsNull)
231 return this.value.CompareTo (((SqlString)value).Value);
234 public static SqlString Concat(SqlString x, SqlString y)
239 public override bool Equals(object value)
241 if (!(value is SqlString))
243 if (this.IsNull && ((SqlString)value).IsNull)
245 else if (((SqlString)value).IsNull)
248 return (bool) (this == (SqlString)value);
251 public static SqlBoolean Equals(SqlString x, SqlString y)
256 public override int GetHashCode()
259 for (int i = 0; i < value.Length; i++)
260 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
262 result = 91 * result + lcid.GetHashCode ();
263 result = 91 * result + (int)compareOptions;
268 public byte[] GetNonUnicodeBytes()
270 byte [] bytes = new byte [value.Length];
272 for (int i = 0; i < bytes.Length; i++)
273 bytes [i] = (byte)value [i];
278 public byte[] GetUnicodeBytes()
280 byte [] bytes = new byte [value.Length * 2];
283 for (int i = 0; i < value.Length; i++) {
284 bytes [j] = (byte)(value [i] & 0x0000FFFF);
285 bytes [j + 1] = (byte)((value [i] & 0xFFFF0000) >> 16);
292 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
297 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
302 public static SqlBoolean LessThan(SqlString x, SqlString y)
307 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
312 public static SqlBoolean NotEquals(SqlString x, SqlString y)
317 // ****************************************
318 // Type Conversions From SqlString To ...
319 // ****************************************
321 public SqlBoolean ToSqlBoolean()
323 return ((SqlBoolean)this);
326 public SqlByte ToSqlByte()
328 return ((SqlByte)this);
331 public SqlDateTime ToSqlDateTime()
333 return ((SqlDateTime)this);
336 public SqlDecimal ToSqlDecimal()
338 return ((SqlDecimal)this);
341 public SqlDouble ToSqlDouble()
343 return ((SqlDouble)this);
346 public SqlGuid ToSqlGuid()
348 return ((SqlGuid)this);
351 public SqlInt16 ToSqlInt16()
353 return ((SqlInt16)this);
356 public SqlInt32 ToSqlInt32()
358 return ((SqlInt32)this);
361 public SqlInt64 ToSqlInt64()
363 return ((SqlInt64)this);
366 public SqlMoney ToSqlMoney()
368 return ((SqlMoney)this);
371 public SqlSingle ToSqlSingle()
373 return ((SqlSingle)this);
376 public override string ToString()
378 return ((string)this);
381 // ***********************************
383 // ***********************************
386 public static SqlString operator + (SqlString x, SqlString y)
388 if (x.IsNull || y.IsNull)
389 return SqlString.Null;
391 return new SqlString (x.Value + y.Value);
395 public static SqlBoolean operator == (SqlString x, SqlString y)
397 if (x.IsNull || y.IsNull)
398 return SqlBoolean.Null;
400 return new SqlBoolean (x.Value == y.Value);
404 public static SqlBoolean operator > (SqlString x, SqlString y)
406 if (x.IsNull || y.IsNull)
407 return SqlBoolean.Null;
409 throw new NotImplementedException ();
412 // Greater Than Or Equal
413 public static SqlBoolean operator >= (SqlString x, SqlString y)
415 if (x.IsNull || y.IsNull)
416 return SqlBoolean.Null;
418 throw new NotImplementedException ();
421 public static SqlBoolean operator != (SqlString x, SqlString y)
423 if (x.IsNull || y.IsNull)
424 return SqlBoolean.Null;
426 return new SqlBoolean (x.Value != y.Value);
430 public static SqlBoolean operator < (SqlString x, SqlString y)
432 if (x.IsNull || y.IsNull)
433 return SqlBoolean.Null;
435 throw new NotImplementedException ();
438 // Less Than Or Equal
439 public static SqlBoolean operator <= (SqlString x, SqlString y)
441 if (x.IsNull || y.IsNull)
442 return SqlBoolean.Null;
444 throw new NotImplementedException ();
447 // **************************************
449 // **************************************
451 public static explicit operator SqlString (SqlBoolean x)
456 return new SqlString (x.Value.ToString ());
459 public static explicit operator SqlString (SqlByte x)
464 return new SqlString (x.Value.ToString ());
467 public static explicit operator SqlString (SqlDateTime x)
472 return new SqlString (x.Value.ToString ());
475 public static explicit operator SqlString (SqlDecimal x)
480 return new SqlString (x.Value.ToString ());
483 public static explicit operator SqlString (SqlDouble x)
488 return new SqlString (x.Value.ToString ());
491 public static explicit operator SqlString (SqlGuid x)
496 return new SqlString (x.Value.ToString ());
499 public static explicit operator SqlString (SqlInt16 x)
504 return new SqlString (x.Value.ToString ());
507 public static explicit operator SqlString (SqlInt32 x)
512 return new SqlString (x.Value.ToString ());
515 public static explicit operator SqlString (SqlInt64 x)
520 return new SqlString (x.Value.ToString ());
523 public static explicit operator SqlString (SqlMoney x)
528 return new SqlString (x.Value.ToString ());
531 public static explicit operator SqlString (SqlSingle x)
536 return new SqlString (x.Value.ToString ());
539 public static explicit operator string (SqlString x)
544 public static implicit operator SqlString (string x)
546 return new SqlString (x);
549 #endregion // Public Methods