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.
38 using System
.Globalization
;
39 using System
.Threading
;
41 namespace System
.Data
.SqlTypes
44 /// A variable-length stream of characters
45 /// to be stored in or retrieved from the database
47 public struct SqlString
: INullable
, IComparable
56 // FIXME: locale id is not working yet
58 private SqlCompareOptions compareOptions
;
60 public static readonly int BinarySort
= 0x8000;
61 public static readonly int IgnoreCase
= 0x1;
62 public static readonly int IgnoreKanaType
= 0x8;
63 public static readonly int IgnoreNonSpace
= 0x2;
64 public static readonly int IgnoreWidth
= 0x10;
65 public static readonly SqlString Null
;
67 internal static readonly NumberFormatInfo MoneyFormat
;
68 internal static NumberFormatInfo DecimalFormat
;
75 MoneyFormat
= (NumberFormatInfo
) NumberFormatInfo
.InvariantInfo
.Clone ();
76 MoneyFormat
.NumberDecimalDigits
= 4;
77 MoneyFormat
.NumberGroupSeparator
= String
.Empty
;
79 DecimalFormat
= (NumberFormatInfo
) NumberFormatInfo
.InvariantInfo
.Clone ();
80 DecimalFormat
.NumberDecimalDigits
= 13;
81 DecimalFormat
.NumberGroupSeparator
= String
.Empty
;
84 // init with a string data
85 public SqlString (string data
)
88 lcid
= CultureInfo
.CurrentCulture
.LCID
;
90 this.compareOptions
= SqlCompareOptions
.IgnoreCase
|
91 SqlCompareOptions
.IgnoreKanaType
|
92 SqlCompareOptions
.IgnoreWidth
;
95 // init with a string data and locale id values.
96 public SqlString (string data
, int lcid
)
101 this.compareOptions
= SqlCompareOptions
.IgnoreCase
|
102 SqlCompareOptions
.IgnoreKanaType
|
103 SqlCompareOptions
.IgnoreWidth
;
106 // init with locale id, compare options,
107 // and an array of bytes data
108 public SqlString (int lcid
, SqlCompareOptions compareOptions
, byte[] data
)
109 : this (lcid
, compareOptions
, data
, true) { }
111 // init with string data, locale id, and compare options
112 public SqlString (string data
, int lcid
, SqlCompareOptions compareOptions
)
116 this.compareOptions
= compareOptions
;
120 // init with locale id, compare options, array of bytes data,
121 // and whether unicode is encoded or not
122 public SqlString (int lcid
, SqlCompareOptions compareOptions
, byte[] data
, bool fUnicode
)
127 chars
= new char [data
.Length
/2];
129 chars
= new char [data
.Length
];
132 for (int i
= 0; i
< chars
.Length
; i
++) {
135 chars
[i
] = (char)(data
[j
] << 16);
136 chars
[i
] += (char)data
[j
+ 1];
139 chars
[i
] = (char)data
[i
];
143 this.value = new String (chars
);
145 this.compareOptions
= compareOptions
;
149 // init with locale id, compare options, array of bytes data,
150 // starting index in the byte array,
151 // and number of bytes to copy
152 public SqlString (int lcid
, SqlCompareOptions compareOptions
, byte[] data
,
153 int index
, int count
)
154 : this (lcid
, compareOptions
, data
, index
, count
, true) { }
156 // init with locale id, compare options, array of bytes data,
157 // starting index in the byte array, number of byte to copy,
158 // and whether unicode is encoded or not
159 public SqlString (int lcid
, SqlCompareOptions compareOptions
, byte[] data
, int index
, int count
, bool fUnicode
)
164 chars
= new char [(count
- index
) / 2];
166 chars
= new char [count
- index
];
168 if (index
>= data
.Length
)
169 throw new ArgumentOutOfRangeException ("index");
171 if ((index
+ count
) > data
.Length
)
172 throw new ArgumentOutOfRangeException ("count");
175 for (int i
= index
; i
< chars
.Length
; i
++) {
178 chars
[i
] = (char)(data
[j
] << 16);
179 chars
[i
] += (char)data
[j
+1];
182 chars
[i
] = (char)data
[j
];
187 this.value = new String (chars
);
189 this.compareOptions
= compareOptions
;
193 #endregion // Constructors
196 #region Public Properties
198 public CompareInfo CompareInfo
{
200 return new CultureInfo (lcid
).CompareInfo
;
204 public CultureInfo CultureInfo
{
206 return new CultureInfo (lcid
);
211 get { return !notNull; }
214 // geographics location and language (locale id)
221 public SqlCompareOptions SqlCompareOptions
{
223 return compareOptions
;
227 public string Value
{
230 throw new SqlNullValueException (Locale
.GetText ("The property contains Null."));
236 #endregion // Public Properties
238 #region Private Properties
240 private CompareOptions CompareOptions
{
243 (this.compareOptions
& SqlCompareOptions
.BinarySort
) != 0 ?
244 CompareOptions
.Ordinal
:
245 // 27 == all SqlCompareOptions - BinarySort
246 // (1,2,8,24 are common to CompareOptions)
247 (CompareOptions
)((int)this.compareOptions
& 27);
251 #endregion Private Properties
253 #region Public Methods
255 public SqlString
Clone()
257 return new SqlString (value, lcid
, compareOptions
);
260 public static CompareOptions
CompareOptionsFromSqlCompareOptions (SqlCompareOptions compareOptions
)
262 CompareOptions options
= CompareOptions
.None
;
264 if ((compareOptions
& SqlCompareOptions
.IgnoreCase
) != 0)
265 options
|= CompareOptions
.IgnoreCase
;
266 if ((compareOptions
& SqlCompareOptions
.IgnoreKanaType
) != 0)
267 options
|= CompareOptions
.IgnoreKanaType
;
268 if ((compareOptions
& SqlCompareOptions
.IgnoreNonSpace
) != 0)
269 options
|= CompareOptions
.IgnoreNonSpace
;
270 if ((compareOptions
& SqlCompareOptions
.IgnoreWidth
) != 0)
271 options
|= CompareOptions
.IgnoreWidth
;
272 if ((compareOptions
& SqlCompareOptions
.BinarySort
) != 0)
273 // FIXME: Exception string
274 throw new ArgumentOutOfRangeException ();
279 // **********************************
280 // Comparison Methods
281 // **********************************
283 public int CompareTo (object value)
287 else if (!(value is SqlString
))
288 throw new ArgumentException (Locale
.GetText ("Value is not a System.Data.SqlTypes.SqlString"));
290 return CompareSqlString ((SqlString
)value);
294 private int CompareSqlString (SqlString
value)
298 else if (value.CompareOptions
!= this.CompareOptions
)
299 throw new SqlTypeException (Locale
.GetText ("Two strings to be compared have different collation"));
301 // return String.Compare (this.value, ((SqlString)value).Value, (this.SqlCompareOptions & SqlCompareOptions.IgnoreCase) != 0, this.CultureInfo);
302 return CultureInfo
.CompareInfo
.Compare (this.value, value.Value
, this.CompareOptions
);
305 public static SqlString
Concat(SqlString x
, SqlString y
)
310 public override bool Equals(object value)
312 if (!(value is SqlString
))
314 if (this.IsNull
&& ((SqlString
)value).IsNull
)
316 else if (((SqlString
)value).IsNull
)
319 return (bool) (this == (SqlString
)value);
322 public static SqlBoolean
Equals(SqlString x
, SqlString y
)
327 public override int GetHashCode()
330 for (int i
= 0; i
< value.Length
; i
++)
331 result
= 91 * result
+ (int)(value [i
] ^
(value [i
] >> 32));
333 result
= 91 * result
+ lcid
.GetHashCode ();
334 result
= 91 * result
+ (int)compareOptions
;
339 public byte[] GetNonUnicodeBytes()
341 byte [] bytes
= new byte [value.Length
];
343 for (int i
= 0; i
< bytes
.Length
; i
++)
344 bytes
[i
] = (byte)value [i
];
349 public byte[] GetUnicodeBytes()
351 byte [] bytes
= new byte [value.Length
* 2];
354 for (int i
= 0; i
< value.Length
; i
++) {
355 bytes
[j
] = (byte)(value [i
] & 0x0000FFFF);
356 bytes
[j
+ 1] = (byte)((value [i
] & 0xFFFF0000) >> 16);
363 public static SqlBoolean
GreaterThan(SqlString x
, SqlString y
)
368 public static SqlBoolean
GreaterThanOrEqual(SqlString x
, SqlString y
)
373 public static SqlBoolean
LessThan(SqlString x
, SqlString y
)
378 public static SqlBoolean
LessThanOrEqual(SqlString x
, SqlString y
)
383 public static SqlBoolean
NotEquals(SqlString x
, SqlString y
)
388 // ****************************************
389 // Type Conversions From SqlString To ...
390 // ****************************************
392 public SqlBoolean
ToSqlBoolean()
394 return ((SqlBoolean
)this);
397 public SqlByte
ToSqlByte()
399 return ((SqlByte
)this);
402 public SqlDateTime
ToSqlDateTime()
404 return ((SqlDateTime
)this);
407 public SqlDecimal
ToSqlDecimal()
409 return ((SqlDecimal
)this);
412 public SqlDouble
ToSqlDouble()
414 return ((SqlDouble
)this);
417 public SqlGuid
ToSqlGuid()
419 return ((SqlGuid
)this);
422 public SqlInt16
ToSqlInt16()
424 return ((SqlInt16
)this);
427 public SqlInt32
ToSqlInt32()
429 return ((SqlInt32
)this);
432 public SqlInt64
ToSqlInt64()
434 return ((SqlInt64
)this);
437 public SqlMoney
ToSqlMoney()
439 return ((SqlMoney
)this);
442 public SqlSingle
ToSqlSingle()
444 return ((SqlSingle
)this);
447 public override string ToString()
451 return ((string)this);
454 // ***********************************
456 // ***********************************
459 public static SqlString
operator + (SqlString x
, SqlString y
)
461 if (x
.IsNull
|| y
.IsNull
)
462 return SqlString
.Null
;
464 if (( x
== null) || (y
== null))
465 return SqlString
.Null
;
467 return new SqlString (x
.Value
+ y
.Value
);
471 public static SqlBoolean
operator == (SqlString x
, SqlString y
)
473 if (x
.IsNull
|| y
.IsNull
)
474 return SqlBoolean
.Null
;
476 return new SqlBoolean (x
.Value
== y
.Value
);
480 public static SqlBoolean
operator > (SqlString x
, SqlString y
)
482 if (x
.IsNull
|| y
.IsNull
)
483 return SqlBoolean
.Null
;
485 return new SqlBoolean (x
.CompareTo (y
) > 0);
488 // Greater Than Or Equal
489 public static SqlBoolean
operator >= (SqlString x
, SqlString y
)
491 if (x
.IsNull
|| y
.IsNull
)
492 return SqlBoolean
.Null
;
494 return new SqlBoolean (x
.CompareTo (y
) >= 0);
497 public static SqlBoolean
operator != (SqlString x
, SqlString y
)
499 if (x
.IsNull
|| y
.IsNull
)
500 return SqlBoolean
.Null
;
502 return new SqlBoolean (x
.Value
!= y
.Value
);
506 public static SqlBoolean
operator < (SqlString x
, SqlString y
)
508 if (x
.IsNull
|| y
.IsNull
)
509 return SqlBoolean
.Null
;
511 return new SqlBoolean (x
.CompareTo (y
) < 0);
514 // Less Than Or Equal
515 public static SqlBoolean
operator <= (SqlString x
, SqlString y
)
517 if (x
.IsNull
|| y
.IsNull
)
518 return SqlBoolean
.Null
;
520 return new SqlBoolean (x
.CompareTo (y
) <= 0);
523 // **************************************
525 // **************************************
527 public static explicit operator SqlString (SqlBoolean x
)
532 return new SqlString (x
.Value
.ToString ());
535 public static explicit operator SqlString (SqlByte x
)
540 return new SqlString (x
.Value
.ToString ());
543 public static explicit operator SqlString (SqlDateTime x
)
548 return new SqlString (x
.Value
.ToString ());
551 public static explicit operator SqlString (SqlDecimal x
)
556 return new SqlString (x
.Value
.ToString ());
557 // return new SqlString (x.Value.ToString ("N", DecimalFormat));
560 public static explicit operator SqlString (SqlDouble x
)
565 return new SqlString (x
.Value
.ToString ());
568 public static explicit operator SqlString (SqlGuid x
)
573 return new SqlString (x
.Value
.ToString ());
576 public static explicit operator SqlString (SqlInt16 x
)
581 return new SqlString (x
.Value
.ToString ());
584 public static explicit operator SqlString (SqlInt32 x
)
589 return new SqlString (x
.Value
.ToString ());
592 public static explicit operator SqlString (SqlInt64 x
)
597 return new SqlString (x
.Value
.ToString ());
600 public static explicit operator SqlString (SqlMoney x
)
605 return new SqlString (x
.Value
.ToString ());
606 // return new SqlString (x.Value.ToString ("N", MoneyFormat));
609 public static explicit operator SqlString (SqlSingle x
)
614 return new SqlString (x
.Value
.ToString ());
617 public static explicit operator string (SqlString x
)
622 public static implicit operator SqlString (string x
)
624 return new SqlString (x
);
628 public static SqlString
Add (SqlString x
, SqlString y
)
634 public int CompareTo (SqlString
value)
636 return CompareSqlString (value);
642 #endregion // Public Methods