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.
40 using System.Threading;
41 using System.Xml.Schema;
42 using System.Globalization;
43 using System.Xml.Serialization;
45 namespace System.Data.SqlTypes
48 /// A variable-length stream of characters
49 /// to be stored in or retrieved from the database
52 [SerializableAttribute]
53 [XmlSchemaProvider ("GetSchema")]
55 public struct SqlString : INullable, IComparable
67 // FIXME: locale id is not working yet
69 private SqlCompareOptions compareOptions;
71 public static readonly int BinarySort = 0x8000;
72 public static readonly int IgnoreCase = 0x1;
73 public static readonly int IgnoreKanaType = 0x8;
74 public static readonly int IgnoreNonSpace = 0x2;
75 public static readonly int IgnoreWidth = 0x10;
76 public static readonly SqlString Null;
78 internal static NumberFormatInfo DecimalFormat;
85 DecimalFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
86 DecimalFormat.NumberDecimalDigits = 13;
87 DecimalFormat.NumberGroupSeparator = String.Empty;
90 // init with a string data
91 public SqlString (string data)
94 lcid = CultureInfo.CurrentCulture.LCID;
96 this.compareOptions = SqlCompareOptions.IgnoreCase |
97 SqlCompareOptions.IgnoreKanaType |
98 SqlCompareOptions.IgnoreWidth;
101 // init with a string data and locale id values.
102 public SqlString (string data, int lcid)
107 this.compareOptions = SqlCompareOptions.IgnoreCase |
108 SqlCompareOptions.IgnoreKanaType |
109 SqlCompareOptions.IgnoreWidth;
112 // init with locale id, compare options,
113 // and an array of bytes data
114 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data)
115 : this (lcid, compareOptions, data, true) { }
117 // init with string data, locale id, and compare options
118 public SqlString (string data, int lcid, SqlCompareOptions compareOptions)
122 this.compareOptions = compareOptions;
126 // init with locale id, compare options, array of bytes data,
127 // and whether unicode is encoded or not
128 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, bool fUnicode)
130 Encoding encoding = (fUnicode ? Encoding.Unicode : Encoding.ASCII);
131 this.value = encoding.GetString (data);
133 this.compareOptions = compareOptions;
137 // init with locale id, compare options, array of bytes data,
138 // starting index in the byte array,
139 // and number of bytes to copy
140 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data,
141 int index, int count)
142 : this (lcid, compareOptions, data, index, count, true) { }
144 // init with locale id, compare options, array of bytes data,
145 // starting index in the byte array, number of byte to copy,
146 // and whether unicode is encoded or not
147 public SqlString (int lcid, SqlCompareOptions compareOptions, byte[] data, int index, int count, bool fUnicode)
149 Encoding encoding = (fUnicode ? Encoding.Unicode : Encoding.ASCII);
150 this.value = encoding.GetString (data, index, count);
152 this.compareOptions = compareOptions;
156 #endregion // Constructors
159 #region Public Properties
161 public CompareInfo CompareInfo {
163 return new CultureInfo (lcid).CompareInfo;
167 public CultureInfo CultureInfo {
169 return new CultureInfo (lcid);
174 get { return !notNull; }
177 // geographics location and language (locale id)
184 public SqlCompareOptions SqlCompareOptions {
186 return compareOptions;
190 public string Value {
193 throw new SqlNullValueException (Locale.GetText ("The property contains Null."));
199 #endregion // Public Properties
201 #region Private Properties
203 private CompareOptions CompareOptions {
206 (this.compareOptions & SqlCompareOptions.BinarySort) != 0 ?
207 CompareOptions.Ordinal :
208 // 27 == all SqlCompareOptions - BinarySort
209 // (1,2,8,24 are common to CompareOptions)
210 (CompareOptions)((int)this.compareOptions & 27);
214 #endregion Private Properties
216 #region Public Methods
218 public SqlString Clone()
220 return new SqlString (value, lcid, compareOptions);
223 public static CompareOptions CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions)
225 CompareOptions options = CompareOptions.None;
227 if ((compareOptions & SqlCompareOptions.IgnoreCase) != 0)
228 options |= CompareOptions.IgnoreCase;
229 if ((compareOptions & SqlCompareOptions.IgnoreKanaType) != 0)
230 options |= CompareOptions.IgnoreKanaType;
231 if ((compareOptions & SqlCompareOptions.IgnoreNonSpace) != 0)
232 options |= CompareOptions.IgnoreNonSpace;
233 if ((compareOptions & SqlCompareOptions.IgnoreWidth) != 0)
234 options |= CompareOptions.IgnoreWidth;
235 if ((compareOptions & SqlCompareOptions.BinarySort) != 0)
236 // FIXME: Exception string
237 throw new ArgumentOutOfRangeException ();
242 // **********************************
243 // Comparison Methods
244 // **********************************
246 public int CompareTo (object value)
250 else if (!(value is SqlString))
251 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
253 return CompareSqlString ((SqlString)value);
257 private int CompareSqlString (SqlString value)
261 else if (value.CompareOptions != this.CompareOptions)
262 throw new SqlTypeException (Locale.GetText ("Two strings to be compared have different collation"));
264 // return String.Compare (this.value, ((SqlString)value).Value, (this.SqlCompareOptions & SqlCompareOptions.IgnoreCase) != 0, this.CultureInfo);
265 return CultureInfo.CompareInfo.Compare (this.value, value.Value, this.CompareOptions);
268 public static SqlString Concat(SqlString x, SqlString y)
273 public override bool Equals(object value)
275 if (!(value is SqlString))
277 if (this.IsNull && ((SqlString)value).IsNull)
279 else if (((SqlString)value).IsNull)
282 return (bool) (this == (SqlString)value);
285 public static SqlBoolean Equals(SqlString x, SqlString y)
290 public override int GetHashCode()
293 for (int i = 0; i < value.Length; i++)
294 result = 91 * result + (int)(value [i] ^ (value [i] >> 32));
296 result = 91 * result + lcid.GetHashCode ();
297 result = 91 * result + (int)compareOptions;
302 public byte[] GetNonUnicodeBytes()
304 return Encoding.ASCII.GetBytes (value);
307 public byte[] GetUnicodeBytes()
309 return Encoding.Unicode.GetBytes (value);
312 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
317 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
322 public static SqlBoolean LessThan(SqlString x, SqlString y)
327 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
332 public static SqlBoolean NotEquals(SqlString x, SqlString y)
337 // ****************************************
338 // Type Conversions From SqlString To ...
339 // ****************************************
341 public SqlBoolean ToSqlBoolean()
343 return ((SqlBoolean)this);
346 public SqlByte ToSqlByte()
348 return ((SqlByte)this);
351 public SqlDateTime ToSqlDateTime()
353 return ((SqlDateTime)this);
356 public SqlDecimal ToSqlDecimal()
358 return ((SqlDecimal)this);
361 public SqlDouble ToSqlDouble()
363 return ((SqlDouble)this);
366 public SqlGuid ToSqlGuid()
368 return ((SqlGuid)this);
371 public SqlInt16 ToSqlInt16()
373 return ((SqlInt16)this);
376 public SqlInt32 ToSqlInt32()
378 return ((SqlInt32)this);
381 public SqlInt64 ToSqlInt64()
383 return ((SqlInt64)this);
386 public SqlMoney ToSqlMoney()
388 return ((SqlMoney)this);
391 public SqlSingle ToSqlSingle()
393 return ((SqlSingle)this);
396 public override string ToString()
400 return ((string)this);
403 // ***********************************
405 // ***********************************
408 public static SqlString operator + (SqlString x, SqlString y)
410 if (x.IsNull || y.IsNull)
411 return SqlString.Null;
413 if (( x == null) || (y == null))
414 return SqlString.Null;
416 return new SqlString (x.Value + y.Value);
420 public static SqlBoolean operator == (SqlString x, SqlString y)
422 if (x.IsNull || y.IsNull)
423 return SqlBoolean.Null;
425 return new SqlBoolean (x.Value == y.Value);
429 public static SqlBoolean operator > (SqlString x, SqlString y)
431 if (x.IsNull || y.IsNull)
432 return SqlBoolean.Null;
434 return new SqlBoolean (x.CompareTo (y) > 0);
437 // Greater Than Or Equal
438 public static SqlBoolean operator >= (SqlString x, SqlString y)
440 if (x.IsNull || y.IsNull)
441 return SqlBoolean.Null;
443 return new SqlBoolean (x.CompareTo (y) >= 0);
446 public static SqlBoolean operator != (SqlString x, SqlString y)
448 if (x.IsNull || y.IsNull)
449 return SqlBoolean.Null;
451 return new SqlBoolean (x.Value != y.Value);
455 public static SqlBoolean operator < (SqlString x, SqlString y)
457 if (x.IsNull || y.IsNull)
458 return SqlBoolean.Null;
460 return new SqlBoolean (x.CompareTo (y) < 0);
463 // Less Than Or Equal
464 public static SqlBoolean operator <= (SqlString x, SqlString y)
466 if (x.IsNull || y.IsNull)
467 return SqlBoolean.Null;
469 return new SqlBoolean (x.CompareTo (y) <= 0);
472 // **************************************
474 // **************************************
476 public static explicit operator SqlString (SqlBoolean x)
481 return new SqlString (x.Value.ToString ());
484 public static explicit operator SqlString (SqlByte x)
489 return new SqlString (x.Value.ToString ());
492 public static explicit operator SqlString (SqlDateTime x)
497 return new SqlString (x.Value.ToString ());
500 public static explicit operator SqlString (SqlDecimal x)
505 return new SqlString (x.Value.ToString ());
506 // return new SqlString (x.Value.ToString ("N", DecimalFormat));
509 public static explicit operator SqlString (SqlDouble x)
514 return new SqlString (x.Value.ToString ());
517 public static explicit operator SqlString (SqlGuid x)
522 return new SqlString (x.Value.ToString ());
525 public static explicit operator SqlString (SqlInt16 x)
530 return new SqlString (x.Value.ToString ());
533 public static explicit operator SqlString (SqlInt32 x)
538 return new SqlString (x.Value.ToString ());
541 public static explicit operator SqlString (SqlInt64 x)
546 return new SqlString (x.Value.ToString ());
549 public static explicit operator SqlString (SqlMoney x)
554 return new SqlString (x.ToString ());
557 public static explicit operator SqlString (SqlSingle x)
562 return new SqlString (x.Value.ToString ());
565 public static explicit operator string (SqlString x)
570 public static implicit operator SqlString (string x)
572 return new SqlString (x);
576 public static SqlString Add (SqlString x, SqlString y)
582 public int CompareTo (SqlString value)
584 return CompareSqlString (value);
590 #endregion // Public Methods
593 XmlSchema IXmlSerializable.GetSchema ()
595 throw new NotImplementedException ();
599 void IXmlSerializable.ReadXml (XmlReader reader)
601 throw new NotImplementedException ();
605 void IXmlSerializable.WriteXml (XmlWriter writer)
607 throw new NotImplementedException ();