2 <clause number="18.4.2" title="Database boolean type" informative="true">
3 <paragraph>The DBBool struct below implements a three-valued logical type. The possible values of this type are DBBool.True, DBBool.False, and DBBool.Null, where the Null member indicates an unknown value. Such three-valued logical types are commonly used in databases. <code_example><![CDATA[
7 // The three possible DBBool values.
8 public static readonly DBBool Null = new DBBool(0);
9 public static readonly DBBool False = new DBBool(-1);
10 public static readonly DBBool True = new DBBool(1);
11 // Private field that stores -1, 0, 1 for False, Null, True.
13 // Private instance constructor. The value parameter must be -1, 0, or 1.
16 this.value = (sbyte)value;
18 // Properties to examine the value of a DBBool. Return true if this
19 // DBBool has the given value, false otherwise.
20 public bool IsNull { get { return value == 0; } }
21 public bool IsFalse { get { return value < 0; } }
22 public bool IsTrue { get { return value > 0; } }
23 // Implicit conversion from bool to DBBool. Maps true to DBBool.True
25 // and false to DBBool.False.
26 public static implicit operator DBBool(bool x) {
27 return x? True: False;
29 // Explicit conversion from DBBool to bool. Throws an exception if the
30 // given DBBool is Null, otherwise returns true or false.
31 public static explicit operator bool(DBBool x) {
32 if (x.value == 0) throw new InvalidOperationException();
35 // Equality operator. Returns Null if either operand is Null,
37 // otherwise returns True or False.
38 public static DBBool operator ==(DBBool x, DBBool y) {
39 if (x.value == 0 || y.value == 0) return Null;
40 return x.value == y.value? True: False;
42 // Inequality operator. Returns Null if either operand is Null,
44 // otherwise returns True or False.
45 public static DBBool operator !=(DBBool x, DBBool y) {
46 if (x.value == 0 || y.value == 0) return Null;
47 return x.value != y.value? True: False;
49 // Logical negation operator. Returns True if the operand is False,
51 // Null if the operand is Null, or False if the operand is True.
52 public static DBBool operator !(DBBool x) {
53 return new DBBool(-x.value);
55 // Logical AND operator. Returns False if either operand is False,
56 // otherwise Null if either operand is Null, otherwise True.
57 public static DBBool operator &(DBBool x, DBBool y) {
58 return new DBBool(x.value < y.value? x.value: y.value);
60 // Logical OR operator. Returns True if either operand is True,
62 // otherwise Null if either operand is Null, otherwise False.
63 public static DBBool operator |(DBBool x, DBBool y) {
64 return new DBBool(x.value > y.value? x.value: y.value);
66 // Definitely true operator. Returns true if the operand is True,
69 public static bool operator true(DBBool x) {
72 // Definitely false operator. Returns true if the operand is False,
75 public static bool operator false(DBBool x) {
79 ]]></code_example></paragraph>
80 <paragraph>End of informative text. </paragraph>