-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project1.sql
6083 lines (5483 loc) · 396 KB
/
Project1.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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 9.4.1
-- Dumped by pg_dump version 9.4.1
-- Started on 2015-06-05 10:53:39
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;
--
-- TOC entry 200 (class 3079 OID 11855)
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- TOC entry 2277 (class 0 OID 0)
-- Dependencies: 200
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET search_path = public, pg_catalog;
--
-- TOC entry 597 (class 1247 OID 159240)
-- Name: action; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE action AS ENUM (
'buy',
'reserve',
'cancel'
);
ALTER TYPE action OWNER TO postgres;
--
-- TOC entry 600 (class 1247 OID 159248)
-- Name: days; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE days AS ENUM (
'MON',
'TUE',
'WED',
'THU',
'FRI',
'SAT',
'SUN'
);
ALTER TYPE days OWNER TO postgres;
--
-- TOC entry 603 (class 1247 OID 159264)
-- Name: sex; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE sex AS ENUM (
'M',
'F'
);
ALTER TYPE sex OWNER TO postgres;
--
-- TOC entry 606 (class 1247 OID 159270)
-- Name: type; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE type AS ENUM (
'gstaff',
'tagent'
);
ALTER TYPE type OWNER TO postgres;
--
-- TOC entry 213 (class 1255 OID 159275)
-- Name: calc_free_seats(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION calc_free_seats(v_fsc_id integer) RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
v_free_seats integer;
BEGIN
SELECT x.num_seats-(SELECT COUNT(*) r_seats FROM transactions t WHERE fschedule_id=v_fsc_id AND action<>'cancel' AND NOT EXISTS(SELECT tid FROM waitinglist WHERE tid=t.t_id)) free_seats FROM
(SELECT num_seats FROM aircraft_type at INNER JOIN aircraft a ON at.id = a.type_id
INNER JOIN flightschedule fs ON a.code=fs.aircraft_code WHERE fs.fschedule_id = v_fsc_id) x INTO v_free_seats;
RETURN v_free_seats;
END;
$$;
ALTER FUNCTION public.calc_free_seats(v_fsc_id integer) OWNER TO postgres;
--
-- TOC entry 214 (class 1255 OID 159276)
-- Name: calcs_1(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION calcs_1(v_tagent_id integer, OUT v_ag_id integer, OUT v_agent_name character varying, OUT v_sales_amount numeric) RETURNS SETOF record
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT am.agent_id, name, TotAmount FROM
(SELECT sum(amount) TotAmount, mt.id agent_id FROM get_trans_details t INNER JOIN madetransaction mt ON t.t_id=mt.t_id INNER JOIN cashier c ON t.t_id=c.tid WHERE t_date::date BETWEEN (date_trunc('week', (current_date)::timestamp)::date)
AND ((date_trunc('week', (current_date)::timestamp)+ '6 days'::interval)::date) AND mt.id=v_tagent_id AND action='buy' GROUP BY mt.id) am
INNER JOIN travelagency ta ON am.agent_id=ta.id;
END;
$$;
ALTER FUNCTION public.calcs_1(v_tagent_id integer, OUT v_ag_id integer, OUT v_agent_name character varying, OUT v_sales_amount numeric) OWNER TO postgres;
--
-- TOC entry 215 (class 1255 OID 159277)
-- Name: calcs_2(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION calcs_2() RETURNS TABLE(v_month text, v_total numeric)
LANGUAGE plpgsql
AS $$
BEGIN
-- ypologismos twn esodwn gia kathe mina tou trexontos etous
RETURN QUERY
WITH w AS ( SELECT month, sum(amount) AS total FROM (SELECT date_trunc('month',t_date) AS month, amount FROM get_trans_details gtd
INNER JOIN madetransaction mt ON gtd.t_id=mt.t_id
INNER JOIN cashier c ON gtd.t_id=c.tid) z GROUP BY month )
SELECT to_char(month, 'Mon-YYYY') AS month, COALESCE(total, 0) AS total
FROM (SELECT (SELECT date_trunc('year', CURRENT_DATE::timestamp)) + (interval '1' month * GENERATE_SERIES(0,11)) as month) m LEFT OUTER JOIN w USING(month);
END;
$$;
ALTER FUNCTION public.calcs_2() OWNER TO postgres;
--
-- TOC entry 216 (class 1255 OID 159278)
-- Name: calcs_3(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION calcs_3(OUT v_cnt_tr bigint, OUT v_fsc_id integer, OUT v_fday_week date, OUT v_lday_week date) RETURNS SETOF record
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT max(fl_count) cnt,fschedule_id,week::date, ((date_trunc('week', (week)::timestamp)+ '6 days'::interval)::date)
FROM
(
SELECT fschedule_id, week, count(*) fl_count
FROM
(
SELECT date_trunc('week',fdate)::timestamp as week, amount, fschedule_id,extract(week from fdate) week_num
FROM get_trans_details gtd
INNER JOIN madetransaction mt ON gtd.t_id=mt.t_id
INNER JOIN cashier c ON gtd.t_id=c.tid
) z GROUP BY fschedule_id, week
) v
GROUP BY week,fschedule_id
ORDER BY cnt desc LIMIT 1;
END;
$$;
ALTER FUNCTION public.calcs_3(OUT v_cnt_tr bigint, OUT v_fsc_id integer, OUT v_fday_week date, OUT v_lday_week date) OWNER TO postgres;
--
-- TOC entry 217 (class 1255 OID 159279)
-- Name: calcs_4(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION calcs_4(OUT v_fsc_id integer, OUT v_fday_week date, OUT v_lday_week date, OUT v_tot_amount numeric, OUT v_tot_expenses numeric, OUT v_net_amount numeric) RETURNS SETOF record
LANGUAGE plpgsql
AS $$
DECLARE
v_trans RECORD;
BEGIN
DROP TABLE IF EXISTS temp1;
CREATE TEMP TABLE temp1 AS
SELECT fschedule_id, week::date , ((date_trunc('week', (week)::timestamp)+ '6 days'::interval)::date) lday_week, sum(amount) AS fl_sales, 0.0 expenses
FROM
(
SELECT date_trunc('week',fdate)::timestamp AS week, amount, fschedule_id
FROM get_trans_details gtd
INNER JOIN madetransaction mt ON gtd.t_id=mt.t_id
INNER JOIN cashier c ON gtd.t_id=c.tid
) z GROUP BY fschedule_id, week;
FOR v_trans IN select fschedule_id, week, fl_sales FROM temp1 LOOP
UPDATE temp1 SET expenses=(
SELECT (CASE WHEN dom=1 THEN (30*dom_dr)+((15*dom_dr)*2) ELSE (60*dom_dr)+((30*dom_dr)*2) END) expenses FROM
(
SELECT (CASE WHEN dep_country='Greece' AND dest_country='Greece' THEN 1 ELSE 0 END) dom,
flight_hours(((arr_time)-(dep_time))::time)/60 dom_dr,dep_country, dest_country
FROM get_flight_details gfd
WHERE gfd.fschedule_id=v_trans.fschedule_id
)
q);
END LOOP;
RETURN query SELECT fschedule_id,week,lday_week,fl_sales,round(expenses,2),round((fl_sales-expenses),2) net_amount FROM temp1 ORDER BY net_amount desc LIMIT 1;
END;
$$;
ALTER FUNCTION public.calcs_4(OUT v_fsc_id integer, OUT v_fday_week date, OUT v_lday_week date, OUT v_tot_amount numeric, OUT v_tot_expenses numeric, OUT v_net_amount numeric) OWNER TO postgres;
--
-- TOC entry 218 (class 1255 OID 159280)
-- Name: calcs_5(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION calcs_5(OUT v_flight_code integer, OUT v_dep_city character varying, OUT v_fl_date timestamp without time zone, OUT v_aircraft_code integer, OUT v_type character varying) RETURNS SETOF record
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY SELECT gfd.fschedule_id,gfd.dep_city,gfd.fdate+gfd.dep_time fl_date,gfd.aircraft_code,air_tp.type
FROM get_flight_details gfd INNER JOIN aircraft_type air_tp ON gfd.aircraft_type=air_tp.id
WHERE date_part('month',fdate::timestamp)=(date_part('month',CURRENT_DATE)-1) AND gfd.dep_city='Athens' ORDER BY fl_date, fschedule_id;
END;
$$;
ALTER FUNCTION public.calcs_5(OUT v_flight_code integer, OUT v_dep_city character varying, OUT v_fl_date timestamp without time zone, OUT v_aircraft_code integer, OUT v_type character varying) OWNER TO postgres;
--
-- TOC entry 219 (class 1255 OID 159281)
-- Name: cancel_by_company(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION cancel_by_company() RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_wtr_id integer;
v_type type;
v_new_trdate timestamp;
v_trans RECORD;
v_flights get_flight_details%ROWTYPE;
BEGIN
SELECT localtimestamp INTO v_new_trdate;
FOR v_flights IN SELECT gfd.fschedule_id FROM get_flight_details gfd
WHERE v_new_trdate BETWEEN ((gfd.fdate+gfd.dep_time)-'12:00:00'::interval) AND ((gfd.fdate+gfd.dep_time)-'04:00:00'::interval) LOOP
FOR v_trans IN SELECT gtd.t_id, gtd.price, gtd.fschedule_id, (gtd.fdate+gtd.dep_time) v_dep_time
FROM get_trans_details gtd WHERE (gtd.fschedule_id=v_flights.fschedule_id) AND gtd.action='reserve' LOOP
-- allazoume to action tis sinallagis se cancel
UPDATE transactions SET action='cancel', t_date=v_new_trdate WHERE t_id=v_trans.t_id AND action='reserve';
DELETE FROM reservation WHERE tid=v_trans.t_id;
IF calc_free_seats(v_trans.fschedule_id)>0 THEN
-- pairnoume to id tis sinallagis pou eina ston pinaka waitinglist kai i opoia tha proxwrisei se agora tou eisitiriou
SELECT t_id FROM transactions t INNER JOIN waitinglist w on t.t_id=w.tid
WHERE t.fschedule_id=v_trans.fschedule_id ORDER BY t_id,t_date LIMIT 1 INTO v_wtr_id;
IF NOT FOUND THEN
EXIT;
END IF;
-- pairnoume poios ekane tin sinallagi pou vrisketai ston pinaka waitinglist
SELECT type FROM madetransaction WHERE t_id=v_wtr_id INTO v_type;
-- elegxw an perasan 4 wres prin ginei i ptisi, kathws toulaxiston 4 wres prin tin ptisi mporei na ginei agora
-- eisitiriou apo kapoion pou itan sto waitinglist
IF (localtimestamp)<=(v_trans.v_dep_time-'04:00'::interval) THEN
-- allazoume to action tis sinallagis pou pairnoume apo to waitinglist se buy
UPDATE transactions SET action='buy', t_date=v_new_trdate WHERE t_id=v_wtr_id;
-- elegxoume an autos pou ekleise tin sinallagi einai taksidiwtikos praktoras
IF v_type='tagent' THEN
INSERT INTO cashier(tid,amount) VALUES (v_wtr_id,v_trans.price*0.988);
ELSE
INSERT INTO cashier(tid,amount) VALUES (v_wtr_id,v_trans.price);
END IF;
-- afou oloklirwthei i agora tou eisitiriou, diagrafoume tin sinallagi apo ton pinaka waitinglist
DELETE FROM waitinglist WHERE tid=v_wtr_id;
END IF;
END IF;
END LOOP;
END LOOP;
END;
$$;
ALTER FUNCTION public.cancel_by_company() OWNER TO postgres;
--
-- TOC entry 220 (class 1255 OID 159282)
-- Name: cancel_by_customer(integer, integer, date); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION cancel_by_customer(v_c_id integer, v_fschedule_id integer, v_fl_date date) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_tr_id integer;
v_wtr_id integer;
v_type type;
v_dep_time timestamp;
v_dep_time_wtr timestamp;
v_price numeric;
v_rcnt integer;
BEGIN
SELECT t_id, (fdate+dep_time) v_dep_time FROM get_trans_details WHERE c_id=v_c_id AND action='reserve' AND fschedule_id=v_fschedule_id
AND fdate=v_fl_date INTO v_tr_id, v_dep_time;
SELECT 1 FROM reservation WHERE tid=v_tr_id INTO v_rcnt;
IF v_rcnt=1 THEN
-- elegxw an perasan 12 wres prin ginei i ptisi
IF (localtimestamp)<=(v_dep_time-'12:00'::interval) THEN
UPDATE transactions SET action='cancel' WHERE t_id=v_tr_id;
DELETE FROM reservation WHERE tid=v_tr_id;
-- pairnoume to id tis sinallagis pou eina ston pinaka waitinglist kai i opoia tha proxwrisei se agora tou eisitiriou
SELECT t_id, (fdate+dep_time), price FROM get_trans_details gtd INNER JOIN waitinglist w on gtd.t_id=w.tid
WHERE gtd.fschedule_id=v_fschedule_id ORDER BY t_id,t_date LIMIT 1 INTO v_wtr_id, v_dep_time_wtr, v_price;
IF NOT FOUND THEN
EXIT;
END IF;
-- pairnoume poios ekane tin sinallagi pou vrisketai ston pinaka waitinglist
SELECT type FROM madetransaction WHERE t_id=v_wtr_id INTO v_type;
-- elegxw an perasan 4 wres prin ginei i ptisi,
-- kathws toulaxiston 4 wres prin tin ptisi mporei na ginei agora eisitiriou apo kapoion pou itan sto waitinglist
IF (localtimestamp)<=(v_dep_time_wtr-'04:00'::interval) THEN
-- allazoume to action tis sinallagis pou pairnoume apo to waitinglist se buy
UPDATE transactions SET action='buy' WHERE t_id=v_wtr_id;
-- elegxoume an autos pou ekleise tin sinallagi einai taksidiwtikos praktoras
IF v_type='tagent' THEN
INSERT INTO cashier(tid,amount) VALUES (v_wtr_id,v_price*0.988);
ELSE
INSERT INTO cashier(tid,amount) VALUES (v_wtr_id,v_price);
END IF;
-- afou oloklirwthei i agora tou eisitiriou, diagrafoume tin sinallagi apo ton pinaka waitinglist
DELETE FROM waitinglist WHERE tid=v_wtr_id;
END IF;
ELSE
RAISE EXCEPTION 'You can cancel your ticket until 12 hours before departure time.';
END IF;
ELSE
UPDATE transactions SET action='cancel' WHERE t_id=v_tr_id;
DELETE FROM waitinglist WHERE tid=v_tr_id;
END IF;
RETURN;
END;
$$;
ALTER FUNCTION public.cancel_by_customer(v_c_id integer, v_fschedule_id integer, v_fl_date date) OWNER TO postgres;
--
-- TOC entry 221 (class 1255 OID 159283)
-- Name: check_flight_fstaff(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION check_flight_fstaff(v_fschedule_id integer) RETURNS text
LANGUAGE plpgsql
AS $$
DECLARE
pils integer;
atts integer;
output text;
BEGIN
IF (SELECT COUNT(*) FROM flightschedule WHERE fschedule_id=v_fschedule_id)=0 THEN
RAISE EXCEPTION 'Flight % not found.', v_fschedule_id;
END IF;
SELECT count(*) from staffschedule ss inner join fspilots pil ON ss.emp_id=pil.emp_id WHERE fschedule_id=v_fschedule_id INTO pils;
SELECT count(*) from staffschedule ss inner join fsattendant att ON ss.emp_id=att.emp_id WHERE fschedule_id=v_fschedule_id INTO atts;
IF pils<1 OR atts<2 THEN
output='Flight ' || v_fschedule_id || ' isnt ready to go.'; -- || : concatenate
ELSE
output='Flight ' || v_fschedule_id || ' is ready to go.';
END IF;
RETURN output;
END;
$$;
ALTER FUNCTION public.check_flight_fstaff(v_fschedule_id integer) OWNER TO postgres;
--
-- TOC entry 222 (class 1255 OID 159284)
-- Name: delete_by_company(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION delete_by_company() RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_trans transactions%ROWTYPE;
v_flights get_flight_details%ROWTYPE;
BEGIN
FOR v_flights IN SELECT gfd.fschedule_id FROM get_flight_details gfd INNER JOIN flightdone fld ON gfd.fschedule_id=fld.fschedule_id
WHERE (localtimestamp-(gfd.fdate+gfd.dep_time))>='12:00'::interval LOOP
FOR v_trans IN (SELECT t.t_id FROM transactions t inner join reservation r on t.t_id=r.tid WHERE t.fschedule_id=v_flights.fschedule_id
union
SELECT t.t_id FROM transactions t inner join waitinglist w on t.t_id=w.tid WHERE t.fschedule_id=v_flights.fschedule_id) LOOP
-- diagrafoume tis sinallages apo tous pinakes reservation kai waitinglist
DELETE FROM reservation WHERE tid=v_trans.t_id;
DELETE FROM waitinglist WHERE tid=v_trans.t_id;
END LOOP;
END LOOP;
END;
$$;
ALTER FUNCTION public.delete_by_company() OWNER TO postgres;
--
-- TOC entry 223 (class 1255 OID 159285)
-- Name: flight_hours(time without time zone); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION flight_hours(t time without time zone) RETURNS numeric
LANGUAGE plpgsql
AS $$
DECLARE
m double precision;
s numeric;
BEGIN
SELECT (EXTRACT(EPOCH FROM t)/60) INTO m;
SELECT round(m::numeric, 2) INTO s;
RETURN s;
END;
$$;
ALTER FUNCTION public.flight_hours(t time without time zone) OWNER TO postgres;
--
-- TOC entry 224 (class 1255 OID 159286)
-- Name: insert_agent_transaction(integer, integer, action, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_agent_transaction(v_fschedule_id integer, v_customer_id integer, v_action action, v_agent_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_tran_date timestamp;
v_id integer;
v_t_id integer;
v_freeseats integer;
v_amount numeric;
v_trans_time timestamp;
v_dep_time timestamp;
cur_fl integer;
v_dep1 integer;
v_dest1 integer;
v_dep2 integer;
v_dest2 integer;
old_action action;
v_dep_country character varying;
v_dest_country character varying;
BEGIN
SELECT localtimestamp INTO v_tran_date;
IF (SELECT COUNT(*) FROM travelagency WHERE id=v_agent_id)=0 THEN
RAISE EXCEPTION 'No tagent with id % found.', v_agent_id;
END IF;
SELECT coalesce(MAX(t_id),0) FROM transactions INTO v_id;
-- an to action einai cancel, tote apotrepw ston xristi na kanei kapoia sinallagi
IF v_action='cancel' THEN
RAISE EXCEPTION 'You can only reserve or buy tickets.';
END IF;
-- elegxw an o pelatis exei kanei idi sinallagi gia tin ptisi auti
SELECT count(*) FROM transactions tr WHERE c_id=v_customer_id AND fschedule_id=v_fschedule_id INTO cur_fl;
IF cur_fl>0 THEN
RAISE EXCEPTION 'Customer % has already reserved or bought ticket for flight % .',v_customer_id,v_fschedule_id;
END IF;
-- elegxw an i ptisi pou paei na kanei sinallagi epikaliptetai me kapoia stin opoia exei idi kanei sinallagi
SELECT price, fdate+dep_time, departure, destination, dep_country, dest_country FROM get_flight_details
WHERE fschedule_id=v_fschedule_id INTO v_amount,v_dep_time, v_dep1, v_dest1,v_dep_country, v_dest_country;
IF EXISTS (SELECT 1 FROM get_trans_details WHERE c_id=v_customer_id
AND v_dep_time BETWEEN (fdate+dep_time)-'00:30'::interval AND (fdate+dep_time)+(arr_time-dep_time)+'00:30'::interval) THEN
RAISE EXCEPTION 'Customer % has a programmed flight in same hour and date.',v_customer_id;
END IF;
IF (SELECT count(*) FROM get_trans_details t1,get_trans_details t2
WHERE t1.departure=t2.departure AND t1.fdate=t2.fdate AND t1.c_id=t2.c_id AND t2.c_id=v_customer_id
AND t2.fdate=(select fdate from get_flight_details where fschedule_id=v_fschedule_id) AND t2.action<>'cancel') > 0 THEN
RAISE EXCEPTION 'You cant fly over than one time from the same airport in the same day.';
END IF;
-- den mporei na ginei kratisi eisitiriou an perasoun 12 wres prin tin pragmatopoiisi tis ptisis
IF v_tran_date>=(v_dep_time-'12:00'::interval) AND v_action='reserve' THEN
RAISE EXCEPTION 'You cant reserve ticket for flight % .',v_fschedule_id;
END IF;
-- den mporei na ginei agora eisitiriou an perasoun 4 wres prin tin pragmatopoiisi tis ptisis
IF v_tran_date>=(v_dep_time-'04:00'::interval) AND v_action='buy' THEN
RAISE EXCEPTION 'You cant buy ticket for flight % .',v_fschedule_id;
END IF;
-- pairnw ta dedomena tis kratisis pou exei kanei autos o pelatis tin idia mera
SELECT t_date, departure, destination, action FROM get_trans_details
WHERE c_id=v_customer_id and t_date::date=v_tran_date::date ORDER BY t_date desc LIMIT 1 INTO v_trans_time, v_dep2, v_dest2, old_action;
IF (v_dest2=v_dep1) AND (v_dest1=v_dep2) AND v_action='buy' AND old_action='buy' THEN
IF v_dep_country<>'Greece' OR v_dest_country<>'Greece' THEN
v_amount=v_amount*0.85;
ELSE
v_amount=v_amount*0.92;
END IF;
END IF;
-- get free seats
SELECT calc_free_seats(v_fschedule_id) INTO v_freeseats;
-- an to action einai buy kai den yparxoun eleutheres theseis sto aeroplano, tote allazw to action apo buy se reserve
IF v_action='buy' AND v_freeseats=0 THEN
v_action='reserve';
END IF;
-- insert new transaction in table transactions and table madetransaction
INSERT INTO transactions(t_id,fschedule_id,c_id,action,t_date) VALUES (v_id+1,v_fschedule_id,v_customer_id,v_action,v_tran_date) RETURNING t_id INTO v_t_id;
INSERT INTO madetransaction(id,t_id,type) VALUES (v_agent_id, v_t_id, 'tagent');
-- an to action einai reserve kai den yparxoun kenes theseis sto aeroplano, tote vazw tin sinallagi auti kai ston pinaka waitinglist
IF v_action='reserve' AND v_freeseats=0 THEN
INSERT INTO waitinglist(tid) VALUES (v_t_id);
RAISE NOTICE 'There arent any free seats for this flight and you have been added to waiting list.';
END IF;
-- an to action einai reserve kai yparxoun kenes theseis sto aeroplano, tote vazw tin sinallagi auti kai ston pinaka reservation
IF v_action='reserve' AND v_freeseats>0 THEN
INSERT INTO reservation(tid) VALUES (v_t_id);
END IF;
-- an to action einai buy kai yparxoun kenes theseis sto aeroplano, tote vazw tin sinallagi auti kai ston pinaka cashier
-- epeidi i sinallagi kleinetai apo taksidiwtiko grafeio, to amount pou mpainei ston pinaka cashier einai to 98.8% tis aksias tou eisitiriou
IF v_action='buy' AND v_freeseats>0 THEN
INSERT INTO cashier(tid,amount) VALUES (v_t_id,v_amount*0.988);
END IF;
END;
$$;
ALTER FUNCTION public.insert_agent_transaction(v_fschedule_id integer, v_customer_id integer, v_action action, v_agent_id integer) OWNER TO postgres;
--
-- TOC entry 225 (class 1255 OID 159288)
-- Name: insert_aircraft(integer, numeric, smallint); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_aircraft(v_type_id integer, v_total_hours numeric, v_status smallint) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
tot_aircrafts integer;
v_code integer;
BEGIN
SELECT coalesce(MAX(code),0) FROM aircraft INTO v_code;
SELECT count(*) FROM aircraft INTO tot_aircrafts;
IF tot_aircrafts = 7 THEN
RAISE EXCEPTION 'You cannot insert anymore aircrafts.';
END IF;
IF v_total_hours>=150 AND v_status=1 THEN
RAISE EXCEPTION 'Aircraft has the max flight hours.';
ELSE
INSERT INTO aircraft(code,type_id,total_hours,status) VALUES (v_code+1,v_type_id,v_total_hours,v_status);
END IF;
END;
$$;
ALTER FUNCTION public.insert_aircraft(v_type_id integer, v_total_hours numeric, v_status smallint) OWNER TO postgres;
--
-- TOC entry 226 (class 1255 OID 159289)
-- Name: insert_aircraft_type(character varying, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_aircraft_type(v_type character varying, v_num_seats integer) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
tot_aircraftypes integer;
v_id integer;
BEGIN
SELECT coalesce(MAX(id),0) FROM aircraft_type INTO v_id;
SELECT count(*) FROM aircraft_type INTO tot_aircraftypes;
IF tot_aircraftypes = 3 THEN
RAISE EXCEPTION 'You cannot insert anymore aircraft types.';
ELSE
INSERT INTO aircraft_type(id,type,num_seats) VALUES (v_id+1,v_type,v_num_seats);
END IF;
END;
$$;
ALTER FUNCTION public.insert_aircraft_type(v_type character varying, v_num_seats integer) OWNER TO postgres;
--
-- TOC entry 227 (class 1255 OID 159290)
-- Name: insert_airport(character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_airport(v_name character varying, v_city character varying, v_country character varying, v_shortcut character varying) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
tot_airports integer;
v_id integer;
BEGIN
SELECT coalesce(MAX(code),0) FROM airport INTO v_id;
SELECT count(*) FROM airport INTO tot_airports;
IF tot_airports = 6 THEN
RAISE EXCEPTION 'You cannot insert anymore airports.';
ELSE
INSERT INTO airport(code,name,city,country,shortcut) VALUES (v_id+1,v_name,v_city,v_country,v_shortcut);
END IF;
END;
$$;
ALTER FUNCTION public.insert_airport(v_name character varying, v_city character varying, v_country character varying, v_shortcut character varying) OWNER TO postgres;
--
-- TOC entry 228 (class 1255 OID 159291)
-- Name: insert_attendants(character varying, character varying, character varying, date, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_attendants(v_fname character varying, v_lname character varying, v_job character varying, v_birthdate date, v_phone character varying, v_address character varying, v_nativelang character varying) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_id integer;
cnt_attendants integer;
BEGIN
SELECT coalesce(MAX(id),0) FROM fstaff INTO v_id;
SELECT COUNT(*) FROM fstaff WHERE job='Attendant' INTO cnt_attendants;
IF cnt_attendants = 20 THEN
RAISE EXCEPTION 'You cannot insert anymore attendants.';
ELSE
INSERT INTO fstaff(id, fname, lname, job, birthdate, phone, address) VALUES(v_id+1, v_fname, v_lname, v_job, v_birthdate, v_phone, v_address);
INSERT INTO fsattendant(emp_id, native_lang) VALUES(v_id+1, v_nativelang);
END IF;
END;
$$;
ALTER FUNCTION public.insert_attendants(v_fname character varying, v_lname character varying, v_job character varying, v_birthdate date, v_phone character varying, v_address character varying, v_nativelang character varying) OWNER TO postgres;
--
-- TOC entry 229 (class 1255 OID 159292)
-- Name: insert_authoritytests(character varying, integer, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_authoritytests(v_authorityname character varying, v_pass_rank integer, v_description text) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_id integer;
BEGIN
SELECT coalesce(MAX(testcode),0) FROM authoritytests INTO v_id;
INSERT INTO authoritytests(testcode,authorityname,pass_rank,description) VALUES (v_id+1,v_authorityname,v_pass_rank,v_description);
END;
$$;
ALTER FUNCTION public.insert_authoritytests(v_authorityname character varying, v_pass_rank integer, v_description text) OWNER TO postgres;
--
-- TOC entry 230 (class 1255 OID 159293)
-- Name: insert_customer(character varying, character varying, integer, sex, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_customer(v_fname character varying, v_lname character varying, v_age integer, v_sex sex, v_phone character varying) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_id integer;
BEGIN
SELECT coalesce(MAX(id),0) FROM customer INTO v_id;
INSERT INTO customer(id,fname,lname,age,sex,phone) VALUES (v_id+1,v_fname,v_lname,v_age,v_sex,v_phone);
END;
$$;
ALTER FUNCTION public.insert_customer(v_fname character varying, v_lname character varying, v_age integer, v_sex sex, v_phone character varying) OWNER TO postgres;
--
-- TOC entry 231 (class 1255 OID 159294)
-- Name: insert_expertise(integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_expertise(v_emp_id integer, v_air_type integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
IF v_air_type NOT IN (SELECT id FROM aircraft_type) THEN
RAISE EXCEPTION 'The % aircraft type doesnt exist.',v_air_type;
END IF;
IF v_emp_id NOT IN (SELECT emp_id FROM fspilots) THEN
RAISE EXCEPTION 'The % pilot doesnt exist.',v_emp_id;
END IF;
INSERT INTO expertise(emp_id,aircraft_type) VALUES (v_emp_id,v_air_type);
END;
$$;
ALTER FUNCTION public.insert_expertise(v_emp_id integer, v_air_type integer) OWNER TO postgres;
--
-- TOC entry 232 (class 1255 OID 159295)
-- Name: insert_flight(integer, integer, time without time zone, time without time zone, numeric); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_flight(v_departure integer, v_destination integer, v_dep_time time without time zone, v_arr_time time without time zone, v_price numeric) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_id integer;
dr interval;
BEGIN
SELECT v_arr_time - v_dep_time INTO dr;
SELECT coalesce(MAX(fcode),0) FROM flight INTO v_id;
INSERT INTO flight(fcode,departure,destination,dep_time,arr_time,price) VALUES (v_id+1,v_departure,v_destination,v_dep_time,v_arr_time,v_price);
INSERT INTO flight(fcode,departure,destination,dep_time,arr_time,price) VALUES (v_id+2,v_destination,v_departure,v_arr_time+'00:30:00',v_arr_time+dr+'00:30:00',v_price);
END;
$$;
ALTER FUNCTION public.insert_flight(v_departure integer, v_destination integer, v_dep_time time without time zone, v_arr_time time without time zone, v_price numeric) OWNER TO postgres;
--
-- TOC entry 233 (class 1255 OID 159296)
-- Name: insert_flight_days(integer, days); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_flight_days(v_fcode integer, v_days days) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO flight_days(fcode,days) VALUES (v_fcode,v_days);
END;
$$;
ALTER FUNCTION public.insert_flight_days(v_fcode integer, v_days days) OWNER TO postgres;
--
-- TOC entry 234 (class 1255 OID 159297)
-- Name: insert_flightsprogram(integer, integer, date, date); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_flightsprogram(v_aircraft_type integer, v_flight_code integer, v_start_date date, v_end_date date) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_program_id integer;
cntr character varying;
air_type character varying;
v_start_date_2 date;
v_end_date_2 date;
v_dep_city character varying;
day_1 character varying[];
day_2 character varying;
BEGIN
IF (SELECT COUNT(*) FROM flight WHERE fcode=v_flight_code) = 0 THEN
RAISE EXCEPTION 'Flight % does not exists.', v_flight_code;
END IF;
IF (SELECT COUNT(*) FROM aircraft_type WHERE id=v_aircraft_type) = 0 THEN
RAISE EXCEPTION 'Aircraft type % does not exists.', v_aircraft_type;
END IF;
SELECT city FROM airport a INNER JOIN flight f on a.code=f.departure WHERE f.fcode=v_flight_code INTO v_dep_city;
IF v_dep_city<>'Athens' THEN
RAISE EXCEPTION 'You must insert a basic flight.';
END IF;
SELECT coalesce(MAX(program_id),0) FROM flightsprogram INTO v_program_id;
SELECT ar.country FROM airport ar,
(SELECT * FROM airport a INNER JOIN flight f ON a.code=f.departure AND city='Athens') x
WHERE ar.code=x.destination AND x.fcode=v_flight_code INTO cntr;
SELECT type FROM aircraft_type airtype WHERE airtype.id=v_aircraft_type INTO air_type;
IF air_type='Boeing737' THEN
IF cntr='Greece' THEN
INSERT INTO flightsprogram(aircraft_type,flight_code,start_date,end_date,program_id) VALUES (v_aircraft_type,v_flight_code,v_start_date,v_end_date,v_program_id+1);
INSERT INTO flightsprogram(aircraft_type,flight_code,start_date,end_date,program_id) VALUES (v_aircraft_type,v_flight_code+1,v_start_date,v_end_date,v_program_id+2);
ELSE
RAISE EXCEPTION 'Aircraft type % cannot join international flights.',v_aircraft_type;
END IF;
ELSE
INSERT INTO flightsprogram(aircraft_type,flight_code,start_date,end_date,program_id) VALUES (v_aircraft_type,v_flight_code,v_start_date,v_end_date,v_program_id+1);
INSERT INTO flightsprogram(aircraft_type,flight_code,start_date,end_date,program_id) VALUES (v_aircraft_type,v_flight_code+1,v_start_date,v_end_date,v_program_id+2);
END IF;
END;
$$;
ALTER FUNCTION public.insert_flightsprogram(v_aircraft_type integer, v_flight_code integer, v_start_date date, v_end_date date) OWNER TO postgres;
--
-- TOC entry 235 (class 1255 OID 159298)
-- Name: insert_gstaff(character varying, character varying, character varying, date, character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_gstaff(v_fname character varying, v_lname character varying, v_job character varying, v_birthdate date, v_phone character varying, v_address character varying) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_id integer;
v_salary numeric;
BEGIN
SELECT coalesce(MAX(id),0) FROM gstaff INTO v_id;
IF (v_job='Engineer') THEN
v_salary = 2000;
ELSE
v_salary = 1200;
END IF;
INSERT INTO gstaff(id, fname, lname, job, birthdate, phone, address, salary) VALUES(v_id+1, v_fname, v_lname, v_job, v_birthdate, v_phone, v_address, v_salary);
END;
$$;
ALTER FUNCTION public.insert_gstaff(v_fname character varying, v_lname character varying, v_job character varying, v_birthdate date, v_phone character varying, v_address character varying) OWNER TO postgres;
--
-- TOC entry 236 (class 1255 OID 159299)
-- Name: insert_gstaff_transaction(integer, integer, action, integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_gstaff_transaction(v_fschedule_id integer, v_customer_id integer, v_action action, v_gstaff_id integer) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_tran_date timestamp;
v_id integer;
v_t_id integer;
v_freeseats integer;
v_amount numeric;
v_trans_time timestamp;
v_dep_time timestamp;
cur_fl integer;
v_dep1 integer;
v_dest1 integer;
v_dep2 integer;
v_dest2 integer;
old_action action;
v_dep_country character varying;
v_dest_country character varying;
BEGIN
SELECT localtimestamp INTO v_tran_date;
IF (SELECT COUNT(*) FROM gstaff WHERE id=v_gstaff_id)=0 THEN
RAISE EXCEPTION 'No gstaff member with id % found.',v_gstaff_id;
END IF;
IF (SELECT job FROM gstaff WHERE id=v_gstaff_id)='Engineer' THEN
RAISE EXCEPTION 'Ground staff % isnt Employee.',v_gstaff_id;
END IF;
-- an to action einai cancel, tote apotrepw ston xristi na kanei kapoia sinallagi
IF v_action='cancel' THEN
RAISE EXCEPTION 'You can only reserve or buy tickets.';
END IF;
-- elegxw an o pelatis exei kanei idi sinallagi gia tin ptisi auti
SELECT count(*) FROM transactions tr WHERE c_id=v_customer_id AND fschedule_id=v_fschedule_id INTO cur_fl;
IF cur_fl>0 THEN
RAISE EXCEPTION 'Customer % has already reserved or bought ticket for flight % .',v_customer_id,v_fschedule_id;
END IF;
-- elegxw an i ptisi pou paei na kanei sinallagi epikaliptetai me kapoia stin opoia exei idi kanei sinallagi
SELECT price, fdate+dep_time, departure, destination, dep_country, dest_country FROM get_flight_details
WHERE fschedule_id=v_fschedule_id INTO v_amount,v_dep_time, v_dep1, v_dest1,v_dep_country, v_dest_country;
IF EXISTS (SELECT 1 FROM get_trans_details WHERE c_id=v_customer_id AND v_dep_time BETWEEN (fdate+dep_time)-'00:30'::interval AND (fdate+dep_time)+(arr_time-dep_time)+'00:30'::interval) THEN
RAISE EXCEPTION 'Customer % has a programmed flight in same hour and date.',v_customer_id;
END IF;
IF (SELECT count(*) FROM get_trans_details t1,get_trans_details t2
WHERE t1.departure=t2.departure AND t1.fdate=t2.fdate AND t1.c_id=t2.c_id AND t2.c_id=v_customer_id
AND t2.fdate=(select fdate from get_flight_details where fschedule_id=v_fschedule_id) AND t2.action<>'cancel') > 0 THEN
RAISE EXCEPTION 'You cant fly over than one time from the same airport in the same day.';
END IF;
-- den mporei na ginei kratisi eisitiriou an perasoun 12 wres prin tin pragmatopoiisi tis ptisis
IF v_tran_date>=(v_dep_time-'12:00'::interval) AND v_action='reserve' THEN
RAISE EXCEPTION 'You cant reserve ticket for flight % .',v_fschedule_id;
END IF;
-- den mporei na ginei agora eisitiriou an perasoun 4 wres prin tin pragmatopoiisi tis ptisis
IF v_tran_date>=(v_dep_time-'04:00'::interval) AND v_action='buy' THEN
RAISE EXCEPTION 'You cant buy ticket for flight % .',v_fschedule_id;
END IF;
-- pairnw ta dedomena tis kratisis pou exei kanei autos o pelatis tin idia mera
SELECT t_date, departure, destination, action FROM get_trans_details WHERE c_id=v_customer_id and t_date::date=v_tran_date::date ORDER BY t_date desc LIMIT 1 INTO v_trans_time, v_dep2, v_dest2, old_action;
IF (v_dest2=v_dep1) AND (v_dest1=v_dep2) AND v_action='buy' AND old_action='buy' THEN
IF v_dep_country<>'Greece' OR v_dest_country<>'Greece' THEN
v_amount=v_amount*0.85;
ELSE
v_amount=v_amount*0.92;
END IF;
END IF;
SELECT coalesce(MAX(t_id),0) FROM transactions INTO v_id;
-- get free seats
SELECT calc_free_seats(v_fschedule_id) INTO v_freeseats;
-- an to action einai buy kai den yparxoun eleutheres theseis sto aeroplano, tote allazw to action apo buy se reserve
IF v_action='buy' AND v_freeseats=0 THEN
v_action='reserve';
END IF;
-- insert new transaction in table transactions and table madetransaction
INSERT INTO transactions(t_id,fschedule_id,c_id,action,t_date) VALUES (v_id+1,v_fschedule_id,v_customer_id,v_action,v_tran_date) RETURNING t_id INTO v_t_id;
INSERT INTO madetransaction(id,t_id,type) VALUES (v_gstaff_id, v_t_id, 'gstaff');
-- an to action einai reserve kai den yparxoun kenes theseis sto aeroplano, tote vazw tin sinallagi auti kai ston pinaka waitinglist
IF v_action='reserve' AND v_freeseats=0 THEN
INSERT INTO waitinglist(tid) VALUES (v_t_id);
RAISE NOTICE 'There arent any free seats for this flight and you have been added to waiting list.';
END IF;
-- an to action einai reserve kai yparxoun kenes theseis sto aeroplano, tote vazw tin sinallagi auti kai ston pinaka reservation
IF v_action='reserve' AND v_freeseats>0 THEN
INSERT INTO reservation(tid) VALUES (v_t_id);
END IF;
-- an to action einai buy kai yparxoun kenes theseis sto aeroplano, tote vazw tin sinallagi auti kai ston pinaka cashier
IF v_action='buy' AND v_freeseats>0 THEN
INSERT INTO cashier(tid,amount) VALUES (v_t_id,v_amount);
END IF;
END;
$$;
ALTER FUNCTION public.insert_gstaff_transaction(v_fschedule_id integer, v_customer_id integer, v_action action, v_gstaff_id integer) OWNER TO postgres;
--
-- TOC entry 237 (class 1255 OID 159301)
-- Name: insert_language(character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_language(v_lang_code character varying, v_name character varying) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO language(lang_code,name) VALUES (v_lang_code,v_name);
END;
$$;
ALTER FUNCTION public.insert_language(v_lang_code character varying, v_name character varying) OWNER TO postgres;
--
-- TOC entry 238 (class 1255 OID 159302)
-- Name: insert_pilots(character varying, character varying, character varying, date, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_pilots(v_fname character varying, v_lname character varying, v_job character varying, v_birthdate date, v_phone character varying, v_address character varying, v_degree character varying) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_id integer;
cnt_pilots integer;
BEGIN
SELECT coalesce(MAX(id),0) FROM fstaff INTO v_id;
SELECT COUNT(*) FROM fstaff WHERE job='Pilot' INTO cnt_pilots;
IF cnt_pilots = 15 THEN
RAISE EXCEPTION 'You cannot insert anymore pilots.';
ELSE
INSERT INTO fstaff(id, fname, lname, job, birthdate, phone, address) VALUES(v_id+1, v_fname, v_lname, v_job, v_birthdate, v_phone, v_address);
INSERT INTO fspilots(emp_id, degree) VALUES(v_id+1, v_degree);
END IF;
END;
$$;
ALTER FUNCTION public.insert_pilots(v_fname character varying, v_lname character varying, v_job character varying, v_birthdate date, v_phone character varying, v_address character varying, v_degree character varying) OWNER TO postgres;
--
-- TOC entry 239 (class 1255 OID 159303)
-- Name: insert_services(integer, integer, timestamp without time zone, integer, integer, smallint); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_services(v_emp_id integer, v_aircraft_id integer, v_service_date timestamp without time zone, v_rank integer, v_test_id integer, v_status smallint) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
psrank integer;
stat smallint;
gs_id integer;
BEGIN
SELECT status from aircraft where code=v_aircraft_id INTO stat;
IF stat=1 THEN
RAISE EXCEPTION 'The aircraft % can join flights.',v_aircraft_id;
END IF;
IF (SELECT COUNT(*) FROM gstaff WHERE id=v_emp_id)=0 THEN
RAISE EXCEPTION 'No gstaff member with id % found.',v_emp_id;
END IF;
IF (SELECT job FROM gstaff WHERE id=v_emp_id)='Employee' THEN
RAISE EXCEPTION 'Ground staff % isnt engineer.',v_emp_id;
END IF;
SELECT pass_rank from authoritytests where testcode=v_test_id INTO psrank;
IF v_rank < psrank OR v_status=0 THEN
RAISE EXCEPTION 'The aircraft % doesnt pass the test.',v_aircraft_id;
ELSE
INSERT INTO services(emp_id,aircraft_id,service_date,rank,test_id,status) VALUES (v_emp_id,v_aircraft_id,v_service_date,v_rank,v_test_id,v_status);
UPDATE aircraft SET status=1 WHERE code=v_aircraft_id;
END IF;
END;
$$;
ALTER FUNCTION public.insert_services(v_emp_id integer, v_aircraft_id integer, v_service_date timestamp without time zone, v_rank integer, v_test_id integer, v_status smallint) OWNER TO postgres;
--
-- TOC entry 240 (class 1255 OID 159304)
-- Name: insert_spoken_langs(integer, character varying); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION insert_spoken_langs(v_id integer, v_otherlangs character varying) RETURNS void
LANGUAGE plpgsql
AS $$