* support-test-*.cs: Rename from test-*-p2.cs.
[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                 private static readonly NumberFormatInfo MoneyFormat;
53
54                 #endregion
55
56                 #region Constructors
57
58                 static SqlMoney ()
59                 {
60                         MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
61                         MoneyFormat.NumberDecimalDigits = 4;
62                         MoneyFormat.NumberGroupSeparator = String.Empty;
63                 }
64
65                 public SqlMoney (decimal value) 
66                 {
67                         if (value > 922337203685477.5807m || value < -922337203685477.5808m)
68                                 throw new OverflowException ();
69                         this.value = Decimal.Round (value, 4);
70                         notNull = true;
71                 }
72
73                 public SqlMoney (double value) : this ((decimal)value)
74                 {                       
75                 }
76
77                 public SqlMoney (int value) : this ((decimal)value)
78                 {
79                 }
80
81                 public SqlMoney (long value) : this ((decimal)value)
82                 {
83                 }
84
85                 #endregion
86
87                 #region Properties
88
89                 public bool IsNull { 
90                         get { return !notNull; }
91                 }
92
93                 public decimal Value { 
94                         get { 
95                                 if (this.IsNull) 
96                                         throw new SqlNullValueException ();
97                                 else 
98                                         return value; 
99                         }
100                 }
101
102                 #endregion
103
104                 #region Methods
105
106                 public static SqlMoney Add (SqlMoney x, SqlMoney y)
107                 {
108                         return (x + y);
109                 }
110
111                 public int CompareTo (object value)
112                 {
113                         if (value == null)
114                                 return 1;
115                         else if (!(value is SqlMoney))
116                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
117                         return CompareSqlMoney ((SqlMoney)value);
118                 }
119                 
120                 private int CompareSqlMoney (SqlMoney value)
121                 {
122                         if (value.IsNull)
123                                 return 1;
124                         else
125                                 return this.value.CompareTo (value.Value);
126                 }
127
128                 #if NET_2_0
129                 public int CompareTo (SqlMoney value)
130                 {
131                         return CompareSqlMoney (value);
132                 }
133                 #endif
134
135                 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
136                 {
137                         return (x / y);
138                 }
139
140                 public override bool Equals (object value)
141                 {
142                         if (!(value is SqlMoney))
143                                 return false;
144                         if (this.IsNull && ((SqlMoney)value).IsNull)
145                                 return true;
146                         else if (((SqlMoney)value).IsNull)
147                                 return false;
148                         else
149                                 return (bool) (this == (SqlMoney)value);
150                 }
151
152                 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
153                 {
154                         return (x == y);
155                 }
156
157                 public override int GetHashCode ()
158                 {
159                         return (int)value;
160                 }
161
162                 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
163                 {
164                         return (x > y);
165                 }
166
167                 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
168                 {
169                         return (x >= y);
170                 }
171
172                 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
173                 {
174                         return (x < y);
175                 }
176
177                 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
178                 {
179                         return (x <= y);
180                 }
181
182                 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
183                 {
184                         return (x * y);
185                 }
186
187                 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
188                 {
189                         return (x != y);
190                 }
191
192                 public static SqlMoney Parse (string s)
193                 {
194                         decimal d = Decimal.Parse (s);
195
196                         if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value) 
197                                 throw new OverflowException ("");
198                         
199                         return new SqlMoney (d);
200                 }
201
202                 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
203                 {
204                         return (x - y);
205                 }
206
207                 public decimal ToDecimal ()
208                 {
209                         return value;
210                 }
211
212                 public double ToDouble ()
213                 {
214                         return (double)value;
215                 }
216
217                 public int ToInt32 ()
218                 {
219                         return (int) Math.Round (value);
220                 }
221
222                 public long ToInt64 ()
223                 {
224                         return (long) Math.Round (value);
225                 }
226
227                 public SqlBoolean ToSqlBoolean ()
228                 {
229                         return ((SqlBoolean)this);
230                 }
231                 
232                 public SqlByte ToSqlByte ()
233                 {
234                         return ((SqlByte)this);
235                 }
236
237                 public SqlDecimal ToSqlDecimal ()
238                 {
239                         return ((SqlDecimal)this);
240                 }
241
242                 public SqlDouble ToSqlDouble ()
243                 {
244                         return ((SqlDouble)this);
245                 }
246
247                 public SqlInt16 ToSqlInt16 ()
248                 {
249                         return ((SqlInt16)this);
250                 }
251
252                 public SqlInt32 ToSqlInt32 ()
253                 {
254                         return ((SqlInt32)this);
255                 }
256
257                 public SqlInt64 ToSqlInt64 ()
258                 {
259                         return ((SqlInt64)this);
260                 }
261
262                 public SqlSingle ToSqlSingle ()
263                 {
264                         return ((SqlSingle)this);
265                 }
266
267                 public SqlString ToSqlString ()
268                 {
269                         return ((SqlString)this);
270                 }
271
272                 public override string ToString ()
273                 {
274                         if (!notNull)
275                                 return "Null";
276                         else
277                                 return value.ToString ("N", MoneyFormat);
278                 }
279
280                 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
281                 {
282                         checked {
283                                 return new SqlMoney (x.Value + y.Value);
284                         }
285                 }
286
287                 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
288                 {
289                         checked {
290                                 // FIXME: It's kinda mystery. should not be required.
291                                 decimal d = x.Value / y.Value;
292                                 return new SqlMoney (x.Value / y.Value);
293                         }
294                 }
295
296                 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
297                 {
298                         if (x.IsNull || y.IsNull) 
299                                 return SqlBoolean.Null;
300                         else
301                                 return new SqlBoolean (x.Value == y.Value);
302                 }
303
304                 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
305                 {
306                         if (x.IsNull || y.IsNull) 
307                                 return SqlBoolean.Null;
308                         else
309                                 return new SqlBoolean (x.Value > y.Value);
310                 }
311
312                 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
313                 {
314                         if (x.IsNull || y.IsNull) 
315                                 return SqlBoolean.Null;
316                         else
317                                 return new SqlBoolean (x.Value >= y.Value);
318                 }
319
320                 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
321                 {
322                         if (x.IsNull || y.IsNull) 
323                                 return SqlBoolean.Null;
324                         else
325                                 return new SqlBoolean (!(x.Value == y.Value));
326                 }
327
328                 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
329                 {
330                         if (x.IsNull || y.IsNull) 
331                                 return SqlBoolean.Null;
332                         else
333                                 return new SqlBoolean (x.Value < y.Value);
334                 }
335
336                 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
337                 {
338                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
339                         return new SqlBoolean (x.Value <= y.Value);
340                 }
341
342                 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
343                 {
344                         checked {
345                                 return new SqlMoney (x.Value * y.Value);
346                         }
347                 }
348
349                 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
350                 {
351                         checked {
352                                 return new SqlMoney (x.Value - y.Value);
353                         }
354                 }
355
356                 public static SqlMoney operator - (SqlMoney n)
357                 {
358                         return new SqlMoney (-(n.Value));
359                 }
360
361                 public static explicit operator SqlMoney (SqlBoolean x)
362                 {                       
363                         if (x.IsNull) 
364                                 return Null;
365                         else {
366                                 checked {
367                                         return new SqlMoney ((decimal)x.ByteValue);
368                                 }
369                         }
370                 }
371
372                 public static explicit operator SqlMoney (SqlDecimal x)
373                 {
374                         if (x.IsNull) 
375                                 return Null;
376                         else {
377                                 checked {
378                                         return new SqlMoney (x.Value);
379                                 }
380                         }
381                 }
382
383                 public static explicit operator SqlMoney (SqlDouble x)
384                 {
385                         if (x.IsNull) 
386                                 return Null;
387                         else {
388                                 checked {
389                                         return new SqlMoney ((decimal)x.Value);
390                                 }
391                         }
392                 }
393
394                 public static explicit operator decimal (SqlMoney x)
395                 {
396                         return x.Value;
397                 }
398
399                 public static explicit operator SqlMoney (SqlSingle x)
400                 {
401                         if (x.IsNull) 
402                                 return Null;
403                         else {
404                                 checked {
405                                         return new SqlMoney ((decimal)x.Value);
406                                 }
407                         }
408                 }
409
410                 public static explicit operator SqlMoney (SqlString x)
411                 {
412                         checked {
413                                 return SqlMoney.Parse (x.Value);
414                         }
415                 }
416
417                 public static implicit operator SqlMoney (decimal x)
418                 {
419                         return new SqlMoney (x);
420                 }
421
422                 public static implicit operator SqlMoney (SqlByte x)
423                 {
424                         if (x.IsNull) 
425                                 return Null;
426                         else
427                                 return new SqlMoney ((decimal)x.Value);
428                 }
429
430                 public static implicit operator SqlMoney (SqlInt16 x)
431                 {
432                         if (x.IsNull) 
433                                 return Null;
434                         else
435                                 return new SqlMoney ((decimal)x.Value);
436                 }
437
438                 public static implicit operator SqlMoney (SqlInt32 x)
439                 {
440                         if (x.IsNull) 
441                                 return Null;
442                         else
443                                 return new SqlMoney (x.Value);
444                 }
445
446                 public static implicit operator SqlMoney (SqlInt64 x)
447                 {
448                         if (x.IsNull) 
449                                 return Null;
450                         else
451                                 return new SqlMoney (x.Value);
452                 }
453
454                 #endregion
455         }
456 }
457