2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes.jvm / SqlString.cs
1 // System.Data.SqlTypes.SqlString\r
2 //
3 // Authors:
4 //      Konstantin Triger <kostat@mainsoft.com>
5 //      Boris Kirzner <borisk@mainsoft.com>
6 //      
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //\r
30 \r
31 namespace System.Data.SqlTypes\r
32 {\r
33 \r
34     using System;\r
35 \r
36     /*\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
54     */\r
55 \r
56 \r
57     public struct SqlString : INullable, IComparable\r
58     {\r
59         private String _value;\r
60         private bool _isNull;\r
61         private SqlCompareOptions _compareOptions;\r
62 \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
69 \r
70         \r
71         private SqlString(bool isNull)\r
72         {\r
73             _isNull = isNull;\r
74             _value = String.Empty;\r
75             _compareOptions = SqlCompareOptions.None;\r
76         }\r
77 \r
78         /**\r
79          * Initializes a new instance of the SqlString structure using the specified string.\r
80          * @param value The string to store.\r
81          */\r
82         public SqlString(String value) \r
83         { \r
84             _value = value;\r
85             _isNull = false;\r
86             _compareOptions = SqlCompareOptions.None;\r
87         }\r
88 \r
89         \r
90         /**\r
91          * Indicates whether or not Value is null.\r
92          * @return true if Value is null, otherwise false.\r
93          */\r
94         public bool IsNull\r
95         {\r
96             get\r
97             {\r
98                 return _isNull;\r
99             }\r
100         }\r
101 \r
102         public SqlCompareOptions CompareOptions\r
103         {\r
104             get\r
105             {\r
106                 return _compareOptions;\r
107             }\r
108         }\r
109 \r
110         public String Value\r
111         {\r
112             get\r
113             {\r
114                 if(IsNull)\r
115                 {\r
116                     throw new SqlNullValueException();\r
117                 }\r
118                 return _value;\r
119             }\r
120         }\r
121 \r
122         /**\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
125          */\r
126         public SqlString Clone()\r
127         {\r
128             SqlString clone;\r
129             if (_value == null)\r
130                 clone = new SqlString();\r
131             else\r
132                 clone = new SqlString(_value);\r
133 \r
134             clone._compareOptions = _compareOptions;\r
135             return clone;\r
136         }\r
137 \r
138         /**\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
146          */\r
147         public int CompareTo(Object obj)\r
148         {\r
149             if (obj == null)\r
150                 return 1;\r
151 \r
152             if (Object.ReferenceEquals(obj, this))\r
153                 return 0;\r
154 \r
155             if (obj is SqlString)\r
156             {\r
157                 SqlString sqlStr = (SqlString)obj;\r
158 \r
159                 if (sqlStr._value == null && this._value == null)\r
160                     return 0;\r
161 \r
162                 if (sqlStr._value == null)\r
163                     return 1;\r
164                 if (this._value == null)\r
165                     return -1;\r
166 \r
167                 if (_compareOptions == SqlCompareOptions.BinarySort ||\r
168                     _compareOptions == SqlCompareOptions.None)\r
169                     return String.Compare(this._value, sqlStr._value);\r
170 \r
171                 if (_compareOptions == SqlCompareOptions.IgnoreCase)\r
172                     return String.Compare(this._value, sqlStr._value, true);\r
173             }\r
174 \r
175             throw new ArgumentException("parameter obj is not SqlString : " + obj.GetType().Name);\r
176 \r
177         }\r
178 \r
179         /**\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
185          */\r
186         public static SqlString Concat(SqlString x, SqlString y)\r
187         {\r
188             if (x.IsNull || y.IsNull)\r
189                 return SqlString.Null;\r
190 \r
191             return new SqlString(x._value + y._value);\r
192         }\r
193 \r
194 \r
195 //        public bool Equals(Object obj)\r
196 //        {\r
197 //            if (Object.ReferenceEquals(obj, this))\r
198 //                return true;\r
199 //\r
200 //            if (obj is SqlString)\r
201 //            {\r
202 //                SqlString s = (SqlString)obj;\r
203 //\r
204 //                if (IsNull && s.IsNull)\r
205 //                    return true;\r
206 //\r
207 //                if (IsNull || s.IsNull)\r
208 //                    return false;\r
209 //\r
210 //                return _value.Equals(s._value);\r
211 //            }\r
212 //\r
213 //            return false;\r
214 //        }\r
215 \r
216         /**\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
222          */\r
223         public static SqlBoolean Equals(SqlString x, SqlString y)\r
224         {\r
225             if (x.IsNull || y.IsNull)\r
226                 return SqlBoolean.Null;\r
227 \r
228             if (x._value.Equals(y._value))\r
229                 return SqlBoolean.True;\r
230 \r
231             return SqlBoolean.False;\r
232         }\r
233 \r
234         public override bool Equals(object obj)\r
235         {\r
236             if(obj is SqlString)\r
237             {\r
238                 SqlString sqlObj = (SqlString)obj;\r
239                 if(sqlObj.IsNull && IsNull)\r
240                     return true;\r
241                 if(sqlObj.IsNull || IsNull)\r
242                     return false;\r
243                 return sqlObj.Value == this.Value;\r
244             }\r
245             return false;\r
246         }\r
247 \r
248         public override int GetHashCode()\r
249         {\r
250             if(IsNull)\r
251                 return -1;\r
252 \r
253             return Value.GetHashCode();\r
254         }\r
255 \r
256 \r
257         /**\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
260          */\r
261         public byte[] GetUnicodeString()\r
262         {\r
263             /** @todo check if it works */\r
264             if (IsNull)\r
265                 return null;\r
266 \r
267             return System.Text.Encoding.Default.GetBytes(_value);\r
268         }\r
269 \r
270         /**\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
276          */\r
277         public static SqlBoolean GreaterThan(SqlString x, SqlString y)\r
278         {\r
279             if (x.IsNull || y.IsNull)\r
280                 return SqlBoolean.Null;\r
281 \r
282             if (x.CompareTo(y) > 0)\r
283                 return SqlBoolean.True;\r
284 \r
285             return SqlBoolean.False;\r
286         }\r
287 \r
288         /**\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
294          */\r
295         public static SqlBoolean GreaterThanOrEqual(SqlString x, SqlString y)\r
296         {\r
297             if (x.IsNull || y.IsNull)\r
298                 return SqlBoolean.Null;\r
299 \r
300             if (x.CompareTo(y) >= 0)\r
301                 return SqlBoolean.True;\r
302 \r
303             return SqlBoolean.False;\r
304         }\r
305 \r
306         /**\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
312          */\r
313         public static SqlBoolean LessThan(SqlString x, SqlString y)\r
314         {\r
315             if (x.IsNull || y.IsNull)\r
316                 return SqlBoolean.Null;\r
317 \r
318             if (x.CompareTo(y) < 0)\r
319                 return SqlBoolean.True;\r
320 \r
321             return SqlBoolean.False;\r
322         }\r
323 \r
324         /**\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
330          */\r
331         public static SqlBoolean LessThanOrEqual(SqlString x, SqlString y)\r
332         {\r
333             if (x.IsNull || y.IsNull)\r
334                 return SqlBoolean.Null;\r
335 \r
336             if (x.CompareTo(y) <= 0)\r
337                 return SqlBoolean.True;\r
338 \r
339             return SqlBoolean.False;\r
340         }\r
341 \r
342 \r
343         /**\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
349          */\r
350         public static SqlBoolean NotEquals(SqlString x, SqlString y)\r
351         {\r
352             SqlBoolean res = Equals(x, y);\r
353 \r
354             if (res.IsNull)\r
355                 return res;\r
356             if (res.IsFalse)\r
357                 return SqlBoolean.True;\r
358 \r
359             return SqlBoolean.False;\r
360         }\r
361 \r
362 \r
363         /**\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
367          */\r
368         public SqlBoolean ToSqlBoolean()\r
369         {\r
370             return SqlBoolean.Parse(_value);\r
371         }\r
372 \r
373         /**\r
374          * Converts this SqlString structure to SqlByte.\r
375          * @return A SqlByte structure whose Value equals the Value of this SqlString structure.\r
376          */\r
377         public SqlByte ToSqlByte()\r
378         {\r
379             return SqlByte.Parse(_value);\r
380         }\r
381 \r
382         /**\r
383          * Converts this SqlString structure to SqlDecimal.\r
384          * @return A SqlDecimal structure whose Value equals the Value of this SqlString structure.\r
385          */\r
386         public SqlDecimal ToSqlDecimal()\r
387         {\r
388             return SqlDecimal.Parse(_value);\r
389         }\r
390 \r
391         /**\r
392          * Converts this SqlString structure to SqlDouble.\r
393          * @return A SqlDouble structure whose Value equals the Value of this SqlString structure.\r
394          */\r
395         public SqlDouble ToSqlDouble()\r
396         {\r
397             return SqlDouble.Parse(_value);\r
398         }\r
399 \r
400         /**\r
401          * Converts this SqlString structure to SqlSingle.\r
402          * @return A SqlDouble structure whose Value equals the Value of this SqlString structure.\r
403          */\r
404         public SqlSingle ToSqlSingle()\r
405         {\r
406             return SqlSingle.Parse(_value);\r
407         }\r
408 \r
409         /**\r
410          * Converts this SqlString structure to SqlDateTime.\r
411          * @return A SqlDateTime structure whose Value equals the Value of this SqlString structure.\r
412          */\r
413         public SqlDateTime ToSqlDateTime()\r
414         {\r
415             return SqlDateTime.Parse(_value);\r
416         }\r
417 \r
418         /**\r
419          * Converts this SqlString structure to SqlInt16.\r
420          * @return A SqlInt16 structure whose Value equals the Value of this SqlString structure.\r
421          */\r
422         public SqlInt16 ToSqlInt16()\r
423         {\r
424             return SqlInt16.Parse(_value);\r
425         }\r
426 \r
427         /**\r
428          * Converts this SqlString structure to SqlInt32.\r
429          * @return A SqlInt32 structure whose Value equals the Value of this SqlString structure.\r
430          */\r
431         public SqlInt32 ToSqlInt32()\r
432         {\r
433             return SqlInt32.Parse(_value);\r
434         }\r
435 \r
436         /**\r
437          * Converts this SqlString structure to SqlInt64.\r
438          * @return A SqlInt64 structure whose Value equals the Value of this SqlString structure.\r
439          */\r
440         public SqlInt64 ToSqlInt64()\r
441         {\r
442             return SqlInt64.Parse(_value);\r
443         }\r
444 \r
445         /**\r
446          * Converts this SqlString instance to SqlString.\r
447          * @return A SqlMoney instance whose Value equals the Value of this SqlString instance.\r
448          */\r
449         public SqlMoney ToSqlMoney()\r
450         {\r
451             return SqlMoney.Parse(_value);\r
452         }\r
453 \r
454         public override String ToString()\r
455         {\r
456             if(IsNull)\r
457                 return "null";\r
458             return _value.ToString();\r
459         }\r
460 \r
461         // Concatenates\r
462         public static SqlString operator + (SqlString x, SqlString y) \r
463         {\r
464             if (x.IsNull || y.IsNull)\r
465                 return SqlString.Null;\r
466 \r
467             return new SqlString (x.Value + y.Value);\r
468         }\r
469 \r
470         // Equality\r
471         public static SqlBoolean operator == (SqlString x, SqlString y) \r
472         {\r
473             if (x.IsNull || y.IsNull)\r
474                 return SqlBoolean.Null;\r
475             else\r
476                 return new SqlBoolean (x.Value == y.Value);\r
477         }\r
478 \r
479         // Greater Than\r
480         public static SqlBoolean operator > (SqlString x, SqlString y) \r
481         {\r
482             if (x.IsNull || y.IsNull)\r
483                 return SqlBoolean.Null;\r
484             else\r
485                 return new SqlBoolean (x.CompareTo (y) > 0);\r
486         }\r
487 \r
488         // Greater Than Or Equal\r
489         public static SqlBoolean operator >= (SqlString x, SqlString y) \r
490         {\r
491             if (x.IsNull || y.IsNull)\r
492                 return SqlBoolean.Null;\r
493             else\r
494                 return new SqlBoolean (x.CompareTo (y) >= 0);\r
495         }\r
496 \r
497         public static SqlBoolean operator != (SqlString x, SqlString y) \r
498         { \r
499             if (x.IsNull || y.IsNull)\r
500                 return SqlBoolean.Null;\r
501             else\r
502                 return new SqlBoolean (x.Value != y.Value);\r
503         }\r
504 \r
505         // Less Than\r
506         public static SqlBoolean operator < (SqlString x, SqlString y) \r
507         {\r
508             if (x.IsNull || y.IsNull)\r
509                 return SqlBoolean.Null;\r
510             else\r
511                 return new SqlBoolean (x.CompareTo (y) < 0);\r
512         }\r
513 \r
514         // Less Than Or Equal\r
515         public static SqlBoolean operator <= (SqlString x, SqlString y) \r
516         {\r
517             if (x.IsNull || y.IsNull)\r
518                 return SqlBoolean.Null;\r
519             else\r
520                 return new SqlBoolean (x.CompareTo (y) <= 0);\r
521         }\r
522 \r
523         // **************************************\r
524         // Type Conversions\r
525         // **************************************\r
526 \r
527         public static explicit operator SqlString (SqlBoolean x) \r
528         {\r
529             if (x.IsNull)\r
530                 return Null;\r
531             else\r
532                 return new SqlString (x.Value.ToString ());\r
533         }\r
534 \r
535         public static explicit operator SqlString (SqlByte x) \r
536         {\r
537             if (x.IsNull)\r
538                 return Null;\r
539             else\r
540                 return new SqlString (x.Value.ToString ());\r
541         }\r
542 \r
543         public static explicit operator SqlString (SqlDateTime x) \r
544         {\r
545             if (x.IsNull)\r
546                 return Null;\r
547             else\r
548                 return new SqlString (x.Value.ToString ());\r
549         }\r
550 \r
551         public static explicit operator SqlString (SqlDecimal x) \r
552         {\r
553             if (x.IsNull)\r
554                 return Null;\r
555             else\r
556                 return new SqlString (x.Value.ToString ());\r
557         }\r
558 \r
559         public static explicit operator SqlString (SqlDouble x) \r
560         {\r
561             if (x.IsNull)\r
562                 return Null;\r
563             else\r
564                 return new SqlString (x.Value.ToString ());\r
565         }\r
566 \r
567         public static explicit operator SqlString (SqlGuid x) \r
568         {\r
569             if (x.IsNull)\r
570                 return Null;\r
571             else\r
572                 return new SqlString (x.Value.ToString ());\r
573         }\r
574 \r
575         public static explicit operator SqlString (SqlInt16 x) \r
576         {\r
577             if (x.IsNull)\r
578                 return Null;\r
579             else\r
580                 return new SqlString (x.Value.ToString ());\r
581         }\r
582 \r
583         public static explicit operator SqlString (SqlInt32 x) \r
584         {\r
585             if (x.IsNull)\r
586                 return Null;\r
587             else\r
588                 return new SqlString (x.Value.ToString ());\r
589         }\r
590 \r
591         public static explicit operator SqlString (SqlInt64 x) \r
592         {\r
593             if (x.IsNull)\r
594                 return Null;\r
595             else\r
596                 return new SqlString (x.Value.ToString ());\r
597         }\r
598 \r
599         public static explicit operator SqlString (SqlMoney x) \r
600         {\r
601             if (x.IsNull)\r
602                 return Null;\r
603             else\r
604                 return new SqlString (x.Value.ToString ());\r
605         }\r
606 \r
607         public static explicit operator SqlString (SqlSingle x) \r
608         {\r
609             if (x.IsNull)\r
610                 return Null;\r
611             else\r
612                 return new SqlString (x.Value.ToString ());\r
613         }\r
614 \r
615         public static explicit operator string (SqlString x) \r
616         {\r
617             return x.Value;\r
618         }\r
619 \r
620         public static implicit operator SqlString (string x) \r
621         {\r
622             return new SqlString (x);\r
623         }\r
624     }}