1 // System.Data.SqlTypes.SqlInt64
\r
4 // Konstantin Triger <kostat@mainsoft.com>
5 // Boris Kirzner <borisk@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Data.SqlTypes
\r
36 public struct SqlInt64 : INullable, IComparable
\r
39 private long _value;
\r
40 private bool _isNull;
\r
42 public static readonly SqlInt64 Null = new SqlInt64(true);
\r
43 public static readonly SqlInt64 MaxValue = new SqlInt64(int.MaxValue);
\r
44 public static readonly SqlInt64 MinValue = new SqlInt64(int.MinValue);
\r
45 public static readonly SqlInt64 Zero = new SqlInt64(0);
\r
48 private SqlInt64(bool isNull)
\r
55 * @param value A long whose value will be used for the new SqlInt64.
\r
57 public SqlInt64(long value)
\r
64 * Indicates whether or not Value is null.
\r
65 * @return true if Value is null, otherwise false.
\r
78 * Gets the value of the SqlInt64 instance.
\r
79 * @return the value of this instance
\r
86 throw new SqlNullValueException();
\r
92 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
93 * @param obj The object to compare.
\r
94 * @return A signed number indicating the relative values of the instance and the object.
\r
95 * Less than zero This instance is less than object.
\r
96 * Zero This instance is the same as object.
\r
97 * Greater than zero This instance is greater than object -or-
\r
98 * object is a null reference.
\r
100 public int CompareTo(Object obj)
\r
105 if (obj is SqlInt64)
\r
107 SqlInt64 i = (SqlInt64)obj;
\r
114 return this._value.CompareTo(i._value);
\r
117 throw new ArgumentException("parameter obj is not SqlInt64 : " + obj.GetType().Name);
\r
122 * The addition operator computes the sum of the two SqlInt64 operands.
\r
123 * @param x A SqlInt64 structure.
\r
124 * @param y A SqlInt64 structure.
\r
125 * @return The sum of the two SqlInt64 operands.
\r
126 * If one of the parameters is null or null value - return SqlInt64.Null.
\r
128 public static SqlInt64 Add(SqlInt64 x, SqlInt64 y)
\r
130 if (x.IsNull || y.IsNull)
\r
131 return SqlInt64.Null;
\r
134 long sum = checked(x._value + y._value);
\r
136 return new SqlInt64(sum);
\r
140 * Computes the bitwise AND of its SqlInt64 operands.
\r
141 * @param x A SqlInt64 instance.
\r
142 * @param y A SqlInt64 instance.
\r
143 * @return The results of the bitwise AND operation.
\r
145 public static SqlInt64 BitwiseAnd(SqlInt64 x, SqlInt64 y)
\r
147 if (x.IsNull || y.IsNull)
\r
148 return SqlInt64.Null;
\r
150 long res = x._value & y._value;
\r
152 return new SqlInt64(res);
\r
156 * Computes the bitwise OR of its SqlInt64 operands.
\r
157 * @param x A SqlInt64 instance.
\r
158 * @param y A SqlInt64 instance.
\r
159 * @return The results of the bitwise OR operation.
\r
161 public static SqlInt64 BitwiseOr(SqlInt64 x, SqlInt64 y)
\r
163 if (x.IsNull || y.IsNull)
\r
164 return SqlInt64.Null;
\r
166 long res = x._value | y._value;
\r
168 return new SqlInt64(res);
\r
172 * The division operator divides the first SqlInt64 operand by the second.
\r
173 * @param x A SqlInt64 instance.
\r
174 * @param y A SqlInt64 instance.
\r
175 * @return A SqlInt64 instance containing the results of the division operation.
\r
176 * If one of the parameters is null or null value - return SqlInt64.Null.
\r
178 public static SqlInt64 Divide(SqlInt64 x, SqlInt64 y)
\r
180 long val = x._value / y._value;
\r
181 return new SqlInt64(val);
\r
184 public override int GetHashCode()
\r
186 return (int) (_value ^ (_value >> 32));
\r
189 public override bool Equals(Object obj)
\r
194 if (obj is SqlInt64)
\r
196 SqlInt64 i = (SqlInt64)obj;
\r
198 if (IsNull && i.IsNull)
\r
201 if (IsNull || i.IsNull)
\r
204 return _value == i._value;
\r
213 * Performs a logical comparison on two instances of SqlInt64 to determine if they are equal.
\r
214 * @param x A SqlInt64 instance.
\r
215 * @param y A SqlInt64 instance.
\r
216 * @return true if the two values are equal, otherwise false.
\r
217 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
219 public static SqlBoolean Equals(SqlInt64 x, SqlInt64 y)
\r
221 if (x.IsNull || y.IsNull)
\r
222 return SqlBoolean.Null;
\r
224 if (x._value == y._value)
\r
225 return SqlBoolean.True;
\r
227 return SqlBoolean.False;
\r
232 * Compares two instances of SqlByte to determine if the first is greater than the second.
\r
233 * @param x A SqlByte instance
\r
234 * @param y A SqlByte instance
\r
235 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
236 * If either instance of SqlInt64 is null, the Value of the SqlBoolean will be Null.
\r
238 public static SqlBoolean GreaterThan(SqlInt64 x, SqlInt64 y)
\r
240 if (x.IsNull || y.IsNull)
\r
241 return SqlBoolean.Null;
\r
243 if (x._value > y._value)
\r
244 return SqlBoolean.True;
\r
246 return SqlBoolean.False;
\r
250 * Compares two instances of SqlInt64 to determine if the first is greater than or equal to the second.
\r
251 * @param x A SqlInt64 instance
\r
252 * @param y A SqlInt64 instance
\r
253 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
254 * If either instance of SqlInt64 is null, the Value of the SqlBoolean will be Null.
\r
256 public static SqlBoolean GreaterThanOrEqual(SqlInt64 x, SqlInt64 y)
\r
258 if (x.IsNull || y.IsNull)
\r
259 return SqlBoolean.Null;
\r
261 if (x._value >= y._value)
\r
262 return SqlBoolean.True;
\r
264 return SqlBoolean.False;
\r
268 * Compares two instances of SqlInt64 to determine if the first is less than the second.
\r
269 * @param x A SqlInt64 instance
\r
270 * @param y A SqlInt64 instance
\r
271 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
272 * If either instance of SqlInt64 is null, the Value of the SqlBoolean will be Null.
\r
274 public static SqlBoolean LessThan(SqlInt64 x, SqlInt64 y)
\r
276 if (x.IsNull || y.IsNull)
\r
277 return SqlBoolean.Null;
\r
279 if (x._value < y._value)
\r
280 return SqlBoolean.True;
\r
282 return SqlBoolean.False;
\r
286 * Compares two instances of SqlInt64 to determine if the first is less than or equal to the second.
\r
287 * @param x A SqlInt64 instance
\r
288 * @param y A SqlInt64 instance
\r
289 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
290 * If either instance of SqlInt64 is null, the Value of the SqlBoolean will be Null.
\r
292 public static SqlBoolean LessThanOrEqual(SqlInt64 x, SqlInt64 y)
\r
294 if (x.IsNull || y.IsNull)
\r
295 return SqlBoolean.Null;
\r
297 if (x._value <= y._value)
\r
298 return SqlBoolean.True;
\r
300 return SqlBoolean.False;
\r
304 * Computes the remainder after dividing its first SqlInt64 operand by its second.
\r
305 * @param x A SqlInt64 instance
\r
306 * @param y A SqlInt64 instance
\r
307 * @return A SqlInt64 instance whose Value contains the remainder.
\r
309 public static SqlInt64 Mod(SqlInt64 x, SqlInt64 y)
\r
311 if (x.IsNull || y.IsNull)
\r
312 return SqlInt64.Null;
\r
314 long mod = x._value % y._value;
\r
316 return new SqlInt64(mod);
\r
320 * The multiplication operator computes the product of the two SqlInt64 operands.
\r
321 * @param x A SqlInt64 instance
\r
322 * @param y A SqlInt64 instance
\r
323 * @return The product of the two SqlInt64 operands.
\r
325 public static SqlInt64 Multiply(SqlInt64 x, SqlInt64 y)
\r
327 if (x.IsNull || y.IsNull)
\r
328 return SqlInt64.Null;
\r
330 long xVal = x._value;
\r
331 long yVal = y._value;
\r
333 long res = checked(xVal * yVal);
\r
335 return new SqlInt64(res);
\r
339 * Compares two instances of SqlInt64 to determine if they are equal.
\r
340 * @param x A SqlInt64 instance
\r
341 * @param y A SqlInt64 instance
\r
342 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
343 * If either instance of SqlInt64 is null, the Value of the SqlBoolean will be Null.
\r
345 public static SqlBoolean NotEquals(SqlInt64 x, SqlInt64 y)
\r
347 if (x.IsNull || y.IsNull)
\r
348 return SqlBoolean.Null;
\r
350 if (x._value != y._value)
\r
351 return SqlBoolean.True;
\r
353 return SqlBoolean.False;
\r
357 * The ones complement operator performs a bitwise one's complement operation on its SqlInt64 operand.
\r
358 * @param x A SqlInt64 instance
\r
359 * @return A SqlInt64 instance whose Value property contains the ones complement of the SqlInt64 parameter.
\r
361 public static SqlInt64 OnesComplement(SqlInt64 x)
\r
363 ulong res = (ulong)x._value ^ 0xFFFFFFFFFFFFFFFF;
\r
365 return new SqlInt64((long)res);
\r
369 * Converts the String representation of a number to its byte equivalent.
\r
370 * @param s The String to be parsed.
\r
371 * @return A SqlInt64 containing the value represented by the String.
\r
373 public static SqlInt64 Parse(String s)
\r
375 long res = long.Parse(s);
\r
377 return new SqlInt64(res);
\r
381 * The subtraction operator the second SqlInt64 operand from the first.
\r
382 * @param x A SqlInt64 instance
\r
383 * @param y A SqlInt64 instance
\r
384 * @return The results of the subtraction operation.
\r
386 public static SqlInt64 Subtract(SqlInt64 x, SqlInt64 y)
\r
388 if (x.IsNull || y.IsNull)
\r
389 return SqlInt64.Null;
\r
391 long xVal = x._value;
\r
392 long yVal = y._value;
\r
395 long res = checked(x._value - y._value);
\r
396 return new SqlInt64(res);
\r
400 * Performs a bitwise exclusive-OR operation on the supplied parameters.
\r
401 * @param x A SqlInt64 instance
\r
402 * @param y A SqlInt64 instance
\r
403 * @return The results of the XOR operation.
\r
405 public static SqlInt64 Xor(SqlInt64 x, SqlInt64 y)
\r
407 long res = x._value ^ y._value;
\r
409 return new SqlInt64(res);
\r
413 * Converts this SqlInt64 structure to SqlBoolean.
\r
414 * @return A SqlBoolean structure whose Value will be True if the SqlInt64 structure's Value is non-zero, False if the SqlInt64 is zero
\r
415 * and Null if the SqlInt64 structure is Null.
\r
417 public SqlBoolean ToSqlBoolean()
\r
420 return SqlBoolean.Null;
\r
423 return new SqlBoolean(0);
\r
425 return new SqlBoolean(1);
\r
429 * Converts this SqlInt64 structure to SqlByte.
\r
430 * @return A SqlByte structure whose Value equals the Value of this SqlInt64 structure.
\r
432 public SqlByte ToSqlByte()
\r
435 return SqlByte.Null;
\r
437 if (_value < 0 || _value > 255)
\r
438 throw new OverflowException("Can not convert this instance to SqlByte - overflowing : " + _value);
\r
440 return new SqlByte((byte)_value);
\r
444 * Converts this SqlInt64 structure to SqlDecimal.
\r
445 * @return A SqlDecimal structure whose Value equals the Value of this SqlInt64 structure.
\r
447 public SqlDecimal ToSqlDecimal()
\r
450 return SqlDecimal.Null;
\r
452 return new SqlDecimal(_value);
\r
456 * Converts this SqlInt64 structure to SqlDecimal.
\r
457 * @return A SqlDouble structure whose Value equals the Value of this SqlInt64 structure.
\r
459 public SqlDouble ToSqlDouble()
\r
462 return SqlDouble.Null;
\r
464 return new SqlDouble((double)_value);
\r
468 * Converts this SqlInt64 structure to SqlInt16.
\r
469 * @return A SqlInt16 structure whose Value equals the Value of this SqlInt64 structure.
\r
471 public SqlInt16 ToSqlInt16()
\r
474 return SqlInt16.Null;
\r
476 if (_value > short.MaxValue || _value < short.MinValue)
\r
477 throw new OverflowException("overflow - can not convert this SqlInt64 to SqlInt16 : " + _value);
\r
479 return new SqlInt16((short)_value);
\r
483 * Converts this SqlInt64 structure to SqlInt32.
\r
484 * @return A SqlInt32 structure whose Value equals the Value of this SqlInt64 structure.
\r
486 public SqlInt32 ToSqlInt32()
\r
489 return SqlInt32.Null;
\r
491 if (_value > int.MaxValue || _value < int.MinValue)
\r
492 throw new OverflowException("overflow - can not convert this SqlInt64 to SqlInt16 : " + _value);
\r
494 return new SqlInt32((int)_value);
\r
498 * Converts this SqlInt64 instance to SqlDouble.
\r
499 * @return A SqlMoney instance whose Value equals the Value of this SqlInt64 instance.
\r
501 public SqlMoney ToSqlMoney()
\r
504 return SqlMoney.Null;
\r
506 return new SqlMoney(_value);
\r
510 * Converts this SqlIn64 instance to SqlSingle.
\r
511 * @return A SqlSingle instance whose Value equals the Value of this SqlInt64 instance.
\r
513 public SqlSingle ToSqlSingle()
\r
516 return SqlSingle.Null;
\r
518 return new SqlSingle((float)_value);
\r
524 * Converts this SqlInt64 structure to SqlString.
\r
525 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlInt64 structure.
\r
527 public SqlString ToSqlString()
\r
529 return new SqlString(ToString());
\r
534 public override String ToString()
\r
538 return _value.ToString();
\r
541 public static SqlInt64 operator + (SqlInt64 x, SqlInt64 y)
\r
545 return new SqlInt64 (x.Value + y.Value);
\r
549 public static SqlInt64 operator & (SqlInt64 x, SqlInt64 y)
\r
551 return new SqlInt64 (x.Value & y.Value);
\r
554 public static SqlInt64 operator | (SqlInt64 x, SqlInt64 y)
\r
556 return new SqlInt64 (x.Value | y.Value);
\r
559 public static SqlInt64 operator / (SqlInt64 x, SqlInt64 y)
\r
563 return new SqlInt64 (x.Value / y.Value);
\r
567 public static SqlBoolean operator == (SqlInt64 x, SqlInt64 y)
\r
569 if (x.IsNull || y.IsNull)
\r
570 return SqlBoolean.Null;
\r
572 return new SqlBoolean (x.Value == y.Value);
\r
575 public static SqlInt64 operator ^ (SqlInt64 x, SqlInt64 y)
\r
577 return new SqlInt64 (x.Value ^ y.Value);
\r
580 public static SqlBoolean operator > (SqlInt64 x, SqlInt64 y)
\r
582 if (x.IsNull || y.IsNull)
\r
583 return SqlBoolean.Null;
\r
585 return new SqlBoolean (x.Value > y.Value);
\r
588 public static SqlBoolean operator >= (SqlInt64 x, SqlInt64 y)
\r
590 if (x.IsNull || y.IsNull)
\r
591 return SqlBoolean.Null;
\r
593 return new SqlBoolean (x.Value >= y.Value);
\r
596 public static SqlBoolean operator != (SqlInt64 x, SqlInt64 y)
\r
598 if (x.IsNull || y.IsNull)
\r
599 return SqlBoolean.Null;
\r
601 return new SqlBoolean (!(x.Value == y.Value));
\r
604 public static SqlBoolean operator < (SqlInt64 x, SqlInt64 y)
\r
606 if (x.IsNull || y.IsNull)
\r
607 return SqlBoolean.Null;
\r
609 return new SqlBoolean (x.Value < y.Value);
\r
612 public static SqlBoolean operator <= (SqlInt64 x, SqlInt64 y)
\r
614 if (x.IsNull || y.IsNull)
\r
615 return SqlBoolean.Null;
\r
617 return new SqlBoolean (x.Value <= y.Value);
\r
620 public static SqlInt64 operator % (SqlInt64 x, SqlInt64 y)
\r
622 return new SqlInt64(x.Value % y.Value);
\r
625 public static SqlInt64 operator * (SqlInt64 x, SqlInt64 y)
\r
629 return new SqlInt64 (x.Value * y.Value);
\r
633 public static SqlInt64 operator ~ (SqlInt64 x)
\r
636 return SqlInt64.Null;
\r
638 return new SqlInt64 (~(x.Value));
\r
641 public static SqlInt64 operator - (SqlInt64 x, SqlInt64 y)
\r
645 return new SqlInt64 (x.Value - y.Value);
\r
649 public static SqlInt64 operator - (SqlInt64 n)
\r
651 return new SqlInt64 (-(n.Value));
\r
654 public static explicit operator SqlInt64 (SqlBoolean x)
\r
657 return SqlInt64.Null;
\r
659 return new SqlInt64 ((long)x.ByteValue);
\r
662 public static explicit operator SqlInt64 (SqlDecimal x)
\r
667 return SqlInt64.Null;
\r
669 return new SqlInt64 ((long)x.Value);
\r
673 public static explicit operator SqlInt64 (SqlDouble x)
\r
676 return SqlInt64.Null;
\r
681 return new SqlInt64 ((long)x.Value);
\r
686 public static explicit operator long (SqlInt64 x)
\r
691 public static explicit operator SqlInt64 (SqlMoney x)
\r
696 return SqlInt64.Null;
\r
698 return new SqlInt64 ((long)x.Value);
\r
702 public static explicit operator SqlInt64 (SqlSingle x)
\r
705 return SqlInt64.Null;
\r
710 return new SqlInt64 ((long)x.Value);
\r
715 public static explicit operator SqlInt64 (SqlString x)
\r
719 return SqlInt64.Parse (x.Value);
\r
723 public static implicit operator SqlInt64 (long x)
\r
725 return new SqlInt64 (x);
\r
728 public static implicit operator SqlInt64 (SqlByte x)
\r
731 return SqlInt64.Null;
\r
733 return new SqlInt64 ((long)x.Value);
\r
736 public static implicit operator SqlInt64 (SqlInt16 x)
\r
739 return SqlInt64.Null;
\r
741 return new SqlInt64 ((long)x.Value);
\r
744 public static implicit operator SqlInt64 (SqlInt32 x)
\r
747 return SqlInt64.Null;
\r
749 return new SqlInt64 ((long)x.Value);
\r