2 * Copyright (c) 2002-2004 Mainsoft Corporation.
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a
\r
5 * copy of this software and associated documentation files (the "Software"),
\r
6 * to deal in the Software without restriction, including without limitation
\r
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
8 * and/or sell copies of the Software, and to permit persons to whom the
\r
9 * Software is furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
\r
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
20 * DEALINGS IN THE SOFTWARE.
\r
22 namespace System.Data.SqlTypes
\r
31 public struct SqlByte : INullable, IComparable
\r
33 private byte _value; // = -1;
\r
34 private bool _isNull;
\r
36 public static readonly SqlByte MaxValue = new SqlByte((byte)0xFF);
\r
37 public static readonly SqlByte MinValue = new SqlByte((byte)0);
\r
38 public static readonly SqlByte Zero = new SqlByte((byte)0);
\r
39 public static readonly SqlByte Null = new SqlByte(true);
\r
42 private SqlByte(bool isNull)
\r
48 * Initializes a new instance of the SqlByte instance using the specified byte value.
\r
49 * @param value A byte value to be stored in the Value property of the new SqlByte instance.
\r
51 public SqlByte(byte value)
\r
58 public override int GetHashCode()
\r
67 * Indicates whether or not Value is null.
\r
68 * @return true if Value is null, otherwise false.
\r
79 * Gets the value of the SqlByte instance.
\r
80 * @return the value of this instance
\r
88 throw new SqlNullValueException();
\r
96 * Computes the sum of the two specified SqlByte instances.
\r
97 * @param x A SqlByte instance.
\r
98 * @param y A SqlByte instance.
\r
99 * @return A SqlByte instance whose Value property contains the results of the addition.
\r
101 public static SqlByte Add(SqlByte x, SqlByte y)
\r
103 if (x.IsNull || y.IsNull)
\r
104 return SqlByte.Null;
\r
106 int res = x._value + y._value;
\r
109 throw new OverflowException("Arithmetic Overflow");
\r
111 return new SqlByte((byte)res);
\r
115 * Computes the bitwise AND of its SqlByte operands.
\r
116 * @param x A SqlByte instance.
\r
117 * @param y A SqlByte instance.
\r
118 * @return The results of the bitwise AND operation.
\r
120 public static SqlByte BitwiseAnd(SqlByte x, SqlByte y)
\r
122 if (x.IsNull || y.IsNull)
\r
123 return SqlByte.Null;
\r
125 int res = x._value & y._value;
\r
126 return new SqlByte((byte)res);
\r
130 * Computes the bitwise OR of its SqlByte operands.
\r
131 * @param x A SqlByte instance.
\r
132 * @param y A SqlByte instance.
\r
133 * @return The results of the bitwise OR operation.
\r
135 public static SqlByte BitwiseOr(SqlByte x, SqlByte y)
\r
137 if (x.IsNull || y.IsNull)
\r
138 return SqlByte.Null;
\r
140 int res = x._value | y._value;
\r
141 return new SqlByte((byte)res);
\r
145 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
146 * @param obj The object to compare.
\r
147 * @return A signed number indicating the relative values of the instance and the object.
\r
148 * Less than zero This instance is less than object.
\r
149 * Zero This instance is the same as object.
\r
150 * Greater than zero This instance is greater than object -or-
\r
151 * object is a null reference.
\r
153 public int CompareTo(Object obj)
\r
158 if ((obj is SqlByte) == false)
\r
160 throw new ArgumentException("Wrong value type " +
\r
161 obj.GetType().Name + "in SqlByte.CompareTo");
\r
164 SqlByte val = (SqlByte)obj;
\r
172 else if (val.IsNull)
\r
175 if (_value > val._value)
\r
178 if (_value < val._value)
\r
186 * The division operator divides the first SqlByte operand by the second.
\r
187 * @param x A SqlByte instance.
\r
188 * @param y A SqlByte instance.
\r
189 * @return A SqlByte instance containing the results of the division operation.
\r
190 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
192 public static SqlByte Divide(SqlByte x, SqlByte y)
\r
194 if (x.IsNull || y.IsNull)
\r
195 return SqlByte.Null;
\r
198 throw new DivideByZeroException("Divide by zero error encountered.");
\r
200 int val = x._value / y._value;
\r
201 return new SqlByte((byte)val);
\r
204 public override bool Equals(Object obj)
\r
209 if (obj is SqlByte)
\r
211 if (((SqlByte)obj)._value == this._value)
\r
219 * Performs a logical comparison on two instances of SqlByte to determine if they are equal.
\r
220 * @param x A SqlByte instance.
\r
221 * @param y A SqlByte instance.
\r
222 * @return true if the two values are equal, otherwise false.
\r
223 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
225 public static SqlBoolean Equals(SqlByte x, SqlByte y)
\r
227 if (x.IsNull || y.IsNull)
\r
228 return SqlBoolean.Null;
\r
230 if (x._value == y._value)
\r
231 return SqlBoolean.True;
\r
233 return SqlBoolean.False;
\r
237 * Compares two instances of SqlByte to determine if the first is greater than the second.
\r
238 * @param x A SqlByte instance
\r
239 * @param y A SqlByte instance
\r
240 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
241 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
243 public static SqlBoolean GreaterThan(SqlByte x, SqlByte y)
\r
245 if (x.IsNull || y.IsNull)
\r
246 return SqlBoolean.Null;
\r
248 if (x._value > y._value)
\r
249 return SqlBoolean.True;
\r
251 return SqlBoolean.False;
\r
255 * Compares two instances of SqlByte to determine if the first is greater than or equal to the second.
\r
256 * @param x A SqlByte instance
\r
257 * @param y A SqlByte instance
\r
258 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
259 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
261 public static SqlBoolean GreaterThanOrEqual(SqlByte x, SqlByte y)
\r
263 if (x.IsNull || y.IsNull)
\r
264 return SqlBoolean.Null;
\r
266 if (x._value >= y._value)
\r
267 return SqlBoolean.True;
\r
269 return SqlBoolean.False;
\r
273 * Compares two instances of SqlByte to determine if the first is less than the second.
\r
274 * @param x A SqlByte instance
\r
275 * @param y A SqlByte instance
\r
276 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
277 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
279 public static SqlBoolean LessThan(SqlByte x, SqlByte y)
\r
281 if (x.IsNull || y.IsNull)
\r
282 return SqlBoolean.Null;
\r
284 if (x._value < y._value)
\r
285 return SqlBoolean.True;
\r
287 return SqlBoolean.False;
\r
291 * Compares two instances of SqlByte to determine if the first is less than or equal to the second.
\r
292 * @param x A SqlByte instance
\r
293 * @param y A SqlByte instance
\r
294 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
295 * If either instance of SqlByte is null, the Value of the SqlBoolean will be Null.
\r
297 public static SqlBoolean LessThanOrEqual(SqlByte x, SqlByte y)
\r
299 if (x.IsNull || y.IsNull)
\r
300 return SqlBoolean.Null;
\r
302 if (x._value <= y._value)
\r
303 return SqlBoolean.True;
\r
305 return SqlBoolean.False;
\r
309 * Computes the remainder after dividing its first SqlByte operand by its second.
\r
310 * @param x A SqlByte instance
\r
311 * @param y A SqlByte instance
\r
312 * @return A SqlByte instance whose Value contains the remainder.
\r
314 public static SqlByte Mod(SqlByte x, SqlByte y)
\r
316 if (x.IsNull || y.IsNull)
\r
317 return SqlByte.Null;
\r
320 throw new DivideByZeroException("Divide by zero error encountered.");
\r
322 int mod = x._value % y._value;
\r
323 return new SqlByte((byte)mod);
\r
327 * The multiplication operator computes the product of the two SqlByte operands.
\r
328 * @param x A SqlByte instance
\r
329 * @param y A SqlByte instance
\r
330 * @return The product of the two SqlByte operands.
\r
332 public static SqlByte Multiply(SqlByte x, SqlByte y)
\r
334 if (x.IsNull || y.IsNull)
\r
335 return SqlByte.Null;
\r
337 int res = x._value * y._value;
\r
340 throw new OverflowException("Arithmetic Overflow.");
\r
342 return new SqlByte((byte)res);
\r
346 * Compares two instances of SqlByte to determine if they are equal.
\r
347 * @param x A SqlByte instance
\r
348 * @param y A SqlByte 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 SqlByte is null, the Value of the SqlBoolean will be Null.
\r
352 public static SqlBoolean NotEquals(SqlByte x, SqlByte 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 SqlByte operand.
\r
365 * @param x A SqlByte instance
\r
366 * @return A SqlByte instance whose Value property contains the ones complement of the SqlByte parameter.
\r
368 public static SqlByte OnesComplement(SqlByte x)
\r
371 return SqlByte.Null;
\r
373 int res = x._value ^ 0xFF; /*@TODO Ma ze ??? */
\r
375 return new SqlByte((byte)res);
\r
379 * Converts the String representation of a number to its byte equivalent.
\r
380 * @param s The String to be parsed.
\r
381 * @return A SqlByte containing the value represented by the String.
\r
383 public static SqlByte Parse(String s)
\r
385 byte val = byte.Parse(s);
\r
386 return new SqlByte(val);
\r
390 * The subtraction operator the second SqlByte operand from the first.
\r
391 * @param x A SqlByte instance
\r
392 * @param y A SqlByte instance
\r
393 * @return The results of the subtraction operation.
\r
395 public static SqlByte Subtract(SqlByte x, SqlByte y)
\r
397 if (x.IsNull || y.IsNull)
\r
398 return SqlByte.Null;
\r
400 int res = x._value - y._value;
\r
403 throw new OverflowException("Arithmetic Overflow.");
\r
405 return new SqlByte((byte)res);
\r
410 * Converts this SqlByte instance to SqlBoolean.
\r
411 * @return A SqlBoolean instance whose Value will be True if the SqlByte instance's Value is non-zero,
\r
412 * False if the SqlByte is zero
\r
413 * and Null if the SqlByte instance is Null.
\r
415 public SqlBoolean ToSqlBoolean()
\r
418 return SqlBoolean.Null;
\r
420 return new SqlBoolean(Value);
\r
424 * Converts this SqlByte instance to SqlDecimal.
\r
425 * @return A SqlDecimal instance whose Value equals the Value of this SqlByte instance.
\r
427 public SqlDecimal ToSqlDecimal()
\r
430 return SqlDecimal.Null;
\r
432 return new SqlDecimal(Value);
\r
436 * Converts this SqlByte instance to SqlDouble.
\r
437 * @return A SqlDouble instance whose Value equals the Value of this SqlByte instance.
\r
439 public SqlDouble ToSqlDouble()
\r
442 return SqlDouble.Null;
\r
444 return new SqlDouble(Value);
\r
448 * Converts this SqlByte instance to SqlSingle.
\r
449 * @return A SqlSingle instance whose Value equals the Value of this SqlByte instance.
\r
451 public SqlSingle ToSqlSingle()
\r
454 return SqlSingle.Null;
\r
456 return new SqlSingle(Value);
\r
460 * Converts this SqlByte structure to SqlInt32.
\r
461 * @return A SqlInt32 structure whose Value equals the Value of this SqlByte structure.
\r
463 public SqlInt32 ToSqlInt32()
\r
466 return SqlInt32.Null;
\r
468 return new SqlInt32(Value);
\r
472 * Converts this SqlByte structure to SqlInt32.
\r
473 * @return A SqlInt32 structure whose Value equals the Value of this SqlByte structure.
\r
475 public SqlInt64 ToSqlInt64()
\r
478 return SqlInt64.Null;
\r
480 return new SqlInt64(Value);
\r
484 * Converts this SqlByte structure to SqlInt16.
\r
485 * @return A SqlInt16 structure whose Value equals the Value of this SqlByte structure.
\r
487 public SqlInt16 ToSqlInt16()
\r
490 return SqlInt16.Null;
\r
492 return new SqlInt16(Value);
\r
496 * Converts this SqlByte instance to SqlDecimal.
\r
497 * @return A SqlDecimal instance whose Value equals the Value of this SqlByte instance.
\r
499 public SqlMoney ToSqlMoney()
\r
502 return SqlMoney.Null;
\r
504 return new SqlMoney(Value);
\r
508 * Converts this SqlByte structure to SqlString.
\r
509 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlByte structure.
\r
511 public SqlString ToSqlString()
\r
514 return SqlString.Null;
\r
516 return new SqlString(_value.ToString());
\r
520 * Performs a bitwise exclusive-OR operation on the supplied parameters.
\r
521 * @param x A SqlByte instance
\r
522 * @param y A SqlByte instance
\r
523 * @return The results of the XOR operation.
\r
525 public static SqlByte Xor(SqlByte x, SqlByte y)
\r
527 if (x.IsNull || y.IsNull)
\r
528 return SqlByte.Null;
\r
530 int res = x._value ^ y._value;
\r
532 return new SqlByte((byte)res);
\r
535 public override String ToString()
\r
540 return _value.ToString();
\r
543 public static SqlByte op_Implicit(byte x)
\r
545 return new SqlByte(x);
\r
548 public static int op_Explicit(SqlByte x)
\r
553 public static SqlByte op_OnesComplement(SqlByte x)
\r
555 return OnesComplement(x);
\r
558 public static SqlByte op_Addition(SqlByte x, SqlByte y)
\r
563 public static SqlByte op_Subtraction(SqlByte x, SqlByte y)
\r
565 return Subtract(x, y);
\r
568 public static SqlByte op_Multiply(SqlByte x, SqlByte y)
\r
570 return Multiply(x, y);
\r
573 public static SqlByte op_Division(SqlByte x, SqlByte y)
\r
575 return Divide(x, y);
\r
578 public static SqlByte op_Modulus(SqlByte x, SqlByte y)
\r
583 public static SqlByte op_BitwiseAnd(SqlByte x, SqlByte y)
\r
585 return BitwiseAnd(x, y);
\r
588 public static SqlByte op_BitwiseOr(SqlByte x, SqlByte y)
\r
590 return BitwiseOr(x, y);
\r
593 public static SqlByte op_ExclusiveOr(SqlByte x, SqlByte y)
\r
598 public static SqlByte op_Explicit(SqlBoolean x)
\r
601 return SqlByte.Null;
\r
603 return new SqlByte((byte)x.ByteValue);
\r
606 public static SqlByte op_Explicit(SqlMoney x)
\r
609 return SqlByte.Null;
\r
611 int val = x.ToSqlInt32().Value;
\r
612 if (val < 0 || val > 255)
\r
613 throw new OverflowException("Arithmetic Overflow.");
\r
615 return new SqlByte((byte)val);
\r
618 public static SqlByte op_Explicit(SqlInt16 x)
\r
621 return SqlByte.Null;
\r
623 short val = x.Value;
\r
624 if (val < 0 || val > 255)
\r
625 throw new OverflowException("Arithmetic Overflow.");
\r
627 return new SqlByte((byte)val);
\r
630 public static SqlByte op_Explicit(SqlInt32 x)
\r
633 return SqlByte.Null;
\r
636 if (val < 0 || val > 255)
\r
637 throw new OverflowException("Arithmetic Overflow.");
\r
639 return new SqlByte((byte)val);
\r
642 public static SqlByte op_Explicit(SqlInt64 x)
\r
645 return SqlByte.Null;
\r
647 long val = x.Value;
\r
648 if (val < 0 || val > 255)
\r
649 throw new OverflowException("Arithmetic Overflow.");
\r
651 return new SqlByte((byte)val);
\r
654 public static SqlByte op_Explicit(SqlSingle x)
\r
657 return SqlByte.Null;
\r
659 float val = x.Value;
\r
660 if (val < 0 || val > 255)
\r
661 throw new OverflowException("Arithmetic Overflow.");
\r
663 return new SqlByte((byte)val);
\r
666 public static SqlByte op_Explicit(SqlDouble x)
\r
669 return SqlByte.Null;
\r
671 double val = x.Value;
\r
672 if (val < 0 || val > 255)
\r
673 throw new OverflowException("Arithmetic Overflow.");
\r
675 return new SqlByte((byte)val);
\r
678 public static SqlByte op_Explicit(SqlDecimal x)
\r
681 return SqlByte.Null;
\r
683 double val = x.ToDouble();
\r
684 if (val < 0 || val > 255)
\r
685 throw new OverflowException("Arithmetic Overflow.");
\r
687 return new SqlByte((byte)val);
\r
690 public static SqlByte op_Explicit(SqlString x)
\r
693 return SqlByte.Null;
\r
695 return Parse(x.Value);
\r
698 public static SqlBoolean op_Equality(SqlByte x, SqlByte y)
\r
700 return Equals(x, y);
\r
703 public static SqlBoolean op_Inequality(SqlByte x, SqlByte y)
\r
705 return NotEquals(x, y);
\r
708 public static SqlBoolean op_LessThan(SqlByte x, SqlByte y)
\r
710 return LessThan(x, y);
\r
713 public static SqlBoolean op_GreaterThan(SqlByte x, SqlByte y)
\r
715 return GreaterThan(x, y);
\r
718 public static SqlBoolean op_LessThanOrEqual(SqlByte x, SqlByte y)
\r
720 return LessThanOrEqual(x, y);
\r
723 public static SqlBoolean op_GreaterThanOrEqual(SqlByte x, SqlByte y)
\r
725 return GreaterThanOrEqual(x, y);
\r
728 public static void main(String[] args)
\r
730 SqlByte b = new SqlByte((byte)1);
\r
731 Console.WriteLine(b);
\r
733 Console.WriteLine(OnesComplement(b));
\r