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