-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_procedures_body.sql
365 lines (348 loc) · 12.8 KB
/
pkg_procedures_body.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
Create or Replace Package Body pkg_procedures IS
-----------------------------------------------------------Procedure customer_signUp---------------------------------------------------------------
procedure customer_signUp(
c_fName customer.firstname%type,
c_lName customer.lastname%type,
c_phoneNum customer.phonenum%type,
c_address customer.address%type,
c_zipcode customer.zipcode%type)
is
phone_excep exception;
input_excep exception;
PRAGMA EXCEPTION_INIT(phone_excep,-00001);
PRAGMA EXCEPTION_INIT(input_excep,-01722);
begin
if REGEXP_LIKE(c_fName, '^[[:digit:]]+$') then
raise input_excep;
end if;
if REGEXP_LIKE(c_lName, '^[[:digit:]]+$') then
raise input_excep;
end if;
insert into customer(id, firstname, lastname, phonenum, address, zipcode)
values (customer_id_seq.nextval, c_fName, c_lName, c_phoneNum, c_address, c_zipcode);
commit;
exception
when phone_excep then dbms_output.put_line('phone num already exsits');
when input_excep then dbms_output.put_line('wrong type of input');
end customer_signUp;
----------------------------------------------------------PROCEDURE customer_update--------------------------------------------------------------
PROCEDURE customer_update (
c_id customer.id%type,
c_fName customer.firstname%type,
c_lName customer.lastname%type,
c_phoneNum customer.phonenum%type,
c_address customer.address%type,
c_zipcode customer.zipcode%type)
IS
phone_excep exception;
input_excep exception;
type_excep exception;
PRAGMA EXCEPTION_INIT(phone_excep,-00001);
PRAGMA EXCEPTION_INIT(input_excep,-01722);
PRAGMA EXCEPTION_INIT(phone_excep,-06502);
BEGIN
if REGEXP_LIKE(c_fName, '^[[:digit:]]+$') then
raise input_excep;
end if;
if REGEXP_LIKE(c_lName, '^[[:digit:]]+$') then
raise input_excep;
end if;
UPDATE CUSTOMER
SET firstname = c_fname,
lastname = c_lname,
phonenum = c_phonenum,
address = c_address,
zipcode = c_zipcode
WHERE id = c_id;
IF SQL%ROWCOUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE('wrong customer id');
END IF;
COMMIT;
exception
when phone_excep then dbms_output.put_line('overlap of phone num');
when input_excep then dbms_output.put_line('wrong type of input');
when type_excep then dbms_output.put_line('wrong type of input');
END customer_update;
--------------------------------------------------------------PROCEDURE deliveryOrder_setup--------------------------------------------------------
PROCEDURE deliveryOrder_setup(
c_id customer.id%type,
do_zipcode deliveryorder.zipcode%type,
do_deliverid deliveryorder.deliverid%type,
do_datetime deliveryorder.datetime%type,
do_address deliveryorder.address%type,
do_phonenum deliveryorder.phonenumber%type)
IS
--phone_excep exception;
input_excep exception;
--PRAGMA EXCEPTION_INIT(phone_excep,-00001);
PRAGMA EXCEPTION_INIT(input_excep,-01722);
v_tmp number := -1;
BEGIN
begin
select id into v_tmp from customer where id = c_id;
exception
when no_data_found then
dbms_output.put_line('wrong input of customer id');
return;
end;
insert into deliveryorder(id, customerid, zipcode, deliverid, datetime, address, phonenumber, orderstatus, deliverystatus)
values(deliveryorder_id_seq.nextval, c_id, do_zipcode, do_deliverid, do_datetime, do_address, do_phonenum, 'preparing', 'accepted');
COMMIT;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE('invalid in input');
--when phone_excep then dbms_output.put_line('overlap of phone num');
when input_excep then dbms_output.put_line('wrong type of input');
END deliveryOrder_setup;
----------------------------------------------------------------PROCEDURE dineInOrder_setup--------------------------------------------------------
PROCEDURE dineInOrder_setup(
t_id diningtable.id%type,
c_id customer.id%type,
do_datetime dineinorder.datetime%type)
IS
v_tmp number := -1;
v_status varchar2(50);
BEGIN
begin
select status into v_status from diningtable where id = t_id;
if lower(v_status) = 'occupied' then
dbms_output.put_line('this table is in use');
return;
end if;
exception
when no_data_found then
dbms_output.put_line('wrong input of table id');
return;
end;
begin
select id into v_tmp from customer where id = c_id;
exception
when no_data_found then
dbms_output.put_line('wrong input of customer id');
return;
end;
insert into dineinorder(id, tableid, customerid, status, datetime)
values(dineinorder_id_seq.nextval, t_id, c_id, 'accepted', do_datetime);
COMMIT;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE('invalid in input');
END dineInOrder_setup;
---------------------------------------------------------------PROCEDURE member_signup--------------------------------------------------------------
PROCEDURE member_signup (
c_id customer.id%type,
m_discount member.discount%type,
m_flavor member.flavor%type,
m_birthday member.birthday%type,
m_balance member.balance%type,
m_expirydatetime member.expirydate%type)
IS
--phone_excep exception;
input_excep exception;
--PRAGMA EXCEPTION_INIT(phone_excep,-00001);
PRAGMA EXCEPTION_INIT(input_excep,-01722);
v_tmp number := -1;
BEGIN
begin
select id into v_tmp from customer where id = c_id;
exception
when no_data_found then
dbms_output.put_line('wrong input of customer id');
return;
end;
select memberid into v_tmp from customer where id = c_id;
if v_tmp != -1
then dbms_output.put_line('this customer is already a memeber!');
end if;
insert into member
values (member_id_seq.nextval, m_discount, m_flavor, m_birthday, m_balance, m_expirydatetime, 'active');
update customer
set memberid = member_id_seq.currval
where id = c_id;
COMMIT;
exception
--when phone_excep then dbms_output.put_line('overlap of phone num');
when input_excep then dbms_output.put_line('wrong type of input');
END member_signup;
----------------------------------------------------------------PROCEDURE member_updateByMemberId---------------------------------------------------
PROCEDURE member_updateByMemberId(
m_id member.id%type,
m_discount member.discount%type,
m_flavor member.flavor%type,
m_birthday member.birthday%type,
m_balance member.balance%type,
m_expirydatetime member.expirydate%type)
IS
--type_excep exception;
input_excep exception;
--PRAGMA EXCEPTION_INIT(type_excep,-06550);
PRAGMA EXCEPTION_INIT(input_excep,-01722);
v_tmp member.id%type := -1;
BEGIN
select id into v_tmp from member where id = m_id;
update member
set
discount = m_discount,
flavor = m_flavor,
birthday = m_birthday,
balance = m_balance,
expirydate = m_expirydatetime
where id = m_id;
COMMIT;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE('wrong input of member id');
when input_excep then dbms_output.put_line('wrong type of input');
when others then
dbms_output.put_line('you provided invalid number');
END member_updateByMemberId;
--------------------------------------------------------------------PROCEDURE ORDER_FOOD---------------------------------------------------------
PROCEDURE ORDER_FOOD(
order_type varchar2,
order_id foodlist.dineinorderid%type,
item_id foodlist.dishid%type,
item_quant foodlist.quantity%type)
IS
BEGIN
if lower(order_type) = 'dinein' then
insert into foodlist(dineinorderid, dishid, quantity)
values(order_id, item_id, item_quant);
COMMIT;
elsif lower(order_type) = 'delivery' then
insert into foodlist(deliveryorderid, dishid, quantity)
values(order_id, item_id, item_quant);
COMMIT;
else DBMS_OUTPUT.PUT_LINE('invalid input of order type');
end if;
exception when no_data_found then DBMS_OUTPUT.PUT_LINE('invalid in input');
END ORDER_FOOD;
----------------------------------------------------------------PROCEDURE PAYMENT_SETUP-----------------------------------------------------------
PROCEDURE PAYMENT_SETUP(
o_type varchar2,
o_id foodlist.dineinorderid%type,
tips_val payment.tipsvalue%type)
IS
main_val payment.mainvalue%type := 0;
v_dinein_att_id diningtable.attendantid%type := -1;
v_delivery_att_id diningtable.attendantid%type := -1;
v_tmp dineinorder.paymentid%type := -1;
cursor c_dinein is (
select a.quantity as quant, b.price as price
from (
select dishid, quantity
from foodlist
where dineinorderid = o_id) a
left join menu b
on a.dishid = b.id
);
cursor c_delivery is (
select a.quantity as quant, b.price as price
from (
select dishid, quantity
from foodlist
where deliveryorderid = o_id) a
left join menu b
on a.dishid = b.id
);
BEGIN
if lower(o_type) = 'dinein' then
for i in c_dinein loop
main_val := main_val + i.quant * i.price;
end loop;
insert into payment
values(payment_id_seq.nextval, sysdate, main_val, tips_val);
update dineinorder
set
paymentid = payment_id_seq.currval
where id = o_id;
v_tmp := payment_id_seq.currval;
select c.attendantid
into v_dinein_att_id
from(
select b.attendantid attendantid, a.paymentid
from dineinorder a
join
diningtable b
on a.tableid = b.id
where a.paymentid = v_tmp
) c;
update attendant
set
tipscount = tipscount + tips_val
where id = v_dinein_att_id;
COMMIT;
elsif lower(o_type) = 'delivery' then
for i in c_delivery loop
main_val := main_val + i.quant * i.price;
end loop;
insert into payment
values(payment_id_seq.nextval, sysdate, main_val, tips_val);
update deliveryorder
set
paymentid = payment_id_seq.currval
where id = o_id;
v_tmp := payment_id_seq.currval;
select deliverid
into v_delivery_att_id
from deliveryorder
where paymentid = v_tmp;
update deliveryattendant
set
tipscount = tipscount + tips_val
where id = v_delivery_att_id;
COMMIT;
else DBMS_OUTPUT.PUT_LINE('invalid input of order type');
end if;
END PAYMENT_SETUP;
-----------------------------------------------------------------PROCEDURE reservation_setup--------------------------------------------------------
PROCEDURE reservation_setup(
c_id customer.id%type,
t_id diningtable.id%type,
in_datetime reservation.datetimeIn%type,
out_datetime reservation.datetimeOut%type
)
IS
v_tmp number := -1;
v_status char(255);
BEGIN
-- t_id
begin
select status into v_status from diningtable where id = t_id;
if lower(v_status) = 'occupied' then
dbms_output.put_line('this table is in use');
return;
end if;
exception
when no_data_found then
dbms_output.put_line('wrong input of table id');
return;
end;
-- c_id
begin
select id into v_tmp from customer where id = c_id;
exception
when no_data_found then
dbms_output.put_line('wrong input of customer id');
return;
end;
insert into reservation
values (reservation_id_seq.nextval, c_id, in_datetime, out_datetime, t_id, 'accepted');
commit;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE('invalid in input');
END reservation_setup;
-----------------------------------------------------------------PROCEDURE customized_menu_setup-----------------------------------------------------
PROCEDURE customized_menu_setup(
m_id member.id%type
)
IS
v_flavor member.flavor%type;
BEGIN
select flavor into v_flavor from member where id = m_id;
for i in (select m.name, m.price, m.flavor from menu m where lower(flavor) = rtrim(lower(v_flavor))) loop
dbms_output.put_line(i.name || ':' || i.price || '(' || i.flavor || ')');
end loop;
for i in (select m.name, m.price, m.flavor from menu m where lower(flavor) != rtrim(lower(v_flavor)) order by m.name) loop
dbms_output.put_line(i.name || ':' || i.price || '(' || i.flavor || ')');
end loop;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE('invalid in input');
END customized_menu_setup;
END pkg_procedures;