-
Notifications
You must be signed in to change notification settings - Fork 0
/
asl_db_clean.pgsql
527 lines (361 loc) · 11.6 KB
/
asl_db_clean.pgsql
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET search_path = public, pg_catalog;
--
-- Name: createclient(); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION createclient() RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
nextval INTEGER;
BEGIN
SELECT nextval('client_id_seq') INTO nextval;
INSERT INTO clients (client_id) VALUES (nextval);
RETURN nextval;
END
$$;
ALTER FUNCTION public.createclient() OWNER TO asl_pg;
--
-- Name: createmessage(integer, integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION createmessage(sender integer, receiver integer, queue integer, message character varying) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
messid integer;
countQ integer;
ok boolean;
BEGIN
SELECT count(*) INTO countQ FROM queues WHERE queue_id = queue;
IF countQ <> 0 THEN
INSERT INTO messages (message_id, sender_id, receiver_id, toa, message) VALUES
(default, sender, receiver, now(), message) RETURNING message_id INTO messid;
INSERT INTO message_queue (queue_id, message_id) VALUES(queue, messid);
ok = True;
ELSE
ok = False;
END IF;
RETURN ok;
END
$$;
ALTER FUNCTION public.createmessage(sender integer, receiver integer, queue integer, message character varying) OWNER TO asl_pg;
--
-- Name: createqueue(); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION createqueue() RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
queueid integer;
BEGIN
INSERT INTO queues (queue_id) VALUES (default) RETURNING queue_id INTO queueid;
RETURN queueid;
END;
$$;
ALTER FUNCTION public.createqueue() OWNER TO asl_pg;
--
-- Name: deletequeue(integer); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION deletequeue(queueid integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
count integer;
deleteOK boolean;
BEGIN
SELECT count(mq.message_id) INTO count FROM queues AS q, message_queue AS mq WHERE q.queue_id = mq.queue_id AND q.queue_id = queueid;
IF count = 0 THEN
DELETE FROM queues WHERE queue_id = queueid;
deleteOK := TRUE;
ELSE
deleteOK := FALSE;
END IF;
RETURN deleteOK;
END;
$$;
ALTER FUNCTION public.deletequeue(queueid integer) OWNER TO asl_pg;
--
-- Name: peekmessagebyqueue(integer, integer); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION peekmessagebyqueue(clientid integer, queueid integer) RETURNS character varying
LANGUAGE plpgsql
AS $$
DECLARE
peekedmessage VARCHAR(2000);
BEGIN
SELECT message INTO peekedmessage FROM messages AS m, message_queue AS mq WHERE m.message_id = mq.message_id AND m.receiver_id in (-1, clientid) AND mq.queue_id = queueid ORDER BY m.toa LIMIT 1;
RETURN peekedmessage;
END;
$$;
ALTER FUNCTION public.peekmessagebyqueue(clientid integer, queueid integer) OWNER TO asl_pg;
--
-- Name: peekmessagebysender(integer, integer); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION peekmessagebysender(clientid integer, senderid integer) RETURNS character varying
LANGUAGE plpgsql
AS $$
DECLARE
peekedmessage VARCHAR(2000);
BEGIN
SELECT message INTO peekedmessage FROM messages AS m, message_queue AS mq WHERE m.message_id = mq.message_id AND m.receiver_id in (-1, clientid) AND m.sender_id = senderid ORDER BY m.toa LIMIT 1;
RETURN peekedmessage;
END;
$$;
ALTER FUNCTION public.peekmessagebysender(clientid integer, senderid integer) OWNER TO asl_pg;
--
-- Name: popmessagebyqueue(integer, integer); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION popmessagebyqueue(clientid integer, queueid integer) RETURNS character varying
LANGUAGE plpgsql
AS $$
DECLARE
popMessage VARCHAR(2000);
messid integer;
BEGIN
SELECT m.message, m.message_id INTO popMessage, messid FROM messages AS m, message_queue AS mq WHERE m.message_id = mq.message_id AND m.receiver_id in (-1, clientid) AND mq.queue_id = queueid ORDER BY m.toa LIMIT 1;
DELETE FROM message_queue WHERE message_id = messid;
DELETE FROM messages WHERE message_id = messid;
RETURN popMessage;
END;
$$;
ALTER FUNCTION public.popmessagebyqueue(clientid integer, queueid integer) OWNER TO asl_pg;
--
-- Name: popmessagebysender(integer, integer); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION popmessagebysender(clientid integer, senderid integer) RETURNS character varying
LANGUAGE plpgsql
AS $$
DECLARE
popMessage VARCHAR(2000);
messid integer;
BEGIN
SELECT m.message, m.message_id INTO popMessage, messid FROM messages AS m, message_queue AS mq WHERE m.message_id = mq.message_id AND m.receiver_id in (-1, clientid) AND m.sender_id = senderid ORDER BY m.toa LIMIT 1;
DELETE FROM message_queue WHERE message_id = messid;
DELETE FROM messages WHERE message_id = messid;
RETURN popMessage;
END;
$$;
ALTER FUNCTION public.popmessagebysender(clientid integer, senderid integer) OWNER TO asl_pg;
--
-- Name: resetdb(); Type: FUNCTION; Schema: public; Owner: asl_pg
--
CREATE FUNCTION resetdb() RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM message_queue;
DELETE FROM queues;
DELETE FROM messages;
DELETE FROM clients;
ALTER SEQUENCE client_id_seq RESTART WITH 1;
ALTER SEQUENCE message_id_seq RESTART WITH 1;
ALTER SEQUENCE queue_id_seq RESTART WITH 1;
INSERT INTO clients (client_id) VALUES (-1);
END;
$$;
ALTER FUNCTION public.resetdb() OWNER TO asl_pg;
--
-- Name: client_id_seq; Type: SEQUENCE; Schema: public; Owner: asl_pg
--
CREATE SEQUENCE client_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE client_id_seq OWNER TO asl_pg;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: clients; Type: TABLE; Schema: public; Owner: asl_pg; Tablespace:
--
CREATE TABLE clients (
client_id integer DEFAULT nextval('client_id_seq'::regclass) NOT NULL
);
ALTER TABLE clients OWNER TO asl_pg;
--
-- Name: message_id_seq; Type: SEQUENCE; Schema: public; Owner: asl_pg
--
CREATE SEQUENCE message_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE message_id_seq OWNER TO asl_pg;
--
-- Name: message_queue; Type: TABLE; Schema: public; Owner: asl_pg; Tablespace:
--
CREATE TABLE message_queue (
queue_id integer NOT NULL,
message_id integer NOT NULL
);
ALTER TABLE message_queue OWNER TO asl_pg;
--
-- Name: messages; Type: TABLE; Schema: public; Owner: asl_pg; Tablespace:
--
CREATE TABLE messages (
message_id integer DEFAULT nextval('message_id_seq'::regclass) NOT NULL,
sender_id integer NOT NULL,
receiver_id integer,
toa timestamp without time zone,
message character varying(2000)
);
ALTER TABLE messages OWNER TO asl_pg;
--
-- Name: queue_id_seq; Type: SEQUENCE; Schema: public; Owner: asl_pg
--
CREATE SEQUENCE queue_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE queue_id_seq OWNER TO asl_pg;
--
-- Name: queues; Type: TABLE; Schema: public; Owner: asl_pg; Tablespace:
--
CREATE TABLE queues (
queue_id integer DEFAULT nextval('queue_id_seq'::regclass) NOT NULL
);
ALTER TABLE queues OWNER TO asl_pg;
--
-- Name: client_id_seq; Type: SEQUENCE SET; Schema: public; Owner: asl_pg
--
SELECT pg_catalog.setval('client_id_seq', 1, false);
--
-- Data for Name: clients; Type: TABLE DATA; Schema: public; Owner: asl_pg
--
COPY clients (client_id) FROM stdin;
-1
\.
--
-- Name: message_id_seq; Type: SEQUENCE SET; Schema: public; Owner: asl_pg
--
SELECT pg_catalog.setval('message_id_seq', 1, false);
--
-- Data for Name: message_queue; Type: TABLE DATA; Schema: public; Owner: asl_pg
--
COPY message_queue (queue_id, message_id) FROM stdin;
\.
--
-- Data for Name: messages; Type: TABLE DATA; Schema: public; Owner: asl_pg
--
COPY messages (message_id, sender_id, receiver_id, toa, message) FROM stdin;
\.
--
-- Name: queue_id_seq; Type: SEQUENCE SET; Schema: public; Owner: asl_pg
--
SELECT pg_catalog.setval('queue_id_seq', 1, false);
--
-- Data for Name: queues; Type: TABLE DATA; Schema: public; Owner: asl_pg
--
COPY queues (queue_id) FROM stdin;
\.
--
-- Name: clients_pkey; Type: CONSTRAINT; Schema: public; Owner: asl_pg; Tablespace:
--
ALTER TABLE ONLY clients
ADD CONSTRAINT clients_pkey PRIMARY KEY (client_id);
--
-- Name: messages_pkey; Type: CONSTRAINT; Schema: public; Owner: asl_pg; Tablespace:
--
ALTER TABLE ONLY messages
ADD CONSTRAINT messages_pkey PRIMARY KEY (message_id);
--
-- Name: queues_pkey; Type: CONSTRAINT; Schema: public; Owner: asl_pg; Tablespace:
--
ALTER TABLE ONLY queues
ADD CONSTRAINT queues_pkey PRIMARY KEY (queue_id);
--
-- Name: mq_idx; Type: INDEX; Schema: public; Owner: asl_pg; Tablespace:
--
CREATE INDEX mq_idx ON message_queue USING btree (queue_id, message_id);
--
-- Name: message_queue_message_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: asl_pg
--
ALTER TABLE ONLY message_queue
ADD CONSTRAINT message_queue_message_id_fkey FOREIGN KEY (message_id) REFERENCES messages(message_id);
--
-- Name: message_queue_queue_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: asl_pg
--
ALTER TABLE ONLY message_queue
ADD CONSTRAINT message_queue_queue_id_fkey FOREIGN KEY (queue_id) REFERENCES queues(queue_id);
--
-- Name: messages_receiver_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: asl_pg
--
ALTER TABLE ONLY messages
ADD CONSTRAINT messages_receiver_id_fkey FOREIGN KEY (receiver_id) REFERENCES clients(client_id);
--
-- Name: messages_sender_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: asl_pg
--
ALTER TABLE ONLY messages
ADD CONSTRAINT messages_sender_id_fkey FOREIGN KEY (sender_id) REFERENCES clients(client_id);
--
-- Name: public; Type: ACL; Schema: -; Owner: florangmehlin
--
REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM florangmehlin;
GRANT ALL ON SCHEMA public TO florangmehlin;
GRANT ALL ON SCHEMA public TO PUBLIC;
--
-- Name: client_id_seq; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON SEQUENCE client_id_seq FROM PUBLIC;
REVOKE ALL ON SEQUENCE client_id_seq FROM asl_pg;
GRANT ALL ON SEQUENCE client_id_seq TO asl_pg;
--
-- Name: clients; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON TABLE clients FROM PUBLIC;
REVOKE ALL ON TABLE clients FROM asl_pg;
GRANT ALL ON TABLE clients TO asl_pg;
--
-- Name: message_id_seq; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON SEQUENCE message_id_seq FROM PUBLIC;
REVOKE ALL ON SEQUENCE message_id_seq FROM asl_pg;
GRANT ALL ON SEQUENCE message_id_seq TO asl_pg;
--
-- Name: message_queue; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON TABLE message_queue FROM PUBLIC;
REVOKE ALL ON TABLE message_queue FROM asl_pg;
GRANT ALL ON TABLE message_queue TO asl_pg;
--
-- Name: messages; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON TABLE messages FROM PUBLIC;
REVOKE ALL ON TABLE messages FROM asl_pg;
GRANT ALL ON TABLE messages TO asl_pg;
--
-- Name: queue_id_seq; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON SEQUENCE queue_id_seq FROM PUBLIC;
REVOKE ALL ON SEQUENCE queue_id_seq FROM asl_pg;
GRANT ALL ON SEQUENCE queue_id_seq TO asl_pg;
--
-- Name: queues; Type: ACL; Schema: public; Owner: asl_pg
--
REVOKE ALL ON TABLE queues FROM PUBLIC;
REVOKE ALL ON TABLE queues FROM asl_pg;
GRANT ALL ON TABLE queues TO asl_pg;
--
-- PostgreSQL database dump complete
--