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 ("GetXsdType")]
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 return new SqlString (x.Value + y.Value);
417 public static SqlBoolean operator == (SqlString x, SqlString y)
419 if (x.IsNull || y.IsNull)
420 return SqlBoolean.Null;
422 return new SqlBoolean (x.Value == y.Value);
426 public static SqlBoolean operator > (SqlString x, SqlString y)
428 if (x.IsNull || y.IsNull)
429 return SqlBoolean.Null;
431 return new SqlBoolean (x.CompareTo (y) > 0);
434 // Greater Than Or Equal
435 public static SqlBoolean operator >= (SqlString x, SqlString y)
437 if (x.IsNull || y.IsNull)
438 return SqlBoolean.Null;
440 return new SqlBoolean (x.CompareTo (y) >= 0);
443 public static SqlBoolean operator != (SqlString x, SqlString y)
445 if (x.IsNull || y.IsNull)
446 return SqlBoolean.Null;
448 return new SqlBoolean (x.Value != y.Value);
452 public static SqlBoolean operator < (SqlString x, SqlString y)
454 if (x.IsNull || y.IsNull)
455 return SqlBoolean.Null;
457 return new SqlBoolean (x.CompareTo (y) < 0);
460 // Less Than Or Equal
461 public static SqlBoolean operator <= (SqlString x, SqlString y)
463 if (x.IsNull || y.IsNull)
464 return SqlBoolean.Null;
466 return new SqlBoolean (x.CompareTo (y) <= 0);
469 // **************************************
471 // **************************************
473 public static explicit operator SqlString (SqlBoolean x)
478 return new SqlString (x.Value.ToString ());
481 public static explicit operator SqlString (SqlByte x)
486 return new SqlString (x.Value.ToString ());
489 public static explicit operator SqlString (SqlDateTime x)
494 return new SqlString (x.Value.ToString ());
497 public static explicit operator SqlString (SqlDecimal x)
502 return new SqlString (x.Value.ToString ());
503 // return new SqlString (x.Value.ToString ("N", DecimalFormat));
506 public static explicit operator SqlString (SqlDouble x)
511 return new SqlString (x.Value.ToString ());
514 public static explicit operator SqlString (SqlGuid x)
519 return new SqlString (x.Value.ToString ());
522 public static explicit operator SqlString (SqlInt16 x)
527 return new SqlString (x.Value.ToString ());
530 public static explicit operator SqlString (SqlInt32 x)
535 return new SqlString (x.Value.ToString ());
538 public static explicit operator SqlString (SqlInt64 x)
543 return new SqlString (x.Value.ToString ());
546 public static explicit operator SqlString (SqlMoney x)
551 return new SqlString (x.ToString ());
554 public static explicit operator SqlString (SqlSingle x)
559 return new SqlString (x.Value.ToString ());
562 public static explicit operator string (SqlString x)
567 public static implicit operator SqlString (string x)
569 return new SqlString (x);
573 public static SqlString Add (SqlString x, SqlString y)
579 public int CompareTo (SqlString value)
581 return CompareSqlString (value);
587 #endregion // Public Methods
589 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
591 XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
592 return qualifiedName;
596 XmlSchema IXmlSerializable.GetSchema ()
598 throw new NotImplementedException ();
602 void IXmlSerializable.ReadXml (XmlReader reader)
604 throw new NotImplementedException ();
608 void IXmlSerializable.WriteXml (XmlWriter writer)
610 throw new NotImplementedException ();