-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.sql
476 lines (425 loc) · 12.7 KB
/
db.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
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
create table public.member
(
id serial not null
constraint member_pk
primary key,
name varchar(30) not null,
surname varchar(30) not null,
username varchar(20) not null,
password char(60) not null,
id_card bigint,
email varchar(30) not null,
bank_account bigint,
address text,
verify boolean default false not null,
pic char(43),
bank_name varchar,
id_card_pic char(43)
);
alter table public.member
owner to postgres;
create unique index member_username_uindex
on public.member (username);
create unique index member_email_uindex
on public.member (email);
create table public.message
(
"from" integer not null
constraint message_member_id_fk
references public.member,
"to" integer not null
constraint message_member_id_fk_2
references public.member,
time timestamp default now() not null,
message text not null,
constraint message_pk
primary key ("from", "to", time)
);
alter table public.message
owner to postgres;
create index message_from_index
on public.message ("from");
create index message_to_index
on public.message ("to");
create table public.tour
(
id serial not null
constraint tour_pk
primary key,
owner integer not null
constraint tour_member_id_fk
references public.member,
name varchar not null,
description text not null,
category varchar not null,
max_member integer not null,
first_day timestamp not null,
last_day timestamp not null,
price integer not null,
status smallint not null,
pic char(43)
);
alter table public.tour
owner to postgres;
create index tour_category_index
on public.tour (category);
create index tour_description_index
on public.tour (description);
create index tour_name_index
on public.tour (name);
create index tour_first_day_index
on public.tour (first_day);
create table public.place
(
id serial not null
constraint place_pk
primary key,
name varchar not null,
pic char(43),
lat double precision not null,
lon double precision not null
);
alter table public.place
owner to postgres;
create index place_name_index
on public.place (name);
create table public.list
(
tour integer not null
constraint tour_place_list_tour_id_fk
references public.tour,
seq integer not null,
place integer not null
constraint tour_place_list_place_id_fk
references public.place,
constraint tour_place_list_pk
primary key (tour, seq)
);
alter table public.list
owner to postgres;
create table public.transcript
(
tour integer not null
constraint transcript_tour_id_fk
references public.tour,
"user" integer not null
constraint transcript_member_id_fk
references public.member,
file char(43),
confirm boolean default false not null,
time timestamp,
constraint transcript_pk
primary key (tour, "user")
);
alter table public.transcript
owner to postgres;
create table public.review
(
tour integer not null
constraint review_tour_id_fk
references public.tour,
"user" integer not null
constraint review_member_id_fk
references public.member,
comment text not null,
ratting smallint not null,
time timestamp default now() not null,
constraint review_pk
primary key ("user", tour)
);
alter table public.review
owner to postgres;
create index review_tour_index
on public.review (tour);
create table public.favorite
(
tour integer not null
constraint favorite_tour_id_fk
references public.tour,
"user" integer not null
constraint favorite_member_id_fk
references public.member,
constraint favorite_pk
primary key (tour, "user")
);
alter table public.favorite
owner to postgres;
create view public.tourdetail
(id, owner, name, description, category, max_member, first_day, last_day, price, status, pic, member,
confirm, ratting, favorite, g_name, g_surname, bank_account, bank_name, list)
as
SELECT tu.id,
tu.owner,
tu.name,
tu.description,
tu.category,
tu.max_member,
tu.first_day,
tu.last_day,
tu.price,
tu.status,
tu.pic,
COALESCE(ts.member, 0::bigint) AS member,
COALESCE(ts.confirm, 0::bigint) AS confirm,
COALESCE(r.ratting, 0::numeric) AS ratting,
COALESCE(f.favorite, 0::bigint) AS favorite,
m.name AS g_name,
m.surname AS g_surname,
m.bank_account,
m.bank_name,
l.list
FROM tour tu
LEFT JOIN (SELECT transcript.tour,
count(transcript."user") AS member,
count(NULLIF(false, transcript.confirm)) AS confirm
FROM transcript
GROUP BY transcript.tour) ts ON tu.id = ts.tour
LEFT JOIN (SELECT favorite.tour,
count(favorite."user") AS favorite
FROM favorite
GROUP BY favorite.tour) f ON tu.id = f.tour
LEFT JOIN (SELECT review.tour,
avg(review.ratting) AS ratting
FROM review
GROUP BY review.tour) r ON tu.id = r.tour
LEFT JOIN member m ON tu.owner = m.id
LEFT JOIN (SELECT l_1.tour,
array_agg(p.name) AS list
FROM (SELECT list.tour,
list.seq,
list.place
FROM list
ORDER BY list.tour, list.seq) l_1
LEFT JOIN place p ON l_1.place = p.id
GROUP BY l_1.tour) l ON l.tour = tu.id;
alter table public.tourdetail
owner to postgres;
create function public.messagewithme(me integer, contact integer)
returns TABLE
(
me boolean,
message text,
"time" timestamp without time zone
)
language sql
as
$$
SELECT case when "from" = $1 then true else false end as me,
message.message,
time
FROM message
WHERE ("from" = $1 AND "to" = $2)
OR ("from" = $2 AND "to" = $1)
ORDER BY time DESC
$$;
alter function public.messagewithme(integer, integer) owner to postgres;
create function public.listupdate(tour integer, list integer[]) returns integer
language plpgsql
as
$$
DECLARE
i int := 0;
x int;
BEGIN
FOREACH x IN ARRAY $2
LOOP
EXECUTE
'INSERT INTO list(tour, seq, place) VALUES($1, $2, $3)
ON CONFLICT (tour, seq)
DO UPDATE SET place = excluded.place' USING $1, i, x;
i := i + 1;
END LOOP;
EXECUTE 'DELETE FROM list WHERE tour = $1 AND seq >= $2' USING $1, i;
RETURN i;
END;
$$;
alter function public.listupdate(integer, integer[]) owner to postgres;
create function public.reviewwithuser("user" integer)
returns TABLE
(
tour integer,
comment text,
ratting smallint,
"time" timestamp without time zone,
name text
)
language sql
as
$$
SELECT r.tour, r.comment, r.ratting, r.time, t.name
FROM review r
LEFT JOIN tour t on r.tour = t.id
WHERE r."user" = $1
ORDER BY r.time DESC ;
$$;
alter function public.reviewwithuser(integer) owner to postgres;
create function public.reviewwithtour(tour integer)
returns TABLE
(
"user" integer,
comment text,
ratting smallint,
"time" timestamp without time zone,
name text,
surname text
)
language sql
as
$$
SELECT r."user", r.comment, r.ratting, r.time, m.name, m.surname
FROM review r
LEFT JOIN member m on r."user" = m.id
WHERE r.tour = $1
ORDER BY r.time DESC ;
$$;
alter function public.reviewwithtour(integer) owner to postgres;
create function public.favoritewithuser("user" integer)
returns TABLE
(
tour integer,
name text
)
language sql
as
$$
SELECT f.tour, t.name
FROM favorite f
LEFT JOIN tour t on f.tour = t.id
WHERE f."user" = $1;
$$;
alter function public.favoritewithuser(integer) owner to postgres;
create function public.placesearch(keyword text)
returns TABLE
(
id integer,
name text,
pic character,
lat double precision,
lon double precision
)
language sql
as
$$
SELECT *
FROM place
WHERE name LIKE ('%' || $1 || '%');
$$;
alter function public.placesearch(text) owner to postgres;
create function public.messagelistme(me integer)
returns TABLE
(
contact integer,
me boolean,
message text,
"time" timestamp without time zone,
name text,
surname text,
pic character
)
language sql
as
$$
SELECT DISTINCT ON (a.contact) a.*, m.name, m.surname, m.pic
FROM (SELECT case when "from" = $1 then "to" else "from" end as contact,
case when "from" = $1 then true else false end as me,
message.message,
time
FROM message
WHERE "from" = $1
OR "to" = $1
ORDER BY time DESC) as a
LEFT JOIN member as m ON m.id = a.contact;
$$;
alter function public.messagelistme(integer) owner to postgres;
create function public.listwithtour(tour integer)
returns TABLE
(
id integer,
name text,
pic character,
lat double precision,
lon double precision
)
language sql
as
$$
SELECT p.*
FROM list l
LEFT JOIN place p on l.place = p.id
WHERE l.tour = $1
ORDER BY l.seq;
$$;
alter function public.listwithtour(integer) owner to postgres;
create function public.tourdetailsearch(keyword text)
returns TABLE
(
id integer,
owner integer,
name text,
description text,
category text,
max_member integer,
first_day timestamp without time zone,
last_day timestamp without time zone,
price integer,
status smallint,
pic character,
member bigint,
confirm bigint,
ratting numeric,
favorite bigint,
g_name text,
g_surname text,
bank_account bigint,
bank_name text,
list character varying[]
)
language sql
as
$$
SELECT *
FROM tourdetail
WHERE description LIKE ('%' || $1 || '%')
OR name LIKE ('%' || $1 || '%')
OR array_to_string(list, ',') LIKE ('%' || $1 || '%');
$$;
alter function public.tourdetailsearch(text) owner to postgres;
create function public.transcriptwithuser("user" integer)
returns TABLE
(
tour integer,
file character,
confirm boolean,
"time" timestamp without time zone,
name text
)
language sql
as
$$
SELECT t.tour, t.file, t.confirm, t.time, t2.name
FROM transcript t
LEFT JOIN tour t2 on t.tour = t2.id
WHERE t."user" = $1
ORDER BY t.time DESC ;
$$;
alter function public.transcriptwithuser(integer) owner to postgres;
create function public.transcriptwithtour(tour integer)
returns TABLE
(
"user" integer,
file character,
confirm boolean,
"time" timestamp without time zone,
name text,
surname text
)
language sql
as
$$
SELECT t."user", t.file, t.confirm, t.time, m.name, m.surname
FROM transcript t
LEFT JOIN member m on t."user" = m.id
WHERE t.tour = $1
ORDER BY t.time DESC ;
$$;
alter function public.transcriptwithtour(integer) owner to postgres;