Project

General

Profile

P2JLogical.cs

Ovidiu Maxiniuc, 02/27/2014 03:26 PM

Download (3.5 KB)

 
1
/*
2
 * TODO: add compatibility with normal SQL datatypes ? :
3
 * INSERT INTO [p2j_wrappers] VALUES (1) -----> 'Operand type clash: int is incompatible with P2JLogical'
4
 */
5
using System;
6
using System.Data.SqlTypes;
7
using Microsoft.SqlServer.Server;
8
using System.Runtime.InteropServices;
9

    
10

    
11

    
12
[Serializable()]
13
//[SqlUserDefinedType(Format.Native)]                                           // without 5: user-defined serialization 
14
[SqlUserDefinedType(Format.UserDefined, IsByteOrdered = true, MaxByteSize = 1)] // with 5: user-defined serialization 
15
//[StructLayout(LayoutKind.Sequential)]                                         // TODO: is this attr useful ?
16
public class P2JLogical : INullable, IBinarySerialize
17
{
18
    // 1. --------------------------- private data ------------------------------------------------
19
    private bool is_Null;
20
    private com.goldencode.p2j.util.logical bool_value;
21

    
22
    // 2. --------------------------- handling null values ----------------------------------------
23
    public bool IsNull
24
    {
25
        get
26
        {
27
            return is_Null;
28
        }
29
    }
30

    
31
    public static P2JLogical Null
32
    {
33
        get
34
        {
35
            P2JLogical logical = new P2JLogical();
36
            logical.is_Null = true;
37
            return logical;
38
        }
39
    }
40

    
41
    // 3. --------------------------- handling conversion to/from string --------------------------
42
    public override string ToString()
43
    {
44
        if (this.IsNull)
45
        {
46
            return "NULL";
47
        }
48
        else
49
        {
50
            return bool_value.ToString();
51
        }
52
    }
53

    
54
    public static P2JLogical Parse(SqlString s)
55
    {
56
        if (s.IsNull)
57
        {
58
            return Null;
59
        }
60

    
61
        // Parse input string here
62
        string str = Convert.ToString(s).ToLower();
63

    
64
        P2JLogical logical = new P2JLogical();
65
        logical.is_Null = false;
66

    
67
        if (str == "0")
68
        {
69
            logical.bool_value = new com.goldencode.p2j.util.logical(false);
70
        }
71
        else if (str == "1")
72
        {
73
            logical.bool_value = new com.goldencode.p2j.util.logical(true);
74
        }
75
        else
76
        {
77
            logical.bool_value = new com.goldencode.p2j.util.logical(str);
78
        }
79

    
80
        return logical;
81
    }
82

    
83
    // 4. --------------------------- UDT Method example ------------------------------------------
84
    [SqlMethod(IsDeterministic = true, IsMutator = false)]
85
    public P2JLogical P2J_And(P2JLogical other)
86
    {
87
        P2JLogical ret = new P2JLogical();
88
        ret.is_Null = false;
89
        ret.bool_value = new com.goldencode.p2j.util.logical(bool_value.booleanValue() & other.bool_value.booleanValue());
90
        return ret;
91
    }
92

    
93
    [SqlMethod(IsDeterministic = true, IsMutator = false)]
94
    public bool value()
95
    {
96
        return bool_value.booleanValue();
97
    }
98

    
99

    
100
    // 5. --------------------------- user-defined serialization ----------------------------------
101
    // IBinarySerialize methods
102
    public void Write(System.IO.BinaryWriter w)
103
    {
104
        if (this.IsNull)
105
        {
106
            w.Write((byte) 2);
107
        }
108
        else if (this.bool_value.booleanValue())
109
        {
110
            w.Write((byte) 1);
111
        }
112
        else
113
        {
114
            w.Write((byte) 0);
115
        }
116
    }
117

    
118
    public void Read(System.IO.BinaryReader r)
119
    {
120
        byte asByte = r.ReadByte();
121

    
122
        is_Null = (asByte == 2);
123
        if (is_Null)
124
        {
125
            bool_value = null;
126
        }
127
        else
128
        {
129
            bool_value = new com.goldencode.p2j.util.logical(asByte == 1);
130
        }
131
    }
132

    
133
}
134