2010-03-26 Veerapuram Varadhan <vvaradhan@novell.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 //   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.Xml;
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
39
40 namespace System.Data.SqlTypes
41 {
42 #if NET_2_0
43         [SerializableAttribute]
44         [XmlSchemaProvider ("GetXsdType")]
45 #endif
46         public struct SqlMoney : INullable, IComparable
47 #if NET_2_0
48                                 , IXmlSerializable
49 #endif
50         {
51                 #region Fields
52
53                 private decimal value;
54                 private bool notNull;
55                 
56                 public static readonly SqlMoney MaxValue = new SqlMoney (922337203685477.5807m);
57                 public static readonly SqlMoney MinValue = new SqlMoney (-922337203685477.5808m);
58                 public static readonly SqlMoney Null;
59                 public static readonly SqlMoney Zero = new SqlMoney (0);
60
61                 private static readonly NumberFormatInfo MoneyFormat;
62
63                 #endregion
64
65                 #region Constructors
66
67                 static SqlMoney ()
68                 {
69                         MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
70                         MoneyFormat.NumberDecimalDigits = 4;
71                         MoneyFormat.NumberGroupSeparator = String.Empty;
72                 }
73
74                 public SqlMoney (decimal value)
75                 {
76                         if (value > 922337203685477.5807m || value < -922337203685477.5808m)
77                                 throw new OverflowException ();
78                         this.value = Decimal.Round (value, 4);
79                         notNull = true;
80                 }
81
82                 public SqlMoney (double value) : this ((decimal) value)
83                 {                       
84                 }
85
86                 public SqlMoney (int value) : this ((decimal) value)
87                 {
88                 }
89
90                 public SqlMoney (long value) : this ((decimal) value)
91                 {
92                 }
93
94                 #endregion
95
96                 #region Properties
97
98                 public bool IsNull {
99                         get { return !notNull; }
100                 }
101
102                 public decimal Value {
103                         get {
104                                 if (this.IsNull)
105                                         throw new SqlNullValueException ();
106                                 else 
107                                         return value; 
108                         }
109                 }
110
111                 #endregion
112
113                 #region Methods
114
115                 public static SqlMoney Add (SqlMoney x, SqlMoney y)
116                 {
117                         return (x + y);
118                 }
119
120                 public int CompareTo (object value)
121                 {
122                         if (value == null)
123                                 return 1;
124                         else if (!(value is SqlMoney))
125                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
126                         return CompareSqlMoney ((SqlMoney)value);
127                 }
128                 
129                 private int CompareSqlMoney (SqlMoney value)
130                 {
131                         if (value.IsNull)
132                                 return 1;
133                         else
134                                 return this.value.CompareTo (value.Value);
135                 }
136
137 #if NET_2_0
138                 public int CompareTo (SqlMoney value)
139                 {
140                         return CompareSqlMoney (value);
141                 }
142 #endif
143
144                 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
145                 {
146                         return (x / y);
147                 }
148
149                 public override bool Equals (object value)
150                 {
151                         if (!(value is SqlMoney))
152                                 return false;
153                         if (this.IsNull)
154                                 return ((SqlMoney)value).IsNull;
155                         else if (((SqlMoney)value).IsNull)
156                                 return false;
157                         else
158                                 return (bool) (this == (SqlMoney)value);
159                 }
160
161                 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
162                 {
163                         return (x == y);
164                 }
165
166                 public override int GetHashCode ()
167                 {
168                         return (int) value;
169                 }
170
171                 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
172                 {
173                         return (x > y);
174                 }
175
176                 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
177                 {
178                         return (x >= y);
179                 }
180
181                 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
182                 {
183                         return (x < y);
184                 }
185
186                 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
187                 {
188                         return (x <= y);
189                 }
190
191                 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
192                 {
193                         return (x * y);
194                 }
195
196                 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
197                 {
198                         return (x != y);
199                 }
200
201                 public static SqlMoney Parse (string s)
202                 {
203                         decimal d = Decimal.Parse (s);
204
205                         if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value) 
206                                 throw new OverflowException ();
207                         
208                         return new SqlMoney (d);
209                 }
210
211                 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
212                 {
213                         return (x - y);
214                 }
215
216                 public decimal ToDecimal ()
217                 {
218                         return value;
219                 }
220
221                 public double ToDouble ()
222                 {
223                         return (double) value;
224                 }
225
226                 public int ToInt32 ()
227                 {
228                         return (int) Math.Round (value);
229                 }
230
231                 public long ToInt64 ()
232                 {
233                         return (long) Math.Round (value);
234                 }
235
236                 public SqlBoolean ToSqlBoolean ()
237                 {
238                         return ((SqlBoolean) this);
239                 }
240                 
241                 public SqlByte ToSqlByte ()
242                 {
243                         return ((SqlByte) this);
244                 }
245
246                 public SqlDecimal ToSqlDecimal ()
247                 {
248                         return ((SqlDecimal) this);
249                 }
250
251                 public SqlDouble ToSqlDouble ()
252                 {
253                         return ((SqlDouble) this);
254                 }
255
256                 public SqlInt16 ToSqlInt16 ()
257                 {
258                         return ((SqlInt16) this);
259                 }
260
261                 public SqlInt32 ToSqlInt32 ()
262                 {
263                         return ((SqlInt32) this);
264                 }
265
266                 public SqlInt64 ToSqlInt64 ()
267                 {
268                         return ((SqlInt64) this);
269                 }
270
271                 public SqlSingle ToSqlSingle ()
272                 {
273                         return ((SqlSingle) this);
274                 }
275
276                 public SqlString ToSqlString ()
277                 {
278                         return ((SqlString) this);
279                 }
280
281                 public override string ToString ()
282                 {
283                         if (!notNull)
284                                 return "Null";
285                         else
286                                 return value.ToString ("N", MoneyFormat);
287                 }
288
289                 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
290                 {
291                         checked {
292                                 return new SqlMoney (x.Value + y.Value);
293                         }
294                 }
295
296                 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
297                 {
298                         checked {
299                                 return new SqlMoney (x.Value / y.Value);
300                         }
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)
330                                 return SqlBoolean.Null;
331                         else
332                                 return new SqlBoolean (!(x.Value == y.Value));
333                 }
334
335                 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
336                 {
337                         if (x.IsNull || y.IsNull)
338                                 return SqlBoolean.Null;
339                         else
340                                 return new SqlBoolean (x.Value < y.Value);
341                 }
342
343                 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
344                 {
345                         if (x.IsNull || y.IsNull)
346                                 return SqlBoolean.Null;
347                         return new SqlBoolean (x.Value <= y.Value);
348                 }
349
350                 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
351                 {
352                         checked {
353                                 return new SqlMoney (x.Value * y.Value);
354                         }
355                 }
356
357                 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
358                 {
359                         checked {
360                                 return new SqlMoney (x.Value - y.Value);
361                         }
362                 }
363
364                 public static SqlMoney operator - (SqlMoney x)
365                 {
366                         return new SqlMoney (-(x.Value));
367                 }
368
369                 public static explicit operator SqlMoney (SqlBoolean x)
370                 {
371                         if (x.IsNull)
372                                 return Null;
373                         else {
374                                 checked {
375                                         return new SqlMoney ((decimal) x.ByteValue);
376                                 }
377                         }
378                 }
379
380                 public static explicit operator SqlMoney (SqlDecimal x)
381                 {
382                         if (x.IsNull)
383                                 return Null;
384                         else {
385                                 checked {
386                                         return new SqlMoney (x.Value);
387                                 }
388                         }
389                 }
390
391                 public static explicit operator SqlMoney (SqlDouble x)
392                 {
393                         if (x.IsNull)
394                                 return Null;
395                         else {
396                                 checked {
397                                         return new SqlMoney ((decimal) x.Value);
398                                 }
399                         }
400                 }
401
402                 public static explicit operator decimal (SqlMoney x)
403                 {
404                         return x.Value;
405                 }
406
407                 public static explicit operator SqlMoney (SqlSingle x)
408                 {
409                         if (x.IsNull)
410                                 return Null;
411                         else {
412                                 checked {
413                                         return new SqlMoney ((decimal) x.Value);
414                                 }
415                         }
416                 }
417
418                 public static explicit operator SqlMoney (SqlString x)
419                 {
420                         checked {
421                                 return SqlMoney.Parse (x.Value);
422                         }
423                 }
424
425 #if NET_2_0
426                 public static explicit operator SqlMoney (double x)
427                 {
428                         return new SqlMoney (x);
429                 }
430
431                 public static implicit operator SqlMoney (long x)
432                 {
433                         return new SqlMoney (x);
434                 }
435 #endif
436
437                 public static implicit operator SqlMoney (decimal x)
438                 {
439                         return new SqlMoney (x);
440                 }
441
442                 public static implicit operator SqlMoney (SqlByte x)
443                 {
444                         if (x.IsNull)
445                                 return Null;
446                         else
447                                 return new SqlMoney ((decimal) x.Value);
448                 }
449
450                 public static implicit operator SqlMoney (SqlInt16 x)
451                 {
452                         if (x.IsNull)
453                                 return Null;
454                         else
455                                 return new SqlMoney ((decimal) x.Value);
456                 }
457
458                 public static implicit operator SqlMoney (SqlInt32 x)
459                 {
460                         if (x.IsNull)
461                                 return Null;
462                         else
463                                 return new SqlMoney (x.Value);
464                 }
465
466                 public static implicit operator SqlMoney (SqlInt64 x)
467                 {
468                         if (x.IsNull)
469                                 return Null;
470                         else
471                                 return new SqlMoney (x.Value);
472                 }
473
474 #if NET_2_0
475                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
476                 {
477                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("decimal", "http://www.w3.org/2001/XMLSchema");
478                         return qualifiedName;
479                 }
480
481                 [MonoTODO]
482                 XmlSchema IXmlSerializable.GetSchema ()
483                 {
484                         throw new NotImplementedException ();
485                 }
486                 
487                 [MonoTODO]
488                 void IXmlSerializable.ReadXml (XmlReader reader)
489                 {
490                         throw new NotImplementedException ();
491                 }
492                 
493                 [MonoTODO]
494                 void IXmlSerializable.WriteXml (XmlWriter writer) 
495                 {
496                         throw new NotImplementedException ();
497                 }
498 #endif
499
500                 #endregion
501         }
502 }