1 // System.Data.SqlTypes.SqlInt32
\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 SqlInt32 : INullable, IComparable
\r
40 private bool _isNull;
\r
42 public static readonly SqlInt32 Null = new SqlInt32(true);
\r
43 public static readonly SqlInt32 MaxValue = new SqlInt32(int.MaxValue);
\r
44 public static readonly SqlInt32 MinValue = new SqlInt32(int.MinValue);
\r
45 public static readonly SqlInt32 Zero = new SqlInt32(0);
\r
49 private SqlInt32(bool isNull)
\r
56 * @param value A int whose value will be used for the new SqlInt32.
\r
58 public SqlInt32(int value)
\r
67 * Indicates whether or not Value is null.
\r
68 * @return true if Value is null, otherwise false.
\r
81 * Gets the value of the SqlInt32 instance.
\r
82 * @return the value of this instance
\r
89 throw new SqlNullValueException();
\r
95 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
96 * @param obj The object to compare.
\r
97 * @return A signed number indicating the relative values of the instance and the object.
\r
98 * Less than zero This instance is less than object.
\r
99 * Zero This instance is the same as object.
\r
100 * Greater than zero This instance is greater than object -or-
\r
101 * object is a null reference.
\r
103 public int CompareTo(Object obj)
\r
108 if (obj is SqlInt32)
\r
110 SqlInt32 i = (SqlInt32)obj;
\r
117 return this._value.CompareTo(i._value);
\r
120 throw new ArgumentException("parameter obj is not SqlInt32 : " + obj.GetType().Name);
\r
125 * The addition operator computes the sum of the two SqlInt32 operands.
\r
126 * @param x A SqlInt32 structure.
\r
127 * @param y A SqlInt32 structure.
\r
128 * @return The sum of the two SqlInt32 operands.
\r
129 * If one of the parameters is null or null value - return SqlInt32.Null.
\r
131 public static SqlInt32 Add(SqlInt32 x, SqlInt32 y)
\r
133 if (x.IsNull || y.IsNull)
\r
134 return SqlInt32.Null;
\r
136 int xVal = x._value;
\r
137 int yVal = y._value;
\r
139 int sum = checked(xVal + yVal);
\r
141 return new SqlInt32(sum);
\r
145 * Computes the bitwise AND of its SqlInt32 operands.
\r
146 * @param x A SqlInt32 instance.
\r
147 * @param y A SqlInt32 instance.
\r
148 * @return The results of the bitwise AND operation.
\r
150 public static SqlInt32 BitwiseAnd(SqlInt32 x, SqlInt32 y)
\r
152 if (x.IsNull || y.IsNull)
\r
153 return SqlInt32.Null;
\r
155 int res = x._value & y._value;
\r
157 return new SqlInt32(res);
\r
161 * Computes the bitwise OR of its SqlInt32 operands.
\r
162 * @param x A SqlInt32 instance.
\r
163 * @param y A SqlInt32 instance.
\r
164 * @return The results of the bitwise OR operation.
\r
166 public static SqlInt32 BitwiseOr(SqlInt32 x, SqlInt32 y)
\r
168 if (x.IsNull || y.IsNull)
\r
169 return SqlInt32.Null;
\r
171 int res = x._value | y._value;
\r
173 return new SqlInt32(res);
\r
177 * The division operator divides the first SqlInt32 operand by the second.
\r
178 * @param x A SqlInt32 instance.
\r
179 * @param y A SqlInt32 instance.
\r
180 * @return A SqlInt32 instance containing the results of the division operation.
\r
181 * If one of the parameters is null or null value - return SqlInt32.Null.
\r
183 public static SqlInt32 Divide(SqlInt32 x, SqlInt32 y)
\r
185 int val = x._value / y._value;
\r
186 return new SqlInt32(val);
\r
189 public override bool Equals(Object obj)
\r
194 if (obj is SqlInt32)
\r
196 SqlInt32 i = (SqlInt32)obj;
\r
198 if (IsNull && i.IsNull)
\r
201 if (IsNull || i.IsNull)
\r
204 return _value == i._value;
\r
212 * Performs a logical comparison on two instances of SqlInt32 to determine if they are equal.
\r
213 * @param x A SqlInt32 instance.
\r
214 * @param y A SqlInt32 instance.
\r
215 * @return true if the two values are equal, otherwise false.
\r
216 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
218 public static SqlBoolean Equals(SqlInt32 x, SqlInt32 y)
\r
220 if (x.IsNull || y.IsNull)
\r
221 return SqlBoolean.Null;
\r
223 if (x._value == y._value)
\r
224 return SqlBoolean.True;
\r
226 return SqlBoolean.False;
\r
230 * Returns the hash code for this SqlInt32 instance.
\r
231 * @return A signed integer hash code.
\r
233 public override int GetHashCode()
\r
239 * Compares two instances of SqlByte to determine if the first is greater than the second.
\r
240 * @param x A SqlByte instance
\r
241 * @param y A SqlByte instance
\r
242 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
243 * If either instance of SqlInt32 is null, the Value of the SqlBoolean will be Null.
\r
245 public static SqlBoolean GreaterThan(SqlInt32 x, SqlInt32 y)
\r
247 if (x.IsNull || y.IsNull)
\r
248 return SqlBoolean.Null;
\r
250 if (x._value > y._value)
\r
251 return SqlBoolean.True;
\r
253 return SqlBoolean.False;
\r
257 * Compares two instances of SqlInt32 to determine if the first is greater than or equal to the second.
\r
258 * @param x A SqlInt32 instance
\r
259 * @param y A SqlInt32 instance
\r
260 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
261 * If either instance of SqlInt32 is null, the Value of the SqlBoolean will be Null.
\r
263 public static SqlBoolean GreaterThanOrEqual(SqlInt32 x, SqlInt32 y)
\r
265 if (x.IsNull || y.IsNull)
\r
266 return SqlBoolean.Null;
\r
268 if (x._value >= y._value)
\r
269 return SqlBoolean.True;
\r
271 return SqlBoolean.False;
\r
275 * Compares two instances of SqlInt32 to determine if the first is less than the second.
\r
276 * @param x A SqlInt32 instance
\r
277 * @param y A SqlInt32 instance
\r
278 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
279 * If either instance of SqlInt32 is null, the Value of the SqlBoolean will be Null.
\r
281 public static SqlBoolean LessThan(SqlInt32 x, SqlInt32 y)
\r
283 if (x.IsNull || y.IsNull)
\r
284 return SqlBoolean.Null;
\r
286 if (x._value < y._value)
\r
287 return SqlBoolean.True;
\r
289 return SqlBoolean.False;
\r
293 * Compares two instances of SqlInt32 to determine if the first is less than or equal to the second.
\r
294 * @param x A SqlInt32 instance
\r
295 * @param y A SqlInt32 instance
\r
296 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
297 * If either instance of SqlInt32 is null, the Value of the SqlBoolean will be Null.
\r
299 public static SqlBoolean LessThanOrEqual(SqlInt32 x, SqlInt32 y)
\r
301 if (x.IsNull || y.IsNull)
\r
302 return SqlBoolean.Null;
\r
304 if (x._value <= y._value)
\r
305 return SqlBoolean.True;
\r
307 return SqlBoolean.False;
\r
311 * Computes the remainder after dividing its first SqlInt32 operand by its second.
\r
312 * @param x A SqlInt32 instance
\r
313 * @param y A SqlInt32 instance
\r
314 * @return A SqlInt32 instance whose Value contains the remainder.
\r
316 public static SqlInt32 Mod(SqlInt32 x, SqlInt32 y)
\r
318 if (x.IsNull || y.IsNull)
\r
319 return SqlInt32.Null;
\r
321 int mod = x._value % y._value;
\r
323 return new SqlInt32(mod);
\r
327 * The multiplication operator computes the product of the two SqlInt32 operands.
\r
328 * @param x A SqlInt32 instance
\r
329 * @param y A SqlInt32 instance
\r
330 * @return The product of the two SqlInt32 operands.
\r
332 public static SqlInt32 Multiply(SqlInt32 x, SqlInt32 y)
\r
334 if (x.IsNull || y.IsNull)
\r
335 return SqlInt32.Null;
\r
337 int xVal = x._value;
\r
338 int yVal = y._value;
\r
340 int res = checked(xVal * yVal);
\r
342 return new SqlInt32(res);
\r
346 * Compares two instances of SqlInt32 to determine if they are equal.
\r
347 * @param x A SqlInt32 instance
\r
348 * @param y A SqlInt32 instance
\r
349 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
350 * If either instance of SqlInt32 is null, the Value of the SqlBoolean will be Null.
\r
352 public static SqlBoolean NotEquals(SqlInt32 x, SqlInt32 y)
\r
354 if (x.IsNull || y.IsNull)
\r
355 return SqlBoolean.Null;
\r
357 if (x._value != y._value)
\r
358 return SqlBoolean.True;
\r
360 return SqlBoolean.False;
\r
364 * The ones complement operator performs a bitwise one's complement operation on its SqlInt32 operand.
\r
365 * @param x A SqlInt32 instance
\r
366 * @return A SqlInt32 instance whose Value property contains the ones complement of the SqlInt32 parameter.
\r
368 public static SqlInt32 OnesComplement(SqlInt32 x)
\r
370 int res = (int)(x._value ^ 0xFFFFFFFF);
\r
372 return new SqlInt32(res);
\r
376 * Converts the String representation of a number to its byte equivalent.
\r
377 * @param s The String to be parsed.
\r
378 * @return A SqlInt32 containing the value represented by the String.
\r
380 public static SqlInt32 Parse(String s)
\r
382 int res = int.Parse(s);
\r
384 return new SqlInt32(res);
\r
388 * The subtraction operator the second SqlInt32 operand from the first.
\r
389 * @param x A SqlInt32 instance
\r
390 * @param y A SqlInt32 instance
\r
391 * @return The results of the subtraction operation.
\r
393 public static SqlInt32 Subtract(SqlInt32 x, SqlInt32 y)
\r
395 if (x.IsNull || y.IsNull)
\r
396 return SqlInt32.Null;
\r
398 int res = checked(x._value - y._value);
\r
399 return new SqlInt32(res);
\r
403 * Performs a bitwise exclusive-OR operation on the supplied parameters.
\r
404 * @param x A SqlInt32 instance
\r
405 * @param y A SqlInt32 instance
\r
406 * @return The results of the XOR operation.
\r
408 public static SqlInt32 Xor(SqlInt32 x, SqlInt32 y)
\r
410 int res = x._value ^ y._value;
\r
412 return new SqlInt32(res);
\r
416 * Converts this SqlInt32 structure to SqlBoolean.
\r
417 * @return A SqlBoolean structure whose Value will be True if the SqlInt32 structure's Value is non-zero, False if the SqlInt32 is zero
\r
418 * and Null if the SqlInt32 structure is Null.
\r
420 public SqlBoolean ToSqlBoolean()
\r
423 return SqlBoolean.Null;
\r
426 return new SqlBoolean(0);
\r
428 return new SqlBoolean(1);
\r
432 * Converts this SqlInt32 structure to SqlByte.
\r
433 * @return A SqlByte structure whose Value equals the Value of this SqlInt32 structure.
\r
435 public SqlByte ToSqlByte()
\r
438 return SqlByte.Null;
\r
440 if (_value < 0 || _value > 255)
\r
441 throw new OverflowException("Can not onvert this instance to SqlByte - overflowing : " + _value);
\r
443 return new SqlByte((byte)_value);
\r
447 * Converts this SqlInt32 structure to SqlDecimal.
\r
448 * @return A SqlDecimal structure whose Value equals the Value of this SqlInt32 structure.
\r
450 public SqlDecimal ToSqlDecimal()
\r
453 return SqlDecimal.Null;
\r
455 return new SqlDecimal(_value);
\r
459 * Converts this SqlInt32 structure to SqlDecimal.
\r
460 * @return A SqlDouble structure whose Value equals the Value of this SqlInt32 structure.
\r
462 public SqlDouble ToSqlDouble()
\r
465 return SqlDouble.Null;
\r
467 return new SqlDouble(_value);
\r
471 * Converts this SqlInt32 structure to SqlInt16.
\r
472 * @return A SqlInt16 structure whose Value equals the Value of this SqlInt32 structure.
\r
474 public SqlInt16 ToSqlInt16()
\r
477 return SqlInt16.Null;
\r
479 return new SqlInt16(_value);
\r
483 * Converts this SqlInt32 structure to SqlInt64.
\r
484 * @return A SqlInt64 structure whose Value equals the Value of this SqlInt32 structure.
\r
486 public SqlInt64 ToSqlInt64()
\r
489 return SqlInt64.Null;
\r
491 return new SqlInt64(_value);
\r
495 * Converts this SqlInt32 instance to SqlDouble.
\r
496 * @return A SqlMoney instance whose Value equals the Value of this SqlInt32 instance.
\r
498 public SqlMoney ToSqlMoney()
\r
501 return SqlMoney.Null;
\r
503 return new SqlMoney(_value);
\r
507 * Converts this SqlInt32 instance to SqlSingle.
\r
508 * @return A SqlSingle instance whose Value equals the Value of this SqlInt32 instance.
\r
510 public SqlSingle ToSqlSingle()
\r
513 return SqlSingle.Null;
\r
515 return new SqlSingle((float)_value);
\r
519 * Converts this SqlInt32 structure to SqlString.
\r
520 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlInt32 structure.
\r
522 public SqlString ToSqlString()
\r
524 return new SqlString(ToString());
\r
529 public override String ToString()
\r
533 return _value.ToString();
\r
536 // Compute Addition
\r
537 public static SqlInt32 operator + (SqlInt32 x, SqlInt32 y)
\r
541 return new SqlInt32 (x.Value + y.Value);
\r
546 public static SqlInt32 operator & (SqlInt32 x, SqlInt32 y)
\r
548 return new SqlInt32 (x.Value & y.Value);
\r
552 public static SqlInt32 operator | (SqlInt32 x, SqlInt32 y)
\r
556 return new SqlInt32 (x.Value | y.Value);
\r
560 // Compute Division
\r
561 public static SqlInt32 operator / (SqlInt32 x, SqlInt32 y)
\r
565 return new SqlInt32 (x.Value / y.Value);
\r
569 // Compare Equality
\r
570 public static SqlBoolean operator == (SqlInt32 x, SqlInt32 y)
\r
572 if (x.IsNull || y.IsNull)
\r
573 return SqlBoolean.Null;
\r
575 return new SqlBoolean (x.Value == y.Value);
\r
578 // Bitwise Exclusive-OR (XOR)
\r
579 public static SqlInt32 operator ^ (SqlInt32 x, SqlInt32 y)
\r
581 return new SqlInt32 (x.Value ^ y.Value);
\r
585 public static SqlBoolean operator >(SqlInt32 x, SqlInt32 y)
\r
587 if (x.IsNull || y.IsNull)
\r
588 return SqlBoolean.Null;
\r
590 return new SqlBoolean (x.Value > y.Value);
\r
594 public static SqlBoolean operator >= (SqlInt32 x, SqlInt32 y)
\r
596 if (x.IsNull || y.IsNull)
\r
597 return SqlBoolean.Null;
\r
599 return new SqlBoolean (x.Value >= y.Value);
\r
602 // != Inequality Compare
\r
603 public static SqlBoolean operator != (SqlInt32 x, SqlInt32 y)
\r
605 if (x.IsNull || y.IsNull)
\r
606 return SqlBoolean.Null;
\r
608 return new SqlBoolean (x.Value != y.Value);
\r
612 public static SqlBoolean operator < (SqlInt32 x, SqlInt32 y)
\r
614 if (x.IsNull || y.IsNull)
\r
615 return SqlBoolean.Null;
\r
617 return new SqlBoolean (x.Value < y.Value);
\r
621 public static SqlBoolean operator <= (SqlInt32 x, SqlInt32 y)
\r
623 if (x.IsNull || y.IsNull)
\r
624 return SqlBoolean.Null;
\r
626 return new SqlBoolean (x.Value <= y.Value);
\r
630 public static SqlInt32 operator % (SqlInt32 x, SqlInt32 y)
\r
632 return new SqlInt32 (x.Value % y.Value);
\r
635 // Compute Multiplication
\r
636 public static SqlInt32 operator * (SqlInt32 x, SqlInt32 y)
\r
640 return new SqlInt32 (x.Value * y.Value);
\r
645 public static SqlInt32 operator ~ (SqlInt32 x)
\r
647 return new SqlInt32 (~x.Value);
\r
651 public static SqlInt32 operator - (SqlInt32 x, SqlInt32 y)
\r
655 return new SqlInt32 (x.Value - y.Value);
\r
659 // Negates the Value
\r
660 public static SqlInt32 operator - (SqlInt32 x)
\r
662 return new SqlInt32 (-x.Value);
\r
665 // Type Conversions
\r
666 public static explicit operator SqlInt32 (SqlBoolean x)
\r
671 return new SqlInt32 ((int)x.ByteValue);
\r
674 public static explicit operator SqlInt32 (SqlDecimal x)
\r
681 return new SqlInt32 ((int)x.Value);
\r
685 public static explicit operator SqlInt32 (SqlDouble x)
\r
692 return new SqlInt32 ((int)x.Value);
\r
696 public static explicit operator int (SqlInt32 x)
\r
701 public static explicit operator SqlInt32 (SqlInt64 x)
\r
708 return new SqlInt32 ((int)x.Value);
\r
712 public static explicit operator SqlInt32(SqlMoney x)
\r
719 return new SqlInt32 ((int)x.Value);
\r
723 public static explicit operator SqlInt32(SqlSingle x)
\r
730 return new SqlInt32 ((int)x.Value);
\r
734 public static explicit operator SqlInt32(SqlString x)
\r
738 return SqlInt32.Parse (x.Value);
\r
742 public static implicit operator SqlInt32(int x)
\r
744 return new SqlInt32 (x);
\r
747 public static implicit operator SqlInt32(SqlByte x)
\r
752 return new SqlInt32 ((int)x.Value);
\r
755 public static implicit operator SqlInt32(SqlInt16 x)
\r
760 return new SqlInt32 ((int)x.Value);
\r