forked from kamrulkonok/ULB-H417-PGchess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess--1.0.sql
292 lines (234 loc) · 8.7 KB
/
chess--1.0.sql
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION chess" to load this file. \quit
/************************************************************************************************************
Chessboard to represent FEN
************************************************************************************************************/
/******************************************************************************
* Input/Output
******************************************************************************/
CREATE OR REPLACE FUNCTION chessboard_in(cstring)
RETURNS chessboard
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION chessboard_out(chessboard)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION chessboard_recv(internal)
RETURNS chessboard
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION chessboard_send(chessboard)
RETURNS bytea
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
/******************************************************************************
* Create the datatype
******************************************************************************/
CREATE TYPE chessboard (
internallength = 69,
input = chessboard_in,
output = chessboard_out,
receive = chessboard_recv,
send = chessboard_send,
alignment = char
);
/******************************************************************************
* Operators
******************************************************************************/
CREATE OR REPLACE FUNCTION chessboard_eq(chessboard, chessboard)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR = (
LEFTARG = chessboard, RIGHTARG = chessboard,
PROCEDURE = chessboard_eq,
COMMUTATOR = =, NEGATOR = <>
);
CREATE OR REPLACE FUNCTION chessboard_neq(chessboard, chessboard)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR <> (
LEFTARG = chessboard, RIGHTARG = chessboard,
PROCEDURE = chessboard_neq,
COMMUTATOR = <>, NEGATOR = =
);
CREATE OR REPLACE FUNCTION chessboard_lt(chessboard, chessboard)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR < (
LEFTARG = chessboard, RIGHTARG = chessboard,
PROCEDURE = chessboard_lt,
COMMUTATOR = >, NEGATOR = >=
);
CREATE OR REPLACE FUNCTION chessboard_lte(chessboard, chessboard)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR <= (
LEFTARG = chessboard, RIGHTARG = chessboard,
PROCEDURE = chessboard_lte,
COMMUTATOR = >=, NEGATOR = <
);
CREATE OR REPLACE FUNCTION chessboard_gt(chessboard, chessboard)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR > (
LEFTARG = chessboard, RIGHTARG = chessboard,
PROCEDURE = chessboard_gt,
COMMUTATOR = <, NEGATOR = <=
);
CREATE OR REPLACE FUNCTION chessboard_gte(chessboard, chessboard)
RETURNS boolean
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR >= (
LEFTARG = chessboard, RIGHTARG = chessboard,
PROCEDURE = chessboard_gte,
COMMUTATOR = <=, NEGATOR = >
);
CREATE FUNCTION chessboard_to_text(chessboard) RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION text_to_chessboard(text) RETURNS chessboard
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE CAST (chessboard AS text)
WITH FUNCTION chessboard_to_text(chessboard)
AS IMPLICIT;
CREATE CAST (text AS chessboard)
WITH FUNCTION text_to_chessboard(text)
AS IMPLICIT;
/************************************************************************************************************
Chessgame to represent SAN and FEN states
************************************************************************************************************/
/******************************************************************************
* Input/Output
******************************************************************************/
CREATE FUNCTION chessgame_in(cstring) RETURNS chessgame
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_out(chessgame) RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_recv(internal) RETURNS chessgame
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_send(chessgame) RETURNS bytea
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
/******************************************************************************
* Create the datatype
******************************************************************************/
CREATE TYPE chessgame (
internallength = -1,
input = chessgame_in,
output = chessgame_out,
receive = chessgame_recv,
send = chessgame_send
);
CREATE FUNCTION text_to_chessgame(text) RETURNS chessgame
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
CREATE CAST (text AS chessgame)
WITH FUNCTION text_to_chessgame(text)
AS IMPLICIT;
/******************************************************************************
* Functions
******************************************************************************/
CREATE FUNCTION getBoard(chessgame, integer)
RETURNS text AS 'MODULE_PATHNAME', 'getBoard'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION getFirstMoves(chessgame, integer)
RETURNS text AS 'MODULE_PATHNAME', 'getFirstMoves'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
/******************************************************************************
* B Tree Index for hasOpening
******************************************************************************/
CREATE FUNCTION chessgame_lt(chessgame, chessgame)
RETURNS boolean
AS 'MODULE_PATHNAME', 'chessgame_lt'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_lte(chessgame, chessgame)
RETURNS boolean
AS 'MODULE_PATHNAME', 'chessgame_lte'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_eq(chessgame, chessgame)
RETURNS boolean
AS 'MODULE_PATHNAME', 'chessgame_eq'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_gte(chessgame, chessgame)
RETURNS boolean
AS 'MODULE_PATHNAME', 'chessgame_gte'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_gt(chessgame, chessgame)
RETURNS boolean
AS 'MODULE_PATHNAME', 'chessgame_gt'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION chessgame_cmp(chessgame, chessgame)
RETURNS int
AS 'MODULE_PATHNAME', 'chessgame_cmp'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR = (
LEFTARG = chessgame,
RIGHTARG = chessgame,
PROCEDURE = chessgame_eq,
COMMUTATOR = =,
NEGATOR = <>
);
CREATE OPERATOR < (
LEFTARG = chessgame,
RIGHTARG = chessgame,
PROCEDURE = chessgame_lt,
COMMUTATOR = >,
NEGATOR = >=
);
CREATE OPERATOR > (
LEFTARG = chessgame,
RIGHTARG = chessgame,
PROCEDURE = chessgame_gt,
COMMUTATOR = <,
NEGATOR = <=
);
CREATE OPERATOR <= (
LEFTARG = chessgame,
RIGHTARG = chessgame,
PROCEDURE = chessgame_lte,
COMMUTATOR = >=,
NEGATOR = >
);
CREATE OPERATOR >= (
LEFTARG = chessgame,
RIGHTARG = chessgame,
PROCEDURE = chessgame_gte,
COMMUTATOR = <=,
NEGATOR = <
);
CREATE OPERATOR CLASS chessgame_ops
DEFAULT FOR TYPE chessgame USING btree AS
OPERATOR 1 < ,
OPERATOR 2 <= ,
OPERATOR 3 = ,
OPERATOR 4 >= ,
OPERATOR 5 > ,
FUNCTION 1 chessgame_cmp(chessgame, chessgame);
CREATE OR REPLACE FUNCTION hasOpening(game1 chessgame, game2 chessgame)
RETURNS boolean AS $$
SELECT game1::text LIKE game2::text || '%';
$$ LANGUAGE sql IMMUTABLE PARALLEL SAFE;
-- Function to display the Array of chessgame states using `chessgame_get_all_states` as text[]
CREATE FUNCTION getAllStates(chessgame)
RETURNS text[] AS 'MODULE_PATHNAME', 'chessgame_get_all_states'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION CREATE_QUERIES(query TEXT, n INT)
RETURNS TEXT[] AS $$
SELECT array_agg(i || ':' || query)
FROM generate_series(0, n) as g(i)
$$ LANGUAGE sql IMMUTABLE;
CREATE OR REPLACE FUNCTION hasBoard(game chessgame, fen text, n INT)
RETURNS BOOLEAN AS $$
SELECT getAllStates(game) && CREATE_QUERIES(fen, n);
$$ LANGUAGE sql IMMUTABLE;