1
|
/*
|
2
|
* Coding User-Defined Types:
|
3
|
* http://technet.microsoft.com/en-us/library/ms131069.aspx
|
4
|
*
|
5
|
*
|
6
|
* TODO: add compatibility with normal SQL datatypes ? :
|
7
|
* INSERT INTO [p2j_wrappers] VALUES (1) -----> 'Operand type clash: int is incompatible with P2JInt64'
|
8
|
*/
|
9
|
using System;
|
10
|
using System.Data.SqlTypes;
|
11
|
using Microsoft.SqlServer.Server;
|
12
|
using System.Runtime.InteropServices;
|
13
|
|
14
|
|
15
|
|
16
|
[Serializable()]
|
17
|
//[SqlUserDefinedType(Format.Native)] // without 5: user-defined serialization
|
18
|
[SqlUserDefinedType(Format.UserDefined, IsByteOrdered = true, MaxByteSize = 9)] // with 5: user-defined serialization
|
19
|
//[StructLayout(LayoutKind.Sequential)] // TODO: is this attr useful ?
|
20
|
public class P2JInt64 : INullable, IBinarySerialize
|
21
|
{
|
22
|
// 1. --------------------------- private data ------------------------------------------------
|
23
|
private bool is_Null;
|
24
|
private com.goldencode.p2j.util.int64 int64_value;
|
25
|
|
26
|
// 2. --------------------------- handling null values ----------------------------------------
|
27
|
public bool IsNull
|
28
|
{
|
29
|
get
|
30
|
{
|
31
|
return is_Null;
|
32
|
}
|
33
|
}
|
34
|
|
35
|
public static P2JInt64 Null
|
36
|
{
|
37
|
get
|
38
|
{
|
39
|
P2JInt64 int64 = new P2JInt64();
|
40
|
int64.is_Null = true;
|
41
|
return int64;
|
42
|
}
|
43
|
}
|
44
|
|
45
|
// 3. --------------------------- handling conversion to/from string --------------------------
|
46
|
public override string ToString()
|
47
|
{
|
48
|
if (this.IsNull)
|
49
|
{
|
50
|
return "NULL";
|
51
|
}
|
52
|
else
|
53
|
{
|
54
|
// return bool_value.ToString(); -- this will return Yes / No, we rather prefer 0 / 1
|
55
|
return int64_value.longValue().ToString();
|
56
|
}
|
57
|
}
|
58
|
|
59
|
public static P2JInt64 Parse(SqlString s)
|
60
|
{
|
61
|
if (s.IsNull)
|
62
|
{
|
63
|
return Null;
|
64
|
}
|
65
|
|
66
|
// Parse input string here
|
67
|
string str = Convert.ToString(s).Trim().ToLower();
|
68
|
|
69
|
P2JInt64 int64 = new P2JInt64();
|
70
|
int64.is_Null = false;
|
71
|
int64.int64_value = new com.goldencode.p2j.util.int64(str);
|
72
|
|
73
|
return int64;
|
74
|
}
|
75
|
|
76
|
// 4. --------------------------- UDT Method example ------------------------------------------
|
77
|
[SqlMethod(IsDeterministic = true, IsMutator = false)]
|
78
|
public P2JInt64 P2J_Add(P2JInt64 other)
|
79
|
{
|
80
|
P2JInt64 ret = new P2JInt64();
|
81
|
ret.is_Null = false;
|
82
|
ret.int64_value = com.goldencode.p2j.util.MathOps.plus(int64_value, other.int64_value);
|
83
|
return ret;
|
84
|
}
|
85
|
|
86
|
[SqlMethod(IsDeterministic = true, IsMutator = false)]
|
87
|
public P2JInt64 P2J_Add(long other)
|
88
|
{
|
89
|
P2JInt64 ret = new P2JInt64();
|
90
|
ret.is_Null = false;
|
91
|
ret.int64_value = com.goldencode.p2j.util.MathOps.plus(int64_value, other);
|
92
|
return ret;
|
93
|
}
|
94
|
|
95
|
[SqlMethod(IsDeterministic = true, IsMutator = false)]
|
96
|
public long value()
|
97
|
{
|
98
|
return int64_value.longValue();
|
99
|
}
|
100
|
|
101
|
|
102
|
// 5. --------------------------- user-defined serialization ----------------------------------
|
103
|
// IBinarySerialize methods
|
104
|
public void Write(System.IO.BinaryWriter w)
|
105
|
{
|
106
|
w.Write((byte) (this.IsNull ? 1 : 0));
|
107
|
|
108
|
if (!this.IsNull)
|
109
|
{
|
110
|
w.Write(this.int64_value.longValue()); // 8 bytes
|
111
|
}
|
112
|
}
|
113
|
|
114
|
public void Read(System.IO.BinaryReader r)
|
115
|
{
|
116
|
byte asByte = r.ReadByte();
|
117
|
|
118
|
is_Null = (asByte == 1);
|
119
|
if (is_Null)
|
120
|
{
|
121
|
int64_value = null;
|
122
|
}
|
123
|
else
|
124
|
{
|
125
|
int64_value = new com.goldencode.p2j.util.int64(r.ReadInt64());
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
|