svn path=/branches/mono-1-1-9/mcs/; revision=51207
[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 //   Ville Palo <vi64pa@koti.soon.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Globalization;
36
37 namespace System.Data.SqlTypes
38 {
39         public struct SqlMoney : INullable, IComparable
40         {
41                 #region Fields
42
43                 decimal value;
44                 
45                 private bool notNull;
46                 
47                 public static readonly SqlMoney MaxValue = new SqlMoney (922337203685477.5807m);
48                 public static readonly SqlMoney MinValue = new SqlMoney (-922337203685477.5808m);
49                 public static readonly SqlMoney Null;
50                 public static readonly SqlMoney Zero = new SqlMoney (0);
51
52                 #endregion
53
54                 #region Constructors
55
56                 public SqlMoney (decimal value) 
57                 {
58                         if (value > 922337203685477.5807m || value < -922337203685477.5808m)
59                                 throw new OverflowException ();
60                         this.value = Decimal.Round (value, 4);
61                         notNull = true;
62                 }
63
64                 public SqlMoney (double value) : this ((decimal)value)
65                 {                       
66                 }
67
68                 public SqlMoney (int value) : this ((decimal)value)
69                 {
70                 }
71
72                 public SqlMoney (long value) : this ((decimal)value)
73                 {
74                 }
75
76                 #endregion
77
78                 #region Properties
79
80                 public bool IsNull { 
81                         get { return !notNull; }
82                 }
83
84                 public decimal Value { 
85                         get { 
86                                 if (this.IsNull) 
87                                         throw new SqlNullValueException ();
88                                 else 
89                                         return value; 
90                         }
91                 }
92
93                 #endregion
94
95                 #region Methods
96
97                 public static SqlMoney Add (SqlMoney x, SqlMoney y)
98                 {
99                         return (x + y);
100                 }
101
102                 public int CompareTo (object value)
103                 {
104                         if (value == null)
105                                 return 1;
106                         else if (!(value is SqlMoney))
107                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
108                         return CompareSqlMoney ((SqlMoney)value);
109                 }
110                 
111                 private int CompareSqlMoney (SqlMoney value)
112                 {
113                         if (value.IsNull)
114                                 return 1;
115                         else
116                                 return this.value.CompareTo (value.Value);
117                 }
118
119                 #if NET_2_0
120                 public int CompareTo (SqlMoney value)
121                 {
122                         return CompareSqlMoney (value);
123                 }
124                 #endif
125
126                 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
127                 {
128                         return (x / y);
129                 }
130
131                 public override bool Equals (object value)
132                 {
133                         if (!(value is SqlMoney))
134                                 return false;
135                         if (this.IsNull && ((SqlMoney)value).IsNull)
136                                 return true;
137                         else if (((SqlMoney)value).IsNull)
138                                 return false;
139                         else
140                                 return (bool) (this == (SqlMoney)value);
141                 }
142
143                 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
144                 {
145                         return (x == y);
146                 }
147
148                 public override int GetHashCode ()
149                 {
150                         return (int)value;
151                 }
152
153                 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
154                 {
155                         return (x > y);
156                 }
157
158                 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
159                 {
160                         return (x >= y);
161                 }
162
163                 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
164                 {
165                         return (x < y);
166                 }
167
168                 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
169                 {
170                         return (x <= y);
171                 }
172
173                 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
174                 {
175                         return (x * y);
176                 }
177
178                 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
179                 {
180                         return (x != y);
181                 }
182
183                 public static SqlMoney Parse (string s)
184                 {
185                         decimal d = Decimal.Parse (s);
186
187                         if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value) 
188                                 throw new OverflowException ("");
189                         
190                         return new SqlMoney (d);
191                 }
192
193                 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
194                 {
195                         return (x - y);
196                 }
197
198                 public decimal ToDecimal ()
199                 {
200                         return value;
201                 }
202
203                 public double ToDouble ()
204                 {
205                         return (double)value;
206                 }
207
208                 public int ToInt32 ()
209                 {
210                         return (int) Math.Round (value);
211                 }
212
213                 public long ToInt64 ()
214                 {
215                         return (long) Math.Round (value);
216                 }
217
218                 public SqlBoolean ToSqlBoolean ()
219                 {
220                         return ((SqlBoolean)this);
221                 }
222                 
223                 public SqlByte ToSqlByte ()
224                 {
225                         return ((SqlByte)this);
226                 }
227
228                 public SqlDecimal ToSqlDecimal ()
229                 {
230                         return ((SqlDecimal)this);
231                 }
232
233                 public SqlDouble ToSqlDouble ()
234                 {
235                         return ((SqlDouble)this);
236                 }
237
238                 public SqlInt16 ToSqlInt16 ()
239                 {
240                         return ((SqlInt16)this);
241                 }
242
243                 public SqlInt32 ToSqlInt32 ()
244                 {
245                         return ((SqlInt32)this);
246                 }
247
248                 public SqlInt64 ToSqlInt64 ()
249                 {
250                         return ((SqlInt64)this);
251                 }
252
253                 public SqlSingle ToSqlSingle ()
254                 {
255                         return ((SqlSingle)this);
256                 }
257
258                 public SqlString ToSqlString ()
259                 {
260                         return ((SqlString)this);
261                 }
262
263                 public override string ToString ()
264                 {
265                         if (!notNull)
266                                 return "Null";
267                         else
268                                 return value.ToString ();
269                 }
270
271                 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
272                 {
273                         checked {
274                                 return new SqlMoney (x.Value + y.Value);
275                         }
276                 }
277
278                 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
279                 {
280                         checked {
281                                 // FIXME: It's kinda mystery. should not be required.
282                                 decimal d = x.Value / y.Value;
283                                 return new SqlMoney (x.Value / y.Value);
284                         }
285                 }
286
287                 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
288                 {
289                         if (x.IsNull || y.IsNull) 
290                                 return SqlBoolean.Null;
291                         else
292                                 return new SqlBoolean (x.Value == y.Value);
293                 }
294
295                 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
296                 {
297                         if (x.IsNull || y.IsNull) 
298                                 return SqlBoolean.Null;
299                         else
300                                 return new SqlBoolean (x.Value > y.Value);
301                 }
302
303                 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
304                 {
305                         if (x.IsNull || y.IsNull) 
306                                 return SqlBoolean.Null;
307                         else
308                                 return new SqlBoolean (x.Value >= y.Value);
309                 }
310
311                 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
312                 {
313                         if (x.IsNull || y.IsNull) 
314                                 return SqlBoolean.Null;
315                         else
316                                 return new SqlBoolean (!(x.Value == y.Value));
317                 }
318
319                 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
320                 {
321                         if (x.IsNull || y.IsNull) 
322                                 return SqlBoolean.Null;
323                         else
324                                 return new SqlBoolean (x.Value < y.Value);
325                 }
326
327                 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
328                 {
329                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
330                         return new SqlBoolean (x.Value <= y.Value);
331                 }
332
333                 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
334                 {
335                         checked {
336                                 return new SqlMoney (x.Value * y.Value);
337                         }
338                 }
339
340                 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
341                 {
342                         checked {
343                                 return new SqlMoney (x.Value - y.Value);
344                         }
345                 }
346
347                 public static SqlMoney operator - (SqlMoney n)
348                 {
349                         return new SqlMoney (-(n.Value));
350                 }
351
352                 public static explicit operator SqlMoney (SqlBoolean x)
353                 {                       
354                         if (x.IsNull) 
355                                 return Null;
356                         else {
357                                 checked {
358                                         return new SqlMoney ((decimal)x.ByteValue);
359                                 }
360                         }
361                 }
362
363                 public static explicit operator SqlMoney (SqlDecimal x)
364                 {
365                         if (x.IsNull) 
366                                 return Null;
367                         else {
368                                 checked {
369                                         return new SqlMoney (x.Value);
370                                 }
371                         }
372                 }
373
374                 public static explicit operator SqlMoney (SqlDouble x)
375                 {
376                         if (x.IsNull) 
377                                 return Null;
378                         else {
379                                 checked {
380                                         return new SqlMoney ((decimal)x.Value);
381                                 }
382                         }
383                 }
384
385                 public static explicit operator decimal (SqlMoney x)
386                 {
387                         return x.Value;
388                 }
389
390                 public static explicit operator SqlMoney (SqlSingle x)
391                 {
392                         if (x.IsNull) 
393                                 return Null;
394                         else {
395                                 checked {
396                                         return new SqlMoney ((decimal)x.Value);
397                                 }
398                         }
399                 }
400
401                 public static explicit operator SqlMoney (SqlString x)
402                 {
403                         checked {
404                                 return SqlMoney.Parse (x.Value);
405                         }
406                 }
407
408                 public static implicit operator SqlMoney (decimal x)
409                 {
410                         return new SqlMoney (x);
411                 }
412
413                 public static implicit operator SqlMoney (SqlByte x)
414                 {
415                         if (x.IsNull) 
416                                 return Null;
417                         else
418                                 return new SqlMoney ((decimal)x.Value);
419                 }
420
421                 public static implicit operator SqlMoney (SqlInt16 x)
422                 {
423                         if (x.IsNull) 
424                                 return Null;
425                         else
426                                 return new SqlMoney ((decimal)x.Value);
427                 }
428
429                 public static implicit operator SqlMoney (SqlInt32 x)
430                 {
431                         if (x.IsNull) 
432                                 return Null;
433                         else
434                                 return new SqlMoney (x.Value);
435                 }
436
437                 public static implicit operator SqlMoney (SqlInt64 x)
438                 {
439                         if (x.IsNull) 
440                                 return Null;
441                         else
442                                 return new SqlMoney (x.Value);
443                 }
444
445                 #endregion
446         }
447 }
448