forked from epilys/bibliothecula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
141 lines (131 loc) · 6.2 KB
/
schema.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
/* Contents
id | summary
------------------------------------+-------------------------------------------------------------------------
CREATE_DOCUMENTS | CREATE TABLE IF NOT EXISTS "Documents" ( "uuid" CHARACTER(32) NOT...
CREATE_TEXTMETADATA | CREATE TABLE IF NOT EXISTS "TextMetadata" ( "uuid" CHARACTER(32) NOT...
CREATE_BINARYMETADATA | CREATE TABLE IF NOT EXISTS "BinaryMetadata" ( "uuid" CHARACTER(32)...
CREATE_DOCUMENTHASTEXTMETADATA | CREATE TABLE IF NOT EXISTS "DocumentHasTextMetadata" ( "id" INTEGER...
CREATE_DOCUMENTHASBINARYMETADATA | CREATE TABLE IF NOT EXISTS "DocumentHasBinaryMetadata" ( "id" INTEGER...
CREATE_VIEW_DOCUMENTS_TITLE_AUTHORS | Auxiliary view for use in document_title_authors_text_view_fts index....
FTS_CREATE_TABLE | Create a full-text search index using the fts5 module....
FTS_CREATE_INSERT_TRIGGER | Trigger to insert full text data when a DocumentHasBinaryMetadata row...
FTS_CREATE_DELETE_TRIGGER | Trigger to remove a document's full text from the full text search...
*/
/* CREATE_DOCUMENTS */
CREATE TABLE IF NOT EXISTS "Documents" (
"uuid" CHARACTER(32) NOT NULL PRIMARY KEY,
"title" TEXT NOT NULL,
"title_suffix" TEXT DEFAULT NULL, -- disambiguate documents with matching titles
"created" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
"last_modified" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
CONSTRAINT unique_title UNIQUE ("title", "title_suffix")
);
/* CREATE_TEXTMETADATA */
CREATE TABLE IF NOT EXISTS "TextMetadata" (
"uuid" CHARACTER(32) NOT NULL PRIMARY KEY,
"name" TEXT NULL,
"data" TEXT NOT NULL,
"created" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
"last_modified" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
CONSTRAINT uniqueness UNIQUE ("name", "data")
);
/* CREATE_BINARYMETADATA */
CREATE TABLE IF NOT EXISTS "BinaryMetadata" (
"uuid" CHARACTER(32) NOT NULL PRIMARY KEY,
"name" TEXT NULL,
"data" BLOB NOT NULL,
"compressed" BOOLEAN NOT NULL DEFAULT (0),
"created" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
"last_modified" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
CONSTRAINT uniqueness UNIQUE ("name", "data")
);
/* CREATE_DOCUMENTHASTEXTMETADATA */
CREATE TABLE IF NOT EXISTS "DocumentHasTextMetadata" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"document_uuid" CHARACTER(32) NOT NULL
REFERENCES "Documents" ("uuid") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
"metadata_uuid" CHARACTER(32) NOT NULL
REFERENCES "TextMetadata" ("uuid") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
"created" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
"last_modified" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
CONSTRAINT uniqueness UNIQUE ("name", "document_uuid", "metadata_uuid")
);
/* CREATE_DOCUMENTHASBINARYMETADATA */
CREATE TABLE IF NOT EXISTS "DocumentHasBinaryMetadata" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"document_uuid" CHARACTER(32) NOT NULL
REFERENCES "Documents" ("uuid") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
"metadata_uuid" CHARACTER(32) NOT NULL
REFERENCES "BinaryMetadata" ("uuid") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
"created" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
"last_modified" DATETIME NOT NULL DEFAULT (strftime ('%Y-%m-%d %H:%M:%f', 'now')),
CONSTRAINT uniqueness UNIQUE ("name", "document_uuid", "metadata_uuid")
);
/* CREATE_VIEW_DOCUMENTS_TITLE_AUTHORS
Auxiliary view for use in document_title_authors_text_view_fts index.
Returns document title and a NULL byte separated string with all
authors or NULL for all documents.
https://sqlite.org/lang_createview.html sqlite3 reference for for
creating views */
CREATE VIEW document_title_authors (rowid, title, authors) AS
SELECT uuid, title, authors
FROM
Documents AS d
LEFT JOIN (SELECT
document_uuid,
GROUP_CONCAT (data, '\0') AS authors
FROM
DocumentHasTextMetadata AS dhtm
JOIN TextMetadata AS tm ON dhtm.metadata_uuid = tm.uuid
WHERE
tm.name = 'author'
GROUP BY
document_uuid) AS authors ON d.uuid = authors.document_uuid;
/* FTS_CREATE_TABLE
Create a full-text search index using the fts5 module.
https://sqlite.org/fts5.html sqlite3 reference */
CREATE VIRTUAL TABLE IF NOT EXISTS document_title_authors_text_view_fts
USING fts5(title, authors, full_text, uuid UNINDEXED);
/* FTS_CREATE_INSERT_TRIGGER
Trigger to insert full text data when a DocumentHasBinaryMetadata row
for a full-text BinaryMetadata is created.
https://sqlite.org/lang_createtrigger.html sqlite3 reference for for
creating triggers */
CREATE TRIGGER insert_full_text_trigger
AFTER INSERT ON DocumentHasBinaryMetadata
WHEN EXISTS (
SELECT
*
FROM
BinaryMetadata AS bm
WHERE
NEW.metadata_uuid = bm.uuid
AND bm.name = 'full-text')
BEGIN
INSERT INTO document_title_authors_text_view_fts (
uuid, title, authors, full_text)
SELECT
d.rowid AS rowid, d.title AS title,
d.authors AS authors, bm.data AS full_text
FROM
document_title_authors AS d,
BinaryMetadata AS bm
WHERE
d.rowid = NEW.document_uuid
AND bm.name = 'full-text'
AND bm.uuid = NEW.metadata_uuid;
END;
/* FTS_CREATE_DELETE_TRIGGER
Trigger to remove a document's full text from the full text search
table when the full-text binary metadata is deleted.
https://sqlite.org/lang_createtrigger.html sqlite3 reference for for
creating triggers */
CREATE TRIGGER IF NOT EXISTS delete_full_text_trigger
AFTER DELETE ON DocumentHasBinaryMetadata
WHEN OLD.name = 'full-text'
BEGIN
DELETE FROM document_title_authors_text_view_fts
WHERE uuid = OLD.document_uuid;
END;