-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
337 lines (306 loc) · 14.5 KB
/
Copy pathqueries.sql
File metadata and controls
337 lines (306 loc) · 14.5 KB
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
---------------
-- Documents --
---------------
-- :name get-documents :? :*
-- :doc retrieve all documents given a limit and an offset
SELECT *, (CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END) AS spelling
FROM documents_document
LIMIT :limit OFFSET :offset
-- :name find-documents :? :*
-- :doc retrieve all documents given a `search` term, a `limit` and an `offset`
SELECT *, (CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END) AS spelling
FROM documents_document
WHERE LOWER(title) LIKE LOWER(:search)
LIMIT :limit OFFSET :offset
-- :name get-document :? :1
-- :doc retrieve a document record given the `id`
SELECT *, (CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END) AS spelling
FROM documents_document
WHERE id = :id
-- :name get-images :? :*
-- :doc retrieve the images for a given document `id`
SELECT * FROM documents_image
WHERE document_id = :id
--------------
-- Versions --
--------------
-- :name get-versions :? :*
-- :doc retrieve all versions of a document given a `document_id`
SELECT * FROM documents_version
WHERE document_id = :document_id
-- :name get-latest-version :? :1
-- :doc retrieve the latest version of a document given a `document_id`
SELECT * FROM documents_version
WHERE document_id = :document_id
AND created_at = (SELECT MAX(created_at) FROM documents_version WHERE document_id = :document_id)
------------------
-- Global Words --
------------------
-- :name get-global-words :? :*
-- :doc retrieve all global words where the column `braille` ("contracted" or "uncontracted") is not null optionally filtered by `types`
SELECT untranslated, uncontracted, contracted, type, homograph_disambiguation
FROM dictionary_globalword
WHERE :i:braille IS NOT NULL
--~ (when (:types params) "AND type IN (:v*:types)")
-- :name find-global-words :? :*
-- :doc retrieve all global words given a simple pattern for `untranslated`, a `limit` and an `offset`
SELECT untranslated, uncontracted, contracted, type, homograph_disambiguation
FROM dictionary_globalword
WHERE untranslated LIKE :untranslated
ORDER BY untranslated
LIMIT :limit OFFSET :offset
-- :name insert-global-word :! :n
-- :doc Insert or update a word in the global dictionary.
INSERT INTO dictionary_globalword (untranslated, uncontracted, contracted, type, homograph_disambiguation)
VALUES (:untranslated, :uncontracted, :contracted, :type, :homograph_disambiguation)
ON DUPLICATE KEY UPDATE
contracted = VALUES(contracted),
uncontracted = VALUES(uncontracted)
-- :name delete-global-word :! :n
-- :doc Delete a word in the global dictionary.
DELETE FROM dictionary_globalword
WHERE untranslated = :untranslated
AND type = :type
AND homograph_disambiguation = :homograph_disambiguation
-----------------
-- Local Words --
-----------------
-- :name get-local-words :? :*
-- :doc retrieve all local words for a given document `id` and `grade`. Optionally you can only get local words that match a `search` term. The words contain braille for both grades and the hyphenation if they exist. Optionally the results can be limited by `limit` and `offset`.
SELECT words.untranslated,
--~ (when (#{0 1} (:grade params)) "words.uncontracted,")
--~ (when (#{0 2} (:grade params)) "words.contracted,")
words.type,
words.homograph_disambiguation,
words.document_id,
words.isLocal,
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END FROM documents_document WHERE id = :id) AS spelling,
hyphenation.hyphenation AS hyphenated
FROM dictionary_localword as words
LEFT JOIN hyphenation_words AS hyphenation
ON words.untranslated = hyphenation.word
AND hyphenation.spelling =
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END
FROM documents_document
WHERE id = :id)
-- either uncontracted or contracted should always be non-null so the following should be implicitely the case and hence not needed
-- (when (= (:grade params) 0) "WHERE words.uncontracted IS NOT NULL OR words.contracted IS NOT NULL")
--~ (when (= (:grade params) 1) "WHERE words.uncontracted IS NOT NULL")
--~ (when (= (:grade params) 2) "WHERE words.contracted IS NOT NULL")
ORDER BY words.untranslated
--~ (when (:limit params) "LIMIT :limit")
--~ (when (:offset params) "OFFSET :offset")
-- :name get-local-word :? :1
-- :doc retrieve a local word record given `document_id`, `untranslated`, `type` and `homograph_disambiguation`.
SELECT *
FROM dictionary_localword
WHERE untranslated = :untranslated
AND type = :type
AND homograph_disambiguation = :homograph_disambiguation
AND document_id = :document_id
-- :name insert-local-word :! :n
-- :doc Insert or update a word in the local dictionary. Optionally specify `isconfirmed`.
INSERT INTO dictionary_localword (
untranslated,
--~ (when (:contracted params) "contracted,")
--~ (when (:uncontracted params) "uncontracted,")
type,
homograph_disambiguation,
document_id,
isLocal,
isConfirmed)
VALUES (
:untranslated,
--~ (when (:contracted params) ":contracted,")
--~ (when (:uncontracted params) ":uncontracted,")
:type,
:homograph_disambiguation,
:document_id,
:islocal,
--~ (if (:isconfirmed params) ":isconfirmed" "DEFAULT")
)
ON DUPLICATE KEY UPDATE
--~ (when (:contracted params) "contracted = VALUES(contracted),")
--~ (when (:uncontracted params) "uncontracted = VALUES(uncontracted),")
--~ (when (:isconfirmed params) "isConfirmed = VALUES(isConfirmed),")
isLocal = VALUES(isLocal)
-- :name delete-local-word-partial :! :n
-- :doc Set either contracted or uncontracted to NULL for given `document_id`, `untranslated`, `type` and `homograph_disambiguation`.
UPDATE dictionary_localword
/*~ (if (:contracted params) */
SET contracted = NULL
/*~*/
SET uncontracted = NULL
/*~ ) ~*/
WHERE untranslated = :untranslated
AND type = :type
AND homograph_disambiguation = :homograph_disambiguation
AND document_id = :document_id
-- :name delete-local-word :! :n
-- :doc Delete a word in the local dictionary.
DELETE FROM dictionary_localword
WHERE untranslated = :untranslated
AND type = :type
AND homograph_disambiguation = :homograph_disambiguation
AND document_id = :document_id
-------------------
-- Unknown words --
-------------------
-- :name delete-unknown-words :! :n
-- :doc empty the "temporary" table containing words from a new document
DELETE FROM dictionary_unknownword
-- :name insert-unknown-words :! :n
-- :doc insert a list of new `words` into a "temporary" table. This later used to join with the already known words to query the unknown words
INSERT INTO dictionary_unknownword (untranslated, type, homograph_disambiguation, document_id)
VALUES :tuple*:words
-- :name delete-non-existing-unknown-words-from-local-words :! :n
-- :doc delete words that are not in the list of unknown words from the local words for given `:document-id`
DELETE l
FROM dictionary_localword l
LEFT JOIN dictionary_unknownword u
ON u.untranslated = l.untranslated AND u.type = l.type AND u.document_id = l.document_id
WHERE u.untranslated IS NULL
AND l.document_id = :document-id
-- :name get-all-unknown-words :? :*
-- :doc given a `document-id` and a `:grade` retrieve all unknown words for it. If `:grade` is 0 then return words for both grade 1 and 2. Otherwise just return the unknown words for the given grade.This assumes that the new words contained in this document have been inserted into the `dictionary_unknownword` table.
-- NOTE: This query assumes that there are only records for the current document-id in the dictionary_unknownword table.
(SELECT unknown.*,
--~ (when (#{0 1} (:grade params)) "COALESCE(l.uncontracted, g.uncontracted) AS uncontracted,")
--~ (when (#{0 2} (:grade params)) "COALESCE(l.contracted, g.contracted) AS contracted,")
hyphenation.hyphenation AS hyphenated,
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END FROM documents_document WHERE id = :document-id) AS spelling
FROM dictionary_unknownword unknown
LEFT JOIN dictionary_localword l ON l.untranslated = unknown.untranslated AND l.type IN (0,1,3) AND l.document_id = :document-id
LEFT JOIN dictionary_globalword g ON g.untranslated = unknown.untranslated AND g.type IN (0,1,3)
LEFT JOIN hyphenation_words AS hyphenation
ON unknown.untranslated = hyphenation.word
AND hyphenation.spelling =
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END
FROM documents_document
WHERE id = :document-id)
WHERE unknown.type = 0
AND g.untranslated IS NULL
AND (((:grade IN (0,2)) AND l.contracted IS NULL) OR ((:grade IN (0,1)) AND l.uncontracted IS NULL))
)
UNION
(SELECT unknown.*,
--~ (when (#{0 1} (:grade params)) "COALESCE(l.uncontracted, g.uncontracted) AS uncontracted,")
--~ (when (#{0 2} (:grade params)) "COALESCE(l.contracted, g.contracted) AS contracted,")
hyphenation.hyphenation AS hyphenated,
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END FROM documents_document WHERE id = :document-id) AS spelling
FROM dictionary_unknownword unknown
LEFT JOIN dictionary_localword l ON l.untranslated = unknown.untranslated AND l.type IN (1,2) AND l.document_id = :document-id
LEFT JOIN dictionary_globalword g ON g.untranslated = unknown.untranslated AND g.type IN (1,2)
LEFT JOIN hyphenation_words AS hyphenation
ON unknown.untranslated = hyphenation.word
AND hyphenation.spelling =
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END
FROM documents_document
WHERE id = :document-id)
WHERE unknown.type = 2
AND g.untranslated IS NULL
AND (((:grade IN (0,2)) AND l.contracted IS NULL) OR ((:grade IN (0,1)) AND l.uncontracted IS NULL))
)
UNION
(SELECT unknown.*,
--~ (when (#{0 1} (:grade params)) "COALESCE(l.uncontracted, g.uncontracted) AS uncontracted,")
--~ (when (#{0 2} (:grade params)) "COALESCE(l.contracted, g.contracted) AS contracted,")
hyphenation.hyphenation AS hyphenated,
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END FROM documents_document WHERE id = :document-id) AS spelling
FROM dictionary_unknownword unknown
LEFT JOIN dictionary_localword l ON l.untranslated = unknown.untranslated AND l.type IN (3,4) AND l.document_id = :document-id
LEFT JOIN dictionary_globalword g ON g.untranslated = unknown.untranslated AND g.type IN (3,4)
LEFT JOIN hyphenation_words AS hyphenation
ON unknown.untranslated = hyphenation.word
AND hyphenation.spelling =
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END
FROM documents_document
WHERE id = :document-id)
WHERE unknown.type = 4
AND g.untranslated IS NULL
AND (((:grade IN (0,2)) AND l.contracted IS NULL) OR ((:grade IN (0,1)) AND l.uncontracted IS NULL))
)
UNION
(SELECT unknown.*,
--~ (when (#{0 1} (:grade params)) "COALESCE(l.uncontracted, g.uncontracted) AS uncontracted,")
--~ (when (#{0 2} (:grade params)) "COALESCE(l.contracted, g.contracted) AS contracted,")
hyphenation.hyphenation AS hyphenated,
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END FROM documents_document WHERE id = :document-id) AS spelling
FROM dictionary_unknownword unknown
LEFT JOIN dictionary_localword l ON l.untranslated = unknown.untranslated AND l.type IN (5) AND l.document_id = :document-id
LEFT JOIN dictionary_globalword g ON g.untranslated = unknown.untranslated AND g.type IN (5)
LEFT JOIN hyphenation_words AS hyphenation
ON unknown.untranslated = hyphenation.word
AND hyphenation.spelling =
(SELECT CASE language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END
FROM documents_document
WHERE id = :document-id)
WHERE unknown.type = 5
AND g.untranslated IS NULL
AND (((:grade IN (0,2)) AND l.contracted IS NULL) OR ((:grade IN (0,1)) AND l.uncontracted IS NULL))
)
ORDER BY untranslated
LIMIT :limit OFFSET :offset
-----------------------------
-- Unknown word statistics --
-----------------------------
-- :name insert-unknown-words-stats :! :n
-- :doc insert statistical data about the number of words and unknown words for a given `document-id`. The `total` must be given. The number of unknown words are calculated based on the local words for the document. This assumes that all unknown words have been inserted in the local word table and they have not yet been confirmed, i.e. moved to the global words table.
INSERT INTO statistics_documentstatistic (date, document_id, total, unknown)
VALUES (NOW(), :document-id, :total, (SELECT count(*) FROM dictionary_localword where document_id = :document_id))
ON DUPLICATE KEY UPDATE
total = VALUES(total),
unknown = VALUES(unknown),
date = VALUES(date)
-----------------------
-- Confirmable words --
-----------------------
-- :name get-confirmable-words :? :*
-- :doc retrieve local words that are ready for confirmation. The words contain braille for both grades and the hyphenation if they exist.
SELECT words.*,
(CASE doc.language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END) AS spelling,
doc.title AS document_title,
hyphenation.hyphenation AS hyphenated
FROM dictionary_localword as words
JOIN documents_document doc ON words.document_id = doc.id
LEFT JOIN hyphenation_words AS hyphenation
ON words.untranslated = hyphenation.word
AND hyphenation.spelling = (CASE doc.language WHEN "de" THEN 1 WHEN "de-1901" THEN 0 ELSE NULL END)
WHERE words.isConfirmed = FALSE
-- only get words from finished productions
AND doc.state_id = (SELECT id FROM documents_state WHERE sort_order = (SELECT MAX(sort_order) FROM documents_state))
ORDER BY words.document_id, words.untranslated
LIMIT :limit OFFSET :offset
------------------
-- Hyphenations --
------------------
-- :name get-hyphenation :? :*
-- :doc retrieve hyphenations given a `spelling` and optionally s `search` term, a `limit` and an `offset`
SELECT * FROM hyphenation_words
WHERE spelling = :spelling
--~ (when (:search params) "AND word LIKE :search")
--~ (when (:limit params) "LIMIT :limit")
--~ (when (:offset params) "OFFSET :offset")
-- :name insert-hyphenation :! :n
-- :doc Insert or update a hyphenation.
INSERT INTO hyphenation_words (word, hyphenation, spelling)
VALUES (:word, :hyphenation, :spelling)
ON DUPLICATE KEY UPDATE
hyphenation = VALUES(hyphenation)
-- :name delete-hyphenation :! :n
-- :doc Delete a hyphenation word `:word` and `:spelling` if there are no more references to it from either the local words, if `:document-id` is given, or the global words otherwise.
DELETE FROM hyphenation_words
WHERE word = :word
AND spelling = :spelling
/*~ (if (:document-id params) */
AND NOT EXISTS (
SELECT * FROM dictionary_localword
WHERE untranslated = :word
AND document_id = :document-id
)
/*~*/
AND NOT EXISTS (
SELECT * FROM dictionary_globalword
WHERE untranslated = :word
)
/*~ ) ~*/