2002-05-08 Rodrigo Moya <rodrigo@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlMoney.cs
1 //
2 // System.Data.SqlTypes.SqlMoney
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //
7 // (C) Copyright 2002 Tim Coleman
8 //
9
10 using System;
11 using System.Globalization;
12
13 namespace System.Data.SqlTypes
14 {
15         public struct SqlMoney : INullable, IComparable
16         {
17                 #region Fields
18
19                 decimal value;
20
21                 public static readonly SqlMoney MaxValue = new SqlMoney (922337203685475.5807);
22                 public static readonly SqlMoney MinValue = new SqlMoney (-922337203685477.5808);
23                 public static readonly SqlMoney Null;
24                 public static readonly SqlMoney Zero = new SqlMoney (0);
25
26                 #endregion
27
28                 #region Constructors
29
30                 public SqlMoney (decimal value) 
31                 {
32                         this.value = value;
33                 }
34
35                 public SqlMoney (double value) 
36                 {
37                         this.value = (decimal)value;
38                 }
39
40                 public SqlMoney (int value) 
41                 {
42                         this.value = (decimal)value;
43                 }
44
45                 public SqlMoney (long value) 
46                 {
47                         this.value = (decimal)value;
48                 }
49
50                 #endregion
51
52                 #region Properties
53
54                 [MonoTODO]
55                 public bool IsNull { 
56                         get { return (bool) (this == Null); }
57                 }
58
59                 public decimal Value { 
60                         get { 
61                                 if (this.IsNull) 
62                                         throw new SqlNullValueException ();
63                                 else 
64                                         return value; 
65                         }
66                 }
67
68                 #endregion
69
70                 #region Methods
71
72                 public static SqlMoney Add (SqlMoney x, SqlMoney y)
73                 {
74                         return (x + y);
75                 }
76
77                 public int CompareTo (object value)
78                 {
79                         if (value == null)
80                                 return 1;
81                         else if (!(value is SqlMoney))
82                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
83                         else if (((SqlMoney)value).IsNull)
84                                 return 1;
85                         else
86                                 return this.value.CompareTo (((SqlMoney)value).Value);
87                 }
88
89                 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
90                 {
91                         return (x / y);
92                 }
93
94                 public override bool Equals (object value)
95                 {
96                         if (!(value is SqlMoney))
97                                 return false;
98                         else
99                                 return (bool) (this == (SqlMoney)value);
100                 }
101
102                 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
103                 {
104                         return (x == y);
105                 }
106
107                 [MonoTODO]
108                 public override int GetHashCode ()
109                 {
110                         return (int)value;
111                 }
112
113                 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
114                 {
115                         return (x > y);
116                 }
117
118                 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
119                 {
120                         return (x >= y);
121                 }
122
123                 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
124                 {
125                         return (x < y);
126                 }
127
128                 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
129                 {
130                         return (x <= y);
131                 }
132
133                 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
134                 {
135                         return (x * y);
136                 }
137
138                 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
139                 {
140                         return (x != y);
141                 }
142
143                 [MonoTODO]
144                 public static SqlMoney Parse (string s)
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
150                 {
151                         return (x - y);
152                 }
153
154                 public decimal ToDecimal ()
155                 {
156                         return value;
157                 }
158
159                 public double ToDouble ()
160                 {
161                         return (double)value;
162                 }
163
164                 public int ToInt32 ()
165                 {
166                         return (int)value;
167                 }
168
169                 public long ToInt64 ()
170                 {
171                         return (long)value;
172                 }
173
174                 public SqlBoolean ToSqlBoolean ()
175                 {
176                         return ((SqlBoolean)this);
177                 }
178                 
179                 public SqlByte ToSqlByte ()
180                 {
181                         return ((SqlByte)this);
182                 }
183
184                 public SqlDecimal ToSqlDecimal ()
185                 {
186                         return ((SqlDecimal)this);
187                 }
188
189                 public SqlDouble ToSqlDouble ()
190                 {
191                         return ((SqlDouble)this);
192                 }
193
194                 public SqlInt16 ToSqlInt16 ()
195                 {
196                         return ((SqlInt16)this);
197                 }
198
199                 public SqlInt32 ToSqlInt32 ()
200                 {
201                         return ((SqlInt32)this);
202                 }
203
204                 public SqlInt64 ToSqlInt64 ()
205                 {
206                         return ((SqlInt64)this);
207                 }
208
209                 public SqlSingle ToSqlSingle ()
210                 {
211                         return ((SqlSingle)this);
212                 }
213
214                 public SqlString ToSqlString ()
215                 {
216                         return ((SqlString)this);
217                 }
218
219                 public override string ToString ()
220                 {
221                         if (this.IsNull)
222                                 return String.Empty;
223                         else
224                                 return value.ToString ();
225                 }
226
227                 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
228                 {
229                         return new SqlMoney (x.Value + y.Value);
230                 }
231
232                 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
233                 {
234                         return new SqlMoney (x.Value / y.Value);
235                 }
236
237                 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
238                 {
239                         if (x.IsNull || y.IsNull) 
240                                 return SqlBoolean.Null;
241                         else
242                                 return new SqlBoolean (x.Value == y.Value);
243                 }
244
245                 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
246                 {
247                         if (x.IsNull || y.IsNull) 
248                                 return SqlBoolean.Null;
249                         else
250                                 return new SqlBoolean (x.Value > y.Value);
251                 }
252
253                 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
254                 {
255                         if (x.IsNull || y.IsNull) 
256                                 return SqlBoolean.Null;
257                         else
258                                 return new SqlBoolean (x.Value >= y.Value);
259                 }
260
261                 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
262                 {
263                         if (x.IsNull || y.IsNull) 
264                                 return SqlBoolean.Null;
265                         else
266                                 return new SqlBoolean (!(x.Value == y.Value));
267                 }
268
269                 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
270                 {
271                         if (x.IsNull || y.IsNull) 
272                                 return SqlBoolean.Null;
273                         else
274                                 return new SqlBoolean (x.Value < y.Value);
275                 }
276
277                 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
278                 {
279                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
280                         return new SqlBoolean (x.Value <= y.Value);
281                 }
282
283                 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
284                 {
285                         return new SqlMoney (x.Value * y.Value);
286                 }
287
288                 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
289                 {
290                         return new SqlMoney (x.Value - y.Value);
291                 }
292
293                 public static SqlMoney operator - (SqlMoney n)
294                 {
295                         return new SqlMoney (-(n.Value));
296                 }
297
298                 public static explicit operator SqlMoney (SqlBoolean x)
299                 {
300                         if (x.IsNull) 
301                                 return Null;
302                         else
303                                 return new SqlMoney ((decimal)x.ByteValue);
304                 }
305
306                 public static explicit operator SqlMoney (SqlDecimal x)
307                 {
308                         if (x.IsNull) 
309                                 return Null;
310                         else
311                                 return new SqlMoney (x.Value);
312                 }
313
314                 public static explicit operator SqlMoney (SqlDouble x)
315                 {
316                         if (x.IsNull) 
317                                 return Null;
318                         else
319                                 return new SqlMoney ((decimal)x.Value);
320                 }
321
322                 public static explicit operator decimal (SqlMoney x)
323                 {
324                         return x.Value;
325                 }
326
327                 public static explicit operator SqlMoney (SqlSingle x)
328                 {
329                         if (x.IsNull) 
330                                 return Null;
331                         else
332                                 return new SqlMoney ((decimal)x.Value);
333                 }
334
335                 [MonoTODO]
336                 public static explicit operator SqlMoney (SqlString x)
337                 {
338                         throw new NotImplementedException ();
339                 }
340
341                 public static implicit operator SqlMoney (decimal x)
342                 {
343                         return new SqlMoney (x);
344                 }
345
346                 public static implicit operator SqlMoney (SqlByte x)
347                 {
348                         if (x.IsNull) 
349                                 return Null;
350                         else
351                                 return new SqlMoney ((decimal)x.Value);
352                 }
353
354                 public static implicit operator SqlMoney (SqlInt16 x)
355                 {
356                         if (x.IsNull) 
357                                 return Null;
358                         else
359                                 return new SqlMoney ((decimal)x.Value);
360                 }
361
362                 public static implicit operator SqlMoney (SqlInt32 x)
363                 {
364                         if (x.IsNull) 
365                                 return Null;
366                         else
367                                 return new SqlMoney ((decimal)x.Value);
368                 }
369
370                 public static implicit operator SqlMoney (SqlInt64 x)
371                 {
372                         if (x.IsNull) 
373                                 return Null;
374                         else
375                                 return new SqlMoney ((decimal)x.Value);
376                 }
377
378                 #endregion
379         }
380 }
381