-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvaguebool_pg.c
242 lines (204 loc) · 6.19 KB
/
vaguebool_pg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**********************************************************************
*
* VagueGeometry - Vague Spatial Objects for PostgreSQL
* http://gbd.dc.ufscar.br/vaguegeometry/
*
* Copyright 2013-2015 Anderson Chaves Carniel <[email protected]>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the COPYING file.
*
* Fully developed by Anderson Chaves Carniel
*
**********************************************************************/
/*************************************************
* Handling the VagueBool objects.
*
**************************************************/
#include "postgres.h"
#include "fmgr.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "libvgeom.h"
/* basic functions for postgres */
Datum VB_in(PG_FUNCTION_ARGS);
Datum VB_out(PG_FUNCTION_ARGS);
Datum VB_send(PG_FUNCTION_ARGS);
Datum VB_recv(PG_FUNCTION_ARGS);
/* cast required */
Datum VB_enforce_cast(PG_FUNCTION_ARGS);
/*
* OPERATORS FOR VAGUEBOOL
*/
/* left operators */
Datum VB_not_op(PG_FUNCTION_ARGS);
Datum VB_and_VB(PG_FUNCTION_ARGS);
Datum VB_or_VB(PG_FUNCTION_ARGS);
Datum VB_maybe_op(PG_FUNCTION_ARGS);
Datum VB_true_op(PG_FUNCTION_ARGS);
Datum VB_false_op(PG_FUNCTION_ARGS);
Datum VB_maybe_maybe_op(PG_FUNCTION_ARGS);
/*
* TRUE is 1
* FALSE is 0
* MAYBE is 2
*/
PG_FUNCTION_INFO_V1(VB_in);
Datum VB_in(PG_FUNCTION_ARGS) {
char *input = PG_GETARG_CSTRING(0);
VAGUEBOOL *result;
if(strcasecmp(input, "TRUE")==0) {
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
result->vbool = VG_TRUE;
PG_RETURN_POINTER(result);
} else if(strcasecmp(input, "FALSE")==0) {
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
result->vbool = VG_FALSE;
PG_RETURN_POINTER(result);
} else if(strcasecmp(input, "MAYBE") ==0 ) {
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
result->vbool = VG_MAYBE;
PG_RETURN_POINTER(result);
} else { //error..
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unexpected value for vague bool - \"%s\"", input)));
}
}
/*
* 1 is 'TRUE'
* 0 is 'FALSE'
* 2 is 'MAYBE'
*/
PG_FUNCTION_INFO_V1(VB_out);
Datum VB_out(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
char *result;
result = (char*) palloc(5);
if(vb->vbool==VG_TRUE) {
snprintf(result, 5, "TRUE");
} else if(vb->vbool==VG_FALSE) {
snprintf(result, 6, "FALSE");
} else if(vb->vbool==VG_MAYBE) {
snprintf(result, 6, "MAYBE");
}
PG_RETURN_CSTRING(result);
}
PG_FUNCTION_INFO_V1(VB_recv);
Datum VB_recv(PG_FUNCTION_ARGS) {
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
VAGUEBOOL *result;
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
result->vbool = pq_getmsgbyte(buf);
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(VB_send);
Datum VB_send(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendbyte(&buf, vb->vbool);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* required casting */
PG_FUNCTION_INFO_V1(VB_enforce_cast);
Datum VB_enforce_cast(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb;
vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
if(vb->vbool==VG_TRUE)
PG_RETURN_BOOL(TRUE);
else if(vb->vbool==VG_FALSE)
PG_RETURN_BOOL(FALSE);
else
PG_RETURN_NULL();
}
/* LEFT OPERATORS */
//like the truth table
PG_FUNCTION_INFO_V1(VB_not_op);
Datum VB_not_op(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb;
VAGUEBOOL *result;
vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
if(vb->vbool==VG_TRUE)
result->vbool = VG_FALSE;
else if(vb->vbool == VG_FALSE)
result->vbool = VG_TRUE;
else
result->vbool = VG_MAYBE;
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(VB_and_VB);
Datum VB_and_VB(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb1 = (VAGUEBOOL*) PG_GETARG_POINTER(0);
VAGUEBOOL *vb2 = (VAGUEBOOL*) PG_GETARG_POINTER(1);
VAGUEBOOL *result;
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
if(vb1->vbool==VG_TRUE && vb2->vbool==VG_TRUE)
result->vbool = VG_TRUE;
else if(vb1->vbool==VG_TRUE && vb2->vbool==VG_MAYBE)
result->vbool = VG_MAYBE;
else if(vb1->vbool==VG_TRUE && vb2->vbool==VG_FALSE)
result->vbool = VG_FALSE;
else if((vb1->vbool==VG_MAYBE) && (vb2->vbool == VG_MAYBE || vb2->vbool==VG_TRUE))
result->vbool = VG_MAYBE;
else if(vb1->vbool == VG_MAYBE && vb2->vbool == VG_FALSE)
result->vbool = VG_FALSE;
else
result->vbool = VG_FALSE;
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(VB_or_VB);
Datum VB_or_VB(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb1 = (VAGUEBOOL*) PG_GETARG_POINTER(0);
VAGUEBOOL *vb2 = (VAGUEBOOL*) PG_GETARG_POINTER(1);
VAGUEBOOL *result;
result = (VAGUEBOOL*) palloc(sizeof(VAGUEBOOL));
if(vb1->vbool==VG_TRUE || vb2->vbool == VG_TRUE)
result->vbool = VG_TRUE;
else if(vb1->vbool==VG_MAYBE || vb2->vbool == VG_MAYBE)
result->vbool = VG_MAYBE;
else if(vb1->vbool == VG_FALSE && vb2->vbool == VG_FALSE)
result->vbool = VG_FALSE;
PG_RETURN_POINTER(result);
}
//only true if, only if vague bool is really true
PG_FUNCTION_INFO_V1(VB_true_op);
Datum VB_true_op(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb;
vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
if(vb->vbool==VG_TRUE)
PG_RETURN_BOOL(TRUE);
else
PG_RETURN_BOOL(FALSE);
}
//only true if, only if vague bool is really false
PG_FUNCTION_INFO_V1(VB_false_op);
Datum VB_false_op(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb;
vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
if(vb->vbool==VG_FALSE)
PG_RETURN_BOOL(TRUE);
else
PG_RETURN_BOOL(FALSE);
}
//only true if, only if vague bool is maybe or true
PG_FUNCTION_INFO_V1(VB_maybe_op);
Datum VB_maybe_op(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb;
vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
if(vb->vbool==VG_TRUE || vb->vbool==VG_MAYBE)
PG_RETURN_BOOL(TRUE);
else
PG_RETURN_BOOL(FALSE);
}
//only true if, only if vague bool is maybe
PG_FUNCTION_INFO_V1(VB_maybe_maybe_op);
Datum VB_maybe_maybe_op(PG_FUNCTION_ARGS) {
VAGUEBOOL *vb;
vb = (VAGUEBOOL*) PG_GETARG_POINTER(0);
if(vb->vbool==VG_MAYBE)
PG_RETURN_BOOL(TRUE);
else
PG_RETURN_BOOL(FALSE);
}