2009-11-21 Daniel Morgan <monodanmorg@yahoo.com>
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OracleString.cs
1 //
2 // OracleString.cs 
3 //
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
6 //
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
9 //
10 // Authors: Tim Coleman <tim@timcoleman.com>
11 //          Atsushi Enomoto <atsushi@ximian.com>
12 //
13 // Copyright (C) Tim Coleman, 2003
14 //
15 // Licensed under the MIT/X11 License.
16 //
17
18 using System;
19 using System.Data.SqlTypes;
20 using System.Globalization;
21
22 namespace System.Data.OracleClient
23 {
24         public struct OracleString : IComparable, INullable
25         {
26                 #region Fields
27
28                 string value;
29                 bool notNull;
30
31                 public static readonly OracleString Empty = new OracleString (String.Empty);
32                 public static readonly OracleString Null = new OracleString ();
33
34                 #endregion // Fields
35
36                 #region Constructors
37
38                 public OracleString (string s)
39                 {
40                         value = s;
41                         notNull = true;
42                 }
43
44                 #endregion // Constructors
45
46                 #region Properties
47
48                 public bool IsNull {
49                         get { return !notNull; }
50                 }
51
52                 public int Length {
53                         get { return value.Length; }
54                 }
55
56                 public char this [int index] {
57                         get { return value [index]; }
58                 }
59
60                 public string Value {
61                         get { return value; }
62                 }
63
64                 #endregion // Properties
65
66                 #region Methods
67
68                 public int CompareTo (object obj)
69                 {
70                         if (obj == null)
71                                 return 1;
72                         else if (!(obj is OracleString))
73                                 throw new ArgumentException ("Value is not a System.Data.OracleClient.OracleString");
74                         else if (((OracleString) obj).IsNull)
75                                 return 1;
76                         else
77                                 return value.CompareTo (((OracleString) obj).Value);
78                 }
79
80                 public static OracleBoolean GreaterThan (OracleString x, OracleString y)
81                 {
82                         if (x.IsNull || y.IsNull)
83                                 return OracleBoolean.Null;
84                         return (x > y);
85                 }
86
87                 public static OracleBoolean GreaterThanOrEqual (OracleString x, OracleString y)
88                 {
89                         if (x.IsNull || y.IsNull)
90                                 return OracleBoolean.Null;
91                         return (x  >= y);
92                 }
93
94                 public static OracleBoolean LessThan (OracleString x, OracleString y)
95                 {
96                         return (x < y);
97                 }
98
99                 public static OracleBoolean LessThanOrEqual (OracleString x, OracleString y)
100                 {
101                         return (x <= y);
102                 }
103
104                 public static OracleString Concat (OracleString x, OracleString y)
105                 {
106                         return x + y;
107                 }
108
109                 public override int GetHashCode ()
110                 {
111                         // It returns value string's HashCode.
112                         return notNull ? value.GetHashCode () : 0;
113                 }
114
115                 public override bool Equals (object value)
116                 {
117                         if (value is OracleString) {
118                                 OracleString s = (OracleString) value;
119                                 if (notNull && s.notNull)
120                                         return this.value == s.value;
121                                 else
122                                         throw new InvalidOperationException ("the value is Null.");
123                         }
124                         return false;
125                 }
126
127                 public static OracleBoolean Equals (OracleString x, OracleString y)
128                 {
129                         return (x == y);
130                 }
131
132                 public static OracleBoolean NotEquals (OracleString x, OracleString y)
133                 {
134                         return (x != y);
135                 }
136
137                 public override string ToString ()
138                 {
139                         return notNull ? value : "Null";
140                 }
141
142                 #endregion // Methods
143
144                 #region Operators
145
146                 public static OracleString operator + (OracleString x, OracleString y)
147                 {
148                         return (x.notNull && y.notNull) ?
149                                 new OracleString (x.value + y.value) :
150                                 Null;
151                 }
152
153                 public static OracleBoolean operator == (OracleString x, OracleString y)
154                 {
155                         return (!x.notNull || !y.notNull) ?
156                                 OracleBoolean.Null : new OracleBoolean (x.value == y.value);
157                 }
158
159                 public static explicit operator string (OracleString x)
160                 {
161                         return x.Value;
162                 }
163
164                 [MonoTODO]
165                 public static OracleBoolean operator > (OracleString x, OracleString y)
166                 {
167                         throw new NotImplementedException ();
168                 }
169
170                 [MonoTODO]
171                 public static OracleBoolean operator >= (OracleString x, OracleString y)
172                 {
173                         throw new NotImplementedException ();
174                 }
175
176                 public static implicit operator OracleString (string s)
177                 {
178                         return new OracleString (s);
179                 }
180
181                 public static OracleBoolean operator != (OracleString x, OracleString y)
182                 {
183                         return (!x.notNull || !y.notNull) ?
184                                 OracleBoolean.Null : x.value != y.value;
185                 }
186
187                 public static OracleBoolean operator < (OracleString x, OracleString y)
188                 {
189                         return (!x.notNull || !y.notNull) ?
190                                 OracleBoolean.Null :
191                                 new OracleBoolean (String.Compare (x.value, y.value, false, CultureInfo.InvariantCulture) < 0);
192                 }
193
194                 public static OracleBoolean operator <= (OracleString x, OracleString y)
195                 {
196                         return (!x.notNull || !y.notNull) ?
197                                 OracleBoolean.Null : new OracleBoolean (String.Compare (x.value, y.value, false, CultureInfo.InvariantCulture) <= 0);
198                 }
199
200                 #endregion // Operators
201         }
202 }