2 // System.Data.SqlTypes.SqlInt32
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 // Ville Palo (vi64pa@koti.soon.fi)
10 // (C) Ximian, Inc. 2002
11 // (C) Copyright 2002 Tim Coleman
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Globalization;
40 namespace System.Data.SqlTypes
44 /// a 32-bit signed integer to be used in reading or writing
45 /// of data from a database
47 public struct SqlInt32 : INullable, IComparable
54 public static readonly SqlInt32 MaxValue = new SqlInt32 (2147483647);
55 public static readonly SqlInt32 MinValue = new SqlInt32 (-2147483648);
56 public static readonly SqlInt32 Null;
57 public static readonly SqlInt32 Zero = new SqlInt32 (0);
63 public SqlInt32(int value)
74 get { return !notNull; }
80 throw new SqlNullValueException ();
90 public static SqlInt32 Add (SqlInt32 x, SqlInt32 y)
95 public static SqlInt32 BitwiseAnd(SqlInt32 x, SqlInt32 y)
100 public static SqlInt32 BitwiseOr(SqlInt32 x, SqlInt32 y)
105 public int CompareTo(object value)
109 else if (!(value is SqlInt32))
110 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlInt32"));
111 return CompareSqlInt32 ((SqlInt32) value);
115 public int CompareTo (SqlInt32 value)
117 return CompareSqlInt32 (value);
121 private int CompareSqlInt32 (SqlInt32 value)
126 return this.value.CompareTo (value.Value);
129 public static SqlInt32 Divide(SqlInt32 x, SqlInt32 y)
134 public override bool Equals(object value)
136 if (!(value is SqlInt32))
138 else if (this.IsNull && ((SqlInt32)value).IsNull)
140 else if (((SqlInt32)value).IsNull)
143 return (bool) (this == (SqlInt32)value);
146 public static SqlBoolean Equals(SqlInt32 x, SqlInt32 y)
151 public override int GetHashCode()
156 public static SqlBoolean GreaterThan (SqlInt32 x, SqlInt32 y)
161 public static SqlBoolean GreaterThanOrEqual (SqlInt32 x, SqlInt32 y)
166 public static SqlBoolean LessThan(SqlInt32 x, SqlInt32 y)
171 public static SqlBoolean LessThanOrEqual(SqlInt32 x, SqlInt32 y)
176 public static SqlInt32 Mod(SqlInt32 x, SqlInt32 y)
182 public static SqlInt32 Modulus (SqlInt32 x, SqlInt32 y)
188 public static SqlInt32 Multiply(SqlInt32 x, SqlInt32 y)
193 public static SqlBoolean NotEquals(SqlInt32 x, SqlInt32 y)
198 public static SqlInt32 OnesComplement(SqlInt32 x)
203 public static SqlInt32 Parse(string s)
205 return new SqlInt32 (Int32.Parse (s));
208 public static SqlInt32 Subtract(SqlInt32 x, SqlInt32 y)
213 public SqlBoolean ToSqlBoolean()
215 return ((SqlBoolean)this);
218 public SqlByte ToSqlByte()
220 return ((SqlByte)this);
223 public SqlDecimal ToSqlDecimal()
225 return ((SqlDecimal)this);
228 public SqlDouble ToSqlDouble()
230 return ((SqlDouble)this);
233 public SqlInt16 ToSqlInt16()
235 return ((SqlInt16)this);
238 public SqlInt64 ToSqlInt64()
240 return ((SqlInt64)this);
243 public SqlMoney ToSqlMoney()
245 return ((SqlMoney)this);
248 public SqlSingle ToSqlSingle()
250 return ((SqlSingle)this);
253 public SqlString ToSqlString ()
255 return ((SqlString)this);
258 public override string ToString()
263 return value.ToString ();
266 public static SqlInt32 Xor(SqlInt32 x, SqlInt32 y)
276 public static SqlInt32 operator + (SqlInt32 x, SqlInt32 y)
279 return new SqlInt32 (x.Value + y.Value);
284 public static SqlInt32 operator & (SqlInt32 x, SqlInt32 y)
286 return new SqlInt32 (x.Value & y.Value);
290 public static SqlInt32 operator | (SqlInt32 x, SqlInt32 y)
293 return new SqlInt32 (x.Value | y.Value);
298 public static SqlInt32 operator / (SqlInt32 x, SqlInt32 y)
301 return new SqlInt32 (x.Value / y.Value);
306 public static SqlBoolean operator == (SqlInt32 x, SqlInt32 y)
308 if (x.IsNull || y.IsNull)
309 return SqlBoolean.Null;
311 return new SqlBoolean (x.Value == y.Value);
314 // Bitwise Exclusive-OR (XOR)
315 public static SqlInt32 operator ^ (SqlInt32 x, SqlInt32 y)
317 return new SqlInt32 (x.Value ^ y.Value);
321 public static SqlBoolean operator >(SqlInt32 x, SqlInt32 y)
323 if (x.IsNull || y.IsNull)
324 return SqlBoolean.Null;
326 return new SqlBoolean (x.Value > y.Value);
330 public static SqlBoolean operator >= (SqlInt32 x, SqlInt32 y)
332 if (x.IsNull || y.IsNull)
333 return SqlBoolean.Null;
335 return new SqlBoolean (x.Value >= y.Value);
338 // != Inequality Compare
339 public static SqlBoolean operator != (SqlInt32 x, SqlInt32 y)
341 if (x.IsNull || y.IsNull)
342 return SqlBoolean.Null;
344 return new SqlBoolean (x.Value != y.Value);
348 public static SqlBoolean operator < (SqlInt32 x, SqlInt32 y)
350 if (x.IsNull || y.IsNull)
351 return SqlBoolean.Null;
353 return new SqlBoolean (x.Value < y.Value);
357 public static SqlBoolean operator <= (SqlInt32 x, SqlInt32 y)
359 if (x.IsNull || y.IsNull)
360 return SqlBoolean.Null;
362 return new SqlBoolean (x.Value <= y.Value);
366 public static SqlInt32 operator % (SqlInt32 x, SqlInt32 y)
368 return new SqlInt32 (x.Value % y.Value);
371 // Compute Multiplication
372 public static SqlInt32 operator * (SqlInt32 x, SqlInt32 y)
375 return new SqlInt32 (x.Value * y.Value);
380 public static SqlInt32 operator ~ (SqlInt32 x)
382 return new SqlInt32 (~x.Value);
386 public static SqlInt32 operator - (SqlInt32 x, SqlInt32 y)
389 return new SqlInt32 (x.Value - y.Value);
394 public static SqlInt32 operator - (SqlInt32 x)
396 return new SqlInt32 (-x.Value);
400 public static explicit operator SqlInt32 (SqlBoolean x)
405 return new SqlInt32 ((int)x.ByteValue);
408 public static explicit operator SqlInt32 (SqlDecimal x)
414 return new SqlInt32 ((int)x.Value);
418 public static explicit operator SqlInt32 (SqlDouble x)
424 return new SqlInt32 ((int)x.Value);
428 public static explicit operator int (SqlInt32 x)
433 public static explicit operator SqlInt32 (SqlInt64 x)
439 return new SqlInt32 ((int)x.Value);
443 public static explicit operator SqlInt32(SqlMoney x)
449 return new SqlInt32 ((int) Math.Round (x.Value));
453 public static explicit operator SqlInt32(SqlSingle x)
459 return new SqlInt32 ((int)x.Value);
463 public static explicit operator SqlInt32(SqlString x)
466 return SqlInt32.Parse (x.Value);
470 public static implicit operator SqlInt32(int x)
472 return new SqlInt32 (x);
475 public static implicit operator SqlInt32(SqlByte x)
480 return new SqlInt32 ((int)x.Value);
483 public static implicit operator SqlInt32(SqlInt16 x)
488 return new SqlInt32 ((int)x.Value);