Project

General

Profile

IkvmLogical.cs

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

Download (3.17 KB)

 
1

    
2
using System;
3
using System.Data.SqlTypes;
4
using Microsoft.SqlServer.Server;
5
using System.Runtime.InteropServices;
6

    
7

    
8

    
9
[Serializable()]
10
//[SqlUserDefinedType(Format.Native)]                                           // without 5: user-defined serialization 
11
[SqlUserDefinedType(Format.UserDefined, IsByteOrdered = true, MaxByteSize = 1)] // with 5: user-defined serialization 
12
//[StructLayout(LayoutKind.Sequential)]                                         // TODO: is this attr useful ?
13
public class IkvmLogical : INullable, IBinarySerialize
14
{
15
    // 1. --------------------------- private data ------------------------------------------------
16
    private bool is_Null;
17
    private java.lang.Boolean bool_value;
18

    
19
    // 2. --------------------------- handling null values ----------------------------------------
20
    public bool IsNull
21
    {
22
        get
23
        {
24
            return is_Null;
25
        }
26
    }
27

    
28
    public static IkvmLogical Null
29
    {
30
        get
31
        {
32
            IkvmLogical logical = new IkvmLogical();
33
            logical.is_Null = true;
34
            return logical;
35
        }
36
    }
37

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

    
51
    public static IkvmLogical Parse(SqlString s)
52
    {
53
        if (s.IsNull)
54
        {
55
            return Null;
56
        }
57

    
58
        // Parse input string here
59
        string str = Convert.ToString(s).ToLower();
60

    
61
        IkvmLogical logical = new IkvmLogical();
62
        logical.is_Null = false;
63

    
64
        if (str == "0")
65
        {
66
            logical.bool_value = java.lang.Boolean.FALSE;
67
        }
68
        else if (str == "1")
69
        {
70
            logical.bool_value = java.lang.Boolean.TRUE;
71
        }
72
        else
73
        {
74
            logical.bool_value = java.lang.Boolean.valueOf(str);
75
        }
76

    
77
        return logical;
78
    }
79

    
80
    // 4. --------------------------- UDT Method example ------------------------------------------
81
    [SqlMethod(IsDeterministic = true, IsMutator = false)]
82
    public IkvmLogical P2J_And(IkvmLogical other)
83
    {
84
        IkvmLogical ret = new IkvmLogical();
85
        ret.bool_value = java.lang.Boolean.valueOf(bool_value.booleanValue() & other.bool_value.booleanValue());
86
        return ret;
87
    }
88

    
89
    [SqlMethod(IsDeterministic = true, IsMutator = false)]
90
    public bool value()
91
    {
92
        return bool_value.booleanValue();
93
    }
94

    
95

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

    
114
    public void Read(System.IO.BinaryReader r)
115
    {
116
        byte asByte = r.ReadByte();
117

    
118
        is_Null = (asByte == 2);
119
        if (!is_Null)
120
        {
121
            bool_value = java.lang.Boolean.valueOf(asByte == 1);
122
        }
123
    }
124

    
125
}
126