Repository navigation
Expand file tree
/
Copy pathcreate.sql
More file actions
811 lines (762 loc) · 22.6 KB
/
Copy pathcreate.sql
File metadata and controls
811 lines (762 loc) · 22.6 KB
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
drop table if exists ads, messages, reactions, users, wait_actions cascade;
drop view if exists empty_reactions, liked_reactions, profile_reactions, reactions_ads;
create table if not exists users
(
id serial,
user_id integer primary key,
first_name text not null,
age integer default 0,
city text not null,
about text,
media text,
locationLatitude real not null,
locationLongitude real not null,
sex integer not null,
search_sex integer default 0,
search_before_age integer default 1,
search_after_age integer default 100,
is_hide boolean default TRUE
);
create table if not exists ads
(
id serial,
message_id integer not null,
is_hide boolean default TRUE,
description text not null,
media text not null,
link text not null,
duration integer not null,
create_time integer default extract(epoch from now())
);
create table if not exists reactions
(
id serial,
message_id integer,
initiator_id integer not null,
rated_id integer not null,
is_ads boolean not null,
liked boolean default FALSE,
disliked boolean default FALSE,
viewed boolean default FALSE,
time integer default extract(epoch from now()),
foreign key (initiator_id) references users (user_id) on delete cascade,
foreign key (rated_id) references users (user_id) on delete cascade
);
create table if not exists wait_actions
(
user_id integer,
action text,
foreign key (user_id) references users (user_id) on delete cascade
);
create view reactions_ads as
select t2.id,
t2.initiator_id as author_id,
t2.rated_id as ads_id,
t2.viewed,
t2.time
from reactions as t2
where t2.is_ads = TRUE;
create view profile_reactions as
select t2.id,
t2.message_id,
t2.initiator_id,
t2.rated_id,
t2.liked,
t2.disliked,
t2.viewed,
t2.time
from reactions as t2
where t2.is_ads = FALSE;
create view empty_reactions as
select t2.id,
t2.message_id,
t2.initiator_id,
t2.rated_id,
t2.viewed,
t2.time
from profile_reactions as t2
where t2.liked is FALSE
and t2.disliked is FALSE;
create view liked_reactions as
select t2.id,
t2.initiator_id,
t2.rated_id,
t2.viewed,
t2.time
from profile_reactions as t2
where t2.liked is TRUE
and t2.disliked is FALSE
and not exists(select t3.id
from profile_reactions as t3
where t3.rated_id = t2.initiator_id
and t3.initiator_id = t2.rated_id);
create table if not exists messages
(
user_id integer not null,
message_id integer,
reaction_id integer,
foreign key (user_id) references users (user_id) on delete cascade
);
create or replace function haversine(latitude1 real, longitude1 real, latitude2 real, longitude2 real) returns real
language plpgsql
as
$$
declare
earth_r constant integer = 6371302;
a real;
begin
a = pow(sin(radians(latitude2 - latitude1) / 2), 2) +
cos(radians(latitude1)) * cos(radians(latitude2)) * pow(sin(radians(longitude2 - longitude1) / 2), 2);
return 2 * earth_r * atan2(sqrt(a), sqrt(1 - a));
end;
$$;
create function viewed_reaction(_user_id integer, _message_id integer) returns boolean
language plpgsql
as
$$
begin
update reactions as t2
set viewed = TRUE
where t2.message_id = _message_id
and t2.initiator_id = _user_id
and t2.viewed = FALSE;
return FOUND;
end;
$$;
create or replace function create_ads(_user_id integer,
_message_id integer,
_text text,
_link text,
_media text,
_duration integer) returns boolean
language plpgsql
as
$$
begin
if user_is_exist(_user_id) then
insert into ads(message_id,
description,
media,
link,
duration)
values (_message_id,
_text,
_media,
_link,
_duration);
perform clear_action(_user_id);
return True;
end if;
return False;
end;
$$;
create or replace function activate_ads(ads_id integer) returns boolean
language plpgsql
as
$$
begin
update ads as t2
set is_hide = FALSE
where t2.id = ads_id;
return FOUND;
end;
$$;
create or replace function deactivate_ads(ads_id integer) returns boolean
language plpgsql
as
$$
begin
update ads as t2
set is_hide = TRUE
where t2.id = ads_id;
return FOUND;
end;
$$;
create function create_user(_user_id integer, user_name text, user_sex integer, _city text, _locationlatitude real,
_locationlongitude real) returns boolean
language plpgsql
as
$$
begin
if not user_is_exist(_user_id) then
insert into users(user_id,
first_name,
city,
sex,
locationLatitude,
locationLongitude)
values (_user_id,
user_name,
_city,
user_sex,
_locationLatitude,
_locationLongitude);
insert into messages(user_id)
values (_user_id);
insert into wait_actions(user_id)
values (_user_id);
return True;
end if;
return False;
end;
$$;
create or replace function next_reaction(_user_id integer, show_interval integer)
returns
table
(
_is_ads boolean,
_reaction_id integer,
ads_id integer,
description text,
link text,
user_id integer,
first_name text,
age integer,
city text,
sex integer,
about text,
media text,
distance real
)
language plpgsql
as
$$
declare
_rated_id integer;
_reaction_id integer;
_is_ads boolean = FALSE;
_count_users_for_ads integer;
u_latitude real;
u_longitude real;
begin
if is_fulled(_user_id) then
update users as t2
set is_hide = FALSE
where t2.user_id = _user_id
returning t2.locationlatitude, t2.locationlongitude into u_latitude, u_longitude;
select count(t2.id)
into _count_users_for_ads
from profile_reactions as t2
where t2.initiator_id = _user_id;
if FOUND and _count_users_for_ads >= 2 then
_rated_id = select_ads(_user_id, show_interval);
if _rated_id is not null then
_is_ads = TRUE;
end if;
end if;
if _rated_id is null then
select t3.id,
t3.rated_id
into _reaction_id,
_rated_id
from empty_reactions as t3
where t3.initiator_id = _user_id
limit 1;
if found then
update reactions as t2
set message_id = null::integer,
time = extract(epoch from now())::integer,
viewed = FALSE
where t2.id = _reaction_id;
end if;
end if;
if _rated_id is null then
_rated_id = next_user(_user_id);
end if;
if _rated_id is not null then
if _reaction_id is null then
_reaction_id = create_reaction(_user_id, _rated_id, _is_ads);
end if;
if _is_ads then
return query select _is_ads,
_reaction_id,
t3.id as ads_id,
t3.description,
t3.link,
null::integer as user_id,
null as first_name,
null::integer as age,
null as city,
null::integer as sex,
null as about,
t3.media,
null::real as distance
from ads as t3
where t3.id = _rated_id;
else
return query select _is_ads,
_reaction_id,
null::integer as ads_id,
null as description,
null as link,
t3.user_id,
t3.first_name,
t3.age,
t3.city,
t3.sex,
t3.about,
t3.media,
haversine(u_latitude,
u_longitude,
t3.locationLatitude,
t3.locationLongitude) as distance
from users as t3
where t3.user_id = _rated_id;
end if;
end if;
end if;
end;
$$;
create or replace function select_ads(_user_id integer, show_interval integer) returns integer
language plpgsql
as
$$
declare
c_time integer;
_ads_id integer;
_author_id integer;
reaction_time integer;
ads_viewed boolean;
ads_duration integer;
t integer;
ads_created_time integer;
begin
c_time = extract(epoch from now());
select t2.id,
t2.author_id,
t2.time,
t2.viewed
into
_ads_id,
_author_id,
reaction_time,
ads_viewed
from reactions_ads as t2
where t2.id = (
select max(t1.id)
from reactions_ads as t1
where t1.author_id = _user_id
);
if reaction_time is null then
t = 0;
else
t = c_time - reaction_time;
end if;
if not found then
_author_id = next_ads(0);
else
select t2.duration, t2.create_time
into ads_duration, ads_created_time
from ads as t2
where t2.id = _author_id
and t2.is_hide = FALSE
and t2.create_time + t2.duration > c_time;
if found then
if ads_viewed then
if t > show_interval then
_author_id = next_ads(_author_id);
else
return null;
end if;
else
return null;
end if;
else
update ads as t2 set is_hide = TRUE where t2.id = _author_id;
if t > show_interval then
_author_id = next_ads(_author_id);
else
return null;
end if;
end if;
end if;
return _author_id;
end;
$$;
create or replace function next_user(_user_id integer) returns integer
language plpgsql
as
$$
declare
search_s integer;
id_next_user integer;
before_age integer;
after_age integer;
user_age integer;
latitude real;
longitude real;
distance real;
begin
select t2.search_sex,
t2.search_before_age,
t2.search_after_age,
t2.age,
t2.locationLatitude,
t2.locationLongitude
into
search_s,
before_age,
after_age,
user_age,
latitude,
longitude
from users as t2
where t2.user_id = _user_id;
if FOUND then
select t3.initiator_id
into id_next_user
from liked_reactions as t3
where t3.rated_id = _user_id
limit 1;
if not FOUND then
if search_s > 0 then
select t2.user_id,
haversine(t2.locationLatitude,
t2.locationLongitude,
latitude,
longitude) as _distance
into id_next_user,
distance
from users as t2
where (t2.user_id not in (
select t3.rated_id
from profile_reactions as t3
where t3.initiator_id = _user_id
) and
t2.user_id != _user_id and
t2.sex = search_s and
t2.is_hide is FALSE and
((t2.age >= before_age and
t2.age <= after_age) or
(t2.search_before_age <= user_age and
t2.search_after_age >= user_age)))
order by _distance
limit 1;
else
select t2.user_id,
haversine(t2.locationLatitude,
t2.locationLongitude,
latitude,
longitude) as _distance
into id_next_user,
distance
from users as t2
where (t2.user_id not in (
select t3.rated_id
from profile_reactions as t3
where t3.initiator_id = _user_id
) and
t2.user_id != _user_id and
t2.is_hide is FALSE and
((t2.age >= before_age and
t2.age <= after_age) or
(t2.search_before_age <= user_age and
t2.search_after_age >= user_age)))
order by _distance
limit 1;
end if;
end if;
end if;
return id_next_user;
end;
$$;
create or replace function next_ads(ads_id integer) returns integer
language plpgsql
as
$$
declare
ret_ads_id integer;
c_time integer;
begin
if ads_id is null then
ads_id = 0;
end if;
c_time = extract(epoch from now());
select t2.id
into ret_ads_id
from ads as t2
where t2.id > ads_id
and t2.create_time + t2.duration > c_time
and t2.is_hide = FALSE;
if not found then
select t2.id
into ret_ads_id
from ads as t2
where t2.id > 0
and t2.create_time + t2.duration > c_time
and t2.is_hide = FALSE;
end if;
return ret_ads_id;
end;
$$;
create function user_is_exist(_user_id integer) returns boolean
language plpgsql
as
$$
begin
return exists(select t2.id from users as t2 where t2.user_id = _user_id);
end;
$$;
create or replace function dislike_profile(_user_id integer) returns boolean
language plpgsql
as
$$
begin
update reactions as t2
set disliked = TRUE
where t2.id = (select max(t2.id)
from empty_reactions as t2
where initiator_id = _user_id
and message_id is not null);
return FOUND;
end;
$$;
create or replace function change_image(_user_id integer, image_url text) returns boolean
language plpgsql
as
$$
begin
update users as t2 set media = image_url where t2.user_id = _user_id;
if FOUND then
perform clear_action(_user_id);
return true;
end if;
return false;
end;
$$;
create or replace function create_reaction(u_id integer, p_id integer,
reaction_is_ads boolean) returns integer
language plpgsql
as
$$
declare
ret integer;
begin
insert into reactions(initiator_id,
rated_id,
is_ads)
values (u_id,
p_id,
reaction_is_ads)
returning id into ret;
return ret;
end;
$$;
create or replace function like_profile(_user_id integer)
returns
table
(
is_respond boolean,
_reaction_id integer,
user_id integer,
first_name text,
age integer,
city text,
sex integer,
about text,
media text,
distance real
)
language plpgsql
as
$$
declare
_rated_id integer;
_reaction_id integer;
u_latitude real;
u_longitude real;
begin
select t1.locationLatitude, t1.locationLongitude
into u_latitude, u_longitude
from users as t1
where t1.user_id = _user_id;
update reactions as t2
set liked = TRUE
where t2.id = (select max(t2.id)
from empty_reactions as t2
where t2.initiator_id = _user_id
and t2.message_id is not null)
returning id, rated_id into _reaction_id, _rated_id;
if FOUND then
return query select exists(select t2.id
from profile_reactions as t2
where t2.rated_id = _user_id
and t2.initiator_id = _rated_id and
t2.liked is TRUE) as is_respond,
_reaction_id,
t3.user_id,
t3.first_name,
t3.age,
t3.city,
t3.sex,
t3.about,
t3.media,
haversine(u_latitude,
u_longitude,
t3.locationLatitude,
t3.locationLongitude) as distance
from users as t3
where t3.user_id = _rated_id;
end if;
end;
$$;
-- create or replace function follow_profile(_user_id integer)
-- returns
-- table
-- (
-- _reaction_id integer,
-- user_id integer,
-- first_name text,
-- age integer,
-- city text,
-- sex integer,
-- about text,
-- media text,
-- distance real
-- )
-- language plpgsql
-- as
-- $$
-- declare
-- _initiator_id integer;
-- _reaction_id integer;
-- u_latitude real;
-- u_longitude real;
-- begin
-- if user_is_exist(_user_id) then
-- select t1.locationLatitude, t1.locationLongitude
-- into u_latitude, u_longitude
-- from users as t1
-- where t1.user_id = _user_id;
-- select t2.initiator_id
-- into _initiator_id
-- from liked_reactions as t2
-- where t2.rated_id = _user_id
-- limit 1;
-- if found then
-- _reaction_id = create_reaction(_user_id, _initiator_id, FALSE);
-- return query select _reaction_id,
-- t2.user_id,
-- t2.first_name,
-- t2.age,
-- t2.city,
-- t2.sex,
-- t2.about,
-- t2.media,
-- haversine(u_latitude,
-- u_longitude,
-- t2.locationLatitude,
-- t2.locationLongitude) as distance
-- from users as t2
-- where t2.user_id = _initiator_id;
-- end if;
-- end if;
-- end;
-- $$;
create or replace function change_action(_user_id integer, action_name text) returns boolean
language plpgsql
as
$$
begin
update wait_actions as t2 set action = action_name where t2.user_id = _user_id;
return FOUND;
end;
$$;
create or replace function clear_action(_user_id integer) returns boolean
language plpgsql
as
$$
begin
update wait_actions as t2 set action = null where t2.user_id = _user_id;
return FOUND;
end;
$$;
create or replace function reset_ads(ads_id integer) returns boolean
language plpgsql
as
$$
begin
update ads as t2 set create_time = extract(epoch from now()) where t2.id = ads_id;
return FOUND;
end;
$$;
create or replace function change_age(_user_id integer, user_years integer) returns boolean
language plpgsql
as
$$
begin
update users as t2 set age = user_years where t2.user_id = _user_id;
if FOUND then
perform clear_action(_user_id);
return true;
end if;
return false;
end;
$$;
create or replace function change_user_about(_user_id integer, user_about text) returns boolean
language plpgsql
as
$$
begin
update users as t2 set about = user_about where t2.user_id = _user_id;
if FOUND then
perform clear_action(_user_id);
return true;
end if;
return false;
end;
$$;
create or replace function change_location(_user_id integer, _city text, latitude real, longitude real) returns boolean
language plpgsql
as
$$
begin
update users as t2
set locationLatitude = latitude,
locationLongitude = longitude,
city = _city
where t2.user_id = _user_id;
if FOUND then
perform clear_action(_user_id);
return true;
end if;
return false;
end;
$$;
create or replace function get_action(_user_id integer) returns text
language plpgsql
as
$$
declare
fun_name text;
begin
select t2.action into fun_name from wait_actions as t2 where t2.user_id = _user_id;
return fun_name;
end;
$$;
create or replace function change_search_sex(_user_id integer, user_search_sex integer) returns boolean
language plpgsql
as
$$
begin
update users as t2 set search_sex = user_search_sex where t2.user_id = _user_id;
if FOUND then
perform clear_action(_user_id);
return true;
end if;
return false;
end
$$;
create or replace function is_fulled(_user_id integer) returns boolean
language plpgsql
as
$$
begin
return exists(select t2.id
from users as t2
where t2.age > 0
and t2.media is not null
and t2.about is not null
and t2.user_id = _user_id);
end
$$;