-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdummy.sql
172 lines (155 loc) · 5.01 KB
/
dummy.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
\echo 'Code or Die Dummy Data'
\echo 'James Powell <[email protected]>'
\set VERBOSITY terse
\set ON_ERROR_STOP true
do language plpgsql $$ declare
exc_message text;
exc_context text;
exc_detail text;
begin
raise notice 'populating schema objects';
do $objects$ begin
set search_path = objects, names, public;
with
homeworld_id as (
insert into objects.systems default values returning id
)
, civilization_id as (
insert into objects.civilizations (name, homeworld, token)
values ('Titanus', (select * from homeworld_id), 'tt')
returning id
)
, _0 as (
insert into names.systems (namer, system, name)
values (
(select * from civilization_id),
(select * from homeworld_id),
'Titanus Earth'
)
)
, ship_id as (
insert into objects.ships (shipyard, location, flag)
values (
(select * from homeworld_id)
, (select * from homeworld_id)
, (select * from civilization_id)
)
returning id
)
insert into names.ships (namer, ship, name)
values (
(select * from civilization_id)
, (select * from ship_id)
, 'Explorer 1'
);
with
homeworld_id as (
insert into objects.systems default values returning id
)
, civilization_id as (
insert into objects.civilizations (name, homeworld, token)
values ('Colossus', (select * from homeworld_id), 'cc')
returning id
)
, _0 as (
insert into names.systems (namer, system, name)
values (
(select * from civilization_id),
(select * from homeworld_id),
'Colossus Major'
)
)
, ship_id as (
insert into objects.ships (shipyard, location, flag)
values (
(select * from homeworld_id)
, (select * from homeworld_id)
, (select * from civilization_id)
)
returning id
)
insert into names.ships (namer, ship, name)
values (
(select * from civilization_id)
, (select * from ship_id)
, 'Destiny 1'
);
with
homeworld_id as (
insert into objects.systems default values returning id
)
, civilization_id as (
insert into objects.civilizations (name, homeworld, token)
values ('Magnifica', (select * from homeworld_id), 'mm')
returning id
)
, _0 as (
insert into names.systems (namer, system, name)
values (
(select * from civilization_id),
(select * from homeworld_id),
'Magnifica Prime'
)
)
, ship_id as (
insert into objects.ships (shipyard, location, flag)
values (
(select * from homeworld_id)
, (select * from homeworld_id)
, (select * from civilization_id)
)
returning id
)
insert into names.ships (namer, ship, name)
values (
(select * from civilization_id)
, (select * from ship_id)
, 'Voyager 1'
);
update systems
set controller = (select id from civilizations where homeworld = 1)
where id = 1;
update systems
set controller = (select id from civilizations where homeworld = 2)
where id = 2;
update systems
set controller = (select id from civilizations where homeworld = 3)
where id = 3;
drop table if exists names.civilizations;
create or replace view names.civilizations as (
select
(c1.id - 1) * (select count(*) from objects.civilizations) + (c2.id - 1) + 1 as id
, c1.id as namer
, c2.id as civilization
, c2.name as name
, tstzrange('-infinity', 'infinity', '[)')::tstzrange as asof
from objects.civilizations as c1,
objects.civilizations as c2
);
end $objects$;
exception when others then
get stacked diagnostics exc_message = message_text;
get stacked diagnostics exc_context = pg_exception_context;
get stacked diagnostics exc_detail = pg_exception_detail;
raise exception E'\n------\n%\n%\n------\n\nCONTEXT:\n%\n', exc_message, exc_detail, exc_context;
end $$;
begin;
\echo 'sample queries'
set search_path = objects, names, public;
\echo 'objects.civilizations'
select * from objects.civilizations;
\echo 'objects.systems'
select * from objects.systems;
\echo 'objects.ships'
select * from objects.ships;
\echo 'names.civilizations'
select * from names.civilizations;
\echo 'names.systems'
select * from names.systems;
\echo 'names.ships'
select * from names.ships;
\echo 'orders.systems'
select * from orders.systems;
\echo 'orders.ships'
select * from orders.ships;
end;