-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabasefunctions.sql
907 lines (785 loc) · 23 KB
/
databasefunctions.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
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
--get number of concurrent logins for all users
select lioh.customerid, count(*)
from log_in_out_history lioh
where (logout is null)
group by lioh.customerid;
--get list of games a user can play
select g.game_name from customer
inner join cust_sub cs on (customer.id = cs.cust_id and customer.id = 1)
inner join cust_sub_featurepk csf on (cs.id = csf.cust_subid)
inner join featurepack f on (csf.featpkid = f.id)
inner join game_feat gf on (f.id = gf.feat_id)
inner join game g on (gf.game_id = g.id or (g.public = true));
--Get subscription history
select c.firstname, c.surname , cs.date_of_origin ,
s.numberofmonths , st.tiername
from public.customer c
inner join public.cust_sub cs on (c.id = cs.cust_id)
inner join public.sub s on (s.id = cs.sub_id)
inner join public.sub_tier st on (s.tier_id = st.id);
--where c.id = target;
select g.game_name , d.developername , sum(gr.duration)
from gameplay_record gr
inner join game g on (g.id = gr.gameid)
inner join developer d on (g.dev_id = d.id)
group by 1,2
order by sum(gr.duration) desc;
call find_renewable();
create or replace procedure find_renewable_fp()
language plpgsql
as $$
declare
renew record;
begin
for renew in
select id, cust_subid, current_term_end, date_of_origin, autorenew, f.active
from cust_sub_featurepk csf inner join featurepack f on (csf.featpkid = f.id) order by 4 desc
loop
if(renew.autorenew and (now() - renew.current_term_exp >= '1 second') and renew.active = true) then
call renew_sub_fp(renew);
end if;
end loop;
end;$$
create or replace procedure renew_sub_fp(mycustsub record)
language plpgsql
as $$
declare
subprice money;
submonths int;
tempexp timestamp;
tempmonths text;
begin
subprice = fp.baseprice from cust_sub_featurepack csf
inner join featurepack fp on (fp.id = csf.featpkid)
where (csf.id = mycustsub.id);
submonths = s.numberofmonths from cust_sub
inner join sub s on (s.id = cust_sub.sub_id)
where (cust_sub.id = mycustsub.id);
tempmonths = submonths||' months';
tempexp = mycustsub.current_term_exp + tempmonths::interval;
tempexp = date_trunc('month', tempexp);
tempmonths = date_part('day', mycustsub.date_of_origin)||' days';
tempexp = tempexp + tempmonths::interval;
if(date_part('day', tempexp) < date_part('day', mycustsub.date_of_origin)) then
tempexp = date_trunc('month', tempexp);
tempexp = tempexp + date_part(
'days',
(date_trunc('month', tempexp) + '1 month - 1 day'::interval)
);
end if;
if(mycustsub.autorenew = true) then
subprice = subprice - 1;
end if;
INSERT INTO public.cust_sub__feat_pay_hist
(cust_sub_id, pay_date, amt, description)
VALUES(mycustsub.id, now(), subprice, 'renew subscription on featurepack');
UPDATE public.cust_sub_featurepk
SET current_term_start= current_term_end, current_term_end= tempexp - '1 day'::interval
WHERE id=mycustsub.id;
end;$$
create or replace procedure find_renewable()
language plpgsql
as $$
declare
renew record;
begin
for renew in
select id, cust_id, sub_id, current_term_exp, date_of_origin, autorenew, active
from cust_sub order by 4 desc
loop
if(renew.autorenew and (now() - renew.current_term_exp >= '1 second') and renew.active = true) then
call renew_sub(renew);
end if;
end loop;
end;$$
create or replace procedure renew_sub(mycustsub record)
language plpgsql
as $$
declare
subprice money;
submonths int;
tempexp timestamp;
tempmonths text;
begin
subprice = st.baseprice from cust_sub
inner join sub on (mycustsub.sub_id = sub.id)
inner join sub_tier st on (st.id = sub.tier_id)
where (cust_sub.id = mycustsub.id);
submonths = s.numberofmonths from cust_sub
inner join sub s on (s.id = cust_sub.sub_id)
where (cust_sub.id = mycustsub.id);
tempmonths = submonths||' months';
tempexp = mycustsub.current_term_exp + tempmonths::interval;
tempexp = date_trunc('month', tempexp);
tempmonths = date_part('day', mycustsub.date_of_origin)||' days';
tempexp = tempexp + tempmonths::interval;
if(date_part('day', tempexp) < date_part('day', mycustsub.date_of_origin)) then
tempexp = date_trunc('month', tempexp);
tempexp = tempexp + date_part(
'days',
(date_trunc('month', tempexp) + '1 month - 1 day'::interval)
);
end if;
if(mycustsub.autorenew = true) then
subprice = subprice * .85;
end if;
INSERT INTO public.cust_sub_pay_hist
(cust_sub_id, pay_date, amt, description)
VALUES(mycustsub.id, now(), subprice, 'renew subscription');
UPDATE public.cust_sub
SET current_term_start= current_term_exp, current_term_exp= tempexp - '1 day'::interval
WHERE id=mycustsub.id;
end;$$
--limited number functions
create or replace procedure makesubtier()
language plpgsql
as $$
declare
begin
INSERT INTO public.sub_tier
(baseprice, tiername, concurrentlogin, active)
VALUES(5.00, 'Bronze', 1, true),
(8.00, 'Silver', 2, true),
(10.00, 'Gold', 5, true);
end;$$
create or replace procedure makesubs()
language plpgsql
as $$
declare
subtier record;
begin
for subtier in
select id from sub_tier s loop
INSERT INTO public.sub
(tier_id, numberofmonths)
VALUES(subtier.id, 1),(subtier.id, 12);
end loop;
end;$$
create or replace procedure makefeaturepack()
language plpgsql
as $$
declare
begin
INSERT INTO public.featurepack
(pack_name, baseprice, active)
VALUES('Retro GamePack', 3, true),
('HighSciFi GamePack', 4, true),
('DungeonDweller GamePack', 2, true);
end;$$
--not limited. Each one should take in an int for the number of records
create or replace procedure makecustomers(counter int)
language plpgsql
as $$
declare
randomnum int;
namename text = '';
begin
for coun in 0..counter by 1 loop
namename = '';
for randcount in 0..2 by 1 loop
for idcount in 0..3 by 1 loop
namename = namename||(((random() * 10)::int) - 1);
end loop;
namename = namename||' ';
end loop;
INSERT INTO public.customer
(email, firstname, surname, username, password_, payment_info)
VALUES('email'||coun||'@email.com', 'Person'||coun, 'Personson'||coun, 'User'||coun, 'password'||coun, namename);
end loop;
commit;
end;$$
-- make_cust_sub
CREATE OR REPLACE PROCEDURE make_cust_sub(IN number_of_potential_contracts integer DEFAULT 1)
LANGUAGE plpgsql
AS $$
declare
cust_curs cursor for select * from customer;
cust_current record;
rSub int;
rDeterminer int;
origin_date timestamp;
temp_term_start timestamp;
temp_term_exp timestamp;
temp_active bool;
temp_autorenew bool;
temp_interval text;
begin
open cust_curs;
loop
fetch cust_curs into cust_current;
exit when not found;
for t in 0..number_of_potential_contracts by 1
loop
SELECT
sub.id
into rSub
FROM
sub OFFSET floor(random() * (
SELECT
COUNT(*)
FROM sub))
LIMIT 1;
origin_date :='2020-01-01'::timestamp + (random() * (interval '2 years')) + '0 days';
temp_term_start := origin_date + (random() * (interval '2 years')) + '0 days';
select s.numberofmonths
into temp_interval
from sub s
where (rSub = s.id);
temp_interval := temp_interval || ' months';
temp_term_exp := temp_term_start + temp_interval::interval;
select (random() * 10)
into rDeterminer;
if t = 1 then
temp_active = true;
else
temp_active = null;
end if;
if rDeterminer % 4 = 0 then
temp_autorenew = true;
else
temp_autorenew = null;
end if;
insert into cust_sub
(cust_id, sub_id, current_term_start, current_term_exp, date_of_origin, autorenew,active)
values (cust_current.id, rsub, temp_term_start, temp_term_exp, origin_date, temp_autorenew, temp_active);
end loop;
end loop;
close cust_curs;
end;
$$
;
CREATE OR REPLACE PROCEDURE public.makecust_sub_feat_pay_hist()
LANGUAGE plpgsql
AS $procedure$
declare
myrecord record;
begin
for myrecord in
select cs.id, cs.current_term_start, s.baseprice, cs.active, cs.autorenew, s.pack_name
from cust_sub_featurepk cs inner join featurepack s on (s.id = cs.featpkid) loop
if(myrecord.active = true ) then
if(myrecord.autorenew = true) then
INSERT INTO public.cust_sub_feat_pay_hist
(cust_sub_feat_id, pay_date, amt, description)
VALUES(myrecord.id, myrecord.current_term_start, myrecord.baseprice * .85, myrecord.pack_name||' renew');
else
INSERT INTO public.cust_sub_feat_pay_hist
(cust_sub_feat_id, pay_date, amt, description)
VALUES(myrecord.id, myrecord.current_term_start, myrecord.baseprice, myrecord.pack_name||' not renew');
end if;
end if;
end loop;
commit;
end;$procedure$
;
-- makes_login_history
CREATE OR REPLACE PROCEDURE public.make_login_history(IN number_of_logins_per_cust integer)
LANGUAGE plpgsql
AS $$
declare
cust_curs cursor for select * from customer;
current_cust record;
rDeterminer int;
mod_success bool;
rlogin timestamp;
rlogout timestamp;
begin
open cust_curs;
loop
fetch cust_curs into current_cust;
exit when not found;
for t in 0..number_of_logins_per_cust by 1
loop
rlogin := null;
rlogout := null;
select (random() * 10)
into rDeterminer;
rlogin :='2020-01-01'::timestamp + (random() * (interval '2 years')) + '0 days';
if rDeterminer % 3 = 0 then
--failed to login
mod_success = false;
rlogout := rlogin + '1 minute';
else
--succeded login
mod_success = true;
if rDeterminer % 7 = 0 then
rlogout := rlogin + (random() * (interval'5 days'));
end if;
end if;
insert into log_in_out_history
(success, customerid,login,logout)
values (mod_success, current_cust.id ,rlogin, rlogout);
end loop;
end loop;
close cust_curs;
end;
$$
;
-- simulates a game being played
CREATE OR REPLACE PROCEDURE public.play_game(IN gameid integer, IN custid integer)
LANGUAGE plpgsql
AS $procedure$
declare
cust_sub_id int;
game_feat record;
gameplay_record_id int;
can_play bool;
temp_record record;
begin
select cs.cust_id
into cust_sub_id
from customer c inner join
cust_sub cs on(c.id = cs.cust_id)
where (cs.cust_id = custid );
select game_playable(gameid, custid) into can_play;
if can_play then
insert into gameplay_record
(cust_subid, gameid, starttime, duration)
values (cust_sub_id, gameid, now(), null)
returning id into gameplay_record_id;
for temp_record in (
select gf.game_id , gf.feat_id
from game_feat gf where (gf.game_id = gameid)
)loop
insert into game_feature_pack_rev
(game_record_id, feature_pack_id)
values (gameplay_record_id, temp_record.feat_id);
end loop;
else
end if;
end;
$procedure$
;
-- checks if a game is playable for a given customer.
CREATE OR REPLACE FUNCTION public.game_playable(gameid integer, custid integer)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
declare
all_feat_for_game record;
target_game int := gameid;
game_count int;
begin
select count(*)
into game_count
from (
((select g.game_name, g.id from customer
inner join cust_sub cs on (customer.id = cs.cust_id and customer.id = custid)
left join cust_sub_featurepk csf on (cs.id = csf.cust_subid)
left join featurepack f on (csf.featpkid = f.id)
left join game_feat gf on (f.id = gf.feat_id)
left join game g on (gf.game_id = g.id)
where (g.id is not null and g.id = gameid)) )
Union (select g.game_name , g.id from game g where (g.public = true and g.id = gameid) )) as x;
if game_count > 0 then
return true;
else
return false;
end if;
end;
$function$
;
CREATE OR REPLACE PROCEDURE public.make_cust_sub(IN number_of_potential_contracts integer DEFAULT 1)
LANGUAGE plpgsql
AS $procedure$
declare
cust_curs cursor for select * from customer;
cust_current record;
rSub int;
rDeterminer int;
origin_date timestamp;
temp_term_start timestamp;
temp_term_exp timestamp;
temp_active bool;
temp_autorenew bool;
temp_interval text;
begin
open cust_curs;
loop
fetch cust_curs into cust_current;
exit when not found;
for t in 0..number_of_potential_contracts by 1
loop
SELECT
sub.id
into rSub
FROM
sub OFFSET floor(random() * (
SELECT
COUNT(*)
FROM sub))
LIMIT 1;
origin_date :='2020-01-01'::timestamp + (random() * (interval '2 years')) + '0 days';
temp_term_start := origin_date + (random() * (interval '2 years')) + '0 days';
select s.numberofmonths
into temp_interval
from sub s
where (rSub = s.id);
temp_interval := temp_interval || ' months';
temp_term_exp := temp_term_start + temp_interval::interval;
select (random() * 10)
into rDeterminer;
if t = 1 then
temp_active = true;
else
temp_active = null;
end if;
if rDeterminer % 4 = 0 then
temp_autorenew = true;
else
temp_autorenew = null;
end if;
insert into cust_sub
(cust_id, sub_id, current_term_start, current_term_exp, date_of_origin, autorenew,active)
values (cust_current.id, rsub, temp_term_start, temp_term_exp, origin_date, temp_autorenew, temp_active);
end loop;
end loop;
close cust_curs;
end;
$procedure$
;
CREATE OR REPLACE PROCEDURE public.makecust_sub_pay_hist()
LANGUAGE plpgsql
AS $procedure$
declare
myrecord record;
begin
for myrecord in
select cs.id , cs.current_term_start, st.baseprice, cs.active, st.tiername, cs.autorenew
from cust_sub cs inner join sub s on (s.id = cs.sub_id) inner join sub_tier st on (s.tier_id = st.id) loop
if(myrecord.active = true ) then
if(myrecord.autorenew = true) then
INSERT INTO public.cust_sub_pay_hist
(cust_sub_id, pay_date, amt, description)
VALUES(myrecord.id, myrecord.current_term_start, myrecord.baseprice*.85, myrecord.tiername||' renew');
else
INSERT INTO public.cust_sub_pay_hist
(cust_sub_id, pay_date, amt, description)
VALUES(myrecord.id, myrecord.current_term_start, myrecord.baseprice, myrecord.tiername||' not renew');
end if;
end if;
end loop;
commit;
end;$procedure$
;
CREATE OR REPLACE PROCEDURE public.make_login_history(IN number_of_logins_per_cust integer)
LANGUAGE plpgsql
AS $procedure$
declare
cust_curs cursor for select * from customer;
current_cust record;
rDeterminer int;
mod_success bool;
rlogin timestamp;
rlogout timestamp;
begin
open cust_curs;
loop
fetch cust_curs into current_cust;
exit when not found;
for t in 0..number_of_logins_per_cust by 1
loop
rlogin := null;
rlogout := null;
select (random() * 10)
into rDeterminer;
rlogin :='2020-01-01'::timestamp + (random() * (interval '2 years')) + '0 days';
if rDeterminer % 3 = 0 then
--failed to login
mod_success = false;
rlogout := rlogin + '1 minute';
else
--succeded login
mod_success = true;
if rDeterminer % 7 = 0 then
rlogout := rlogin + (random() * (interval'5 days'));
end if;
end if;
insert into log_in_out_history
(success, customerid,login,logout)
values (mod_success, current_cust.id ,rlogin, rlogout);
end loop;
end loop;
close cust_curs;
end;
$procedure$
;
CREATE OR REPLACE PROCEDURE public.make_cust_sub_feat(IN number_of_potential_contracts integer DEFAULT 1)
LANGUAGE plpgsql
AS $procedure$
declare
cust_curs cursor for select * from cust_sub;
cust_current record;
rSub int;
rDeterminer int;
origin_date timestamp;
temp_term_start timestamp;
temp_term_exp timestamp;
temp_active bool;
temp_autorenew bool;
temp_interval text;
begin
open cust_curs;
loop
fetch cust_curs into cust_current;
exit when not found;
for t in 0..number_of_potential_contracts by 1
loop
SELECT
f.id
into rSub
FROM
featurepack f OFFSET floor(random() * (
SELECT
COUNT(*)
FROM featurepack))
LIMIT 1;
origin_date :='2020-01-01'::timestamp + (random() * (interval '2 years')) + '0 days';
temp_term_start := origin_date + (random() * (interval '2 years')) + '0 days';
select (random() * 10)
into rDeterminer;
if t = 1 then
temp_active = true;
else
temp_active = null;
end if;
if rDeterminer % 4 = 0 then
temp_autorenew = true;
else
temp_autorenew = null;
end if;
if((cust_current.id % 2) = 0) then
insert into cust_sub_featurepk
(cust_subid, featpkid, current_term_start, current_term_end, date_of_origin, autorenew,active, numberofmonths)
values (cust_current.id, rsub, temp_term_start, temp_term_start + '1 month', origin_date, temp_autorenew, temp_active, 1);
else
insert into cust_sub_featurepk
(cust_subid, featpkid, current_term_start, current_term_end, date_of_origin, autorenew,active, numberofmonths)
values (cust_current.id, rsub, temp_term_start, temp_term_start + '1 year', origin_date, temp_autorenew, temp_active, 12);
end if;
end loop;
end loop;
close cust_curs;
end;$procedure$
;
create or replace procedure make_developers(num_of_dev int default 10)
language plpgsql as
$$
declare
dev_id int;
begin
for t in 1..num_of_dev by 1
loop
insert into developer
(developername, company_address)
values ('', '')
returning id into dev_id;
update developer
set developername = 'Game Company ' || dev_id,
company_address = 'Company on ' || dev_id || ' Sesame St NY, US'
where (id = dev_id);
end loop;
end;
$$
create or replace procedure make_Games(num_of_games_per_dev int default 10)
language plpgsql as
$$
declare
all_devs_curs cursor for select d.developername, id from developer d;
current_dev record;
temp_game_id int;
rDeterminer int;
rMod int;
temp_public bool;
begin
open all_devs_curs;
loop
fetch all_devs_curs into current_dev;
exit when not found;
for t in 1..num_of_games_per_dev by 1
loop
select (random() * 100)
into rDeterminer;
select (random() * 10)
into rMod;
if rMod = 0 or rDeterminer % rMod = 0 then
temp_public := true;
else
temp_public := false;
end if;
insert into game
(game_name, dev_id, public)
values ('', current_dev.id, temp_public ) returning id into temp_game_id;
update game
set game_name = 'Game ' || temp_game_id
where (game.id = temp_game_id );
end loop;
end loop;
end;
$$
create or replace procedure connect_games_to_featurepack()
language plpgsql as
$$
declare
private_games_curs cursor for select g.id from game g where (public = false);
all_private_games record;
game_feat_count int;
feat_count int;
Feat int;
begin
select count(*)
into feat_count
from featurepack;
open private_games_curs;
loop
fetch private_games_curs into all_private_games;
exit when not found;
Feat := all_private_games.id % feat_count;
if Feat <> 0 and Feat = 1 then
INSERT INTO public.game_feat (game_id, feat_id) VALUES(all_private_games.id, Feat);
INSERT INTO public.game_feat (game_id, feat_id) VALUES(all_private_games.id, Feat+1);
elseif Feat <> 0 then
INSERT INTO public.game_feat (game_id, feat_id) VALUES(all_private_games.id, Feat);
else
end if;
end loop;
close private_games_curs;
end;
$$
-- makes the game play record and the game_feat_rev
CREATE OR REPLACE PROCEDURE public.simulate_playing_games(IN number_of_plays_per_game integer DEFAULT 10)
LANGUAGE plpgsql
AS $procedure$
declare
customer_curs cursor for select c.id from customer c;
cust_rec record;
game_ record;
begin
open customer_curs;
loop
fetch customer_curs into cust_rec;
exit when not found;
for game_ in (
select g.game_name, g.id from customer
inner join cust_sub cs on (customer.id = cs.cust_id and customer.id = 1)
left join cust_sub_featurepk csf on (cs.id = csf.cust_subid)
left join featurepack f on (csf.featpkid = f.id)
left join game_feat gf on (f.id = gf.feat_id)
left join game g on (gf.game_id = g.id)
where (g.id is not null)
Union (select g.game_name , g.id from game g where (g.public = true))
)
loop
call play_game(game_.id, cust_rec.id);
end loop;
end loop;
close customer_curs;
end;
$procedure$
;
create or replace procedure find_renewable_fp()
language plpgsql
as $$
declare
renew record;
isactive bool;
begin
for renew in
select id , cust_subid, featpkid, current_term_end, date_of_origin, autorenew
from cust_sub_featurepk csf order by 4 desc loop
isactive = f.active from featurepack f where (renew.featpkid = f.id);
if(renew.autorenew and (now() - renew.current_term_end >= '1 second') and isactive = true) then
call renew_sub_fp(renew);
end if;
end loop;
end;$$
create or replace procedure renew_sub_fp(mycustsub record)
language plpgsql
as $$
declare
subprice money;
submonths int;
tempexp timestamp;
tempmonths text;
begin
subprice = fp.baseprice from cust_sub_featurepk csf
inner join featurepack fp on (fp.id = csf.featpkid)
where (csf.id = mycustsub.id);
submonths = s.numberofmonths from cust_sub
inner join sub s on (s.id = cust_sub.sub_id)
where (cust_sub.id = mycustsub.id);
tempmonths = submonths||' months';
tempexp = mycustsub.current_term_end + tempmonths::interval;
tempexp = date_trunc('month', tempexp);
tempmonths = date_part('day', mycustsub.date_of_origin)||' days';
tempexp = tempexp + tempmonths::interval;
if(date_part('day', tempexp) < date_part('day', mycustsub.date_of_origin)) then
tempexp = date_trunc('month', tempexp);
tempexp = tempexp + date_part(
'days',
(date_trunc('month', tempexp) + '1 month - 1 day'::interval)
);
end if;
if(mycustsub.autorenew = true) then
subprice = subprice - 1::money;
end if;
INSERT INTO public.cust_sub_feat_pay_hist
(cust_sub_feat_id, pay_date, amt, description)
VALUES(mycustsub.id, now(), subprice, 'renew subscription on featurepack');
UPDATE public.cust_sub_featurepk
SET current_term_start= current_term_end, current_term_end = (tempexp - '1 day'::interval)
WHERE id=mycustsub.id;
end;$$
create or replace procedure find_renewable()
language plpgsql
as $$
declare
renew record;
begin
for renew in
select id, cust_id, sub_id, current_term_exp, date_of_origin, autorenew, active
from cust_sub order by 4 desc
loop
if(renew.autorenew and (now() - renew.current_term_exp >= '1 second') and renew.active = true) then
call renew_sub(renew);
end if;
end loop;
end;$$
create or replace procedure renew_sub(mycustsub record)
language plpgsql
as $$
declare
subprice money;
submonths int;
tempexp timestamp;
tempmonths text;
begin
subprice = st.baseprice from cust_sub
inner join sub on (mycustsub.sub_id = sub.id)
inner join sub_tier st on (st.id = sub.tier_id)
where (cust_sub.id = mycustsub.id);
submonths = s.numberofmonths from cust_sub
inner join sub s on (s.id = cust_sub.sub_id)
where (cust_sub.id = mycustsub.id);
tempmonths = submonths||' months';
tempexp = mycustsub.current_term_exp + tempmonths::interval;
tempexp = date_trunc('month', tempexp);
tempmonths = date_part('day', mycustsub.date_of_origin)||' days';
tempexp = tempexp + tempmonths::interval;
if(date_part('day', tempexp) < date_part('day', mycustsub.date_of_origin)) then
tempexp = date_trunc('month', tempexp);
tempexp = tempexp + date_part(
'days',
(date_trunc('month', tempexp) + '1 month - 1 day'::interval)
);
end if;
if(mycustsub.autorenew = true) then
subprice = subprice * .85;
end if;
INSERT INTO public.cust_sub_pay_hist
(cust_sub_id, pay_date, amt, description)
VALUES(mycustsub.id, now(), subprice, 'renew subscription');
UPDATE public.cust_sub
SET current_term_start= current_term_exp, current_term_exp= tempexp - '1 day'::interval
WHERE id=mycustsub.id;
end;$$