simple-sql 0.5.8 → 0.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Rakefile +13 -0
- data/bin/db_restore +14 -0
- data/bin/pg +1 -1
- data/lib/simple/sql.rb +11 -1
- data/lib/simple/sql/connection/base.rb +5 -7
- data/lib/simple/sql/connection/reflection.rb +26 -1
- data/lib/simple/sql/connection/scope/count.rb +9 -10
- data/lib/simple/sql/connection/scope/count_by_groups.rb +7 -6
- data/lib/simple/sql/result.rb +3 -19
- data/lib/simple/sql/scope/count.rb +1 -1
- data/lib/simple/sql/version.rb +1 -1
- data/spec/fixtures/booktown.sql +1793 -0
- data/spec/simple/sql/count_by_groups_spec.rb +4 -4
- data/spec/simple/sql/count_spec.rb +3 -3
- data/spec/simple/sql/result_count_spec.rb +3 -11
- data/spec/simple/sql/scope_spec.rb +0 -3
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 913129df1de6aab1bd350652782ea671e56f6a396dd8c6a2e7a8cebc972fab0e
|
4
|
+
data.tar.gz: 6aca39154c90cdfa0cb089c3358afa0f3c28ab261c837c99536c1fa39c3569f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '019f7c7b5c0b9364858b0af3a4cbf4ccffc69ae961610cbe5b552d4d646a2df0870c0e20d990dd77a36821a2c6395d05687464093b030cef7bbdc7115c9af3bb'
|
7
|
+
data.tar.gz: 4cf3ec7ff055cb174ca25043c46f714086483e73908d30daf723df062ed809c747500251555823eba676d6e4c747bdb80a2cb34c303e1ed904d4ef752e4f619d
|
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
Dir.glob("tasks/*.rake").each { |r| import r }
|
2
2
|
|
3
|
+
task "test:generate_fixtures" do
|
4
|
+
FileUtils.mkdir_p "tmp"
|
5
|
+
FileUtils.mkdir_p "spec/fixtures"
|
6
|
+
|
7
|
+
Dir.chdir "tmp" do
|
8
|
+
sh "curl -L -O https://raw.githubusercontent.com/comperiosearch/booktownDemo/master/booktown.sql"
|
9
|
+
end
|
10
|
+
|
11
|
+
sh "dropdb booktown || true"
|
12
|
+
sh "psql -f tmp/booktown.sql"
|
13
|
+
sh "psql booktown -c 'alter schema public rename to booktown'"
|
14
|
+
sh "pg_dump --no-owner booktown > spec/fixtures/booktown.sql"
|
15
|
+
end
|
3
16
|
|
4
17
|
task "test:prepare_db" do
|
5
18
|
sh "createdb simple-sql-test 2>&1 > /dev/null || true"
|
data/bin/db_restore
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
env = ENV["POSTJOB_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
5
|
+
|
6
|
+
configs = YAML.load_file "config/database.yml"
|
7
|
+
config = configs.fetch(env) { configs.fetch("defaults") }
|
8
|
+
|
9
|
+
ENV["PGHOST"] = config["host"]
|
10
|
+
ENV["PGPORT"] = config["port"] && config["port"].to_s
|
11
|
+
ENV["PGUSER"] = config["username"]
|
12
|
+
ENV["PGPASSWORD"] = config["password"]
|
13
|
+
|
14
|
+
system "pg_restore", "-d", config.fetch("database"), *ARGV
|
data/bin/pg
CHANGED
data/lib/simple/sql.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
2
|
+
|
1
3
|
require "forwardable"
|
2
4
|
require "logger"
|
3
5
|
require "expectation"
|
@@ -16,7 +18,7 @@ module Simple
|
|
16
18
|
extend self
|
17
19
|
|
18
20
|
extend Forwardable
|
19
|
-
delegate [:ask, :all, :each, :exec, :locked, :print, :transaction, :wait_for_notify, :
|
21
|
+
delegate [:ask, :all, :each, :exec, :locked, :print, :transaction, :wait_for_notify, :estimate_cost] => :default_connection
|
20
22
|
delegate [:reflection] => :default_connection
|
21
23
|
delegate [:duplicate] => :default_connection
|
22
24
|
delegate [:insert] => :default_connection
|
@@ -24,6 +26,14 @@ module Simple
|
|
24
26
|
|
25
27
|
delegate [:logger, :logger=] => ::Simple::SQL::Logging
|
26
28
|
|
29
|
+
def escape_string(s)
|
30
|
+
expect! s => [Symbol, String, nil]
|
31
|
+
|
32
|
+
return "NULL" unless s
|
33
|
+
|
34
|
+
"'#{PG::Connection.escape_string(s)}'"
|
35
|
+
end
|
36
|
+
|
27
37
|
# connects to the database specified via the url parameter. If called
|
28
38
|
# without argument it tries to determine a DATABASE_URL from either the
|
29
39
|
# environment setting (DATABASE_URL) or from a config/database.yml file,
|
@@ -86,13 +86,9 @@ class Simple::SQL::Connection
|
|
86
86
|
end
|
87
87
|
|
88
88
|
# returns an Array [min_cost, max_cost] based on the database's estimation
|
89
|
-
def
|
90
|
-
|
91
|
-
|
92
|
-
raise "Cannot determine cost"
|
93
|
-
end
|
94
|
-
|
95
|
-
[Float($1), Float($3)]
|
89
|
+
def estimate_cost(sql, *args)
|
90
|
+
explanation = ask "EXPLAIN (FORMAT JSON) #{sql}", *args
|
91
|
+
explanation.first.dig "Plan", "Total Cost"
|
96
92
|
end
|
97
93
|
|
98
94
|
# Executes a block, usually of db insert code, while holding an
|
@@ -128,6 +124,8 @@ class Simple::SQL::Connection
|
|
128
124
|
Logging.with_logged_query self, sql, *args do
|
129
125
|
raw_connection.exec_params(sql, Encoder.encode_args(raw_connection, args))
|
130
126
|
end
|
127
|
+
rescue PG::InvalidTextRepresentation
|
128
|
+
raise ArgumentError, $!.to_s
|
131
129
|
end
|
132
130
|
|
133
131
|
# returns an array of decoded entries, if any
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# rubocop:disable Metrics/ClassLength
|
2
|
+
|
1
3
|
class Simple::SQL::Connection
|
2
4
|
def reset_reflection
|
3
5
|
@reflection = nil
|
@@ -16,16 +18,39 @@ class Simple::SQL::Connection
|
|
16
18
|
table_info(schema: schema).keys
|
17
19
|
end
|
18
20
|
|
21
|
+
def primary_key_column(table_name)
|
22
|
+
@primary_key_column ||= {}
|
23
|
+
@primary_key_column[table_name] ||= begin
|
24
|
+
pk_column, other = primary_key_columns(table_name)
|
25
|
+
|
26
|
+
raise "#{table_name}: No support for combined primary keys" if other
|
27
|
+
raise "#{table_name}: No primary key" if pk_column.nil?
|
28
|
+
|
29
|
+
pk_column
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
19
33
|
def primary_key_columns(table_name)
|
20
|
-
@
|
34
|
+
@primary_key_columns ||= {}
|
35
|
+
@primary_key_columns[table_name] ||= _primary_key_columns(table_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def _primary_key_columns(table_name)
|
41
|
+
sql = <<~SQL
|
21
42
|
SELECT pg_attribute.attname
|
22
43
|
FROM pg_index
|
23
44
|
JOIN pg_attribute ON pg_attribute.attrelid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)
|
24
45
|
WHERE pg_index.indrelid = $1::regclass
|
25
46
|
AND pg_index.indisprimary;
|
26
47
|
SQL
|
48
|
+
|
49
|
+
@connection.all(sql, table_name)
|
27
50
|
end
|
28
51
|
|
52
|
+
public
|
53
|
+
|
29
54
|
TIMESTAMP_COLUMN_NAMES = %w(inserted_at created_at updated_at)
|
30
55
|
|
31
56
|
# timestamp_columns are columns that will be set automatically after
|
@@ -11,7 +11,7 @@ class Simple::SQL::Connection::Scope
|
|
11
11
|
# Returns the fast count of matching records
|
12
12
|
#
|
13
13
|
# For counts larger than EXACT_COUNT_THRESHOLD this returns an estimate
|
14
|
-
def
|
14
|
+
def count_estimate
|
15
15
|
estimate = estimated_count
|
16
16
|
return estimate if estimate > EXACT_COUNT_THRESHOLD
|
17
17
|
|
@@ -19,17 +19,16 @@ class Simple::SQL::Connection::Scope
|
|
19
19
|
@connection.ask("SELECT COUNT(*) FROM (#{sql}) _total_count", *args)
|
20
20
|
end
|
21
21
|
|
22
|
+
# returns the query plan as a Hash.
|
23
|
+
def explain
|
24
|
+
sql = to_sql(pagination: false)
|
25
|
+
explanation = @connection.ask("EXPLAIN (FORMAT JSON) #{sql}", *args).first || {}
|
26
|
+
explanation["Plan"]
|
27
|
+
end
|
28
|
+
|
22
29
|
private
|
23
30
|
|
24
31
|
def estimated_count
|
25
|
-
|
26
|
-
lines = @connection.all("EXPLAIN #{sql}", *args)
|
27
|
-
lines.each do |line|
|
28
|
-
next unless line =~ /\brows=(\d+)/
|
29
|
-
|
30
|
-
return Integer($1)
|
31
|
-
end
|
32
|
-
|
33
|
-
-1
|
32
|
+
order_by(nil).explain.fetch("Plan Rows", -1)
|
34
33
|
end
|
35
34
|
end
|
@@ -20,8 +20,8 @@ class Simple::SQL::Connection::Scope
|
|
20
20
|
def enumerate_groups(sql_fragment)
|
21
21
|
sql = order_by(nil).to_sql(pagination: false)
|
22
22
|
|
23
|
-
|
24
|
-
raise "enumerate_groups
|
23
|
+
cost = @connection.estimate_cost "SELECT MIN(#{sql_fragment}) FROM (#{sql}) sq", *args
|
24
|
+
raise "enumerate_groups(#{sql_fragment.inspect}) takes too much time. Make sure to create a suitable index" if cost > 10_000
|
25
25
|
|
26
26
|
groups = []
|
27
27
|
var_name = "$#{@args.count + 1}"
|
@@ -42,12 +42,12 @@ class Simple::SQL::Connection::Scope
|
|
42
42
|
Hash[recs]
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
45
|
+
def count_by_estimate(sql_fragment)
|
46
46
|
sql = order_by(nil).to_sql(pagination: false)
|
47
47
|
|
48
|
-
|
48
|
+
cost = @connection.estimate_cost "SELECT COUNT(*) FROM (#{sql}) sq GROUP BY #{sql_fragment}", *args
|
49
49
|
|
50
|
-
return count_by(sql_fragment) if
|
50
|
+
return count_by(sql_fragment) if cost < 10_000
|
51
51
|
|
52
52
|
# iterate over all groups, estimating the count for each. If the count is
|
53
53
|
# less than EXACT_COUNT_THRESHOLD we ask for the exact count in that and
|
@@ -58,7 +58,8 @@ class Simple::SQL::Connection::Scope
|
|
58
58
|
sparse_groups = []
|
59
59
|
enumerate_groups(sql_fragment).each do |group|
|
60
60
|
scope = @connection.scope("SELECT * FROM (#{sql}) sq WHERE #{sql_fragment}=#{var_name}", *args, group)
|
61
|
-
|
61
|
+
estimated_count = scope.send(:estimated_count)
|
62
|
+
counts[group] = estimated_count
|
62
63
|
sparse_groups << group if estimated_count < EXACT_COUNT_THRESHOLD
|
63
64
|
end
|
64
65
|
|
data/lib/simple/sql/result.rb
CHANGED
@@ -38,15 +38,8 @@ class ::Simple::SQL::Result < Array
|
|
38
38
|
# returns the (potentialy estimated) total count of results
|
39
39
|
#
|
40
40
|
# This is only available for paginated scopes
|
41
|
-
def
|
42
|
-
@
|
43
|
-
end
|
44
|
-
|
45
|
-
# returns the (potentialy estimated) total number of pages
|
46
|
-
#
|
47
|
-
# This is only available for paginated scopes
|
48
|
-
def total_fast_pages
|
49
|
-
@total_fast_pages ||= (total_fast_count * 1.0 / pagination_scope.per).ceil
|
41
|
+
def total_count_estimate
|
42
|
+
@total_count_estimate ||= pagination_scope.count_estimate
|
50
43
|
end
|
51
44
|
|
52
45
|
# returns the (potentialy slow) exact total count of results
|
@@ -56,13 +49,6 @@ class ::Simple::SQL::Result < Array
|
|
56
49
|
@total_count ||= pagination_scope.count
|
57
50
|
end
|
58
51
|
|
59
|
-
# returns the (potentialy estimated) total number of pages
|
60
|
-
#
|
61
|
-
# This is only available for paginated scopes
|
62
|
-
def total_pages
|
63
|
-
@total_pages ||= (total_count * 1.0 / pagination_scope.per).ceil
|
64
|
-
end
|
65
|
-
|
66
52
|
# returns the current page number in a paginated search
|
67
53
|
#
|
68
54
|
# This is only available for paginated scopes
|
@@ -91,9 +77,7 @@ class ::Simple::SQL::Result < Array
|
|
91
77
|
if scope.page <= 1 && empty?
|
92
78
|
@current_page = 1
|
93
79
|
@total_count = 0
|
94
|
-
@
|
95
|
-
@total_fast_count = 0
|
96
|
-
@total_fast_pages = 1
|
80
|
+
@total_count_estimate = 0
|
97
81
|
end
|
98
82
|
end
|
99
83
|
end
|
@@ -11,7 +11,7 @@ class Simple::SQL::Connection::Scope
|
|
11
11
|
# Returns the fast count of matching records
|
12
12
|
#
|
13
13
|
# For counts larger than EXACT_COUNT_THRESHOLD this returns an estimate
|
14
|
-
def
|
14
|
+
def count_estimate
|
15
15
|
estimate = estimated_count
|
16
16
|
return estimate if estimate > EXACT_COUNT_THRESHOLD
|
17
17
|
|
data/lib/simple/sql/version.rb
CHANGED
@@ -0,0 +1,1793 @@
|
|
1
|
+
--
|
2
|
+
-- PostgreSQL database dump
|
3
|
+
--
|
4
|
+
|
5
|
+
-- Dumped from database version 9.6.10
|
6
|
+
-- Dumped by pg_dump version 11.1
|
7
|
+
|
8
|
+
SET statement_timeout = 0;
|
9
|
+
SET lock_timeout = 0;
|
10
|
+
SET idle_in_transaction_session_timeout = 0;
|
11
|
+
SET client_encoding = 'UTF8';
|
12
|
+
SET standard_conforming_strings = on;
|
13
|
+
SELECT pg_catalog.set_config('search_path', '', false);
|
14
|
+
SET check_function_bodies = false;
|
15
|
+
SET client_min_messages = warning;
|
16
|
+
SET row_security = off;
|
17
|
+
|
18
|
+
--
|
19
|
+
-- Name: booktown; Type: SCHEMA; Schema: -; Owner: -
|
20
|
+
--
|
21
|
+
|
22
|
+
CREATE SCHEMA booktown;
|
23
|
+
|
24
|
+
|
25
|
+
--
|
26
|
+
-- Name: SCHEMA booktown; Type: COMMENT; Schema: -; Owner: -
|
27
|
+
--
|
28
|
+
|
29
|
+
COMMENT ON SCHEMA booktown IS 'standard public schema';
|
30
|
+
|
31
|
+
|
32
|
+
--
|
33
|
+
-- Name: add_shipment(integer, text); Type: FUNCTION; Schema: booktown; Owner: -
|
34
|
+
--
|
35
|
+
|
36
|
+
CREATE FUNCTION booktown.add_shipment(integer, text) RETURNS timestamp with time zone
|
37
|
+
LANGUAGE plpgsql
|
38
|
+
AS $_$
|
39
|
+
DECLARE
|
40
|
+
customer_id ALIAS FOR $1;
|
41
|
+
isbn ALIAS FOR $2;
|
42
|
+
shipment_id INTEGER;
|
43
|
+
right_now timestamp;
|
44
|
+
BEGIN
|
45
|
+
right_now := 'now';
|
46
|
+
SELECT INTO shipment_id id FROM shipments ORDER BY id DESC;
|
47
|
+
shipment_id := shipment_id + 1;
|
48
|
+
INSERT INTO shipments VALUES ( shipment_id, customer_id, isbn, right_now );
|
49
|
+
RETURN right_now;
|
50
|
+
END;
|
51
|
+
$_$;
|
52
|
+
|
53
|
+
|
54
|
+
--
|
55
|
+
-- Name: add_two_loop(integer, integer); Type: FUNCTION; Schema: booktown; Owner: -
|
56
|
+
--
|
57
|
+
|
58
|
+
CREATE FUNCTION booktown.add_two_loop(integer, integer) RETURNS integer
|
59
|
+
LANGUAGE plpgsql
|
60
|
+
AS $_$
|
61
|
+
DECLARE
|
62
|
+
|
63
|
+
-- Declare aliases for function arguments.
|
64
|
+
|
65
|
+
low_number ALIAS FOR $1;
|
66
|
+
high_number ALIAS FOR $2;
|
67
|
+
|
68
|
+
-- Declare a variable to hold the result.
|
69
|
+
|
70
|
+
result INTEGER = 0;
|
71
|
+
|
72
|
+
BEGIN
|
73
|
+
|
74
|
+
WHILE result != high_number LOOP
|
75
|
+
result := result + 1;
|
76
|
+
END LOOP;
|
77
|
+
|
78
|
+
RETURN result;
|
79
|
+
END;
|
80
|
+
$_$;
|
81
|
+
|
82
|
+
|
83
|
+
--
|
84
|
+
-- Name: audit_test(); Type: FUNCTION; Schema: booktown; Owner: -
|
85
|
+
--
|
86
|
+
|
87
|
+
CREATE FUNCTION booktown.audit_test() RETURNS opaque
|
88
|
+
LANGUAGE plpgsql
|
89
|
+
AS $$
|
90
|
+
BEGIN
|
91
|
+
|
92
|
+
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
|
93
|
+
|
94
|
+
NEW.user_aud := current_user;
|
95
|
+
NEW.mod_time := 'NOW';
|
96
|
+
|
97
|
+
INSERT INTO inventory_audit SELECT * FROM inventory WHERE prod_id=NEW.prod_id;
|
98
|
+
|
99
|
+
RETURN NEW;
|
100
|
+
|
101
|
+
ELSE if TG_OP = 'DELETE' THEN
|
102
|
+
INSERT INTO inventory_audit SELECT *, current_user, 'NOW' FROM inventory WHERE prod_id=OLD.prod_id;
|
103
|
+
|
104
|
+
RETURN OLD;
|
105
|
+
END IF;
|
106
|
+
END IF;
|
107
|
+
END;
|
108
|
+
$$;
|
109
|
+
|
110
|
+
|
111
|
+
--
|
112
|
+
-- Name: books_by_subject(text); Type: FUNCTION; Schema: booktown; Owner: -
|
113
|
+
--
|
114
|
+
|
115
|
+
CREATE FUNCTION booktown.books_by_subject(text) RETURNS text
|
116
|
+
LANGUAGE plpgsql
|
117
|
+
AS $_$
|
118
|
+
DECLARE
|
119
|
+
sub_title ALIAS FOR $1;
|
120
|
+
sub_id INTEGER;
|
121
|
+
found_text TEXT :='';
|
122
|
+
BEGIN
|
123
|
+
SELECT INTO sub_id id FROM subjects WHERE subject = sub_title;
|
124
|
+
RAISE NOTICE 'sub_id = %',sub_id;
|
125
|
+
IF sub_title = 'all' THEN
|
126
|
+
found_text := extract_all_titles();
|
127
|
+
RETURN found_text;
|
128
|
+
ELSE IF sub_id >= 0 THEN
|
129
|
+
found_text := extract_title(sub_id);
|
130
|
+
RETURN '
|
131
|
+
' || sub_title || ':
|
132
|
+
' || found_text;
|
133
|
+
END IF;
|
134
|
+
END IF;
|
135
|
+
RETURN 'Subject not found.';
|
136
|
+
END;
|
137
|
+
$_$;
|
138
|
+
|
139
|
+
|
140
|
+
--
|
141
|
+
-- Name: check_book_addition(); Type: FUNCTION; Schema: booktown; Owner: -
|
142
|
+
--
|
143
|
+
|
144
|
+
CREATE FUNCTION booktown.check_book_addition() RETURNS opaque
|
145
|
+
LANGUAGE plpgsql
|
146
|
+
AS $$
|
147
|
+
DECLARE
|
148
|
+
id_number INTEGER;
|
149
|
+
book_isbn TEXT;
|
150
|
+
BEGIN
|
151
|
+
|
152
|
+
SELECT INTO id_number id FROM customers WHERE id = NEW.customer_id;
|
153
|
+
|
154
|
+
IF NOT FOUND THEN
|
155
|
+
RAISE EXCEPTION 'Invalid customer ID number.';
|
156
|
+
END IF;
|
157
|
+
|
158
|
+
SELECT INTO book_isbn isbn FROM editions WHERE isbn = NEW.isbn;
|
159
|
+
|
160
|
+
IF NOT FOUND THEN
|
161
|
+
RAISE EXCEPTION 'Invalid ISBN.';
|
162
|
+
END IF;
|
163
|
+
|
164
|
+
UPDATE stock SET stock = stock -1 WHERE isbn = NEW.isbn;
|
165
|
+
|
166
|
+
RETURN NEW;
|
167
|
+
END;
|
168
|
+
$$;
|
169
|
+
|
170
|
+
|
171
|
+
--
|
172
|
+
-- Name: check_shipment_addition(); Type: FUNCTION; Schema: booktown; Owner: -
|
173
|
+
--
|
174
|
+
|
175
|
+
CREATE FUNCTION booktown.check_shipment_addition() RETURNS trigger
|
176
|
+
LANGUAGE plpgsql
|
177
|
+
AS $$
|
178
|
+
DECLARE
|
179
|
+
-- Declare a variable to hold the customer ID.
|
180
|
+
id_number INTEGER;
|
181
|
+
|
182
|
+
-- Declare a variable to hold the ISBN.
|
183
|
+
book_isbn TEXT;
|
184
|
+
BEGIN
|
185
|
+
|
186
|
+
-- If there is an ID number that matches the customer ID in
|
187
|
+
-- the new table, retrieve it from the customers table.
|
188
|
+
SELECT INTO id_number id FROM customers WHERE id = NEW.customer_id;
|
189
|
+
|
190
|
+
-- If there was no matching ID number, raise an exception.
|
191
|
+
IF NOT FOUND THEN
|
192
|
+
RAISE EXCEPTION 'Invalid customer ID number.';
|
193
|
+
END IF;
|
194
|
+
|
195
|
+
-- If there is an ISBN that matches the ISBN specified in the
|
196
|
+
-- new table, retrieve it from the editions table.
|
197
|
+
SELECT INTO book_isbn isbn FROM editions WHERE isbn = NEW.isbn;
|
198
|
+
|
199
|
+
-- If there is no matching ISBN, raise an exception.
|
200
|
+
IF NOT FOUND THEN
|
201
|
+
RAISE EXCEPTION 'Invalid ISBN.';
|
202
|
+
END IF;
|
203
|
+
|
204
|
+
-- If the previous checks succeeded, update the stock amount
|
205
|
+
-- for INSERT commands.
|
206
|
+
IF TG_OP = 'INSERT' THEN
|
207
|
+
UPDATE stock SET stock = stock -1 WHERE isbn = NEW.isbn;
|
208
|
+
END IF;
|
209
|
+
|
210
|
+
RETURN NEW;
|
211
|
+
END;
|
212
|
+
$$;
|
213
|
+
|
214
|
+
|
215
|
+
--
|
216
|
+
-- Name: compound_word(text, text); Type: FUNCTION; Schema: booktown; Owner: -
|
217
|
+
--
|
218
|
+
|
219
|
+
CREATE FUNCTION booktown.compound_word(text, text) RETURNS text
|
220
|
+
LANGUAGE plpgsql
|
221
|
+
AS $_$
|
222
|
+
DECLARE
|
223
|
+
-- defines an alias name for the two input values
|
224
|
+
word1 ALIAS FOR $1;
|
225
|
+
word2 ALIAS FOR $2;
|
226
|
+
BEGIN
|
227
|
+
-- displays the resulting joined words
|
228
|
+
RETURN word1 || word2;
|
229
|
+
END;
|
230
|
+
$_$;
|
231
|
+
|
232
|
+
|
233
|
+
--
|
234
|
+
-- Name: count_by_two(integer); Type: FUNCTION; Schema: booktown; Owner: -
|
235
|
+
--
|
236
|
+
|
237
|
+
CREATE FUNCTION booktown.count_by_two(integer) RETURNS integer
|
238
|
+
LANGUAGE plpgsql
|
239
|
+
AS $_$
|
240
|
+
DECLARE
|
241
|
+
userNum ALIAS FOR $1;
|
242
|
+
i integer;
|
243
|
+
BEGIN
|
244
|
+
i := 1;
|
245
|
+
WHILE userNum[1] < 20 LOOP
|
246
|
+
i = i+1;
|
247
|
+
return userNum;
|
248
|
+
END LOOP;
|
249
|
+
|
250
|
+
END;
|
251
|
+
$_$;
|
252
|
+
|
253
|
+
|
254
|
+
--
|
255
|
+
-- Name: double_price(double precision); Type: FUNCTION; Schema: booktown; Owner: -
|
256
|
+
--
|
257
|
+
|
258
|
+
CREATE FUNCTION booktown.double_price(double precision) RETURNS double precision
|
259
|
+
LANGUAGE plpgsql
|
260
|
+
AS $_$
|
261
|
+
DECLARE
|
262
|
+
BEGIN
|
263
|
+
return $1 * 2;
|
264
|
+
END;
|
265
|
+
$_$;
|
266
|
+
|
267
|
+
|
268
|
+
--
|
269
|
+
-- Name: extract_all_titles(); Type: FUNCTION; Schema: booktown; Owner: -
|
270
|
+
--
|
271
|
+
|
272
|
+
CREATE FUNCTION booktown.extract_all_titles() RETURNS text
|
273
|
+
LANGUAGE plpgsql
|
274
|
+
AS $$
|
275
|
+
DECLARE
|
276
|
+
sub_id INTEGER;
|
277
|
+
text_output TEXT = ' ';
|
278
|
+
sub_title TEXT;
|
279
|
+
row_data books%ROWTYPE;
|
280
|
+
BEGIN
|
281
|
+
FOR i IN 0..15 LOOP
|
282
|
+
SELECT INTO sub_title subject FROM subjects WHERE id = i;
|
283
|
+
text_output = text_output || '
|
284
|
+
' || sub_title || ':
|
285
|
+
';
|
286
|
+
|
287
|
+
FOR row_data IN SELECT * FROM books
|
288
|
+
WHERE subject_id = i LOOP
|
289
|
+
|
290
|
+
IF NOT FOUND THEN
|
291
|
+
text_output := text_output || 'None.
|
292
|
+
';
|
293
|
+
ELSE
|
294
|
+
text_output := text_output || row_data.title || '
|
295
|
+
';
|
296
|
+
END IF;
|
297
|
+
|
298
|
+
END LOOP;
|
299
|
+
END LOOP;
|
300
|
+
RETURN text_output;
|
301
|
+
END;
|
302
|
+
$$;
|
303
|
+
|
304
|
+
|
305
|
+
--
|
306
|
+
-- Name: extract_all_titles2(); Type: FUNCTION; Schema: booktown; Owner: -
|
307
|
+
--
|
308
|
+
|
309
|
+
CREATE FUNCTION booktown.extract_all_titles2() RETURNS text
|
310
|
+
LANGUAGE plpgsql
|
311
|
+
AS $$
|
312
|
+
DECLARE
|
313
|
+
sub_id INTEGER;
|
314
|
+
text_output TEXT = ' ';
|
315
|
+
sub_title TEXT;
|
316
|
+
row_data books%ROWTYPE;
|
317
|
+
BEGIN
|
318
|
+
FOR i IN 0..15 LOOP
|
319
|
+
SELECT INTO sub_title subject FROM subjects WHERE id = i;
|
320
|
+
text_output = text_output || '
|
321
|
+
' || sub_title || ':
|
322
|
+
';
|
323
|
+
|
324
|
+
FOR row_data IN SELECT * FROM books
|
325
|
+
WHERE subject_id = i LOOP
|
326
|
+
|
327
|
+
text_output := text_output || row_data.title || '
|
328
|
+
';
|
329
|
+
|
330
|
+
END LOOP;
|
331
|
+
END LOOP;
|
332
|
+
RETURN text_output;
|
333
|
+
END;
|
334
|
+
$$;
|
335
|
+
|
336
|
+
|
337
|
+
--
|
338
|
+
-- Name: extract_title(integer); Type: FUNCTION; Schema: booktown; Owner: -
|
339
|
+
--
|
340
|
+
|
341
|
+
CREATE FUNCTION booktown.extract_title(integer) RETURNS text
|
342
|
+
LANGUAGE plpgsql
|
343
|
+
AS $_$
|
344
|
+
DECLARE
|
345
|
+
sub_id ALIAS FOR $1;
|
346
|
+
text_output TEXT :='
|
347
|
+
';
|
348
|
+
row_data RECORD;
|
349
|
+
BEGIN
|
350
|
+
FOR row_data IN SELECT * FROM books
|
351
|
+
WHERE subject_id = sub_id ORDER BY title LOOP
|
352
|
+
text_output := text_output || row_data.title || '
|
353
|
+
';
|
354
|
+
END LOOP;
|
355
|
+
RETURN text_output;
|
356
|
+
END;
|
357
|
+
$_$;
|
358
|
+
|
359
|
+
|
360
|
+
--
|
361
|
+
-- Name: first(); Type: FUNCTION; Schema: booktown; Owner: -
|
362
|
+
--
|
363
|
+
|
364
|
+
CREATE FUNCTION booktown.first() RETURNS integer
|
365
|
+
LANGUAGE plpgsql
|
366
|
+
AS $$
|
367
|
+
DecLarE
|
368
|
+
oNe IntEgER := 1;
|
369
|
+
bEGiN
|
370
|
+
ReTUrn oNE;
|
371
|
+
eNd;
|
372
|
+
$$;
|
373
|
+
|
374
|
+
|
375
|
+
--
|
376
|
+
-- Name: get_author(integer); Type: FUNCTION; Schema: booktown; Owner: -
|
377
|
+
--
|
378
|
+
|
379
|
+
CREATE FUNCTION booktown.get_author(integer) RETURNS text
|
380
|
+
LANGUAGE plpgsql
|
381
|
+
AS $_$
|
382
|
+
DECLARE
|
383
|
+
|
384
|
+
-- Declare an alias for the function argument,
|
385
|
+
-- which should be the id of the author.
|
386
|
+
author_id ALIAS FOR $1;
|
387
|
+
|
388
|
+
-- Declare a variable that uses the structure of
|
389
|
+
-- the authors table.
|
390
|
+
found_author authors%ROWTYPE;
|
391
|
+
|
392
|
+
BEGIN
|
393
|
+
|
394
|
+
-- Retrieve a row of author information for
|
395
|
+
-- the author whose id number matches
|
396
|
+
-- the argument received by the function.
|
397
|
+
SELECT INTO found_author * FROM authors WHERE id = author_id;
|
398
|
+
|
399
|
+
-- Return the first
|
400
|
+
RETURN found_author.first_name || ' ' || found_author.last_name;
|
401
|
+
|
402
|
+
END;
|
403
|
+
$_$;
|
404
|
+
|
405
|
+
|
406
|
+
--
|
407
|
+
-- Name: get_author(text); Type: FUNCTION; Schema: booktown; Owner: -
|
408
|
+
--
|
409
|
+
|
410
|
+
CREATE FUNCTION booktown.get_author(text) RETURNS text
|
411
|
+
LANGUAGE plpgsql
|
412
|
+
AS $_$
|
413
|
+
DECLARE
|
414
|
+
|
415
|
+
-- Declare an alias for the function argument,
|
416
|
+
-- which should be the first name of an author.
|
417
|
+
f_name ALIAS FOR $1;
|
418
|
+
|
419
|
+
-- Declare a variable with the same type as
|
420
|
+
-- the last_name field of the authors table.
|
421
|
+
l_name authors.last_name%TYPE;
|
422
|
+
|
423
|
+
BEGIN
|
424
|
+
|
425
|
+
-- Retrieve the last name of an author from the
|
426
|
+
-- authors table whose first name matches the
|
427
|
+
-- argument received by the function, and
|
428
|
+
-- insert it into the l_name variable.
|
429
|
+
SELECT INTO l_name last_name FROM authors WHERE first_name = f_name;
|
430
|
+
|
431
|
+
-- Return the first name and last name, separated
|
432
|
+
-- by a space.
|
433
|
+
return f_name || ' ' || l_name;
|
434
|
+
|
435
|
+
END;
|
436
|
+
$_$;
|
437
|
+
|
438
|
+
|
439
|
+
--
|
440
|
+
-- Name: get_customer_id(text, text); Type: FUNCTION; Schema: booktown; Owner: -
|
441
|
+
--
|
442
|
+
|
443
|
+
CREATE FUNCTION booktown.get_customer_id(text, text) RETURNS integer
|
444
|
+
LANGUAGE plpgsql
|
445
|
+
AS $_$
|
446
|
+
DECLARE
|
447
|
+
|
448
|
+
-- Declare aliases for user input.
|
449
|
+
l_name ALIAS FOR $1;
|
450
|
+
f_name ALIAS FOR $2;
|
451
|
+
|
452
|
+
-- Declare a variable to hold the customer ID number.
|
453
|
+
customer_id INTEGER;
|
454
|
+
|
455
|
+
BEGIN
|
456
|
+
|
457
|
+
-- Retrieve the customer ID number of the customer whose first and last
|
458
|
+
-- name match the values supplied as function arguments.
|
459
|
+
SELECT INTO customer_id id FROM customers
|
460
|
+
WHERE last_name = l_name AND first_name = f_name;
|
461
|
+
|
462
|
+
-- Return the ID number.
|
463
|
+
RETURN customer_id;
|
464
|
+
END;
|
465
|
+
$_$;
|
466
|
+
|
467
|
+
|
468
|
+
--
|
469
|
+
-- Name: get_customer_name(integer); Type: FUNCTION; Schema: booktown; Owner: -
|
470
|
+
--
|
471
|
+
|
472
|
+
CREATE FUNCTION booktown.get_customer_name(integer) RETURNS text
|
473
|
+
LANGUAGE plpgsql
|
474
|
+
AS $_$
|
475
|
+
DECLARE
|
476
|
+
|
477
|
+
-- Declare aliases for user input.
|
478
|
+
customer_id ALIAS FOR $1;
|
479
|
+
|
480
|
+
-- Declare variables to hold the customer name.
|
481
|
+
customer_fname TEXT;
|
482
|
+
customer_lname TEXT;
|
483
|
+
|
484
|
+
BEGIN
|
485
|
+
|
486
|
+
-- Retrieve the customer first and last name for the customer whose
|
487
|
+
-- ID matches the value supplied as a function argument.
|
488
|
+
SELECT INTO customer_fname, customer_lname
|
489
|
+
first_name, last_name FROM customers
|
490
|
+
WHERE id = customer_id;
|
491
|
+
|
492
|
+
-- Return the name.
|
493
|
+
RETURN customer_fname || ' ' || customer_lname;
|
494
|
+
END;
|
495
|
+
$_$;
|
496
|
+
|
497
|
+
|
498
|
+
--
|
499
|
+
-- Name: givename(); Type: FUNCTION; Schema: booktown; Owner: -
|
500
|
+
--
|
501
|
+
|
502
|
+
CREATE FUNCTION booktown.givename() RETURNS opaque
|
503
|
+
LANGUAGE plpgsql
|
504
|
+
AS $$
|
505
|
+
DECLARE
|
506
|
+
tablename text;
|
507
|
+
BEGIN
|
508
|
+
|
509
|
+
tablename = TG_RELNAME;
|
510
|
+
INSERT INTO INVENTORY values (123, tablename);
|
511
|
+
return old;
|
512
|
+
END;
|
513
|
+
$$;
|
514
|
+
|
515
|
+
|
516
|
+
--
|
517
|
+
-- Name: html_linebreaks(text); Type: FUNCTION; Schema: booktown; Owner: -
|
518
|
+
--
|
519
|
+
|
520
|
+
CREATE FUNCTION booktown.html_linebreaks(text) RETURNS text
|
521
|
+
LANGUAGE plpgsql
|
522
|
+
AS $_$
|
523
|
+
DECLARE
|
524
|
+
formatted_string text := '';
|
525
|
+
BEGIN
|
526
|
+
FOR i IN 0 .. length($1) LOOP
|
527
|
+
IF substr($1, i, 1) = '
|
528
|
+
' THEN
|
529
|
+
formatted_string := formatted_string || '<br>';
|
530
|
+
ELSE
|
531
|
+
formatted_string := formatted_string || substr($1, i, 1);
|
532
|
+
END IF;
|
533
|
+
END LOOP;
|
534
|
+
RETURN formatted_string;
|
535
|
+
END;
|
536
|
+
$_$;
|
537
|
+
|
538
|
+
|
539
|
+
--
|
540
|
+
-- Name: in_stock(integer, integer); Type: FUNCTION; Schema: booktown; Owner: -
|
541
|
+
--
|
542
|
+
|
543
|
+
CREATE FUNCTION booktown.in_stock(integer, integer) RETURNS boolean
|
544
|
+
LANGUAGE plpgsql
|
545
|
+
AS $_$
|
546
|
+
DECLARE
|
547
|
+
b_id ALIAS FOR $1;
|
548
|
+
b_edition ALIAS FOR $2;
|
549
|
+
b_isbn TEXT;
|
550
|
+
stock_amount INTEGER;
|
551
|
+
BEGIN
|
552
|
+
-- This SELECT INTO statement retrieves the ISBN
|
553
|
+
-- number of the row in the editions table that had
|
554
|
+
-- both the book ID number and edition number that
|
555
|
+
-- were provided as function arguments.
|
556
|
+
SELECT INTO b_isbn isbn FROM editions WHERE
|
557
|
+
book_id = b_id AND edition = b_edition;
|
558
|
+
|
559
|
+
-- Check to see if the ISBN number retrieved
|
560
|
+
-- is NULL. This will happen if there is not an
|
561
|
+
-- existing book with both the ID number and edition
|
562
|
+
-- number specified in the function arguments.
|
563
|
+
-- If the ISBN is null, the function returns a
|
564
|
+
-- FALSE value and ends.
|
565
|
+
IF b_isbn IS NULL THEN
|
566
|
+
RETURN FALSE;
|
567
|
+
END IF;
|
568
|
+
|
569
|
+
-- Retrieve the amount of books available from the
|
570
|
+
-- stock table and record the number in the
|
571
|
+
-- stock_amount variable.
|
572
|
+
SELECT INTO stock_amount stock FROM stock WHERE isbn = b_isbn;
|
573
|
+
|
574
|
+
-- Use an IF/THEN/ELSE check to see if the amount
|
575
|
+
-- of books available is less than, or equal to 0.
|
576
|
+
-- If so, return FALSE. If not, return TRUE.
|
577
|
+
IF stock_amount <= 0 THEN
|
578
|
+
RETURN FALSE;
|
579
|
+
ELSE
|
580
|
+
RETURN TRUE;
|
581
|
+
END IF;
|
582
|
+
END;
|
583
|
+
$_$;
|
584
|
+
|
585
|
+
|
586
|
+
--
|
587
|
+
-- Name: isbn_to_title(text); Type: FUNCTION; Schema: booktown; Owner: -
|
588
|
+
--
|
589
|
+
|
590
|
+
CREATE FUNCTION booktown.isbn_to_title(text) RETURNS text
|
591
|
+
LANGUAGE sql
|
592
|
+
AS $_$SELECT title FROM books
|
593
|
+
JOIN editions AS e (isbn, id)
|
594
|
+
USING (id)
|
595
|
+
WHERE isbn = $1$_$;
|
596
|
+
|
597
|
+
|
598
|
+
--
|
599
|
+
-- Name: mixed(); Type: FUNCTION; Schema: booktown; Owner: -
|
600
|
+
--
|
601
|
+
|
602
|
+
CREATE FUNCTION booktown.mixed() RETURNS integer
|
603
|
+
LANGUAGE plpgsql
|
604
|
+
AS $$
|
605
|
+
DecLarE
|
606
|
+
--assigns 1 to the oNe variable
|
607
|
+
oNe IntEgER
|
608
|
+
:= 1;
|
609
|
+
|
610
|
+
bEGiN
|
611
|
+
|
612
|
+
--displays the value of oNe
|
613
|
+
ReTUrn oNe;
|
614
|
+
eNd;
|
615
|
+
$$;
|
616
|
+
|
617
|
+
|
618
|
+
--
|
619
|
+
-- Name: raise_test(); Type: FUNCTION; Schema: booktown; Owner: -
|
620
|
+
--
|
621
|
+
|
622
|
+
CREATE FUNCTION booktown.raise_test() RETURNS integer
|
623
|
+
LANGUAGE plpgsql
|
624
|
+
AS $$
|
625
|
+
DECLARE
|
626
|
+
|
627
|
+
-- Declare an integer variable for testing.
|
628
|
+
|
629
|
+
an_integer INTEGER = 1;
|
630
|
+
|
631
|
+
BEGIN
|
632
|
+
|
633
|
+
-- Raise a debug level message.
|
634
|
+
|
635
|
+
RAISE DEBUG 'The raise_test() function began.';
|
636
|
+
|
637
|
+
an_integer = an_integer + 1;
|
638
|
+
|
639
|
+
-- Raise a notice stating that the an_integer
|
640
|
+
-- variable was changed, then raise another notice
|
641
|
+
-- stating its new value.
|
642
|
+
|
643
|
+
RAISE NOTICE 'Variable an_integer was changed.';
|
644
|
+
RAISE NOTICE 'Variable an_integer value is now %.',an_integer;
|
645
|
+
|
646
|
+
-- Raise an exception.
|
647
|
+
|
648
|
+
RAISE EXCEPTION 'Variable % changed. Aborting transaction.',an_integer;
|
649
|
+
|
650
|
+
END;
|
651
|
+
$$;
|
652
|
+
|
653
|
+
|
654
|
+
--
|
655
|
+
-- Name: ship_item(text, text, text); Type: FUNCTION; Schema: booktown; Owner: -
|
656
|
+
--
|
657
|
+
|
658
|
+
CREATE FUNCTION booktown.ship_item(text, text, text) RETURNS integer
|
659
|
+
LANGUAGE plpgsql
|
660
|
+
AS $_$
|
661
|
+
DECLARE
|
662
|
+
l_name ALIAS FOR $1;
|
663
|
+
f_name ALIAS FOR $2;
|
664
|
+
book_isbn ALIAS FOR $3;
|
665
|
+
book_id INTEGER;
|
666
|
+
customer_id INTEGER;
|
667
|
+
|
668
|
+
BEGIN
|
669
|
+
|
670
|
+
SELECT INTO customer_id get_customer_id(l_name,f_name);
|
671
|
+
|
672
|
+
IF customer_id = -1 THEN
|
673
|
+
RETURN -1;
|
674
|
+
END IF;
|
675
|
+
|
676
|
+
SELECT INTO book_id book_id FROM editions WHERE isbn = book_isbn;
|
677
|
+
|
678
|
+
IF NOT FOUND THEN
|
679
|
+
RETURN -1;
|
680
|
+
END IF;
|
681
|
+
|
682
|
+
PERFORM add_shipment(customer_id,book_isbn);
|
683
|
+
|
684
|
+
RETURN 1;
|
685
|
+
END;
|
686
|
+
$_$;
|
687
|
+
|
688
|
+
|
689
|
+
--
|
690
|
+
-- Name: stock_amount(integer, integer); Type: FUNCTION; Schema: booktown; Owner: -
|
691
|
+
--
|
692
|
+
|
693
|
+
CREATE FUNCTION booktown.stock_amount(integer, integer) RETURNS integer
|
694
|
+
LANGUAGE plpgsql
|
695
|
+
AS $_$
|
696
|
+
DECLARE
|
697
|
+
-- Declare aliases for function arguments.
|
698
|
+
b_id ALIAS FOR $1;
|
699
|
+
b_edition ALIAS FOR $2;
|
700
|
+
-- Declare variable to store the ISBN number.
|
701
|
+
b_isbn TEXT;
|
702
|
+
-- Declare variable to store the stock amount.
|
703
|
+
stock_amount INTEGER;
|
704
|
+
BEGIN
|
705
|
+
-- This SELECT INTO statement retrieves the ISBN
|
706
|
+
-- number of the row in the editions table that had
|
707
|
+
-- both the book ID number and edition number that
|
708
|
+
-- were provided as function arguments.
|
709
|
+
SELECT INTO b_isbn isbn FROM editions WHERE
|
710
|
+
book_id = b_id AND edition = b_edition;
|
711
|
+
|
712
|
+
-- Check to see if the ISBN number retrieved
|
713
|
+
-- is NULL. This will happen if there is not an
|
714
|
+
-- existing book with both the ID number and edition
|
715
|
+
-- number specified in the function arguments.
|
716
|
+
-- If the ISBN is null, the function returns a
|
717
|
+
-- value of -1 and ends.
|
718
|
+
IF b_isbn IS NULL THEN
|
719
|
+
RETURN -1;
|
720
|
+
END IF;
|
721
|
+
|
722
|
+
-- Retrieve the amount of books available from the
|
723
|
+
-- stock table and record the number in the
|
724
|
+
-- stock_amount variable.
|
725
|
+
SELECT INTO stock_amount stock FROM stock WHERE isbn = b_isbn;
|
726
|
+
|
727
|
+
-- Return the amount of books available.
|
728
|
+
RETURN stock_amount;
|
729
|
+
END;
|
730
|
+
$_$;
|
731
|
+
|
732
|
+
|
733
|
+
--
|
734
|
+
-- Name: sync_authors_and_books(); Type: FUNCTION; Schema: booktown; Owner: -
|
735
|
+
--
|
736
|
+
|
737
|
+
CREATE FUNCTION booktown.sync_authors_and_books() RETURNS trigger
|
738
|
+
LANGUAGE plpgsql
|
739
|
+
AS $$
|
740
|
+
BEGIN
|
741
|
+
IF TG_OP = 'UPDATE' THEN
|
742
|
+
UPDATE books SET author_id = new.id WHERE author_id = old.id;
|
743
|
+
END IF;
|
744
|
+
RETURN new;
|
745
|
+
END;
|
746
|
+
$$;
|
747
|
+
|
748
|
+
|
749
|
+
--
|
750
|
+
-- Name: test(integer); Type: FUNCTION; Schema: booktown; Owner: -
|
751
|
+
--
|
752
|
+
|
753
|
+
CREATE FUNCTION booktown.test(integer) RETURNS integer
|
754
|
+
LANGUAGE plpgsql
|
755
|
+
AS $_$
|
756
|
+
|
757
|
+
DECLARE
|
758
|
+
-- defines the variable as ALIAS
|
759
|
+
variable ALIAS FOR $1;
|
760
|
+
BEGIN
|
761
|
+
-- displays the variable after multiplying it by two
|
762
|
+
return variable * 2.0;
|
763
|
+
END;
|
764
|
+
$_$;
|
765
|
+
|
766
|
+
|
767
|
+
--
|
768
|
+
-- Name: test_check_a_id(); Type: FUNCTION; Schema: booktown; Owner: -
|
769
|
+
--
|
770
|
+
|
771
|
+
CREATE FUNCTION booktown.test_check_a_id() RETURNS opaque
|
772
|
+
LANGUAGE plpgsql
|
773
|
+
AS $$
|
774
|
+
BEGIN
|
775
|
+
-- checks to make sure the author id
|
776
|
+
-- inserted is not left blank or less than 100
|
777
|
+
|
778
|
+
IF NEW.a_id ISNULL THEN
|
779
|
+
RAISE EXCEPTION
|
780
|
+
'The author id cannot be left blank!';
|
781
|
+
ELSE
|
782
|
+
IF NEW.a_id < 100 THEN
|
783
|
+
RAISE EXCEPTION
|
784
|
+
'Please insert a valid author id.';
|
785
|
+
ELSE
|
786
|
+
RETURN NEW;
|
787
|
+
END IF;
|
788
|
+
END IF;
|
789
|
+
END;
|
790
|
+
$$;
|
791
|
+
|
792
|
+
|
793
|
+
--
|
794
|
+
-- Name: title(integer); Type: FUNCTION; Schema: booktown; Owner: -
|
795
|
+
--
|
796
|
+
|
797
|
+
CREATE FUNCTION booktown.title(integer) RETURNS text
|
798
|
+
LANGUAGE sql
|
799
|
+
AS $_$SELECT title from books where id = $1$_$;
|
800
|
+
|
801
|
+
|
802
|
+
--
|
803
|
+
-- Name: triple_price(double precision); Type: FUNCTION; Schema: booktown; Owner: -
|
804
|
+
--
|
805
|
+
|
806
|
+
CREATE FUNCTION booktown.triple_price(double precision) RETURNS double precision
|
807
|
+
LANGUAGE plpgsql
|
808
|
+
AS $_$
|
809
|
+
DECLARE
|
810
|
+
-- Declare input_price as an alias for the
|
811
|
+
-- argument variable normally referenced with
|
812
|
+
-- the $1 identifier.
|
813
|
+
input_price ALIAS FOR $1;
|
814
|
+
|
815
|
+
BEGIN
|
816
|
+
-- Return the input price multiplied by three.
|
817
|
+
RETURN input_price * 3;
|
818
|
+
END;
|
819
|
+
$_$;
|
820
|
+
|
821
|
+
|
822
|
+
--
|
823
|
+
-- Name: sum(text); Type: AGGREGATE; Schema: booktown; Owner: -
|
824
|
+
--
|
825
|
+
|
826
|
+
CREATE AGGREGATE booktown.sum(text) (
|
827
|
+
SFUNC = textcat,
|
828
|
+
STYPE = text,
|
829
|
+
INITCOND = ''
|
830
|
+
);
|
831
|
+
|
832
|
+
|
833
|
+
SET default_tablespace = '';
|
834
|
+
|
835
|
+
SET default_with_oids = false;
|
836
|
+
|
837
|
+
--
|
838
|
+
-- Name: alternate_stock; Type: TABLE; Schema: booktown; Owner: -
|
839
|
+
--
|
840
|
+
|
841
|
+
CREATE TABLE booktown.alternate_stock (
|
842
|
+
isbn text,
|
843
|
+
cost numeric(5,2),
|
844
|
+
retail numeric(5,2),
|
845
|
+
stock integer
|
846
|
+
);
|
847
|
+
|
848
|
+
|
849
|
+
--
|
850
|
+
-- Name: author_ids; Type: SEQUENCE; Schema: booktown; Owner: -
|
851
|
+
--
|
852
|
+
|
853
|
+
CREATE SEQUENCE booktown.author_ids
|
854
|
+
START WITH 0
|
855
|
+
INCREMENT BY 1
|
856
|
+
MINVALUE 0
|
857
|
+
MAXVALUE 2147483647
|
858
|
+
CACHE 1;
|
859
|
+
|
860
|
+
|
861
|
+
--
|
862
|
+
-- Name: authors; Type: TABLE; Schema: booktown; Owner: -
|
863
|
+
--
|
864
|
+
|
865
|
+
CREATE TABLE booktown.authors (
|
866
|
+
id integer NOT NULL,
|
867
|
+
last_name text,
|
868
|
+
first_name text
|
869
|
+
);
|
870
|
+
|
871
|
+
|
872
|
+
--
|
873
|
+
-- Name: book_backup; Type: TABLE; Schema: booktown; Owner: -
|
874
|
+
--
|
875
|
+
|
876
|
+
CREATE TABLE booktown.book_backup (
|
877
|
+
id integer,
|
878
|
+
title text,
|
879
|
+
author_id integer,
|
880
|
+
subject_id integer
|
881
|
+
);
|
882
|
+
|
883
|
+
|
884
|
+
--
|
885
|
+
-- Name: book_ids; Type: SEQUENCE; Schema: booktown; Owner: -
|
886
|
+
--
|
887
|
+
|
888
|
+
CREATE SEQUENCE booktown.book_ids
|
889
|
+
START WITH 0
|
890
|
+
INCREMENT BY 1
|
891
|
+
MINVALUE 0
|
892
|
+
MAXVALUE 2147483647
|
893
|
+
CACHE 1;
|
894
|
+
|
895
|
+
|
896
|
+
--
|
897
|
+
-- Name: book_queue; Type: TABLE; Schema: booktown; Owner: -
|
898
|
+
--
|
899
|
+
|
900
|
+
CREATE TABLE booktown.book_queue (
|
901
|
+
title text NOT NULL,
|
902
|
+
author_id integer,
|
903
|
+
subject_id integer,
|
904
|
+
approved boolean
|
905
|
+
);
|
906
|
+
|
907
|
+
|
908
|
+
--
|
909
|
+
-- Name: books; Type: TABLE; Schema: booktown; Owner: -
|
910
|
+
--
|
911
|
+
|
912
|
+
CREATE TABLE booktown.books (
|
913
|
+
id integer NOT NULL,
|
914
|
+
title text NOT NULL,
|
915
|
+
author_id integer,
|
916
|
+
subject_id integer
|
917
|
+
);
|
918
|
+
|
919
|
+
|
920
|
+
--
|
921
|
+
-- Name: customers; Type: TABLE; Schema: booktown; Owner: -
|
922
|
+
--
|
923
|
+
|
924
|
+
CREATE TABLE booktown.customers (
|
925
|
+
id integer NOT NULL,
|
926
|
+
last_name text,
|
927
|
+
first_name text
|
928
|
+
);
|
929
|
+
|
930
|
+
|
931
|
+
--
|
932
|
+
-- Name: daily_inventory; Type: TABLE; Schema: booktown; Owner: -
|
933
|
+
--
|
934
|
+
|
935
|
+
CREATE TABLE booktown.daily_inventory (
|
936
|
+
isbn text,
|
937
|
+
is_stocked boolean
|
938
|
+
);
|
939
|
+
|
940
|
+
|
941
|
+
--
|
942
|
+
-- Name: distinguished_authors; Type: TABLE; Schema: booktown; Owner: -
|
943
|
+
--
|
944
|
+
|
945
|
+
CREATE TABLE booktown.distinguished_authors (
|
946
|
+
award text
|
947
|
+
)
|
948
|
+
INHERITS (booktown.authors);
|
949
|
+
|
950
|
+
|
951
|
+
--
|
952
|
+
-- Name: editions; Type: TABLE; Schema: booktown; Owner: -
|
953
|
+
--
|
954
|
+
|
955
|
+
CREATE TABLE booktown.editions (
|
956
|
+
isbn text NOT NULL,
|
957
|
+
book_id integer,
|
958
|
+
edition integer,
|
959
|
+
publisher_id integer,
|
960
|
+
publication date,
|
961
|
+
type character(1),
|
962
|
+
CONSTRAINT integrity CHECK (((book_id IS NOT NULL) AND (edition IS NOT NULL)))
|
963
|
+
);
|
964
|
+
|
965
|
+
|
966
|
+
--
|
967
|
+
-- Name: employees; Type: TABLE; Schema: booktown; Owner: -
|
968
|
+
--
|
969
|
+
|
970
|
+
CREATE TABLE booktown.employees (
|
971
|
+
id integer NOT NULL,
|
972
|
+
last_name text NOT NULL,
|
973
|
+
first_name text,
|
974
|
+
CONSTRAINT employees_id CHECK ((id > 100))
|
975
|
+
);
|
976
|
+
|
977
|
+
|
978
|
+
--
|
979
|
+
-- Name: favorite_authors; Type: TABLE; Schema: booktown; Owner: -
|
980
|
+
--
|
981
|
+
|
982
|
+
CREATE TABLE booktown.favorite_authors (
|
983
|
+
employee_id integer,
|
984
|
+
authors_and_titles text[]
|
985
|
+
);
|
986
|
+
|
987
|
+
|
988
|
+
--
|
989
|
+
-- Name: favorite_books; Type: TABLE; Schema: booktown; Owner: -
|
990
|
+
--
|
991
|
+
|
992
|
+
CREATE TABLE booktown.favorite_books (
|
993
|
+
employee_id integer,
|
994
|
+
books text[]
|
995
|
+
);
|
996
|
+
|
997
|
+
|
998
|
+
--
|
999
|
+
-- Name: money_example; Type: TABLE; Schema: booktown; Owner: -
|
1000
|
+
--
|
1001
|
+
|
1002
|
+
CREATE TABLE booktown.money_example (
|
1003
|
+
money_cash money,
|
1004
|
+
numeric_cash numeric(6,2)
|
1005
|
+
);
|
1006
|
+
|
1007
|
+
|
1008
|
+
--
|
1009
|
+
-- Name: my_list; Type: TABLE; Schema: booktown; Owner: -
|
1010
|
+
--
|
1011
|
+
|
1012
|
+
CREATE TABLE booktown.my_list (
|
1013
|
+
todos text
|
1014
|
+
);
|
1015
|
+
|
1016
|
+
|
1017
|
+
--
|
1018
|
+
-- Name: numeric_values; Type: TABLE; Schema: booktown; Owner: -
|
1019
|
+
--
|
1020
|
+
|
1021
|
+
CREATE TABLE booktown.numeric_values (
|
1022
|
+
num numeric(30,6)
|
1023
|
+
);
|
1024
|
+
|
1025
|
+
|
1026
|
+
--
|
1027
|
+
-- Name: publishers; Type: TABLE; Schema: booktown; Owner: -
|
1028
|
+
--
|
1029
|
+
|
1030
|
+
CREATE TABLE booktown.publishers (
|
1031
|
+
id integer NOT NULL,
|
1032
|
+
name text,
|
1033
|
+
address text
|
1034
|
+
);
|
1035
|
+
|
1036
|
+
|
1037
|
+
--
|
1038
|
+
-- Name: shipments; Type: TABLE; Schema: booktown; Owner: -
|
1039
|
+
--
|
1040
|
+
|
1041
|
+
CREATE TABLE booktown.shipments (
|
1042
|
+
id integer DEFAULT nextval(('"shipments_ship_id_seq"'::text)::regclass) NOT NULL,
|
1043
|
+
customer_id integer,
|
1044
|
+
isbn text,
|
1045
|
+
ship_date timestamp with time zone
|
1046
|
+
);
|
1047
|
+
|
1048
|
+
|
1049
|
+
--
|
1050
|
+
-- Name: recent_shipments; Type: VIEW; Schema: booktown; Owner: -
|
1051
|
+
--
|
1052
|
+
|
1053
|
+
CREATE VIEW booktown.recent_shipments AS
|
1054
|
+
SELECT count(*) AS num_shipped,
|
1055
|
+
max(shipments.ship_date) AS max,
|
1056
|
+
b.title
|
1057
|
+
FROM ((booktown.shipments
|
1058
|
+
JOIN booktown.editions USING (isbn))
|
1059
|
+
JOIN booktown.books b(book_id, title, author_id, subject_id) USING (book_id))
|
1060
|
+
GROUP BY b.title
|
1061
|
+
ORDER BY (count(*)) DESC;
|
1062
|
+
|
1063
|
+
|
1064
|
+
--
|
1065
|
+
-- Name: schedules; Type: TABLE; Schema: booktown; Owner: -
|
1066
|
+
--
|
1067
|
+
|
1068
|
+
CREATE TABLE booktown.schedules (
|
1069
|
+
employee_id integer NOT NULL,
|
1070
|
+
schedule text
|
1071
|
+
);
|
1072
|
+
|
1073
|
+
|
1074
|
+
--
|
1075
|
+
-- Name: shipments_ship_id_seq; Type: SEQUENCE; Schema: booktown; Owner: -
|
1076
|
+
--
|
1077
|
+
|
1078
|
+
CREATE SEQUENCE booktown.shipments_ship_id_seq
|
1079
|
+
START WITH 0
|
1080
|
+
INCREMENT BY 1
|
1081
|
+
MINVALUE 0
|
1082
|
+
MAXVALUE 2147483647
|
1083
|
+
CACHE 1;
|
1084
|
+
|
1085
|
+
|
1086
|
+
--
|
1087
|
+
-- Name: states; Type: TABLE; Schema: booktown; Owner: -
|
1088
|
+
--
|
1089
|
+
|
1090
|
+
CREATE TABLE booktown.states (
|
1091
|
+
id integer NOT NULL,
|
1092
|
+
name text,
|
1093
|
+
abbreviation character(2)
|
1094
|
+
);
|
1095
|
+
|
1096
|
+
|
1097
|
+
--
|
1098
|
+
-- Name: stock; Type: TABLE; Schema: booktown; Owner: -
|
1099
|
+
--
|
1100
|
+
|
1101
|
+
CREATE TABLE booktown.stock (
|
1102
|
+
isbn text NOT NULL,
|
1103
|
+
cost numeric(5,2),
|
1104
|
+
retail numeric(5,2),
|
1105
|
+
stock integer
|
1106
|
+
);
|
1107
|
+
|
1108
|
+
|
1109
|
+
--
|
1110
|
+
-- Name: stock_backup; Type: TABLE; Schema: booktown; Owner: -
|
1111
|
+
--
|
1112
|
+
|
1113
|
+
CREATE TABLE booktown.stock_backup (
|
1114
|
+
isbn text,
|
1115
|
+
cost numeric(5,2),
|
1116
|
+
retail numeric(5,2),
|
1117
|
+
stock integer
|
1118
|
+
);
|
1119
|
+
|
1120
|
+
|
1121
|
+
--
|
1122
|
+
-- Name: stock_view; Type: VIEW; Schema: booktown; Owner: -
|
1123
|
+
--
|
1124
|
+
|
1125
|
+
CREATE VIEW booktown.stock_view AS
|
1126
|
+
SELECT stock.isbn,
|
1127
|
+
stock.retail,
|
1128
|
+
stock.stock
|
1129
|
+
FROM booktown.stock;
|
1130
|
+
|
1131
|
+
|
1132
|
+
--
|
1133
|
+
-- Name: subject_ids; Type: SEQUENCE; Schema: booktown; Owner: -
|
1134
|
+
--
|
1135
|
+
|
1136
|
+
CREATE SEQUENCE booktown.subject_ids
|
1137
|
+
START WITH 0
|
1138
|
+
INCREMENT BY 1
|
1139
|
+
MINVALUE 0
|
1140
|
+
MAXVALUE 2147483647
|
1141
|
+
CACHE 1;
|
1142
|
+
|
1143
|
+
|
1144
|
+
--
|
1145
|
+
-- Name: subjects; Type: TABLE; Schema: booktown; Owner: -
|
1146
|
+
--
|
1147
|
+
|
1148
|
+
CREATE TABLE booktown.subjects (
|
1149
|
+
id integer NOT NULL,
|
1150
|
+
subject text,
|
1151
|
+
location text
|
1152
|
+
);
|
1153
|
+
|
1154
|
+
|
1155
|
+
--
|
1156
|
+
-- Name: text_sorting; Type: TABLE; Schema: booktown; Owner: -
|
1157
|
+
--
|
1158
|
+
|
1159
|
+
CREATE TABLE booktown.text_sorting (
|
1160
|
+
letter character(1)
|
1161
|
+
);
|
1162
|
+
|
1163
|
+
|
1164
|
+
--
|
1165
|
+
-- Data for Name: alternate_stock; Type: TABLE DATA; Schema: booktown; Owner: -
|
1166
|
+
--
|
1167
|
+
|
1168
|
+
COPY booktown.alternate_stock (isbn, cost, retail, stock) FROM stdin;
|
1169
|
+
0385121679 29.00 36.95 65
|
1170
|
+
039480001X 30.00 32.95 31
|
1171
|
+
0394900014 23.00 23.95 0
|
1172
|
+
044100590X 36.00 45.95 89
|
1173
|
+
0441172717 17.00 21.95 77
|
1174
|
+
0451160916 24.00 28.95 22
|
1175
|
+
0451198492 36.00 46.95 0
|
1176
|
+
0451457994 17.00 22.95 0
|
1177
|
+
0590445065 23.00 23.95 10
|
1178
|
+
0679803335 20.00 24.95 18
|
1179
|
+
0694003611 25.00 28.95 50
|
1180
|
+
0760720002 18.00 23.95 28
|
1181
|
+
0823015505 26.00 28.95 16
|
1182
|
+
0929605942 19.00 21.95 25
|
1183
|
+
1885418035 23.00 24.95 77
|
1184
|
+
0394800753 16.00 16.95 4
|
1185
|
+
\.
|
1186
|
+
|
1187
|
+
|
1188
|
+
--
|
1189
|
+
-- Data for Name: authors; Type: TABLE DATA; Schema: booktown; Owner: -
|
1190
|
+
--
|
1191
|
+
|
1192
|
+
COPY booktown.authors (id, last_name, first_name) FROM stdin;
|
1193
|
+
1111 Denham Ariel
|
1194
|
+
1212 Worsley John
|
1195
|
+
15990 Bourgeois Paulette
|
1196
|
+
25041 Bianco Margery Williams
|
1197
|
+
16 Alcott Louisa May
|
1198
|
+
4156 King Stephen
|
1199
|
+
1866 Herbert Frank
|
1200
|
+
1644 Hogarth Burne
|
1201
|
+
2031 Brown Margaret Wise
|
1202
|
+
115 Poe Edgar Allen
|
1203
|
+
7805 Lutz Mark
|
1204
|
+
7806 Christiansen Tom
|
1205
|
+
1533 Brautigan Richard
|
1206
|
+
1717 Brite Poppy Z.
|
1207
|
+
2112 Gorey Edward
|
1208
|
+
2001 Clarke Arthur C.
|
1209
|
+
1213 Brookins Andrew
|
1210
|
+
\.
|
1211
|
+
|
1212
|
+
|
1213
|
+
--
|
1214
|
+
-- Data for Name: book_backup; Type: TABLE DATA; Schema: booktown; Owner: -
|
1215
|
+
--
|
1216
|
+
|
1217
|
+
COPY booktown.book_backup (id, title, author_id, subject_id) FROM stdin;
|
1218
|
+
7808 The Shining 4156 9
|
1219
|
+
4513 Dune 1866 15
|
1220
|
+
4267 2001: A Space Odyssey 2001 15
|
1221
|
+
1608 The Cat in the Hat 1809 2
|
1222
|
+
1590 Bartholomew and the Oobleck 1809 2
|
1223
|
+
25908 Franklin in the Dark 15990 2
|
1224
|
+
1501 Goodnight Moon 2031 2
|
1225
|
+
190 Little Women 16 6
|
1226
|
+
1234 The Velveteen Rabbit 25041 3
|
1227
|
+
2038 Dynamic Anatomy 1644 0
|
1228
|
+
156 The Tell-Tale Heart 115 9
|
1229
|
+
41472 Practical PostgreSQL 1212 4
|
1230
|
+
41473 Programming Python 7805 4
|
1231
|
+
41477 Learning Python 7805 4
|
1232
|
+
41478 Perl Cookbook 7806 4
|
1233
|
+
7808 The Shining 4156 9
|
1234
|
+
4513 Dune 1866 15
|
1235
|
+
4267 2001: A Space Odyssey 2001 15
|
1236
|
+
1608 The Cat in the Hat 1809 2
|
1237
|
+
1590 Bartholomew and the Oobleck 1809 2
|
1238
|
+
25908 Franklin in the Dark 15990 2
|
1239
|
+
1501 Goodnight Moon 2031 2
|
1240
|
+
190 Little Women 16 6
|
1241
|
+
1234 The Velveteen Rabbit 25041 3
|
1242
|
+
2038 Dynamic Anatomy 1644 0
|
1243
|
+
156 The Tell-Tale Heart 115 9
|
1244
|
+
41473 Programming Python 7805 4
|
1245
|
+
41477 Learning Python 7805 4
|
1246
|
+
41478 Perl Cookbook 7806 4
|
1247
|
+
41472 Practical PostgreSQL 1212 4
|
1248
|
+
\.
|
1249
|
+
|
1250
|
+
|
1251
|
+
--
|
1252
|
+
-- Data for Name: book_queue; Type: TABLE DATA; Schema: booktown; Owner: -
|
1253
|
+
--
|
1254
|
+
|
1255
|
+
COPY booktown.book_queue (title, author_id, subject_id, approved) FROM stdin;
|
1256
|
+
Learning Python 7805 4 t
|
1257
|
+
Perl Cookbook 7806 4 t
|
1258
|
+
\.
|
1259
|
+
|
1260
|
+
|
1261
|
+
--
|
1262
|
+
-- Data for Name: books; Type: TABLE DATA; Schema: booktown; Owner: -
|
1263
|
+
--
|
1264
|
+
|
1265
|
+
COPY booktown.books (id, title, author_id, subject_id) FROM stdin;
|
1266
|
+
7808 The Shining 4156 9
|
1267
|
+
4513 Dune 1866 15
|
1268
|
+
4267 2001: A Space Odyssey 2001 15
|
1269
|
+
1608 The Cat in the Hat 1809 2
|
1270
|
+
1590 Bartholomew and the Oobleck 1809 2
|
1271
|
+
25908 Franklin in the Dark 15990 2
|
1272
|
+
1501 Goodnight Moon 2031 2
|
1273
|
+
190 Little Women 16 6
|
1274
|
+
1234 The Velveteen Rabbit 25041 3
|
1275
|
+
2038 Dynamic Anatomy 1644 0
|
1276
|
+
156 The Tell-Tale Heart 115 9
|
1277
|
+
41473 Programming Python 7805 4
|
1278
|
+
41477 Learning Python 7805 4
|
1279
|
+
41478 Perl Cookbook 7806 4
|
1280
|
+
41472 Practical PostgreSQL 1212 4
|
1281
|
+
\.
|
1282
|
+
|
1283
|
+
|
1284
|
+
--
|
1285
|
+
-- Data for Name: customers; Type: TABLE DATA; Schema: booktown; Owner: -
|
1286
|
+
--
|
1287
|
+
|
1288
|
+
COPY booktown.customers (id, last_name, first_name) FROM stdin;
|
1289
|
+
107 Jackson Annie
|
1290
|
+
112 Gould Ed
|
1291
|
+
142 Allen Chad
|
1292
|
+
146 Williams James
|
1293
|
+
172 Brown Richard
|
1294
|
+
185 Morrill Eric
|
1295
|
+
221 King Jenny
|
1296
|
+
270 Bollman Julie
|
1297
|
+
388 Morrill Royce
|
1298
|
+
409 Holloway Christine
|
1299
|
+
430 Black Jean
|
1300
|
+
476 Clark James
|
1301
|
+
480 Thomas Rich
|
1302
|
+
488 Young Trevor
|
1303
|
+
574 Bennett Laura
|
1304
|
+
652 Anderson Jonathan
|
1305
|
+
655 Olson Dave
|
1306
|
+
671 Brown Chuck
|
1307
|
+
723 Eisele Don
|
1308
|
+
724 Holloway Adam
|
1309
|
+
738 Gould Shirley
|
1310
|
+
830 Robertson Royce
|
1311
|
+
853 Black Wendy
|
1312
|
+
860 Owens Tim
|
1313
|
+
880 Robinson Tammy
|
1314
|
+
898 Gerdes Kate
|
1315
|
+
964 Gould Ramon
|
1316
|
+
1045 Owens Jean
|
1317
|
+
1125 Bollman Owen
|
1318
|
+
1149 Becker Owen
|
1319
|
+
1123 Corner Kathy
|
1320
|
+
\.
|
1321
|
+
|
1322
|
+
|
1323
|
+
--
|
1324
|
+
-- Data for Name: daily_inventory; Type: TABLE DATA; Schema: booktown; Owner: -
|
1325
|
+
--
|
1326
|
+
|
1327
|
+
COPY booktown.daily_inventory (isbn, is_stocked) FROM stdin;
|
1328
|
+
039480001X t
|
1329
|
+
044100590X t
|
1330
|
+
0451198492 f
|
1331
|
+
0394900014 f
|
1332
|
+
0441172717 t
|
1333
|
+
0451160916 f
|
1334
|
+
0385121679 \N
|
1335
|
+
\.
|
1336
|
+
|
1337
|
+
|
1338
|
+
--
|
1339
|
+
-- Data for Name: distinguished_authors; Type: TABLE DATA; Schema: booktown; Owner: -
|
1340
|
+
--
|
1341
|
+
|
1342
|
+
COPY booktown.distinguished_authors (id, last_name, first_name, award) FROM stdin;
|
1343
|
+
25043 Simon Neil Pulitzer Prize
|
1344
|
+
1809 Geisel Theodor Seuss Pulitzer Prize
|
1345
|
+
\.
|
1346
|
+
|
1347
|
+
|
1348
|
+
--
|
1349
|
+
-- Data for Name: editions; Type: TABLE DATA; Schema: booktown; Owner: -
|
1350
|
+
--
|
1351
|
+
|
1352
|
+
COPY booktown.editions (isbn, book_id, edition, publisher_id, publication, type) FROM stdin;
|
1353
|
+
039480001X 1608 1 59 1957-03-01 h
|
1354
|
+
0451160916 7808 1 75 1981-08-01 p
|
1355
|
+
0394800753 1590 1 59 1949-03-01 p
|
1356
|
+
0590445065 25908 1 150 1987-03-01 p
|
1357
|
+
0694003611 1501 1 65 1947-03-04 p
|
1358
|
+
0679803335 1234 1 102 1922-01-01 p
|
1359
|
+
0760720002 190 1 91 1868-01-01 p
|
1360
|
+
0394900014 1608 1 59 1957-01-01 p
|
1361
|
+
0385121679 7808 2 75 1993-10-01 h
|
1362
|
+
1885418035 156 1 163 1995-03-28 p
|
1363
|
+
0929605942 156 2 171 1998-12-01 p
|
1364
|
+
0441172717 4513 2 99 1998-09-01 p
|
1365
|
+
044100590X 4513 3 99 1999-10-01 h
|
1366
|
+
0451457994 4267 3 101 2000-09-12 p
|
1367
|
+
0451198492 4267 3 101 1999-10-01 h
|
1368
|
+
0823015505 2038 1 62 1958-01-01 p
|
1369
|
+
0596000855 41473 2 113 2001-03-01 p
|
1370
|
+
\.
|
1371
|
+
|
1372
|
+
|
1373
|
+
--
|
1374
|
+
-- Data for Name: employees; Type: TABLE DATA; Schema: booktown; Owner: -
|
1375
|
+
--
|
1376
|
+
|
1377
|
+
COPY booktown.employees (id, last_name, first_name) FROM stdin;
|
1378
|
+
101 Appel Vincent
|
1379
|
+
102 Holloway Michael
|
1380
|
+
105 Connoly Sarah
|
1381
|
+
104 Noble Ben
|
1382
|
+
103 Joble David
|
1383
|
+
106 Hall Timothy
|
1384
|
+
1008 Williams \N
|
1385
|
+
\.
|
1386
|
+
|
1387
|
+
|
1388
|
+
--
|
1389
|
+
-- Data for Name: favorite_authors; Type: TABLE DATA; Schema: booktown; Owner: -
|
1390
|
+
--
|
1391
|
+
|
1392
|
+
COPY booktown.favorite_authors (employee_id, authors_and_titles) FROM stdin;
|
1393
|
+
102 {{"J.R.R. Tolkien","The Silmarillion"},{"Charles Dickens","Great Expectations"},{"Ariel Denham","Attic Lives"}}
|
1394
|
+
\.
|
1395
|
+
|
1396
|
+
|
1397
|
+
--
|
1398
|
+
-- Data for Name: favorite_books; Type: TABLE DATA; Schema: booktown; Owner: -
|
1399
|
+
--
|
1400
|
+
|
1401
|
+
COPY booktown.favorite_books (employee_id, books) FROM stdin;
|
1402
|
+
102 {"The Hitchhiker's Guide to the Galaxy","The Restauraunt at the End of the Universe"}
|
1403
|
+
103 {"There and Back Again: A Hobbit's Holiday","Kittens Squared"}
|
1404
|
+
\.
|
1405
|
+
|
1406
|
+
|
1407
|
+
--
|
1408
|
+
-- Data for Name: money_example; Type: TABLE DATA; Schema: booktown; Owner: -
|
1409
|
+
--
|
1410
|
+
|
1411
|
+
COPY booktown.money_example (money_cash, numeric_cash) FROM stdin;
|
1412
|
+
$12.24 12.24
|
1413
|
+
\.
|
1414
|
+
|
1415
|
+
|
1416
|
+
--
|
1417
|
+
-- Data for Name: my_list; Type: TABLE DATA; Schema: booktown; Owner: -
|
1418
|
+
--
|
1419
|
+
|
1420
|
+
COPY booktown.my_list (todos) FROM stdin;
|
1421
|
+
Pick up laundry.
|
1422
|
+
Send out bills.
|
1423
|
+
Wrap up Grand Unifying Theory for publication.
|
1424
|
+
\.
|
1425
|
+
|
1426
|
+
|
1427
|
+
--
|
1428
|
+
-- Data for Name: numeric_values; Type: TABLE DATA; Schema: booktown; Owner: -
|
1429
|
+
--
|
1430
|
+
|
1431
|
+
COPY booktown.numeric_values (num) FROM stdin;
|
1432
|
+
68719476736.000000
|
1433
|
+
68719476737.000000
|
1434
|
+
6871947673778.000000
|
1435
|
+
999999999999999999999999.999900
|
1436
|
+
999999999999999999999999.999999
|
1437
|
+
-999999999999999999999999.999999
|
1438
|
+
-100000000000000000000000.999999
|
1439
|
+
1.999999
|
1440
|
+
2.000000
|
1441
|
+
2.000000
|
1442
|
+
999999999999999999999999.999999
|
1443
|
+
999999999999999999999999.000000
|
1444
|
+
\.
|
1445
|
+
|
1446
|
+
|
1447
|
+
--
|
1448
|
+
-- Data for Name: publishers; Type: TABLE DATA; Schema: booktown; Owner: -
|
1449
|
+
--
|
1450
|
+
|
1451
|
+
COPY booktown.publishers (id, name, address) FROM stdin;
|
1452
|
+
150 Kids Can Press Kids Can Press, 29 Birch Ave. Toronto, ON M4V 1E2
|
1453
|
+
91 Henry Holt & Company, Inc. Henry Holt & Company, Inc. 115 West 18th Street New York, NY 10011
|
1454
|
+
113 O'Reilly & Associates O'Reilly & Associates, Inc. 101 Morris St, Sebastopol, CA 95472
|
1455
|
+
62 Watson-Guptill Publications 1515 Boradway, New York, NY 10036
|
1456
|
+
105 Noonday Press Farrar Straus & Giroux Inc, 19 Union Square W, New York, NY 10003
|
1457
|
+
99 Ace Books The Berkley Publishing Group, Penguin Putnam Inc, 375 Hudson St, New York, NY 10014
|
1458
|
+
101 Roc Penguin Putnam Inc, 375 Hudson St, New York, NY 10014
|
1459
|
+
163 Mojo Press Mojo Press, PO Box 1215, Dripping Springs, TX 78720
|
1460
|
+
171 Books of Wonder Books of Wonder, 16 W. 18th St. New York, NY, 10011
|
1461
|
+
102 Penguin Penguin Putnam Inc, 375 Hudson St, New York, NY 10014
|
1462
|
+
75 Doubleday Random House, Inc, 1540 Broadway, New York, NY 10036
|
1463
|
+
65 HarperCollins HarperCollins Publishers, 10 E 53rd St, New York, NY 10022
|
1464
|
+
59 Random House Random House, Inc, 1540 Broadway, New York, NY 10036
|
1465
|
+
\.
|
1466
|
+
|
1467
|
+
|
1468
|
+
--
|
1469
|
+
-- Data for Name: schedules; Type: TABLE DATA; Schema: booktown; Owner: -
|
1470
|
+
--
|
1471
|
+
|
1472
|
+
COPY booktown.schedules (employee_id, schedule) FROM stdin;
|
1473
|
+
102 Mon - Fri, 9am - 5pm
|
1474
|
+
\.
|
1475
|
+
|
1476
|
+
|
1477
|
+
--
|
1478
|
+
-- Data for Name: shipments; Type: TABLE DATA; Schema: booktown; Owner: -
|
1479
|
+
--
|
1480
|
+
|
1481
|
+
COPY booktown.shipments (id, customer_id, isbn, ship_date) FROM stdin;
|
1482
|
+
375 142 039480001X 2001-08-06 18:29:21+02
|
1483
|
+
323 671 0451160916 2001-08-14 19:36:41+02
|
1484
|
+
998 1045 0590445065 2001-08-12 21:09:47+02
|
1485
|
+
749 172 0694003611 2001-08-11 19:52:34+02
|
1486
|
+
662 655 0679803335 2001-08-09 16:30:07+02
|
1487
|
+
806 1125 0760720002 2001-08-05 18:34:04+02
|
1488
|
+
102 146 0394900014 2001-08-11 22:34:08+02
|
1489
|
+
813 112 0385121679 2001-08-08 18:53:46+02
|
1490
|
+
652 724 1885418035 2001-08-14 22:41:39+02
|
1491
|
+
599 430 0929605942 2001-08-10 17:29:42+02
|
1492
|
+
969 488 0441172717 2001-08-14 17:42:58+02
|
1493
|
+
433 898 044100590X 2001-08-12 17:46:35+02
|
1494
|
+
660 409 0451457994 2001-08-07 20:56:42+02
|
1495
|
+
310 738 0451198492 2001-08-15 23:02:01+02
|
1496
|
+
510 860 0823015505 2001-08-14 16:33:47+02
|
1497
|
+
997 185 039480001X 2001-08-10 22:47:52+02
|
1498
|
+
999 221 0451160916 2001-08-14 22:45:51+02
|
1499
|
+
56 880 0590445065 2001-08-14 22:49:00+02
|
1500
|
+
72 574 0694003611 2001-08-06 16:49:44+02
|
1501
|
+
146 270 039480001X 2001-08-13 18:42:10+02
|
1502
|
+
981 652 0451160916 2001-08-08 17:36:44+02
|
1503
|
+
95 480 0590445065 2001-08-10 16:29:52+02
|
1504
|
+
593 476 0694003611 2001-08-15 20:57:40+02
|
1505
|
+
977 853 0679803335 2001-08-09 18:30:46+02
|
1506
|
+
117 185 0760720002 2001-08-07 22:00:48+02
|
1507
|
+
406 1123 0394900014 2001-08-13 18:47:04+02
|
1508
|
+
340 1149 0385121679 2001-08-12 22:39:22+02
|
1509
|
+
871 388 1885418035 2001-08-07 20:31:57+02
|
1510
|
+
1000 221 039480001X 2001-09-15 01:46:32+02
|
1511
|
+
1001 107 039480001X 2001-09-15 02:42:22+02
|
1512
|
+
754 107 0394800753 2001-08-11 18:55:05+02
|
1513
|
+
458 107 0394800753 2001-08-07 19:58:36+02
|
1514
|
+
189 107 0394800753 2001-08-06 20:46:36+02
|
1515
|
+
720 107 0394800753 2001-08-08 19:46:13+02
|
1516
|
+
1002 107 0394800753 2001-09-22 20:23:28+02
|
1517
|
+
2 107 0394800753 2001-09-23 05:58:56+02
|
1518
|
+
\.
|
1519
|
+
|
1520
|
+
|
1521
|
+
--
|
1522
|
+
-- Data for Name: states; Type: TABLE DATA; Schema: booktown; Owner: -
|
1523
|
+
--
|
1524
|
+
|
1525
|
+
COPY booktown.states (id, name, abbreviation) FROM stdin;
|
1526
|
+
42 Washington WA
|
1527
|
+
51 Oregon OR
|
1528
|
+
\.
|
1529
|
+
|
1530
|
+
|
1531
|
+
--
|
1532
|
+
-- Data for Name: stock; Type: TABLE DATA; Schema: booktown; Owner: -
|
1533
|
+
--
|
1534
|
+
|
1535
|
+
COPY booktown.stock (isbn, cost, retail, stock) FROM stdin;
|
1536
|
+
0385121679 29.00 36.95 65
|
1537
|
+
039480001X 30.00 32.95 31
|
1538
|
+
0394900014 23.00 23.95 0
|
1539
|
+
044100590X 36.00 45.95 89
|
1540
|
+
0441172717 17.00 21.95 77
|
1541
|
+
0451160916 24.00 28.95 22
|
1542
|
+
0451198492 36.00 46.95 0
|
1543
|
+
0451457994 17.00 22.95 0
|
1544
|
+
0590445065 23.00 23.95 10
|
1545
|
+
0679803335 20.00 24.95 18
|
1546
|
+
0694003611 25.00 28.95 50
|
1547
|
+
0760720002 18.00 23.95 28
|
1548
|
+
0823015505 26.00 28.95 16
|
1549
|
+
0929605942 19.00 21.95 25
|
1550
|
+
1885418035 23.00 24.95 77
|
1551
|
+
0394800753 16.00 16.95 4
|
1552
|
+
\.
|
1553
|
+
|
1554
|
+
|
1555
|
+
--
|
1556
|
+
-- Data for Name: stock_backup; Type: TABLE DATA; Schema: booktown; Owner: -
|
1557
|
+
--
|
1558
|
+
|
1559
|
+
COPY booktown.stock_backup (isbn, cost, retail, stock) FROM stdin;
|
1560
|
+
0385121679 29.00 36.95 65
|
1561
|
+
039480001X 30.00 32.95 31
|
1562
|
+
0394800753 16.00 16.95 0
|
1563
|
+
0394900014 23.00 23.95 0
|
1564
|
+
044100590X 36.00 45.95 89
|
1565
|
+
0441172717 17.00 21.95 77
|
1566
|
+
0451160916 24.00 28.95 22
|
1567
|
+
0451198492 36.00 46.95 0
|
1568
|
+
0451457994 17.00 22.95 0
|
1569
|
+
0590445065 23.00 23.95 10
|
1570
|
+
0679803335 20.00 24.95 18
|
1571
|
+
0694003611 25.00 28.95 50
|
1572
|
+
0760720002 18.00 23.95 28
|
1573
|
+
0823015505 26.00 28.95 16
|
1574
|
+
0929605942 19.00 21.95 25
|
1575
|
+
1885418035 23.00 24.95 77
|
1576
|
+
\.
|
1577
|
+
|
1578
|
+
|
1579
|
+
--
|
1580
|
+
-- Data for Name: subjects; Type: TABLE DATA; Schema: booktown; Owner: -
|
1581
|
+
--
|
1582
|
+
|
1583
|
+
COPY booktown.subjects (id, subject, location) FROM stdin;
|
1584
|
+
0 Arts Creativity St
|
1585
|
+
1 Business Productivity Ave
|
1586
|
+
2 Children's Books Kids Ct
|
1587
|
+
3 Classics Academic Rd
|
1588
|
+
4 Computers Productivity Ave
|
1589
|
+
5 Cooking Creativity St
|
1590
|
+
6 Drama Main St
|
1591
|
+
7 Entertainment Main St
|
1592
|
+
8 History Academic Rd
|
1593
|
+
9 Horror Black Raven Dr
|
1594
|
+
10 Mystery Black Raven Dr
|
1595
|
+
11 Poetry Sunset Dr
|
1596
|
+
12 Religion \N
|
1597
|
+
13 Romance Main St
|
1598
|
+
14 Science Productivity Ave
|
1599
|
+
15 Science Fiction Main St
|
1600
|
+
\.
|
1601
|
+
|
1602
|
+
|
1603
|
+
--
|
1604
|
+
-- Data for Name: text_sorting; Type: TABLE DATA; Schema: booktown; Owner: -
|
1605
|
+
--
|
1606
|
+
|
1607
|
+
COPY booktown.text_sorting (letter) FROM stdin;
|
1608
|
+
0
|
1609
|
+
1
|
1610
|
+
2
|
1611
|
+
3
|
1612
|
+
A
|
1613
|
+
B
|
1614
|
+
C
|
1615
|
+
D
|
1616
|
+
a
|
1617
|
+
b
|
1618
|
+
c
|
1619
|
+
d
|
1620
|
+
\.
|
1621
|
+
|
1622
|
+
|
1623
|
+
--
|
1624
|
+
-- Name: author_ids; Type: SEQUENCE SET; Schema: booktown; Owner: -
|
1625
|
+
--
|
1626
|
+
|
1627
|
+
SELECT pg_catalog.setval('booktown.author_ids', 25044, true);
|
1628
|
+
|
1629
|
+
|
1630
|
+
--
|
1631
|
+
-- Name: book_ids; Type: SEQUENCE SET; Schema: booktown; Owner: -
|
1632
|
+
--
|
1633
|
+
|
1634
|
+
SELECT pg_catalog.setval('booktown.book_ids', 41478, true);
|
1635
|
+
|
1636
|
+
|
1637
|
+
--
|
1638
|
+
-- Name: shipments_ship_id_seq; Type: SEQUENCE SET; Schema: booktown; Owner: -
|
1639
|
+
--
|
1640
|
+
|
1641
|
+
SELECT pg_catalog.setval('booktown.shipments_ship_id_seq', 1011, true);
|
1642
|
+
|
1643
|
+
|
1644
|
+
--
|
1645
|
+
-- Name: subject_ids; Type: SEQUENCE SET; Schema: booktown; Owner: -
|
1646
|
+
--
|
1647
|
+
|
1648
|
+
SELECT pg_catalog.setval('booktown.subject_ids', 15, true);
|
1649
|
+
|
1650
|
+
|
1651
|
+
--
|
1652
|
+
-- Name: authors authors_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1653
|
+
--
|
1654
|
+
|
1655
|
+
ALTER TABLE ONLY booktown.authors
|
1656
|
+
ADD CONSTRAINT authors_pkey PRIMARY KEY (id);
|
1657
|
+
|
1658
|
+
|
1659
|
+
--
|
1660
|
+
-- Name: books books_id_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1661
|
+
--
|
1662
|
+
|
1663
|
+
ALTER TABLE ONLY booktown.books
|
1664
|
+
ADD CONSTRAINT books_id_pkey PRIMARY KEY (id);
|
1665
|
+
|
1666
|
+
|
1667
|
+
--
|
1668
|
+
-- Name: customers customers_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1669
|
+
--
|
1670
|
+
|
1671
|
+
ALTER TABLE ONLY booktown.customers
|
1672
|
+
ADD CONSTRAINT customers_pkey PRIMARY KEY (id);
|
1673
|
+
|
1674
|
+
|
1675
|
+
--
|
1676
|
+
-- Name: employees employees_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1677
|
+
--
|
1678
|
+
|
1679
|
+
ALTER TABLE ONLY booktown.employees
|
1680
|
+
ADD CONSTRAINT employees_pkey PRIMARY KEY (id);
|
1681
|
+
|
1682
|
+
|
1683
|
+
--
|
1684
|
+
-- Name: editions pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1685
|
+
--
|
1686
|
+
|
1687
|
+
ALTER TABLE ONLY booktown.editions
|
1688
|
+
ADD CONSTRAINT pkey PRIMARY KEY (isbn);
|
1689
|
+
|
1690
|
+
|
1691
|
+
--
|
1692
|
+
-- Name: publishers publishers_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1693
|
+
--
|
1694
|
+
|
1695
|
+
ALTER TABLE ONLY booktown.publishers
|
1696
|
+
ADD CONSTRAINT publishers_pkey PRIMARY KEY (id);
|
1697
|
+
|
1698
|
+
|
1699
|
+
--
|
1700
|
+
-- Name: schedules schedules_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1701
|
+
--
|
1702
|
+
|
1703
|
+
ALTER TABLE ONLY booktown.schedules
|
1704
|
+
ADD CONSTRAINT schedules_pkey PRIMARY KEY (employee_id);
|
1705
|
+
|
1706
|
+
|
1707
|
+
--
|
1708
|
+
-- Name: states state_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1709
|
+
--
|
1710
|
+
|
1711
|
+
ALTER TABLE ONLY booktown.states
|
1712
|
+
ADD CONSTRAINT state_pkey PRIMARY KEY (id);
|
1713
|
+
|
1714
|
+
|
1715
|
+
--
|
1716
|
+
-- Name: stock stock_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1717
|
+
--
|
1718
|
+
|
1719
|
+
ALTER TABLE ONLY booktown.stock
|
1720
|
+
ADD CONSTRAINT stock_pkey PRIMARY KEY (isbn);
|
1721
|
+
|
1722
|
+
|
1723
|
+
--
|
1724
|
+
-- Name: subjects subjects_pkey; Type: CONSTRAINT; Schema: booktown; Owner: -
|
1725
|
+
--
|
1726
|
+
|
1727
|
+
ALTER TABLE ONLY booktown.subjects
|
1728
|
+
ADD CONSTRAINT subjects_pkey PRIMARY KEY (id);
|
1729
|
+
|
1730
|
+
|
1731
|
+
--
|
1732
|
+
-- Name: books_title_idx; Type: INDEX; Schema: booktown; Owner: -
|
1733
|
+
--
|
1734
|
+
|
1735
|
+
CREATE INDEX books_title_idx ON booktown.books USING btree (title);
|
1736
|
+
|
1737
|
+
|
1738
|
+
--
|
1739
|
+
-- Name: shipments_ship_id_key; Type: INDEX; Schema: booktown; Owner: -
|
1740
|
+
--
|
1741
|
+
|
1742
|
+
CREATE UNIQUE INDEX shipments_ship_id_key ON booktown.shipments USING btree (id);
|
1743
|
+
|
1744
|
+
|
1745
|
+
--
|
1746
|
+
-- Name: text_idx; Type: INDEX; Schema: booktown; Owner: -
|
1747
|
+
--
|
1748
|
+
|
1749
|
+
CREATE INDEX text_idx ON booktown.text_sorting USING btree (letter);
|
1750
|
+
|
1751
|
+
|
1752
|
+
--
|
1753
|
+
-- Name: unique_publisher_idx; Type: INDEX; Schema: booktown; Owner: -
|
1754
|
+
--
|
1755
|
+
|
1756
|
+
CREATE UNIQUE INDEX unique_publisher_idx ON booktown.publishers USING btree (name);
|
1757
|
+
|
1758
|
+
|
1759
|
+
--
|
1760
|
+
-- Name: editions sync_stock_with_editions; Type: RULE; Schema: booktown; Owner: -
|
1761
|
+
--
|
1762
|
+
|
1763
|
+
CREATE RULE sync_stock_with_editions AS
|
1764
|
+
ON UPDATE TO booktown.editions DO UPDATE booktown.stock SET isbn = new.isbn
|
1765
|
+
WHERE (stock.isbn = old.isbn);
|
1766
|
+
|
1767
|
+
|
1768
|
+
--
|
1769
|
+
-- Name: shipments check_shipment; Type: TRIGGER; Schema: booktown; Owner: -
|
1770
|
+
--
|
1771
|
+
|
1772
|
+
CREATE TRIGGER check_shipment BEFORE INSERT OR UPDATE ON booktown.shipments FOR EACH ROW EXECUTE PROCEDURE booktown.check_shipment_addition();
|
1773
|
+
|
1774
|
+
|
1775
|
+
--
|
1776
|
+
-- Name: authors sync_authors_books; Type: TRIGGER; Schema: booktown; Owner: -
|
1777
|
+
--
|
1778
|
+
|
1779
|
+
CREATE TRIGGER sync_authors_books BEFORE UPDATE ON booktown.authors FOR EACH ROW EXECUTE PROCEDURE booktown.sync_authors_and_books();
|
1780
|
+
|
1781
|
+
|
1782
|
+
--
|
1783
|
+
-- Name: schedules valid_employee; Type: FK CONSTRAINT; Schema: booktown; Owner: -
|
1784
|
+
--
|
1785
|
+
|
1786
|
+
ALTER TABLE ONLY booktown.schedules
|
1787
|
+
ADD CONSTRAINT valid_employee FOREIGN KEY (employee_id) REFERENCES booktown.employees(id) MATCH FULL;
|
1788
|
+
|
1789
|
+
|
1790
|
+
--
|
1791
|
+
-- PostgreSQL database dump complete
|
1792
|
+
--
|
1793
|
+
|