1 // System.Data.SqlTypes.SqlString
\r
4 // Konstantin Triger <kostat@mainsoft.com>
5 // Boris Kirzner <borisk@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Data.SqlTypes
\r
37 * CURRENT LIMITATIONS:
\r
38 * 1. constructor public SqlString(String, int) not supported
\r
39 * 2. constructor public SqlString(int, SqlCompareOption, byte[]) not supported
\r
40 * 3. constructor public SqlString(String, int, SqlCompareOption) not supported
\r
41 * 4. constructor public SqlString(int, SqlCompareOption, byte[], boolen) not supported
\r
42 * 5. constructor public SqlString(int, SqlCompareOption, byte[], boolen) not supported
\r
43 * 6. constructor public SqlString(int, SqlCompareOption, byte[], int, int) not supported
\r
44 * 7. field IgnoreKanaType is ignored
\r
45 * 8. field IgnoreNonSpace is ignored
\r
46 * 9. field IgnoreWidth is ignored
\r
47 * 10. any Localeinformation is ignored that is the reson for limitation 1-6
\r
48 * 11. property CompareInfo not implemented - it relates to the Locale issu
\r
49 * 12. property CultureInfo not implemented - it relates to the Locale issu
\r
50 * 12. property LCID not implemented - it relates to the Locale issu
\r
51 * 13. method GetCompareOptionsFromSqlCompareOptions not implemented - it relates to the Locale issu
\r
52 * 14. method GetNonUnicodeBytes() - not implemented. Need more information
\r
53 * 15. method GetUnicodeBytes() - implemented - not sure the right way!!!
\r
57 public struct SqlString : INullable, IComparable
\r
59 private String _value;
\r
60 private bool _isNull;
\r
61 private SqlCompareOptions _compareOptions;
\r
63 public static readonly SqlString Null = new SqlString();
\r
64 public static readonly int IgnoreCase = 1;
\r
65 public static readonly int IgnoreWidth = 16;
\r
66 public static readonly int IgnoreNonSpace = 2;
\r
67 public static readonly int IgnoreKanaType = 8;
\r
68 public static readonly int BinarySort = 32786;
\r
71 private SqlString(bool isNull)
\r
74 _value = String.Empty;
\r
75 _compareOptions = SqlCompareOptions.None;
\r
79 * Initializes a new instance of the SqlString structure using the specified string.
\r
80 * @param value The string to store.
\r
82 public SqlString(String value)
\r
86 _compareOptions = SqlCompareOptions.None;
\r
91 * Indicates whether or not Value is null.
\r
92 * @return true if Value is null, otherwise false.
\r
102 public SqlCompareOptions CompareOptions
\r
106 return _compareOptions;
\r
110 public String Value
\r
116 throw new SqlNullValueException();
\r
123 * Creates a copy of this SqlString object.
\r
124 * @return A new SqlString object in which all property values are the same as the original.
\r
126 public SqlString Clone()
\r
129 if (_value == null)
\r
130 clone = new SqlString();
\r
132 clone = new SqlString(_value);
\r
134 clone._compareOptions = _compareOptions;
\r
139 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
140 * @param obj The object to compare.
\r
141 * @return A signed number indicating the relative values of the instance and the object.
\r
142 * Less than zero This instance is less than object.
\r
143 * Zero This instance is the same as object.
\r
144 * Greater than zero This instance is greater than object -or-
\r
145 * object is a null reference.
\r
147 public int CompareTo(Object obj)
\r
152 if (Object.ReferenceEquals(obj, this))
\r
155 if (obj is SqlString)
\r
157 SqlString sqlStr = (SqlString)obj;
\r
159 if (sqlStr._value == null && this._value == null)
\r
162 if (sqlStr._value == null)
\r
164 if (this._value == null)
\r
167 if (_compareOptions == SqlCompareOptions.BinarySort ||
\r
168 _compareOptions == SqlCompareOptions.None)
\r
169 return String.Compare(this._value, sqlStr._value);
\r
171 if (_compareOptions == SqlCompareOptions.IgnoreCase)
\r
172 return String.Compare(this._value, sqlStr._value, true);
\r
175 throw new ArgumentException("parameter obj is not SqlString : " + obj.GetType().Name);
\r
180 * Concatenates the two specified SqlString structures.
\r
181 * @param x A SqlString.
\r
182 * @param y A SqlString.
\r
183 * @return A SqlString containing the newly concatenated value representing the contents of the two SqlString parameters.
\r
184 * If any of the parameters or their value equals null the returned value is SqlString.Null.
\r
186 public static SqlString Concat(SqlString x, SqlString y)
\r
188 if (x.IsNull || y.IsNull)
\r
189 return SqlString.Null;
\r
191 return new SqlString(x._value + y._value);
\r
195 // public bool Equals(Object obj)
\r
197 // if (Object.ReferenceEquals(obj, this))
\r
200 // if (obj is SqlString)
\r
202 // SqlString s = (SqlString)obj;
\r
204 // if (IsNull && s.IsNull)
\r
207 // if (IsNull || s.IsNull)
\r
210 // return _value.Equals(s._value);
\r
217 * Performs a logical comparison on two instances of SqlString to determine if they are equal.
\r
218 * @param x A SqlString instance.
\r
219 * @param y A SqlString instance.
\r
220 * @return true if the two values are equal, otherwise false.
\r
221 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
223 public static SqlBoolean Equals(SqlString x, SqlString y)
\r
225 if (x.IsNull || y.IsNull)
\r
226 return SqlBoolean.Null;
\r
228 if (x._value.Equals(y._value))
\r
229 return SqlBoolean.True;
\r
231 return SqlBoolean.False;
\r
234 public override bool Equals(object obj)
\r
236 if(obj is SqlString)
\r
238 SqlString sqlObj = (SqlString)obj;
\r
239 if(sqlObj.IsNull && IsNull)
\r
241 if(sqlObj.IsNull || IsNull)
\r
243 return sqlObj.Value == this.Value;
\r
248 public override int GetHashCode()
\r
253 return Value.GetHashCode();
\r
258 * Gets an array of bytes, containing the contents of the SqlString in Unicode format.
\r
259 * @return An byte array, containing the contents of the SqlString in Unicode format.
\r
261 public byte[] GetUnicodeString()
\r
263 /** @todo check if it works */
\r
267 return System.Text.Encoding.Default.GetBytes(_value);
\r
271 * Compares two instances of SqlString to determine if the first is greater than the second.
\r
272 * @param x A SqlString instance
\r
273 * @param y A SqlString instance
\r
274 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
275 * If either instance of SqlString is null, the Value of the SqlBoolean will be Null.
\r
277 public static SqlBoolean GreaterThan(SqlString x, SqlString y)
\r
279 if (x.IsNull || y.IsNull)
\r
280 return SqlBoolean.Null;
\r
282 if (x.CompareTo(y) > 0)
\r
283 return SqlBoolean.True;
\r
285 return SqlBoolean.False;
\r
289 * Compares two instances of SqlString to determine if the first is greater than or equal to the second.
\r
290 * @param x A SqlString instance
\r
291 * @param y A SqlString instance
\r
292 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
293 * If either instance of SqlString is null, the Value of the SqlBoolean will be Null.
\r
295 public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)
\r
297 if (x.IsNull || y.IsNull)
\r
298 return SqlBoolean.Null;
\r
300 if (x.CompareTo(y) >= 0)
\r
301 return SqlBoolean.True;
\r
303 return SqlBoolean.False;
\r
307 * Compares two instances of SqlString to determine if the first is less than the second.
\r
308 * @param x A SqlString instance
\r
309 * @param y A SqlString instance
\r
310 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
311 * If either instance of SqlString is null, the Value of the SqlBoolean will be Null.
\r
313 public static SqlBoolean LessThan(SqlString x, SqlString y)
\r
315 if (x.IsNull || y.IsNull)
\r
316 return SqlBoolean.Null;
\r
318 if (x.CompareTo(y) < 0)
\r
319 return SqlBoolean.True;
\r
321 return SqlBoolean.False;
\r
325 * Compares two instances of SqlString to determine if the first is less than the second.
\r
326 * @param x A SqlString instance
\r
327 * @param y A SqlString instance
\r
328 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
329 * If either instance of SqlString is null, the Value of the SqlBoolean will be Null.
\r
331 public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)
\r
333 if (x.IsNull || y.IsNull)
\r
334 return SqlBoolean.Null;
\r
336 if (x.CompareTo(y) <= 0)
\r
337 return SqlBoolean.True;
\r
339 return SqlBoolean.False;
\r
344 * Compares two instances of SqlString to determine if they are equal.
\r
345 * @param x A SqlString instance
\r
346 * @param y A SqlString instance
\r
347 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
348 * If either instance of SqlString is null, the Value of the SqlBoolean will be Null.
\r
350 public static SqlBoolean NotEquals(SqlString x, SqlString y)
\r
352 SqlBoolean res = Equals(x, y);
\r
357 return SqlBoolean.True;
\r
359 return SqlBoolean.False;
\r
364 * Converts this SqlString structure to SqlBoolean.
\r
365 * @return A SqlBoolean structure whose Value will be True if the SqlString structure's Value is non-zero, False if the SqlString is zero
\r
366 * and Null if the SqlString structure is Null.
\r
368 public SqlBoolean ToSqlBoolean()
\r
370 return SqlBoolean.Parse(_value);
\r
374 * Converts this SqlString structure to SqlByte.
\r
375 * @return A SqlByte structure whose Value equals the Value of this SqlString structure.
\r
377 public SqlByte ToSqlByte()
\r
379 return SqlByte.Parse(_value);
\r
383 * Converts this SqlString structure to SqlDecimal.
\r
384 * @return A SqlDecimal structure whose Value equals the Value of this SqlString structure.
\r
386 public SqlDecimal ToSqlDecimal()
\r
388 return SqlDecimal.Parse(_value);
\r
392 * Converts this SqlString structure to SqlDouble.
\r
393 * @return A SqlDouble structure whose Value equals the Value of this SqlString structure.
\r
395 public SqlDouble ToSqlDouble()
\r
397 return SqlDouble.Parse(_value);
\r
401 * Converts this SqlString structure to SqlSingle.
\r
402 * @return A SqlDouble structure whose Value equals the Value of this SqlString structure.
\r
404 public SqlSingle ToSqlSingle()
\r
406 return SqlSingle.Parse(_value);
\r
410 * Converts this SqlString structure to SqlDateTime.
\r
411 * @return A SqlDateTime structure whose Value equals the Value of this SqlString structure.
\r
413 public SqlDateTime ToSqlDateTime()
\r
415 return SqlDateTime.Parse(_value);
\r
419 * Converts this SqlString structure to SqlInt16.
\r
420 * @return A SqlInt16 structure whose Value equals the Value of this SqlString structure.
\r
422 public SqlInt16 ToSqlInt16()
\r
424 return SqlInt16.Parse(_value);
\r
428 * Converts this SqlString structure to SqlInt32.
\r
429 * @return A SqlInt32 structure whose Value equals the Value of this SqlString structure.
\r
431 public SqlInt32 ToSqlInt32()
\r
433 return SqlInt32.Parse(_value);
\r
437 * Converts this SqlString structure to SqlInt64.
\r
438 * @return A SqlInt64 structure whose Value equals the Value of this SqlString structure.
\r
440 public SqlInt64 ToSqlInt64()
\r
442 return SqlInt64.Parse(_value);
\r
446 * Converts this SqlString instance to SqlString.
\r
447 * @return A SqlMoney instance whose Value equals the Value of this SqlString instance.
\r
449 public SqlMoney ToSqlMoney()
\r
451 return SqlMoney.Parse(_value);
\r
454 public override String ToString()
\r
458 return _value.ToString();
\r
462 public static SqlString operator + (SqlString x, SqlString y)
\r
464 if (x.IsNull || y.IsNull)
\r
465 return SqlString.Null;
\r
467 return new SqlString (x.Value + y.Value);
\r
471 public static SqlBoolean operator == (SqlString x, SqlString y)
\r
473 if (x.IsNull || y.IsNull)
\r
474 return SqlBoolean.Null;
\r
476 return new SqlBoolean (x.Value == y.Value);
\r
480 public static SqlBoolean operator > (SqlString x, SqlString y)
\r
482 if (x.IsNull || y.IsNull)
\r
483 return SqlBoolean.Null;
\r
485 return new SqlBoolean (x.CompareTo (y) > 0);
\r
488 // Greater Than Or Equal
\r
489 public static SqlBoolean operator >= (SqlString x, SqlString y)
\r
491 if (x.IsNull || y.IsNull)
\r
492 return SqlBoolean.Null;
\r
494 return new SqlBoolean (x.CompareTo (y) >= 0);
\r
497 public static SqlBoolean operator != (SqlString x, SqlString y)
\r
499 if (x.IsNull || y.IsNull)
\r
500 return SqlBoolean.Null;
\r
502 return new SqlBoolean (x.Value != y.Value);
\r
506 public static SqlBoolean operator < (SqlString x, SqlString y)
\r
508 if (x.IsNull || y.IsNull)
\r
509 return SqlBoolean.Null;
\r
511 return new SqlBoolean (x.CompareTo (y) < 0);
\r
514 // Less Than Or Equal
\r
515 public static SqlBoolean operator <= (SqlString x, SqlString y)
\r
517 if (x.IsNull || y.IsNull)
\r
518 return SqlBoolean.Null;
\r
520 return new SqlBoolean (x.CompareTo (y) <= 0);
\r
523 // **************************************
\r
524 // Type Conversions
\r
525 // **************************************
\r
527 public static explicit operator SqlString (SqlBoolean x)
\r
532 return new SqlString (x.Value.ToString ());
\r
535 public static explicit operator SqlString (SqlByte x)
\r
540 return new SqlString (x.Value.ToString ());
\r
543 public static explicit operator SqlString (SqlDateTime x)
\r
548 return new SqlString (x.Value.ToString ());
\r
551 public static explicit operator SqlString (SqlDecimal x)
\r
556 return new SqlString (x.Value.ToString ());
\r
559 public static explicit operator SqlString (SqlDouble x)
\r
564 return new SqlString (x.Value.ToString ());
\r
567 public static explicit operator SqlString (SqlGuid x)
\r
572 return new SqlString (x.Value.ToString ());
\r
575 public static explicit operator SqlString (SqlInt16 x)
\r
580 return new SqlString (x.Value.ToString ());
\r
583 public static explicit operator SqlString (SqlInt32 x)
\r
588 return new SqlString (x.Value.ToString ());
\r
591 public static explicit operator SqlString (SqlInt64 x)
\r
596 return new SqlString (x.Value.ToString ());
\r
599 public static explicit operator SqlString (SqlMoney x)
\r
604 return new SqlString (x.Value.ToString ());
\r
607 public static explicit operator SqlString (SqlSingle x)
\r
612 return new SqlString (x.Value.ToString ());
\r
615 public static explicit operator string (SqlString x)
\r
620 public static implicit operator SqlString (string x)
\r
622 return new SqlString (x);
\r