1 |
|
---|
2 | drop table if exists project.Policy;
|
---|
3 | drop table if exists project.Customer;
|
---|
4 | drop table if exists project.Pol_dog;
|
---|
5 | drop table if exists project.Payment;
|
---|
6 | drop table if exists project.Package;
|
---|
7 | drop table if exists project.Covers;
|
---|
8 | drop table if exists project.Property_pol;
|
---|
9 | drop table if exists project.Property;
|
---|
10 | drop table if exists project.Travel_pol;
|
---|
11 | drop table if exists project.Pol_osi;
|
---|
12 | drop table if exists project.Auto_pol;
|
---|
13 | drop table if exists project.Vehicle;
|
---|
14 |
|
---|
15 | create schema project;
|
---|
16 |
|
---|
17 | create table project.Package(
|
---|
18 | code serial4 primary key,
|
---|
19 | title varchar(255) not null,
|
---|
20 | total numeric NULL,
|
---|
21 | valuet varchar(255) NULL,
|
---|
22 | type_pol int4 NULL
|
---|
23 | );
|
---|
24 |
|
---|
25 | create table project.Policy(
|
---|
26 | p_id serial4 primary key NOT NULL,
|
---|
27 | sdate date not null,
|
---|
28 | edate date not null,
|
---|
29 | package int4,
|
---|
30 | constraint fk_package_pol foreign key (package) references project.Package(code)
|
---|
31 | );
|
---|
32 |
|
---|
33 | create table project.Payment(
|
---|
34 | payment_num serial4 primary key,
|
---|
35 | "policy" int4,
|
---|
36 | p_date date NULL,
|
---|
37 | p_amount int4 NULL,
|
---|
38 | visa_number varchar(255)
|
---|
39 | constraint fk_payment_pol foreign key (policy) references project.Policy(p_id)
|
---|
40 | );
|
---|
41 |
|
---|
42 | create table project.Covers(
|
---|
43 | cov_id int serial4 primary key,
|
---|
44 | cov_amount int4,
|
---|
45 | package int4,
|
---|
46 | cov_type varchar(max),
|
---|
47 | constraint fk_covers_pol foreign key (package) references project.Package(code)
|
---|
48 | );
|
---|
49 |
|
---|
50 |
|
---|
51 | create table project.Customer(
|
---|
52 | c_id serial4 primary key,
|
---|
53 | name varchar(50),
|
---|
54 | email varchar(50) not null,
|
---|
55 | password varchar(50) not null,
|
---|
56 | type bool not null
|
---|
57 | );
|
---|
58 |
|
---|
59 | create table project.Pol_dog(
|
---|
60 | d_embg varchar(13) primary key,
|
---|
61 | c_id int4 NULL,
|
---|
62 | name varchar(255) not null,
|
---|
63 | policy int4,
|
---|
64 | surname varchar(255),
|
---|
65 | birthdate date not null,
|
---|
66 | kontakt varchar(255),
|
---|
67 | constraint fk_Profile_User foreign key (c_id) references project.Customer(c_id),
|
---|
68 | constraint fk_Pol foreign key (policy) references project.Policy(p_id)
|
---|
69 | );
|
---|
70 |
|
---|
71 | CREATE TABLE project.auto_pol (
|
---|
72 | a_id serial4 NOT NULL,
|
---|
73 | pol_id int4 NULL,
|
---|
74 | CONSTRAINT auto_pol_pkey PRIMARY KEY (a_id)
|
---|
75 | );
|
---|
76 | ALTER TABLE project.auto_pol ADD CONSTRAINT fk_pol_vehicle FOREIGN KEY (pol_id) REFERENCES project."policy"(p_id);
|
---|
77 |
|
---|
78 | create table project.Vehicle(
|
---|
79 | v_id serial4 primary key,
|
---|
80 | policy int4,
|
---|
81 | type varchar(255) not null,
|
---|
82 | marka varchar(255),
|
---|
83 | model varchar(255),
|
---|
84 | license_plate varchar(25) not null,
|
---|
85 | constraint fk_Pol_veh foreign key (policy) references project.Auto_pol(a_id)
|
---|
86 | );
|
---|
87 |
|
---|
88 | create table project.Travel_pol(
|
---|
89 | tr_id serial4 primary key,
|
---|
90 | pol_id int4 ,
|
---|
91 | constraint fk_pol_travel foreign key (pol_id) references project.Policy(p_id)
|
---|
92 | );
|
---|
93 |
|
---|
94 | create table project.Pol_osi(
|
---|
95 | o_embg varchar(13) primary key,
|
---|
96 | policy int4,
|
---|
97 | name varchar(255) not null,
|
---|
98 | surname varchar(255),
|
---|
99 | birthdate date not null,
|
---|
100 | kontakt varchar(255),
|
---|
101 | constraint fk_Polosi foreign key (policy) references project.Travel_pol(tr_id)
|
---|
102 | );
|
---|
103 |
|
---|
104 | create table project.Property_pol(
|
---|
105 | pr_id serial4 primary key,
|
---|
106 | pol_id int4,
|
---|
107 | constraint fk_PolProperty foreign key (pol_id) references project.Policy(p_id)
|
---|
108 | );
|
---|
109 |
|
---|
110 | create table project.Property(
|
---|
111 | prop_id serial4 primary key,
|
---|
112 | policy int4,
|
---|
113 | address varchar(255),
|
---|
114 | floor int4,
|
---|
115 | year_build varchar(255),
|
---|
116 | security bool,
|
---|
117 | constraint fk_pol_propp foreign key (policy) references project.Property_pol(pr_id)
|
---|
118 | );
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|
122 | -------------------------------------------------------------------------------------
|
---|
123 |
|
---|
124 | --View
|
---|
125 | -- project.packageview source
|
---|
126 |
|
---|
127 | CREATE OR REPLACE VIEW project.packageview
|
---|
128 | AS SELECT c.cov_amount,
|
---|
129 | c.cov_type,
|
---|
130 | p.title AS packagetitle,
|
---|
131 | p.total AS packagetotal,
|
---|
132 | p.valuet AS packagevalue,
|
---|
133 | CASE
|
---|
134 | WHEN p.type_pol = 3 THEN 'Auto Policy'::text
|
---|
135 | WHEN p.type_pol = 1 THEN 'Travel Health'::text
|
---|
136 | ELSE 'Property Policy'::text
|
---|
137 | END AS policytype
|
---|
138 | FROM project.covers c
|
---|
139 | JOIN project.package p ON c.package::integer = p.code;
|
---|
140 |
|
---|
141 |
|
---|
142 | --Function
|
---|
143 |
|
---|
144 | CREATE OR REPLACE FUNCTION project.getpolicydata(userwhere text)
|
---|
145 | RETURNS TABLE(p_id integer, policytype text, customername text, startdate date, enddate date, packagecode text, packagetitle text, packagetotal numeric)
|
---|
146 | LANGUAGE plpgsql
|
---|
147 | AS $function$
|
---|
148 | BEGIN
|
---|
149 | RETURN QUERY
|
---|
150 | EXECUTE '
|
---|
151 | SELECT p.p_id,
|
---|
152 | CASE
|
---|
153 | WHEN v.pol_id IS NOT NULL THEN ''Auto Policy''
|
---|
154 | WHEN t.pol_id IS NOT NULL THEN ''Travel Health''
|
---|
155 | ELSE ''Property Policy''
|
---|
156 | END AS PolicyType,
|
---|
157 | c.name::TEXT AS CustomerName,
|
---|
158 | p.sdate AS StartDate,
|
---|
159 | p.edate AS EndDate,
|
---|
160 | p.package::TEXT AS PackageCode,
|
---|
161 | pkg.title::TEXT AS PackageTitle,
|
---|
162 | pkg.total AS PackageTotal
|
---|
163 | FROM project.policy p
|
---|
164 | LEFT JOIN project.Auto_pol v ON p.p_id = v.pol_id
|
---|
165 | LEFT JOIN project.Travel_pol t ON p.p_id = t.pol_id
|
---|
166 | LEFT JOIN project.property_pol pp ON p.p_id = pp.pr_id
|
---|
167 | LEFT JOIN project.pol_dog pd ON p.p_id = pd.policy
|
---|
168 | LEFT JOIN project.customer c ON pd.c_id = c.c_id --OR t.o_embg = c.c_id
|
---|
169 | LEFT JOIN project.package pkg ON p.package = pkg.code
|
---|
170 | ' || userwhere || '
|
---|
171 | ORDER BY p.p_id DESC;
|
---|
172 | ';
|
---|
173 | END;
|
---|
174 | $function$
|
---|
175 | ;
|
---|
176 |
|
---|
177 | --Trigger
|
---|
178 | --#1
|
---|
179 | create trigger trigger_check_duplicate_p_id before
|
---|
180 | insert
|
---|
181 | on
|
---|
182 | project.policy for each row execute function project.check_duplicate_p_id();
|
---|
183 |
|
---|
184 | CREATE OR REPLACE FUNCTION project.check_duplicate_p_id()
|
---|
185 | RETURNS trigger
|
---|
186 | LANGUAGE plpgsql
|
---|
187 | AS $function$
|
---|
188 | BEGIN
|
---|
189 | -- Check if the p_id already exists in the table
|
---|
190 | IF EXISTS (SELECT 1 FROM project."policy" WHERE p_id = NEW.p_id) THEN
|
---|
191 | -- Raise an exception if a duplicate p_id is found
|
---|
192 | RAISE EXCEPTION 'Duplicate p_id: %, cannot insert.', NEW.p_id;
|
---|
193 | END IF;
|
---|
194 | RETURN NEW;
|
---|
195 | END;
|
---|
196 | $function$
|
---|
197 | ;
|
---|
198 |
|
---|
199 | --#2
|
---|
200 |
|
---|
201 | CREATE OR REPLACE FUNCTION validate_license_plate()
|
---|
202 | RETURNS TRIGGER AS $$
|
---|
203 | BEGIN
|
---|
204 | -- Check if the license plate matches the expected format (XX-1234-XX)
|
---|
205 | IF NEW.license_plate !~ '^[A-Z]{2}-\d{4}-[A-Z]{2}$' THEN
|
---|
206 | RAISE EXCEPTION 'Invalid license plate format: %', NEW.license_plate;
|
---|
207 | END IF;
|
---|
208 | RETURN NEW;
|
---|
209 | END;
|
---|
210 | $$ LANGUAGE plpgsql;
|
---|
211 |
|
---|
212 | --Index
|
---|
213 |
|
---|
214 | CREATE UNIQUE INDEX policy_pkey ON project.policy USING btree (p_id);
|
---|
215 |
|
---|