-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddl.sql
2103 lines (1861 loc) · 58.9 KB
/
ddl.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
IF EXISTS(
SELECT *
FROM sys.databases
WHERE [name] = 'conference_database'
)
DROP DATABASE conference_database;
CREATE DATABASE conference_database;
GO
USE conference_database;
-- HELPER FUNCTIONS
IF EXISTS(SELECT *
FROM sys.objects
WHERE [name] = 'customers_email' AND TYPE = 'C')
ALTER TABLE dbo.customers
DROP CONSTRAINT customers_email;
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE [name] = 'validate_email' AND TYPE = 'FN')
DROP FUNCTION [dbo].[validate_email]
GO
CREATE FUNCTION [dbo].[validate_email](@email NVARCHAR(255))
RETURNS BIT
AS
BEGIN
RETURN
CASE WHEN (
PATINDEX('%[ &'',":;!+=\/()<>]%', @email) > 0 -- Invalid characters
OR patindex('[@.-_]%', @email) > 0 -- Valid but cannot be starting character
OR patindex('%[@.-_]', @email) > 0 -- Valid but cannot be ending character
OR @email NOT LIKE '%@%.%' -- Must contain at least one @ and one .
OR @email LIKE '%..%' -- Cannot have two periods in a row
OR @email LIKE '%@%@%' -- Cannot have two @ anyWHERE
OR @email LIKE '%.@%' OR @email LIKE '%@.%' -- Cannot have @ and . next to each other
)
THEN 0
ELSE 1
END
END
GO
-- TABLES
IF OBJECT_ID('dbo.payments', 'U') IS NOT NULL
DROP TABLE dbo.payments;
CREATE TABLE payments (
id INT IDENTITY NOT NULL,
order_id INT NOT NULL,
value DECIMAL(8, 2) NULL,
CONSTRAINT payments_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX payments_id_uindex
ON payments (id)
GO
IF OBJECT_ID('dbo.conference_day_participants', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('workshop_participants_conference_day_participants', 'F') IS NOT NULL
ALTER TABLE dbo.workshop_participants
DROP CONSTRAINT workshop_participants_conference_day_participants;
DROP TABLE dbo.conference_day_participants;
END
CREATE TABLE conference_day_participants (
id INT IDENTITY NOT NULL,
participant_id INT NOT NULL,
conference_day_id INT NOT NULL,
day_reservation_id INT NOT NULL,
CONSTRAINT conference_day_participant_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX conference_day_participant_id_uindex
ON conference_day_participants (id)
GO
IF OBJECT_ID('dbo.conference_days', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('ConferenceDayParticipant_conference_days', 'F') IS NOT NULL
ALTER TABLE dbo.conference_day_participants
DROP CONSTRAINT ConferenceDayParticipant_conference_days;
IF OBJECT_ID('day_reservations_conference_days', 'F') IS NOT NULL
ALTER TABLE dbo.day_reservations
DROP CONSTRAINT day_reservations_conference_days;
IF OBJECT_ID('workshops_conference_days', 'F') IS NOT NULL
ALTER TABLE dbo.workshops
DROP CONSTRAINT workshops_conference_days;
DROP TABLE dbo.conference_days;
END
CREATE TABLE conference_days (
id INT IDENTITY NOT NULL,
conference_id INT NOT NULL,
day DATETIME NOT NULL,
seats INT NOT NULL CHECK (seats > 0)
CONSTRAINT conference_days_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX conference_days_id_uindex
ON conference_days (id)
GO
IF OBJECT_ID('dbo.conference_has_pricing', 'U') IS NOT NULL
DROP TABLE dbo.conference_has_pricing;
CREATE TABLE conference_has_pricing (
id INT IDENTITY NOT NULL,
conference_pricing_id INT NOT NULL,
conference_id INT NOT NULL,
CONSTRAINT conference_has_pricing_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX conference_has_pricing_id_uindex
ON conference_has_pricing (id)
GO
IF OBJECT_ID('dbo.conference_pricings', 'U') IS NOT NULL
DROP TABLE dbo.conference_pricings;
CREATE TABLE conference_pricings (
id INT IDENTITY NOT NULL,
price DECIMAL(8, 2) NOT NULL,
since_date DATETIME NOT NULL,
CONSTRAINT conference_pricings_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX conference_pricings_id_uindex
ON conference_pricings (id)
GO
IF OBJECT_ID('dbo.conferences', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('ConferenceHasPricing_conferences', 'F') IS NOT NULL
ALTER TABLE dbo.conference_has_pricing
DROP CONSTRAINT ConferenceHasPricing_conferences;
IF OBJECT_ID('conference_day_conference', 'F') IS NOT NULL
ALTER TABLE dbo.conference_days
DROP CONSTRAINT conference_day_conference;
DROP TABLE dbo.conferences;
END
CREATE TABLE conferences (
id INT IDENTITY NOT NULL,
[name] VARCHAR(50),
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
discount NUMERIC(2, 2) NOT NULL,
CONSTRAINT conferences_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX conferences_id_uindex
ON conferences (id)
GO
IF OBJECT_ID('dbo.customers', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('customers_payment', 'F') IS NOT NULL
ALTER TABLE dbo.orders
DROP CONSTRAINT customers_payment;
IF OBJECT_ID('participants_customers', 'F') IS NOT NULL
ALTER TABLE dbo.participants
DROP CONSTRAINT participants_customers;
DROP TABLE dbo.customers;
END
CREATE TABLE customers (
id INT IDENTITY NOT NULL,
name VARCHAR(256) NOT NULL,
phone CHAR(12) CHECK (LEN(TRIM(phone)) = 9),
NIP VARCHAR(10),
email NVARCHAR(256),
address TEXT,
zip_code TEXT,
country TEXT,
is_company BIT NULL DEFAULT 0,
CONSTRAINT customers_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX customers_id_uindex
ON customers (id)
GO
IF OBJECT_ID('dbo.day_reservations', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('participants_day_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.participants
DROP CONSTRAINT participants_day_reservations;
DROP TABLE dbo.day_reservations;
END
GO
CREATE TABLE day_reservations (
id INT IDENTITY NOT NULL,
order_id INT NOT NULL,
conference_day_id INT NOT NULL,
places_reserved INT NOT NULL CHECK (places_reserved > 0),
CONSTRAINT day_reservations_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX day_reservations_id_uindex
ON day_reservations (id)
GO
IF OBJECT_ID('dbo.workshop_reservations', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('workshop_participants_workshop_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.workshop_participants
DROP CONSTRAINT workshop_participants_workshop_reservations;
DROP TABLE dbo.workshop_reservations;
END
GO
CREATE TABLE workshop_reservations (
id INT IDENTITY NOT NULL,
workshop_id INT NOT NULL,
day_reservation_id INT NOT NULL,
places_reserved INT NOT NULL CHECK (places_reserved > 0),
CONSTRAINT workshop_reservations_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX workshop_reservations_id_uindex
ON workshop_reservations (id)
GO
IF OBJECT_ID('dbo.orders', 'U') IS NOT NULL
DROP TABLE dbo.orders;
CREATE TABLE orders (
id INT IDENTITY NOT NULL,
customer_id INT NOT NULL,
order_date DATETIME NOT NULL,
is_cancelled BIT NULL,
CONSTRAINT orders_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX orders_id_uindex
ON orders (id)
GO
IF OBJECT_ID('dbo.participants', 'U') IS NOT NULL
DROP TABLE dbo.participants;
CREATE TABLE participants (
id INT IDENTITY NOT NULL,
customer_id INT NOT NULL,
name VARCHAR(256) NOT NULL,
card_no VARCHAR(10) NULL,
CONSTRAINT participants_pk PRIMARY KEY (id)
);
SELECT *
FROM sys.objects
IF OBJECT_ID('dbo.workshop_participants', 'U') IS NOT NULL
begin
IF OBJECT_ID('workshop_participants_workshop_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.workshop_participants
DROP CONSTRAINT workshop_participants_workshop_reservations;
DROP TABLE dbo.workshop_participants;
END
GO
CREATE TABLE workshop_participants (
id INT IDENTITY NOT NULL,
workshop_id INT NOT NULL,
conference_day_participant_id INT NOT NULL,
workshop_reservation_id INT NOT NULL,
CONSTRAINT workshop_participants_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX participants_id_uindex
ON participants (id)
GO
IF OBJECT_ID('dbo.workshops', 'U') IS NOT NULL
DROP TABLE dbo.workshops;
CREATE TABLE workshops (
id INT IDENTITY NOT NULL,
conference_day_id INT NOT NULL,
title TEXT NOT NULL,
places INT NOT NULL,
price DECIMAL(8, 2) NULL,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
CONSTRAINT workshops_pk PRIMARY KEY (id)
);
CREATE UNIQUE INDEX workshops_id_uindex
ON workshops (id)
GO
-- INDEXES
IF EXISTS(
SELECT *
FROM sys.indexes
WHERE name = 'participant_card_no'
AND object_id = OBJECT_ID('[dbo].[participants]')
)
DROP INDEX participant_card_no
ON participants
CREATE INDEX participant_card_no
ON participants (card_no DESC)
-- FOREIGN KEYS
IF OBJECT_ID('conference_day_participants_conference_days', 'F') IS NOT NULL
ALTER TABLE dbo.conference_day_participants
DROP CONSTRAINT conference_day_participants_conference_days;
ALTER TABLE conference_day_participants
ADD CONSTRAINT conference_day_participants_conference_days
FOREIGN KEY (conference_day_id)
REFERENCES conference_days (id);
IF OBJECT_ID('conference_day_participants_participants', 'F') IS NOT NULL
ALTER TABLE dbo.conference_day_participants
DROP CONSTRAINT conference_day_participants_participants;
ALTER TABLE conference_day_participants
ADD CONSTRAINT conference_day_participants_participants
FOREIGN KEY (participant_id)
REFERENCES participants (id);
IF OBJECT_ID('conference_day_participants_day_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.conference_day_participants
DROP CONSTRAINT conference_day_participants_day_reservations;
ALTER TABLE conference_day_participants
ADD CONSTRAINT conference_day_participants_day_reservations
FOREIGN KEY (day_reservation_id)
REFERENCES day_reservations (id);
IF OBJECT_ID('conference_day_participants_day_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.conference_day_participants
DROP CONSTRAINT conference_day_participants_day_reservations;
ALTER TABLE conference_day_participants
ADD CONSTRAINT conference_day_participants_day_reservations
FOREIGN KEY (day_reservation_id)
REFERENCES day_reservations (id);
IF OBJECT_ID('conference_has_pricing_conference_pricing', 'F') IS NOT NULL
ALTER TABLE dbo.conference_has_pricing
DROP CONSTRAINT conference_has_pricing_conference_pricing;
ALTER TABLE conference_has_pricing
ADD CONSTRAINT conference_has_pricing_conference_pricing
FOREIGN KEY (conference_pricing_id)
REFERENCES conference_pricings (id);
IF OBJECT_ID('conference_has_pricing_conference', 'F') IS NOT NULL
ALTER TABLE dbo.conference_has_pricing
DROP CONSTRAINT conference_has_pricing_conference;
ALTER TABLE conference_has_pricing
ADD CONSTRAINT conference_has_pricing_conferences
FOREIGN KEY (conference_id)
REFERENCES conferences (id);
IF OBJECT_ID('payments_orders', 'F') IS NOT NULL
ALTER TABLE dbo.Payments
DROP CONSTRAINT payments_orders;
ALTER TABLE payments
ADD CONSTRAINT payments_orders
FOREIGN KEY (order_id)
REFERENCES orders (id);
IF OBJECT_ID('conference_day_conference', 'F') IS NOT NULL
ALTER TABLE dbo.conference_days
DROP CONSTRAINT conference_day_conference;
ALTER TABLE conference_days
ADD CONSTRAINT conference_day_conference
FOREIGN KEY (conference_id)
REFERENCES conferences (id);
IF OBJECT_ID('customers_payment', 'F') IS NOT NULL
ALTER TABLE dbo.orders
DROP CONSTRAINT customers_payment;
ALTER TABLE orders
ADD CONSTRAINT customers_payment
FOREIGN KEY (customer_id)
REFERENCES customers (id);
IF OBJECT_ID('day_reservations_conference_days', 'F') IS NOT NULL
ALTER TABLE dbo.day_reservations
DROP CONSTRAINT day_reservations_conference_days;
ALTER TABLE day_reservations
ADD CONSTRAINT day_reservations_conference_days
FOREIGN KEY (conference_day_id)
REFERENCES conference_days (id);
IF OBJECT_ID('orders_day_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.day_reservations
DROP CONSTRAINT orders_day_reservations;
ALTER TABLE day_reservations
ADD CONSTRAINT orders_day_reservations
FOREIGN KEY (order_id)
REFERENCES orders (id);
IF OBJECT_ID('participants_customers', 'F') IS NOT NULL
ALTER TABLE dbo.participants
DROP CONSTRAINT participants_customers;
ALTER TABLE participants
ADD CONSTRAINT participants_customers
FOREIGN KEY (customer_id)
REFERENCES customers (id);
IF OBJECT_ID('workshop_participants_conference_day_participants', 'F') IS NOT NULL
ALTER TABLE dbo.workshop_participants
DROP CONSTRAINT workshop_participants_conference_day_participants;
ALTER TABLE workshop_participants
ADD CONSTRAINT workshop_participants_conference_day_participants
FOREIGN KEY (conference_day_participant_id)
REFERENCES conference_day_participants (id);
IF OBJECT_ID('workshop_participants_workshop_reservations', 'F') IS NOT NULL
ALTER TABLE dbo.workshop_participants
DROP CONSTRAINT workshop_participants_workshop_reservations;
ALTER TABLE workshop_participants
ADD CONSTRAINT workshop_participants_workshop_reservations
FOREIGN KEY (workshop_reservation_id)
REFERENCES workshop_reservations (id);
IF OBJECT_ID('workshops_conference_days', 'F') IS NOT NULL
ALTER TABLE dbo.workshops
DROP CONSTRAINT workshops_conference_days;
IF OBJECT_ID('customers_email', 'F') IS NOT NULL
ALTER TABLE dbo.customers
DROP CONSTRAINT customers_email;
ALTER TABLE customers
ADD CONSTRAINT customers_email
CHECK ([dbo].[validate_email](email) = 1);
ALTER TABLE workshops
ADD CONSTRAINT workshops_conference_days
FOREIGN KEY (conference_day_id)
REFERENCES conference_days (id);
IF OBJECT_ID('workshops_workshop_participants', 'F') IS NOT NULL
ALTER TABLE dbo.workshop_participants
DROP CONSTRAINT workshops_workshop_participants;
ALTER TABLE workshop_participants
ADD CONSTRAINT workshops_workshop_participants
FOREIGN KEY (workshop_id)
REFERENCES workshops (id);
-- PROCEDURES
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'create_conference') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[create_conference]
GO
CREATE PROCEDURE [dbo].[create_conference](
@name NVARCHAR(127),
@start_time DATETIME,
@end_time DATETIME,
@discount DECIMAL(2, 2),
@conference_id INT = NULL OUT
) AS BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
IF @name IS NULL OR LTRIM(@name) = ''
THROW 50000, '@name is NULL or empty', 1
IF @start_time >= @end_time
THROW 50000, '@start_time is later or equal than @end_time', 1
IF @start_time < GETDATE()
THROW 50000, '@start_time is from past', 1
INSERT INTO conferences (
[name],
start_time,
end_time,
discount
) VALUES (
@name,
@start_time,
@end_time,
@discount
)
SET @conference_id = SCOPE_IDENTITY()
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'create_workshop') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[create_workshop]
GO
CREATE PROCEDURE [dbo].[create_workshop]
@conference_day_id INT,
@title VARCHAR(256),
@places INT,
@price DECIMAL(8, 2),
@start_time DATETIME,
@end_time DATETIME,
@workshop_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION;
IF @start_time >= @end_time
THROW 50000, '@start_time is later or equal than @end_time', 1
IF @start_time < GETDATE()
THROW 50000, '@start_time is from past', 1
IF @price < 0
THROW 50000, '@price is lower than 0', 1
IF @places <= 0
THROW 50000, '@seats are lower or equal 0', 1
IF NOT EXISTS(SELECT *
FROM conference_days
WHERE conference_days.id = @conference_day_id)
THROW 50000, '@Conference_day not found', 1
INSERT INTO workshops (
conference_day_id,
title,
places,
price,
start_time,
end_time
) VALUES (
@conference_day_id,
@title,
@places,
@price,
@start_time,
@end_time
)
SET @workshop_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_customer') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_customer]
GO
CREATE PROCEDURE [dbo].[add_customer]
@name VARCHAR(256),
@phone CHAR(9),
@NIP CHAR(10),
@email NVARCHAR(256),
@adress TEXT,
@zip_code TEXT,
@county TEXT,
@is_company BIT,
@customer_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF (@phone IS NULL) AND (@email IS NULL)
THROW 50000, '@phone and @email is null. No contact with customer', 1
IF @name IS NULL OR LTRIM(@name) = ''
THROW 50000, '@name is null or empty', 1
INSERT INTO customers (
[name],
phone,
NIP,
email,
address,
zip_code,
country,
is_company
) VALUES (
@name,
@phone,
@NIP,
@email,
@adress,
@zip_code,
@county,
@is_company
)
SET @customer_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_conference_day') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_conference_day]
GO
CREATE PROCEDURE [dbo].[add_conference_day]
@conference_id INT,
@day DATETIME,
@places INT,
@conference_day_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT *
FROM conferences
WHERE id = @conference_id)
THROW 50000, '@conference_id is invalid. No such conference', 1
IF @day < GETDATE()
THROW 50000, '@date is from past', 1
IF EXISTS(SELECT id
FROM conference_days
WHERE conference_id = @conference_id AND [day] = @day)
THROW 50000, 'Conference already in database', 1
IF @places <= 0
THROW 50000, '@places are lower or equal 0', 1
INSERT INTO conference_days (
conference_id,
[day],
seats
) VALUES (
@conference_id,
@day,
@places
)
SET @conference_day_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_order') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_order]
GO
CREATE PROCEDURE [dbo].[add_order]
@phone CHAR(9),
@email NVARCHAR(256),
@order_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
DECLARE @customerid INT, @orderdate DATETIME
IF @phone IS NOT NULL AND @email IS NOT NULL
BEGIN
SET @customerid = (SELECT id
FROM customers
WHERE phone LIKE @phone AND email LIKE @email)
END
IF @phone IS NULL
BEGIN
SET @customerid = (SELECT id
FROM customers
WHERE email LIKE @email)
END
IF @email IS NULL
BEGIN
SET @customerid = (SELECT id
FROM customers
WHERE phone LIKE @phone)
END
IF @customerid IS NULL
THROW 50000, 'No such customer in database', 1
SET @orderdate = GETDATE()
INSERT INTO orders (
customer_id,
order_date,
is_cancelled
) VALUES (
@customerid,
@orderdate,
0
)
SET @order_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_day_reservation') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_day_reservation]
GO
CREATE PROCEDURE [dbo].[add_day_reservation]
@order_id INT,
@conference_day_id INT,
@places_reserved INT,
@day_reservation_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT *
FROM orders
WHERE id = @order_id)
THROW 50000, '@order_id not in database', 1
IF NOT EXISTS(SELECT *
FROM conference_days
WHERE id = @conference_day_id)
THROW 50000, '@conference_day_id not in database', 1
IF @places_reserved <= 0
THROW 50000, '@places_reserved below or equal 0', 1
INSERT INTO day_reservations (
order_id,
conference_day_id,
places_reserved
) VALUES (
@order_id,
@conference_day_id,
@places_reserved
)
SET @day_reservation_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_workshop_reservation') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_workshop_reservation]
GO
CREATE PROCEDURE [dbo].[add_workshop_reservation]
@day_reservation_id INT,
@workshop_id INT,
@places_reserved INT,
@workshop_reservation_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT *
FROM workshops
WHERE id = @workshop_id)
THROW 50000, 'Workshop not in database', 1
IF NOT EXISTS(SELECT *
FROM day_reservations
WHERE id = @day_reservation_id)
THROW 50000, 'Day reservation not in database', 1
IF @places_reserved <= 0
THROW 50000, '@places_reserved below or equal 0', 1
INSERT INTO workshop_reservations (
day_reservation_id,
workshop_id,
places_reserved
) VALUES (
@day_reservation_id,
@workshop_id,
@places_reserved
)
SET @workshop_reservation_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_participant') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_participant]
GO
CREATE PROCEDURE [dbo].[add_participant]
@customer_id INT,
@name VARCHAR(256),
@participant_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT *
FROM customers
WHERE id = @customer_id)
THROW 50000, '@customer_id not in database', 1
IF LTRIM(@name) = ''
THROW 50000, '@name is empty', 1
INSERT INTO participants (
customer_id,
[name]
) VALUES (
@customer_id,
@name
)
SET @participant_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_student_participant') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_student_participant]
GO
CREATE PROCEDURE [dbo].[add_student_participant]
@customer_id INT,
@name VARCHAR(256),
@card_no VARCHAR(10),
@participant_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT *
FROM customers
WHERE id = @customer_id)
THROW 50000, '@customer_id not in database', 1
IF LTRIM(@name) = ''
THROW 50000, '@name is empty', 1
INSERT INTO participants (
customer_id,
[name],
card_no
) VALUES (
@customer_id,
@name,
@card_no
)
SET @participant_id = SCOPE_IDENTITY()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_workshop_participant') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_workshop_participant]
GO
CREATE PROCEDURE [dbo].[add_workshop_participant]
@workshop_id INT,
@conference_day_participant_id INT,
@participant_id INT,
@workshop_reservation_id INT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF (
SELECT count(id)
FROM workshop_participants
WHERE workshop_id = @workshop_id
) >= (
SELECT places
FROM workshops
WHERE id = @workshop_id
)
THROW 50000, 'No more places left in this workshop', 1
IF (
SELECT count(wp.id)
FROM workshop_participants AS wp
WHERE wp.workshop_id = @workshop_id
AND wp.workshop_reservation_id = @workshop_reservation_id
) >= (
SELECT places_reserved
FROM workshop_reservations
WHERE id = @workshop_reservation_id
)
THROW 50000, 'No more places left available in this order', 1
INSERT INTO workshop_participants (
workshop_id,
conference_day_participant_id,
workshop_reservation_id
) VALUES (
@workshop_id,
@conference_day_participant_id,
@workshop_reservation_id
)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_payment') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_payment]
GO
CREATE PROCEDURE [dbo].[add_payment]
@value DECIMAL(8, 2),
@order_id INT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS(SELECT id
FROM orders
WHERE id = @order_id)
THROW 50000, '@order_id not in database', 1
INSERT INTO payments (
[value],
order_id
) VALUES (
@value,
@order_id
)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW
END CATCH
END
GO
IF EXISTS(SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'add_conference_day_participant') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[add_conference_day_participant]
GO
CREATE PROCEDURE [dbo].[add_conference_day_participant]
@participant_id INT,
@conference_day_id INT,
@day_reservation_id INT,
@conference_day_participant_id INT = NULL OUT
AS BEGIN
BEGIN TRY
BEGIN TRANSACTION