-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_queue.sql
292 lines (213 loc) · 6.76 KB
/
db_queue.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
--
-- PostgreSQL database dump
--
-- Dumped from database version 10.14 (Ubuntu 10.14-1.pgdg18.04+1)
-- Dumped by pg_dump version 12.4 (Ubuntu 12.4-1.pgdg18.04+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: func_consume_message(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.func_consume_message(_id integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
access_granted bool;
del_tot int;
BEGIN
WITH rows AS (
delete from tbl_message where "id" = _id returning "id")
SELECT count(*) into del_tot FROM rows;
if del_tot = 1
then
return true;
end if;
return false;
END;
$$;
ALTER FUNCTION public.func_consume_message(_id integer) OWNER TO postgres;
--
-- Name: func_create_queue(text, text, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.func_create_queue(_name text, _username text, _password text) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
access_granted bool;
userid text;
BEGIN
select true into access_granted from tbl_user where username = _username and "password" = _password;
if access_granted
then
if (select true from tbl_queue where "name" = _name) is null
then
insert into tbl_queue ("name", "owner") values (_name, _username);
return true;
end if;
return false;
end if;
return false;
END;
$$;
ALTER FUNCTION public.func_create_queue(_name text, _username text, _password text) OWNER TO postgres;
--
-- Name: func_delete_queue(text, text, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.func_delete_queue(_name text, _username text, _password text) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
access_granted bool;
userid text;
BEGIN
select true into access_granted from tbl_user where username = _username and "password" = _password;
if access_granted
then
if (select true from tbl_queue where "owner" = _username and "name" = _name)
then
delete from tbl_queue where "owner" = _username and "name" = _name;
return true;
end if;
return false;
end if;
return false;
END;
$$;
ALTER FUNCTION public.func_delete_queue(_name text, _username text, _password text) OWNER TO postgres;
--
-- Name: func_produce_message(text, text, text, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.func_produce_message(_name text, _message text, _username text, _password text) RETURNS TABLE(_status boolean, _id integer)
LANGUAGE plpgsql
AS $$
DECLARE
access_granted bool;
idmsg int;
BEGIN
select true into access_granted from tbl_user where username = _username and "password" = _password;
if access_granted
then
insert into tbl_message (name_queue, "message") values (_name, _message) returning id into idmsg;
return query
select true, idmsg;
else
return query
select false, 0;
end if;
END;
$$;
ALTER FUNCTION public.func_produce_message(_name text, _message text, _username text, _password text) OWNER TO postgres;
SET default_tablespace = '';
--
-- Name: tbl_message; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.tbl_message (
name_queue text NOT NULL,
message text,
id integer NOT NULL
);
ALTER TABLE public.tbl_message OWNER TO postgres;
--
-- Name: tbl_message_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.tbl_message_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.tbl_message_id_seq OWNER TO postgres;
--
-- Name: tbl_message_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.tbl_message_id_seq OWNED BY public.tbl_message.id;
--
-- Name: tbl_queue; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.tbl_queue (
name text NOT NULL,
owner text NOT NULL
);
ALTER TABLE public.tbl_queue OWNER TO postgres;
--
-- Name: tbl_user; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.tbl_user (
username text NOT NULL,
password text NOT NULL
);
ALTER TABLE public.tbl_user OWNER TO postgres;
--
-- Name: tbl_message id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.tbl_message ALTER COLUMN id SET DEFAULT nextval('public.tbl_message_id_seq'::regclass);
--
-- Data for Name: tbl_message; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY public.tbl_message (name_queue, message, id) FROM stdin;
\.
--
-- Data for Name: tbl_queue; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY public.tbl_queue (name, owner) FROM stdin;
q-1 admin
\.
--
-- Data for Name: tbl_user; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY public.tbl_user (username, password) FROM stdin;
admin 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
\.
--
-- Name: tbl_message_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('public.tbl_message_id_seq', 72, true);
--
-- Name: tbl_message tbl_message_pk; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.tbl_message
ADD CONSTRAINT tbl_message_pk PRIMARY KEY (id);
--
-- Name: tbl_queue tbl_queue_pk; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.tbl_queue
ADD CONSTRAINT tbl_queue_pk PRIMARY KEY (name);
--
-- Name: tbl_user tbl_user_pk; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.tbl_user
ADD CONSTRAINT tbl_user_pk PRIMARY KEY (username);
--
-- Name: tbl_message_id_uindex; Type: INDEX; Schema: public; Owner: postgres
--
CREATE UNIQUE INDEX tbl_message_id_uindex ON public.tbl_message USING btree (id);
--
-- Name: tbl_queue_name_uindex; Type: INDEX; Schema: public; Owner: postgres
--
CREATE UNIQUE INDEX tbl_queue_name_uindex ON public.tbl_queue USING btree (name);
--
-- Name: tbl_user_username_uindex; Type: INDEX; Schema: public; Owner: postgres
--
CREATE UNIQUE INDEX tbl_user_username_uindex ON public.tbl_user USING btree (username);
--
-- Name: tbl_message tbl_message_tbl_queue_name_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.tbl_message
ADD CONSTRAINT tbl_message_tbl_queue_name_fk FOREIGN KEY (name_queue) REFERENCES public.tbl_queue(name) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: tbl_queue tbl_queue_tbl_user_username_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.tbl_queue
ADD CONSTRAINT tbl_queue_tbl_user_username_fk FOREIGN KEY (owner) REFERENCES public.tbl_user(username);
--
-- PostgreSQL database dump complete
--