src/litestorepkg/lib/queries.nim
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 |
import db_connector/db_sqlite # SQL QUERIES const SQL_CREATE_INDEX_DOCUMENTS_DOCID* = sql"CREATE INDEX IF NOT EXISTS documents_docid ON documents(docid)" SQL_CREATE_INDEX_DOCUMENTS_ID* = sql"CREATE INDEX IF NOT EXISTS documents_id ON documents(id)" SQL_CREATE_INDEX_SYSTEM_DOCUMENTS_DOCID* = sql"CREATE INDEX IF NOT EXISTS system_documents_docid ON documents(docid)" SQL_CREATE_INDEX_SYSTEM_DOCUMENTS_ID* = sql"CREATE INDEX IF NOT EXISTS system_documents_id ON documents(id)" SQL_CREATE_INDEX_TAGS_DOCUMENT_ID* = sql"CREATE INDEX IF NOT EXISTS tags_document_id ON tags(document_id)" SQL_CREATE_INDEX_TAGS_TAG_ID* = sql"CREATE INDEX IF NOT EXISTS tags_tag_id ON tags(tag_id)" SQL_DROP_INDEX_DOCUMENTS_DOCID* = sql"DROP INDEX IF EXISTS documents_docid" SQL_DROP_INDEX_DOCUMENTS_ID* = sql"DROP INDEX IF EXISTS documents_id" SQL_DROP_INDEX_SYSTEM_DOCUMENTS_DOCID* = sql"DROP INDEX IF EXISTS system_documents_docid" SQL_DROP_INDEX_SYSTEM_DOCUMENTS_ID* = sql"DROP INDEX IF EXISTS system_documents_id" SQL_DROP_INDEX_TAGS_DOCUMENT_ID* = sql"DROP INDEX IF EXISTS tags_document_id" SQL_DROP_INDEX_TAGS_TAG_ID* = sql"DROP INDEX IF EXISTS tags_tag_id" SQL_REINDEX* = sql"REINDEX" SQL_OPTIMIZE* = sql"INSERT INTO searchdata(searchdata) VALUES('optimize')" SQL_REBUILD* = sql"INSERT INTO searchdata(searchdata) VALUES('rebuild')" SQL_VACUUM* = sql"VACUUM" const SQL_CREATE_DOCUMENTS_TABLE* = sql""" CREATE TABLE documents ( docid INTEGER PRIMARY KEY, id TEXT UNIQUE NOT NULL, data TEXT, content_type TEXT, binary INTEGER, searchable INTEGER, created TEXT, modified TEXT) """ const SQL_CREATE_SYSTEM_DOCUMENTS_TABLE* = sql""" CREATE TABLE system_documents ( docid INTEGER PRIMARY KEY, id TEXT UNIQUE NOT NULL, data TEXT, content_type TEXT, binary INTEGER, created TEXT, modified TEXT ) """ const SQL_CREATE_SEARCHDATA_TABLE* = sql""" CREATE VIRTUAL TABLE searchdata USING fts4( id TEXT UNIQUE NOT NULL, data TEXT, tokenize=porter) """ const SQL_CREATE_TAGS_TABLE* = sql""" CREATE TABLE tags ( tag_id TEXT NOT NULL, document_id TEXT NOT NULL, FOREIGN KEY(document_id) REFERENCES documents(id) ON DELETE CASCADE, PRIMARY KEY (tag_id, document_id)) """ const SQL_CREATE_INFO_TABLE* = sql""" CREATE TABLE info ( version INT, total_documents INT) """ const SQL_INSERT_INFO* = sql""" INSERT INTO info (version, total_documents) VALUES (?, ?) """ const SQL_UPDATE_VERSION* = sql""" UPDATE info SET version = ? """ const SQL_SELECT_INFO* = sql""" SELECT * FROM info """ const SQL_SET_TOTAL_DOCS* = sql""" UPDATE info SET total_documents = ? """ const SQL_INCREMENT_DOCS* = sql""" UPDATE info SET total_documents = total_documents + 1 """ const SQL_DECREMENT_DOCS* = sql""" UPDATE info SET total_documents = total_documents - 1 """ const SQL_INSERT_DOCUMENT* = sql""" INSERT INTO documents (id, data, content_type, binary, searchable, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?) """ const SQL_INSERT_SYSTEM_DOCUMENT* = sql""" INSERT INTO system_documents (id, data, content_type, binary, created) VALUES (?, ?, ?, ?, ?) """ const SQL_UPDATE_DOCUMENT* = sql""" UPDATE documents SET data = ?, content_type = ?, binary = ?, searchable = ?, modified = ? WHERE id = ? """ const SQL_UPDATE_SYSTEM_DOCUMENT* = sql""" UPDATE system_documents SET data = ?, content_type = ?, binary = ?, modified = ? WHERE id = ? """ const SQL_SET_DOCUMENT_MODIFIED* = sql""" UPDATE documents SET modified = ? WHERE id = ? """ const SQL_DELETE_DOCUMENT* = sql""" DELETE FROM documents WHERE id = ? """ const SQL_INSERT_TAG* = sql""" INSERT INTO tags (tag_id, document_id) VALUES (?, ?) """ const SQL_DELETE_TAG* = sql""" DELETE FROM tags WHERE tag_id = ? AND document_id = ? """ const SQL_DELETE_DOCUMENT_TAGS* = sql""" DELETE FROM tags WHERE document_id = ? """ const SQL_SELECT_DOCUMENT_TAGS* = sql""" SELECT tag_id FROM tags WHERE document_id = ? """ const SQL_DELETE_DOCUMENT_SYSTEM_TAGS* = sql""" DELETE FROM tags WHERE document_id = ? AND tag_id LIKE "$%" """ const SQL_INSERT_SEARCHCONTENT* = sql""" INSERT INTO searchdata (docid, id, data) VALUES (?, ?, ?) """ const SQL_DELETE_SEARCHCONTENT* = sql""" DELETE FROM searchdata WHERE id = ? """ const SQL_UPDATE_SEARCHCONTENT* = sql""" UPDATE searchdata SET data = ? WHERE id = ? """ const SQL_SELECT_DOCUMENTS_BY_TAG* = sql""" SELECT * FROM documents, tags WHERE documents.id = tags.document_id AND tag_id = ? """ const SQL_SELECT_SYSTEM_DOCUMENTS* = sql""" SELECT * FROM system_documents """ const SQL_SELECT_DOCUMENT_IDS_BY_TAG* = sql""" SELECT id FROM documents, tags WHERE documents.id = tags.document_id AND tag_id = ? """ const SQL_SELECT_TAGS_WITH_TOTALS* = sql""" SELECT DISTINCT tag_id, COUNT(document_id) FROM tags GROUP BY tag_id ORDER BY tag_id ASC """ const SQL_COUNT_TAGS* = sql""" SELECT COUNT(DISTINCT tag_id) FROM tags """ const SQL_COUNT_INDEXES* = sql""" SELECT COUNT(DISTINCT name) FROM from sqlite_master WHERE type = 'index' AND tbl_name = 'documents' AND name LIKE 'json_index_%' """ const SQL_COUNT_DOCUMENTS* = sql""" SELECT COUNT(docid) FROM documents """ const SQL_DELETE_DOCUMENTS_BY_TAG* = sql""" DELETE FROM documents WHERE documents.id IN (SELECT document_id FROM tags WHERE tag_id = ?) """ const SQL_DELETE_SYSTEM_DOCUMENTS* = sql""" DELETE FROM system_documents """ const SQL_DELETE_SEARCHDATA_BY_TAG* = sql""" DELETE FROM searchdata WHERE id IN (SELECT document_id FROM tags WHERE tag_id = ?) """ const SQL_DELETE_TAGS_BY_TAG* = sql""" DELETE FROM tags WHERE document_id IN (SELECT document_id FROM tags WHERE tag_id = ?) """ |