2002-05-07 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlDecimal.cs
1 //
2 // System.Data.SqlTypes.SqlDecimal
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //
7 // (C) Copyright 2002 Tim Coleman
8 //
9
10 using System;
11
12 namespace System.Data.SqlTypes
13 {
14         public struct SqlDecimal : INullable, IComparable
15         {
16                 #region Fields
17
18                 private decimal value;
19
20                 public static readonly byte MaxPrecision = 38; 
21                 public static readonly byte MaxScale = 28;
22                 public static readonly SqlDecimal MaxValue = new SqlDecimal (79228162514264337593543950335.0);
23                 public static readonly SqlDecimal MinValue = new SqlDecimal (-79228162514264337593543950335.0);
24                 public static readonly SqlDecimal Null;
25
26                 #endregion
27
28                 #region Constructors
29
30                 public SqlDecimal (decimal value) 
31                 {
32                         this.value = value;
33                 }
34
35                 public SqlDecimal (double value) 
36                 {
37                         this.value = ((decimal)value);
38                 }
39
40                 public SqlDecimal (int value) 
41                 {
42                         this.value = ((decimal)value);
43                 }
44
45                 public SqlDecimal (long value) 
46                 {
47                         this.value = ((decimal)value);
48                 }
49
50                 [MonoTODO]
51                 public SqlDecimal (byte bPrecision, byte bScale, bool fPositive, int[] bits)
52                 {
53                         throw new NotImplementedException ();
54                 }
55
56                 [MonoTODO]
57                 public SqlDecimal (byte bPrecision, byte bScale, bool fPositive, int data1, int data2, int data3, int data4) 
58                 {
59                         throw new NotImplementedException ();
60                 }
61
62                 #endregion
63
64                 #region Properties
65
66                 [MonoTODO]
67                 public byte[] BinData {
68                         get { throw new NotImplementedException (); }
69                 }
70
71                 public int[] Data { 
72                         get { 
73                                 if (this.IsNull)
74                                         throw new SqlNullValueException ();
75                                 else
76                                         return Decimal.GetBits (value);
77                         }
78                 }
79
80                 public bool IsNull { 
81                         get { return (bool) (this == Null); }
82                 }
83
84                 public bool IsPositive { 
85                         get { return (this.Value > 0); }
86                 }
87
88                 [MonoTODO]
89                 public byte Precision { 
90                         get { throw new NotImplementedException (); }
91                 }
92
93                 [MonoTODO]
94                 public byte Scale { 
95                         get { throw new NotImplementedException (); }
96                 }
97
98                 public decimal Value { 
99                         get { 
100                                 if (this.IsNull) 
101                                         throw new SqlNullValueException ();
102                                 else 
103                                         return value; 
104                         }
105                 }
106
107                 #endregion
108
109                 #region Methods
110
111                 [MonoTODO]
112                 public static SqlDecimal Abs (SqlDecimal n)
113                 {
114                         throw new NotImplementedException();
115                 }
116
117                 public static SqlDecimal Add (SqlDecimal x, SqlDecimal y)
118                 {
119                         return (x + y);
120                 }
121
122                 [MonoTODO]
123                 public static SqlDecimal AdjustScale (SqlDecimal n, int digits, bool fRound)
124                 {
125                         throw new NotImplementedException ();
126                 }
127
128                 [MonoTODO]
129                 public static SqlDecimal Ceiling (SqlDecimal n)
130                 {
131                         throw new NotImplementedException();
132                 }
133
134                 public int CompareTo (object value)
135                 {
136                         if (value == null)
137                                 return 1;
138                         else if (!(value is SqlDecimal))
139                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlDecimal"));
140                         else if (value.IsNull)
141                                 return 1;
142                         else
143                                 return value.CompareTo (value.Value);
144                 }
145
146                 [MonoTODO]
147                 public static SqlDecimal ConvertToPrecScale (SqlDecimal n, int precision, int scale)
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 public static SqlDecimal Divide (SqlDecimal x, SqlDecimal y)
153                 {
154                         return (x / y);
155                 }
156
157                 public override bool Equals (object value)
158                 {
159                         if (!(value is SqlDecimal))
160                                 return false;
161                         else
162                                 return (bool) (this == value);
163                 }
164
165                 public static SqlBoolean Equals (SqlDecimal x, SqlDecimal y)
166                 {
167                         return (x == y);
168                 }
169
170                 [MonoTODO]
171                 public static SqlDecimal Floor (SqlDecimal n)
172                 {
173                         throw new NotImplementedException ();
174                 }
175
176                 [MonoTODO]
177                 public override int GetHashCode ()
178                 {
179                         return (int)value;
180                 }
181
182                 public static SqlBoolean GreaterThan (SqlDecimal x, SqlDecimal y)
183                 {
184                         return (x > y);
185                 }
186
187                 public static SqlBoolean GreaterThanOrEqual (SqlDecimal x, SqlDecimal y)
188                 {
189                         return (x >= y);
190                 }
191
192                 public static SqlBoolean LessThan (SqlDecimal x, SqlDecimal y)
193                 {
194                         return (x < y);
195                 }
196
197                 public static SqlBoolean LessThanOrEqual (SqlDecimal x, SqlDecimal y)
198                 {
199                         return (x <= y);
200                 }
201
202                 public static SqlDecimal Multiply (SqlDecimal x, SqlDecimal y)
203                 {
204                         return (x * y);
205                 }
206
207                 public static SqlBoolean NotEquals (SqlDecimal x, SqlDecimal y)
208                 {
209                         return (x != y);
210                 }
211
212                 [MonoTODO]
213                 public static SqlDecimal Parse (string s)
214                 {
215                         throw new NotImplementedException ();
216                 }
217
218                 [MonoTODO]
219                 public static SqlDecimal Power (SqlDecimal n, double exp)
220                 {
221                         throw new NotImplementedException ();
222                 }
223
224                 [MonoTODO]
225                 public static SqlDecimal Round (SqlDecimal n, int position)
226                 {
227                         throw new NotImplementedException ();
228                 }
229
230                 [MonoTODO]
231                 public static SqlInt32 Sign (SqlDecimal n)
232                 {
233                         throw new NotImplementedException ();
234                 }
235
236                 public static SqlDecimal Subtract (SqlDecimal x, SqlDecimal y)
237                 {
238                         return (x - y);
239                 }
240
241                 public double ToDouble ()
242                 {
243                         return ((double)value);
244                 }
245
246                 public SqlBoolean ToSqlBoolean ()
247                 {
248                         return ((SqlBoolean)this);
249                 }
250                 
251                 public SqlByte ToSqlByte ()
252                 {
253                         return ((SqlByte)this);
254                 }
255
256                 public SqlDouble ToSqlDouble ()
257                 {
258                         return ((SqlDouble)this);
259                 }
260
261                 public SqlInt16 ToSqlInt16 ()
262                 {
263                         return ((SqlInt16)this);
264                 }
265
266                 public SqlInt32 ToSqlInt32 ()
267                 {
268                         return ((SqlInt32)this);
269                 }
270
271                 public SqlInt64 ToSqlInt64 ()
272                 {
273                         return ((SqlInt64)this);
274                 }
275
276                 public SqlMoney ToSqlMoney ()
277                 {
278                         return ((SqlMoney)this);
279                 }
280
281                 public SqlSingle ToSqlSingle ()
282                 {
283                         return ((SqlSingle)this);
284                 }
285
286                 public SqlString ToSqlString ()
287                 {
288                         return ((SqlString)this);
289                 }
290
291                 public override string ToString ()
292                 {
293                         if (this.IsNull)
294                                 return String.Empty;
295                         else
296                                 return value.ToString ();
297                 }
298
299                 [MonoTODO]
300                 public static SqlDecimal Truncate (SqlDecimal n, int position)
301                 {
302                         throw new NotImplementedException ();
303                 }
304
305                 public static SqlDecimal operator + (SqlDecimal x, SqlDecimal y)
306                 {
307                         return new SqlDecimal (x.Value + y.Value);
308                 }
309
310                 public static SqlDecimal operator / (SqlDecimal x, SqlDecimal y)
311                 {
312                         return new SqlDecimal (x.Value / y.Value);
313                 }
314
315                 public static SqlBoolean operator == (SqlDecimal x, SqlDecimal y)
316                 {
317                         if (x.IsNull || y.IsNull) 
318                                 return SqlBoolean.Null;
319                         else
320                                 return new SqlBoolean (x.Value == y.Value);
321                 }
322
323                 public static SqlBoolean operator > (SqlDecimal x, SqlDecimal y)
324                 {
325                         if (x.IsNull || y.IsNull) 
326                                 return SqlBoolean.Null;
327                         else
328                                 return new SqlBoolean (x.Value > y.Value);
329                 }
330
331                 public static SqlBoolean operator >= (SqlDecimal x, SqlDecimal y)
332                 {
333                         if (x.IsNull || y.IsNull) 
334                                 return SqlBoolean.Null;
335                         else
336                                 return new SqlBoolean (x.Value >= y.Value);
337                 }
338
339                 public static SqlBoolean operator != (SqlDecimal x, SqlDecimal y)
340                 {
341                         if (x.IsNull || y.IsNull) 
342                                 return SqlBoolean.Null;
343                         else
344                                 return new SqlBoolean (!(x.Value == y.Value));
345                 }
346
347                 public static SqlBoolean operator < (SqlDecimal x, SqlDecimal y)
348                 {
349                         if (x.IsNull || y.IsNull) 
350                                 return SqlBoolean.Null;
351                         else
352                                 return new SqlBoolean (x.Value < y.Value);
353                 }
354
355                 public static SqlBoolean operator <= (SqlDecimal x, SqlDecimal y)
356                 {
357                         if (x.IsNull || y.IsNull) 
358                                 return SqlBoolean.Null;
359                         else
360                                 return new SqlBoolean (x.Value <= y.Value);
361                 }
362
363                 public static SqlDecimal operator * (SqlDecimal x, SqlDecimal y)
364                 {
365                         return new SqlDecimal (x.Value * y.Value);
366                 }
367
368                 public static SqlDecimal operator - (SqlDecimal x, SqlDecimal y)
369                 {
370                         return new SqlDecimal (x.Value - y.Value);
371                 }
372
373                 public static SqlDecimal operator - (SqlDecimal n)
374                 {
375                         return new SqlDecimal (-(n.Value));
376                 }
377
378                 public static explicit operator SqlDecimal (SqlBoolean x)
379                 {
380                         if (x.IsNull) 
381                                 return Null;
382                         else
383                                 return new SqlDecimal ((decimal)x.ByteValue);
384                 }
385
386                 public static explicit operator Decimal (SqlDecimal n)
387                 {
388                         return n.Value;
389                 }
390
391                 public static explicit operator SqlDecimal (SqlDouble x)
392                 {
393                         if (x.IsNull) 
394                                 return Null;
395                         else
396                                 return new SqlDecimal ((decimal)x.Value);
397                 }
398
399                 public static explicit operator SqlDecimal (SqlSingle x)
400                 {
401                         if (x.IsNull) 
402                                 return Null;
403                         else
404                                 return new SqlDecimal ((decimal)x.Value);
405                 }
406
407                 [MonoTODO]
408                 public static explicit operator SqlDecimal (SqlString x)
409                 {
410                         throw new NotImplementedException ();
411                 }
412
413                 public static implicit operator SqlDecimal (decimal x)
414                 {
415                         return new SqlDecimal (x);
416                 }
417
418                 public static implicit operator SqlDecimal (SqlByte x)
419                 {
420                         if (x.IsNull) 
421                                 return Null;
422                         else
423                                 return new SqlDecimal ((decimal)x.Value);
424                 }
425
426                 public static implicit operator SqlDecimal (SqlInt16 x)
427                 {
428                         if (x.IsNull) 
429                                 return Null;
430                         else
431                                 return new SqlDecimal ((decimal)x.Value);
432                 }
433
434                 public static implicit operator SqlDecimal (SqlInt32 x)
435                 {
436                         if (x.IsNull) 
437                                 return Null;
438                         else
439                                 return new SqlDecimal ((decimal)x.Value);
440                 }
441
442                 public static implicit operator SqlDecimal (SqlInt64 x)
443                 {
444                         if (x.IsNull) 
445                                 return Null;
446                         else
447                                 return new SqlDecimal ((decimal)x.Value);
448                 }
449
450                 public static implicit operator SqlDecimal (SqlMoney x)
451                 {
452                         if (x.IsNull) 
453                                 return Null;
454                         else
455                                 return new SqlDecimal ((decimal)x.Value);
456                 }
457
458                 #endregion
459         }
460 }
461