-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbooktown.sql
1868 lines (1558 loc) · 41.9 KB
/
booktown.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
--
-- Selected TOC Entries:
--
--
-- TOC Entry ID 1 (OID 0)
--
-- Name: booktown Type: DATABASE Owner: postgres
--
Create Database "booktown";
\connect booktown postgres
--
-- TOC Entry ID 2 (OID 2991542)
--
-- Name: DATABASE "booktown" Type: COMMENT Owner:
--
COMMENT ON DATABASE "booktown" IS 'The Book Town Database.';
--
-- TOC Entry ID 33 (OID 3629264)
--
-- Name: books Type: TABLE Owner: manager
--
CREATE TABLE "books" (
"id" integer NOT NULL,
"title" text NOT NULL,
"author_id" integer,
"subject_id" integer,
Constraint "books_id_pkey" Primary Key ("id")
);
--
-- TOC Entry ID 47 (OID 2991733)
--
-- Name: "plpgsql_call_handler" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "plpgsql_call_handler" () RETURNS opaque AS '/usr/local/pgsql/lib/plpgsql.so', 'plpgsql_call_handler' LANGUAGE 'C';
--
-- TOC Entry ID 48 (OID 2991734)
--
-- Name: plpgsql Type: PROCEDURAL LANGUAGE Owner:
--
CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql' HANDLER "plpgsql_call_handler" LANCOMPILER 'PL/pgSQL';
--
-- TOC Entry ID 51 (OID 2991735)
--
-- Name: "audit_bk" (integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "audit_bk" (integer) RETURNS integer AS '
DECLARE
key ALIAS FOR $1;
table_data inventory%ROWTYPE;
BEGIN
INSERT INTO inventory_audit SELECT table_data WHERE sort_key=key;
IF NOT FOUND THEN
RAISE EXCEPTION ''View'' || key || '' not found '';
END IF;
return 1;
end;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 52 (OID 2991736)
--
-- Name: "audit" (integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "audit" (integer) RETURNS integer AS '
DECLARE
key ALIAS FOR $1;
table_data inventory%ROWTYPE;
BEGIN
INSERT INTO inventory_audit SELECT table_data WHERE sort_key=key;
IF NOT FOUND THEN
RAISE EXCEPTION ''View'' || key || '' not found '';
END IF;
return 1;
end;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 53 (OID 2991737)
--
-- Name: "auditbk" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "auditbk" () RETURNS integer AS '
DECLARE
key ALIAS FOR $1;
table_data inventory%ROWTYPE;
BEGIN
INSERT INTO inventory_audit SELECT table_data WHERE sort_key=key;
IF NOT FOUND THEN
RAISE EXCEPTION ''View'' || key || '' not found '';
END IF;
return 1;
end;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 54 (OID 2991738)
--
-- Name: "audit_bk1" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "audit_bk1" () RETURNS opaque AS '
DECLARE
key ALIAS FOR $1;
table_data inventory%ROWTYPE;
BEGIN
INSERT INTO inventory_audit SELECT table_data WHERE sort_key=key;
IF NOT FOUND THEN
RAISE EXCEPTION ''View'' || key || '' not found '';
END IF;
return 1;
end;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 73 (OID 2991835)
--
-- Name: "test_check_a_id" () Type: FUNCTION Owner: example
--
CREATE FUNCTION "test_check_a_id" () RETURNS opaque AS '
BEGIN
-- checks to make sure the author id
-- inserted is not left blank or less than 100
IF NEW.a_id ISNULL THEN
RAISE EXCEPTION
''The author id cannot be left blank!'';
ELSE
IF NEW.a_id < 100 THEN
RAISE EXCEPTION
''Please insert a valid author id.'';
ELSE
RETURN NEW;
END IF;
END IF;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 66 (OID 2992619)
--
-- Name: "audit_test" () Type: FUNCTION Owner: example
--
CREATE FUNCTION "audit_test" () RETURNS opaque AS '
BEGIN
IF TG_OP = ''INSERT'' OR TG_OP = ''UPDATE'' THEN
NEW.user_aud := current_user;
NEW.mod_time := ''NOW'';
INSERT INTO inventory_audit SELECT * FROM inventory WHERE prod_id=NEW.prod_id;
RETURN NEW;
ELSE if TG_OP = ''DELETE'' THEN
INSERT INTO inventory_audit SELECT *, current_user, ''NOW'' FROM inventory WHERE prod_id=OLD.prod_id;
RETURN OLD;
END IF;
END IF;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 67 (OID 3000878)
--
-- Name: "first" () Type: FUNCTION Owner: example
--
CREATE FUNCTION "first" () RETURNS integer AS '
DecLarE
oNe IntEgER := 1;
bEGiN
ReTUrn oNE;
eNd;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 68 (OID 3000881)
--
-- Name: "test" (integer) Type: FUNCTION Owner: example
--
CREATE FUNCTION "test" (integer) RETURNS integer AS '
DECLARE
-- defines the variable as ALIAS
variable ALIAS FOR $1;
BEGIN
-- displays the variable after multiplying it by two
return variable * 2.0;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 69 (OID 3000991)
--
-- Name: "you_me" (integer) Type: FUNCTION Owner: example
--
CREATE FUNCTION "you_me" (integer) RETURNS integer AS '
DECLARE
RENAME $1 TO user_no;
--you INTEGER := 5;
BEGIN
return user_no;
END;' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 62 (OID 3001136)
--
-- Name: "count_by_two" (integer) Type: FUNCTION Owner: example
--
CREATE FUNCTION "count_by_two" (integer) RETURNS integer AS '
DECLARE
userNum ALIAS FOR $1;
i integer;
BEGIN
i := 1;
WHILE userNum[1] < 20 LOOP
i = i+1;
return userNum;
END LOOP;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 63 (OID 3001139)
--
-- Name: "me" () Type: FUNCTION Owner: example
--
CREATE FUNCTION "me" () RETURNS text AS '
DECLARE
you text := ''testing'';
RENAME you to me;
BEGIN
return me;
END;' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 64 (OID 3001149)
--
-- Name: "display_cust" (integer) Type: FUNCTION Owner: example
--
CREATE FUNCTION "display_cust" (integer) RETURNS text AS '
DECLARE
-- declares an alias name for input
cust_num ALIAS FOR $1;
-- declares a row type
cust_info customer%ROWTYPE;
BEGIN
-- puts information into the newly declared rowtype
SELECT into cust_info *
FROM customer
WHERE cust_id=cust_num;
-- displays the customer lastname
-- extracted from the rowtype
return cust_info.lastname;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 65 (OID 3001151)
--
-- Name: "mixed" () Type: FUNCTION Owner: example
--
CREATE FUNCTION "mixed" () RETURNS integer AS '
DecLarE
--assigns 1 to the oNe variable
oNe IntEgER
:= 1;
bEGiN
--displays the value of oNe
ReTUrn oNe;
eNd;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 12 (OID 3117548)
--
-- Name: publishers Type: TABLE Owner: postgres
--
CREATE TABLE "publishers" (
"id" integer NOT NULL,
"name" text,
"address" text,
Constraint "publishers_pkey" Primary Key ("id")
);
--
-- TOC Entry ID 55 (OID 3117729)
--
-- Name: "compound_word" (text,text) Type: FUNCTION Owner: example
--
CREATE FUNCTION "compound_word" (text,text) RETURNS text AS '
DECLARE
-- defines an alias name for the two input values
word1 ALIAS FOR $1;
word2 ALIAS FOR $2;
BEGIN
-- displays the resulting joined words
RETURN word1 || word2;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 56 (OID 3117787)
--
-- Name: "givename" () Type: FUNCTION Owner: example
--
CREATE FUNCTION "givename" () RETURNS opaque AS '
DECLARE
tablename text;
BEGIN
tablename = TG_RELNAME;
INSERT INTO INVENTORY values (123, tablename);
return old;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 14 (OID 3389594)
--
-- Name: authors Type: TABLE Owner: manager
--
CREATE TABLE "authors" (
"id" integer NOT NULL,
"last_name" text,
"first_name" text,
Constraint "authors_pkey" Primary Key ("id")
);
--
-- TOC Entry ID 15 (OID 3389632)
--
-- Name: states Type: TABLE Owner: postgres
--
CREATE TABLE "states" (
"id" integer NOT NULL,
"name" text,
"abbreviation" character(2),
Constraint "state_pkey" Primary Key ("id")
);
--
-- TOC Entry ID 16 (OID 3389702)
--
-- Name: my_list Type: TABLE Owner: postgres
--
CREATE TABLE "my_list" (
"todos" text
);
--
-- TOC Entry ID 17 (OID 3390348)
--
-- Name: stock Type: TABLE Owner: postgres
--
CREATE TABLE "stock" (
"isbn" text NOT NULL,
"cost" numeric(5,2),
"retail" numeric(5,2),
"stock" integer,
Constraint "stock_pkey" Primary Key ("isbn")
);
--
-- TOC Entry ID 4 (OID 3390416)
--
-- Name: subject_ids Type: SEQUENCE Owner: postgres
--
CREATE SEQUENCE "subject_ids" start 0 increment 1 maxvalue 2147483647 minvalue 0 cache 1 ;
--
-- TOC Entry ID 19 (OID 3390653)
--
-- Name: numeric_values Type: TABLE Owner: postgres
--
CREATE TABLE "numeric_values" (
"num" numeric(30,6)
);
--
-- TOC Entry ID 20 (OID 3390866)
--
-- Name: daily_inventory Type: TABLE Owner: postgres
--
CREATE TABLE "daily_inventory" (
"isbn" text,
"is_stocked" boolean
);
--
-- TOC Entry ID 21 (OID 3391084)
--
-- Name: money_example Type: TABLE Owner: postgres
--
CREATE TABLE "money_example" (
"money_cash" money,
"numeric_cash" numeric(6,2)
);
--
-- TOC Entry ID 22 (OID 3391184)
--
-- Name: shipments Type: TABLE Owner: postgres
--
CREATE TABLE "shipments" (
"id" integer DEFAULT nextval('"shipments_ship_id_seq"'::text) NOT NULL,
"customer_id" integer,
"isbn" text,
"ship_date" timestamp with time zone
);
--
-- TOC Entry ID 24 (OID 3391454)
--
-- Name: customers Type: TABLE Owner: manager
--
CREATE TABLE "customers" (
"id" integer NOT NULL,
"last_name" text,
"first_name" text,
Constraint "customers_pkey" Primary Key ("id")
);
--
-- TOC Entry ID 6 (OID 3574018)
--
-- Name: book_ids Type: SEQUENCE Owner: postgres
--
CREATE SEQUENCE "book_ids" start 0 increment 1 maxvalue 2147483647 minvalue 0 cache 1 ;
--
-- TOC Entry ID 26 (OID 3574043)
--
-- Name: book_queue Type: TABLE Owner: postgres
--
CREATE TABLE "book_queue" (
"title" text NOT NULL,
"author_id" integer,
"subject_id" integer,
"approved" boolean
);
--
-- TOC Entry ID 78 (OID 3574403)
--
-- Name: "title" (integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "title" (integer) RETURNS text AS 'SELECT title from books where id = $1' LANGUAGE 'sql';
--
-- TOC Entry ID 27 (OID 3574983)
--
-- Name: stock_backup Type: TABLE Owner: postgres
--
CREATE TABLE "stock_backup" (
"isbn" text,
"cost" numeric(5,2),
"retail" numeric(5,2),
"stock" integer
);
--
-- TOC Entry ID 89 (OID 3625934)
--
-- Name: "double_price" (double precision) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "double_price" (double precision) RETURNS double precision AS '
DECLARE
BEGIN
return $1 * 2;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 90 (OID 3625935)
--
-- Name: "triple_price" (double precision) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "triple_price" (double precision) RETURNS double precision AS '
DECLARE
-- Declare input_price as an alias for the
-- argument variable normally referenced with
-- the $1 identifier.
input_price ALIAS FOR $1;
BEGIN
-- Return the input price multiplied by three.
RETURN input_price * 3;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 87 (OID 3625944)
--
-- Name: "stock_amount" (integer,integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "stock_amount" (integer,integer) RETURNS integer AS '
DECLARE
-- Declare aliases for function arguments.
b_id ALIAS FOR $1;
b_edition ALIAS FOR $2;
-- Declare variable to store the ISBN number.
b_isbn TEXT;
-- Declare variable to store the stock amount.
stock_amount INTEGER;
BEGIN
-- This SELECT INTO statement retrieves the ISBN
-- number of the row in the editions table that had
-- both the book ID number and edition number that
-- were provided as function arguments.
SELECT INTO b_isbn isbn FROM editions WHERE
book_id = b_id AND edition = b_edition;
-- Check to see if the ISBN number retrieved
-- is NULL. This will happen if there is not an
-- existing book with both the ID number and edition
-- number specified in the function arguments.
-- If the ISBN is null, the function returns a
-- value of -1 and ends.
IF b_isbn IS NULL THEN
RETURN -1;
END IF;
-- Retrieve the amount of books available from the
-- stock table and record the number in the
-- stock_amount variable.
SELECT INTO stock_amount stock FROM stock WHERE isbn = b_isbn;
-- Return the amount of books available.
RETURN stock_amount;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 86 (OID 3625946)
--
-- Name: "in_stock" (integer,integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "in_stock" (integer,integer) RETURNS boolean AS '
DECLARE
b_id ALIAS FOR $1;
b_edition ALIAS FOR $2;
b_isbn TEXT;
stock_amount INTEGER;
BEGIN
-- This SELECT INTO statement retrieves the ISBN
-- number of the row in the editions table that had
-- both the book ID number and edition number that
-- were provided as function arguments.
SELECT INTO b_isbn isbn FROM editions WHERE
book_id = b_id AND edition = b_edition;
-- Check to see if the ISBN number retrieved
-- is NULL. This will happen if there is not an
-- existing book with both the ID number and edition
-- number specified in the function arguments.
-- If the ISBN is null, the function returns a
-- FALSE value and ends.
IF b_isbn IS NULL THEN
RETURN FALSE;
END IF;
-- Retrieve the amount of books available from the
-- stock table and record the number in the
-- stock_amount variable.
SELECT INTO stock_amount stock FROM stock WHERE isbn = b_isbn;
-- Use an IF/THEN/ELSE check to see if the amount
-- of books available is less than, or equal to 0.
-- If so, return FALSE. If not, return TRUE.
IF stock_amount <= 0 THEN
RETURN FALSE;
ELSE
RETURN TRUE;
END IF;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 82 (OID 3626013)
--
-- Name: "extract_all_titles" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "extract_all_titles" () RETURNS text AS '
DECLARE
sub_id INTEGER;
text_output TEXT = '' '';
sub_title TEXT;
row_data books%ROWTYPE;
BEGIN
FOR i IN 0..15 LOOP
SELECT INTO sub_title subject FROM subjects WHERE id = i;
text_output = text_output || ''
'' || sub_title || '':
'';
FOR row_data IN SELECT * FROM books
WHERE subject_id = i LOOP
IF NOT FOUND THEN
text_output := text_output || ''None.
'';
ELSE
text_output := text_output || row_data.title || ''
'';
END IF;
END LOOP;
END LOOP;
RETURN text_output;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 79 (OID 3626052)
--
-- Name: "books_by_subject" (text) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "books_by_subject" (text) RETURNS text AS '
DECLARE
sub_title ALIAS FOR $1;
sub_id INTEGER;
found_text TEXT :='''';
BEGIN
SELECT INTO sub_id id FROM subjects WHERE subject = sub_title;
RAISE NOTICE ''sub_id = %'',sub_id;
IF sub_title = ''all'' THEN
found_text := extract_all_titles();
RETURN found_text;
ELSE IF sub_id >= 0 THEN
found_text := extract_title(sub_id);
RETURN ''
'' || sub_title || '':
'' || found_text;
END IF;
END IF;
RETURN ''Subject not found.'';
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 81 (OID 3626590)
--
-- Name: "add_two_loop" (integer,integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "add_two_loop" (integer,integer) RETURNS integer AS '
DECLARE
-- Declare aliases for function arguments.
low_number ALIAS FOR $1;
high_number ALIAS FOR $2;
-- Declare a variable to hold the result.
result INTEGER = 0;
BEGIN
WHILE result != high_number LOOP
result := result + 1;
END LOOP;
RETURN result;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 92 (OID 3627916)
--
-- Name: "extract_all_titles2" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "extract_all_titles2" () RETURNS text AS '
DECLARE
sub_id INTEGER;
text_output TEXT = '' '';
sub_title TEXT;
row_data books%ROWTYPE;
BEGIN
FOR i IN 0..15 LOOP
SELECT INTO sub_title subject FROM subjects WHERE id = i;
text_output = text_output || ''
'' || sub_title || '':
'';
FOR row_data IN SELECT * FROM books
WHERE subject_id = i LOOP
text_output := text_output || row_data.title || ''
'';
END LOOP;
END LOOP;
RETURN text_output;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 94 (OID 3627974)
--
-- Name: "extract_title" (integer) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "extract_title" (integer) RETURNS text AS '
DECLARE
sub_id ALIAS FOR $1;
text_output TEXT :=''
'';
row_data RECORD;
BEGIN
FOR row_data IN SELECT * FROM books
WHERE subject_id = sub_id ORDER BY title LOOP
text_output := text_output || row_data.title || ''
'';
END LOOP;
RETURN text_output;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 95 (OID 3628021)
--
-- Name: "raise_test" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "raise_test" () RETURNS integer AS '
DECLARE
-- Declare an integer variable for testing.
an_integer INTEGER = 1;
BEGIN
-- Raise a debug level message.
RAISE DEBUG ''The raise_test() function began.'';
an_integer = an_integer + 1;
-- Raise a notice stating that the an_integer
-- variable was changed, then raise another notice
-- stating its new value.
RAISE NOTICE ''Variable an_integer was changed.'';
RAISE NOTICE ''Variable an_integer value is now %.'',an_integer;
-- Raise an exception.
RAISE EXCEPTION ''Variable % changed. Aborting transaction.'',an_integer;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 93 (OID 3628069)
--
-- Name: "add_shipment" (integer,text) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "add_shipment" (integer,text) RETURNS timestamp with time zone AS '
DECLARE
customer_id ALIAS FOR $1;
isbn ALIAS FOR $2;
shipment_id INTEGER;
right_now timestamp;
BEGIN
right_now := ''now'';
SELECT INTO shipment_id id FROM shipments ORDER BY id DESC;
shipment_id := shipment_id + 1;
INSERT INTO shipments VALUES ( shipment_id, customer_id, isbn, right_now );
RETURN right_now;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 102 (OID 3628076)
--
-- Name: "ship_item" (text,text,text) Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "ship_item" (text,text,text) RETURNS integer AS '
DECLARE
l_name ALIAS FOR $1;
f_name ALIAS FOR $2;
book_isbn ALIAS FOR $3;
book_id INTEGER;
customer_id INTEGER;
BEGIN
SELECT INTO customer_id get_customer_id(l_name,f_name);
IF customer_id = -1 THEN
RETURN -1;
END IF;
SELECT INTO book_id book_id FROM editions WHERE isbn = book_isbn;
IF NOT FOUND THEN
RETURN -1;
END IF;
PERFORM add_shipment(customer_id,book_isbn);
RETURN 1;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 103 (OID 3628114)
--
-- Name: "check_book_addition" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "check_book_addition" () RETURNS opaque AS '
DECLARE
id_number INTEGER;
book_isbn TEXT;
BEGIN
SELECT INTO id_number id FROM customers WHERE id = NEW.customer_id;
IF NOT FOUND THEN
RAISE EXCEPTION ''Invalid customer ID number.'';
END IF;
SELECT INTO book_isbn isbn FROM editions WHERE isbn = NEW.isbn;
IF NOT FOUND THEN
RAISE EXCEPTION ''Invalid ISBN.'';
END IF;
UPDATE stock SET stock = stock -1 WHERE isbn = NEW.isbn;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 28 (OID 3628246)
--
-- Name: stock_view Type: VIEW Owner: postgres
--
CREATE VIEW "stock_view" as SELECT stock.isbn, stock.retail, stock.stock FROM stock;
--
-- TOC Entry ID 30 (OID 3628247)
--
-- Name: favorite_books Type: TABLE Owner: manager
--
CREATE TABLE "favorite_books" (
"employee_id" integer,
"books" text[]
);
--
-- TOC Entry ID 8 (OID 3628626)
--
-- Name: shipments_ship_id_seq Type: SEQUENCE Owner: manager
--
CREATE SEQUENCE "shipments_ship_id_seq" start 0 increment 1 maxvalue 2147483647 minvalue 0 cache 1 ;
--
-- TOC Entry ID 74 (OID 3628648)
--
-- Name: "check_shipment_addition" () Type: FUNCTION Owner: postgres
--
CREATE FUNCTION "check_shipment_addition" () RETURNS opaque AS '
DECLARE
-- Declare a variable to hold the customer ID.
id_number INTEGER;
-- Declare a variable to hold the ISBN.
book_isbn TEXT;
BEGIN
-- If there is an ID number that matches the customer ID in
-- the new table, retrieve it from the customers table.
SELECT INTO id_number id FROM customers WHERE id = NEW.customer_id;
-- If there was no matching ID number, raise an exception.
IF NOT FOUND THEN
RAISE EXCEPTION ''Invalid customer ID number.'';
END IF;
-- If there is an ISBN that matches the ISBN specified in the
-- new table, retrieve it from the editions table.
SELECT INTO book_isbn isbn FROM editions WHERE isbn = NEW.isbn;
-- If there is no matching ISBN, raise an exception.
IF NOT FOUND THEN
RAISE EXCEPTION ''Invalid ISBN.'';
END IF;
-- If the previous checks succeeded, update the stock amount
-- for INSERT commands.
IF TG_OP = ''INSERT'' THEN
UPDATE stock SET stock = stock -1 WHERE isbn = NEW.isbn;
END IF;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
--
-- TOC Entry ID 31 (OID 3628899)
--
-- Name: employees Type: TABLE Owner: postgres
--
CREATE TABLE "employees" (
"id" integer NOT NULL,
"last_name" text NOT NULL,
"first_name" text,
CONSTRAINT "employees_id" CHECK ((id > 100)),
Constraint "employees_pkey" Primary Key ("id")
);
--
-- TOC Entry ID 32 (OID 3629174)
--
-- Name: editions Type: TABLE Owner: manager
--
CREATE TABLE "editions" (
"isbn" text NOT NULL,
"book_id" integer,
"edition" integer,
"publisher_id" integer,
"publication" date,
"type" character(1),
CONSTRAINT "integrity" CHECK (((book_id NOTNULL) AND (edition NOTNULL))),
Constraint "pkey" Primary Key ("isbn")
);