-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplate.sql
278 lines (223 loc) · 8.82 KB
/
Template.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
-- Enfore foreign key constraints
pragma foreign_keys = on;
-- Document version. Incrementet when the schema changes.
pragma user_version = 1;
-------------------------------------------------------------------------------
-- Project
-------------------------------------------------------------------------------
create table project (
project_id integer primary key,
project_guid blob not null,
project_version integer not null,
constraint "project_guid is not a valid guid"
check (
typeof(project_guid) = 'blob' and
length(project_guid) = 16
),
constraint "Non-integer value used for project_version"
check (typeof(project_version) = 'integer')
);
-------------------------------------------------------------------------------
-- Attachment
-------------------------------------------------------------------------------
create table attachment (
attachment_id integer primary key,
name text not null,
mime_type text not null,
content blob not null, -- Encode as UTF-8 if storing text
hash blob, -- SHA1 hash (optional)
constraint "content is not a blob" check (typeof(content) = 'blob'),
constraint "hash is not a valid SHA1 hash"
check (
hash is null or
( typeof(hash) = 'blob' and
length(hash) = 20 )),
constraint "duplicate hash detected" unique (hash)
);
-------------------------------------------------------------------------------
-- Construction Element Specification
-------------------------------------------------------------------------------
create table construction_element_specification (
construction_element_specification_id integer primary key,
molio_specification_guid blob not null,
title text not null,
constraint "molio_specification_guid is not a valid guid"
check (
typeof(molio_specification_guid) = 'blob' and
length(molio_specification_guid) = 16
)
);
create table construction_element_specification_section (
construction_element_specification_section_id integer primary key,
construction_element_specification_id integer not null,
section_no integer not null,
heading text not null,
body text not null default '',
molio_section_guid blob,
parent_id integer,
foreign key (construction_element_specification_id)
references construction_element_specification,
foreign key (parent_id)
references construction_element_specification_section,
constraint "molio_section_guid is not a valid guid"
check (
molio_section_guid is null or
( typeof(molio_section_guid) = 'blob' and
length(molio_section_guid) = 16 )),
constraint "Non-integer value used for section_no"
check (typeof(section_no) = 'integer')
);
create unique index construction_element_specification_section_unique_idx
on construction_element_specification_section (
construction_element_specification_id,
ifnull(parent_id, -1), -- All nulls are treated as unique, convert to -1 instead
section_no
);
-------------------------------------------------------------------------------
-- Work Specification
-------------------------------------------------------------------------------
create table work_specification (
work_specification_id integer primary key,
work_area_code text not null,
work_area_name text not null,
-- Key is used to associate external data with an work_specification
-- and must be the same for every export of this project
key blob not null,
constraint "key is not a valid guid"
check (
typeof(key) = 'blob' and
length(key) = 16
)
);
create table work_specification_section (
work_specification_section_id integer primary key,
work_specification_id integer not null,
section_no int not null,
heading text not null,
body text not null default '',
molio_section_guid blob,
parent_id integer,
foreign key (work_specification_id) references work_specification,
foreign key (parent_id) references work_specification_section,
constraint "molio_section_guid is not a valid guid"
check (
molio_section_guid is null or
( typeof(molio_section_guid) = 'blob' and
length(molio_section_guid) = 16 )),
constraint "Non-integer value used for section_no"
check (typeof(section_no) = 'integer')
);
create table work_specification_section_construction_element_specification (
work_specification_section_construction_element_specification_id integer primary key,
work_specification_section_id integer not null,
construction_element_specification_id integer not null,
foreign key (work_specification_section_id)
references work_specification_section,
foreign key (construction_element_specification_id)
references construction_element_specification,
constraint "Same construction_element_specification cannot be referenced more than once for the same work_specification_section"
unique (work_specification_section_id, construction_element_specification_id)
);
create unique index work_specification_section_unique_idx
on work_specification_section (
work_specification_id,
ifnull(parent_id, -1), -- All nulls are treated as unique, convert to -1 instead
section_no
);
-------------------------------------------------------------------------------
-- Custom Data
-------------------------------------------------------------------------------
-- Used to store any kind of custom key-value pairs
create table custom_data (
key text primary key,
value blob
);
-------------------------------------------------------------------------------
-- Helper views
-------------------------------------------------------------------------------
/**
Description:
`work_specification_section` is a self-referencing table where sections might
have 0 to many sub sections. This view can be joined for useful columns when
displaying the tree.
Columns:
work_specification_section_id integer
Used for joins.
section_path text
Contains the section_no path to the row, separated by a dot (.)
If parent_id points to a parent with section_no = 3 and the row contains
section_no = 1, then section_path = 3.1
level integer
The level in the tree of sections, starting at 0.
Example:
select * from work_specification_section
natural join work_specification_section_path
order by section_path;
*/
create view work_specification_section_path as
with recursive tree (
work_specification_section_id,
section_no,
section_path,
level
) as (
select
work_specification_section_id,
section_no,
cast(section_no as text),
0 as level
from work_specification_section
where parent_id is null
union all
select
node.work_specification_section_id,
node.section_no,
tree.section_path || '.' || node.section_no,
tree.level + 1
from work_specification_section node, tree
where node.parent_id = tree.work_specification_section_id
)
select work_specification_section_id, section_path, level from tree;
/**
Description:
`construction_element_specification_section` is a self-referencing table where sections might
have 0 to many sub sections. This view can be joined for useful columns when
displaying the tree.
Columns:
construction_element_specification_section_id integer
Used for joins.
section_path text
Contains the section_no path to the row, separated by a dot (.)
If parent_id points to a parent with section_no = 3 and the row contains
section_no = 1, then section_path = 3.1
level integer
The level in the tree of sections, starting at 0.
Example:
select * from construction_element_specification_section
natural join construction_element_specification_section_path
order by section_path;
*/
create view construction_element_specification_section_path as
with recursive tree (
construction_element_specification_section_id,
section_no,
section_path,
level
) as (
select
construction_element_specification_section_id,
section_no,
cast(section_no as text),
0 as level
from construction_element_specification_section
where parent_id is null
union all
select
node.construction_element_specification_section_id,
node.section_no,
tree.section_path || '.' || node.section_no,
tree.level + 1
from construction_element_specification_section node, tree
where node.parent_id = tree.construction_element_specification_section_id
)
select construction_element_specification_section_id, section_path, level from tree;