|
13 | 13 | (def db-path (.join path (.getPath app "userData") db-name))
|
14 | 14 | (def db (sqlite. db-path))
|
15 | 15 |
|
16 |
| - |
17 | 16 | ;; (defn remove-db-file! []
|
18 | 17 | ;; (.unlink fs db-path #(when % (prn "Failed to delete db file") %)))
|
19 | 18 |
|
|
27 | 26 | ;; versions of their actual spelling and ID, making it possible to reconstruct
|
28 | 27 | ;; the word from the delimisted string (word_ids) in the article table.
|
29 | 28 |
|
30 |
| -;; FIXME: words table needs new columns |
31 |
| -;; TODO - last updated |
32 |
| -;; TODO - last seen |
| 29 | +;; TODO: rename `article_id` -> `id`; same with `settings_id` |
33 | 30 | (def db-seed "
|
34 | 31 |
|
35 | 32 | CREATE TABLE IF NOT EXISTS words (
|
|
69 | 66 | current_page INTEGER DEFAULT 0
|
70 | 67 | );
|
71 | 68 |
|
| 69 | + CREATE TABLE IF NOT EXISTS languages ( |
| 70 | + id INTEGER PRIMARY KEY, |
| 71 | + name TEXT NOT NULL, |
| 72 | + iso_639_1 TEXT NOT NULL, |
| 73 | + text_splitting_regex TEXT NOT NULL, |
| 74 | + word_regex TEXT NOT NULL, |
| 75 | + UNIQUE(name, iso_639_1) |
| 76 | + ); |
| 77 | +
|
72 | 78 | CREATE TABLE IF NOT EXISTS settings (
|
73 | 79 | settings_id INTEGER PRIMARY KEY,
|
74 | 80 | user TEXT
|
75 |
| - ) |
| 81 | + ); |
76 | 82 |
|
77 | 83 | ")
|
78 | 84 |
|
|
103 | 109 | js->clj
|
104 | 110 | (get "version")))
|
105 | 111 |
|
106 |
| - |
107 | 112 | (defn read-sample-file
|
108 | 113 | [name]
|
109 | 114 | (-> (.readFileSync ^:export fs (.join path js/__dirname ".." "test/sample_texts/" name) "utf8")))
|
110 | 115 |
|
| 116 | +;; -- DB: Langs ---------------------------------------------------------------- |
| 117 | +;; |
| 118 | + |
| 119 | +(defn langs-init |
| 120 | + "Create the starting languages for the database, if they don't exist yet." |
| 121 | + [] |
| 122 | + (let [langs (sql {:op :get :stmt "SELECT * FROM languages" :params []})] |
| 123 | + (when (= (count langs) 0) |
| 124 | + (doseq [[lang lang-map] specs/langs-db] |
| 125 | + (sql {:op :run |
| 126 | + :params (assoc lang-map :id nil) |
| 127 | + :stmt "INSERT INTO languages VALUES (@id, @name, @iso_639_1, @text_splitting_regex, @word_regex)"}))))) |
| 128 | + |
| 129 | +(defn langs-all |
| 130 | + [] |
| 131 | + (sql {:op :all :params [] :stmt "SELECT * FROM languages;"})) |
| 132 | + |
| 133 | +(defn lang-get-by-code |
| 134 | + [lang-code] |
| 135 | + (sql {:op :get :params [lang-code] :stmt "SELECT * FROM languages where iso_639_1 = ?;"})) |
111 | 136 |
|
| 137 | +(defn lang-get-text-split-regex |
| 138 | + [lang-code] |
| 139 | + (get (lang-get-by-code lang-code) :text_splitting_regex)) |
| 140 | + |
| 141 | +(defn lang-get-word-regex |
| 142 | + [lang-code] |
| 143 | + (get (lang-get-by-code lang-code) :word_regex)) |
| 144 | + |
| 145 | +(defn lang-update |
| 146 | + "Takes a new language and updates it in the database, |
| 147 | + then finds all texts (articles) in this language, and resplits and inserts |
| 148 | + words based on how it splits up." |
| 149 | + [language] |
| 150 | + (sql {:op :run |
| 151 | + :stmt "UPDATE languages SET word_regex=@word_regex, text_splitting_regex=@text_splitting_regex WHERE id = @id" |
| 152 | + :params (select-keys language [:word_regex :text_splitting_regex :id])}) |
| 153 | + (lang-get-by-code (language :iso_639_1))) |
| 154 | + |
| 155 | +(defn lang-create |
| 156 | + "Allows user's to add their own language." |
| 157 | + [lang-form-data] |
| 158 | + (let [form-data (select-keys lang-form-data [:name :iso_639_1 :word_regex :text_splitting_regex]) |
| 159 | + form-data (assoc form-data :id nil)] |
| 160 | + (sql {:op :run |
| 161 | + :params form-data |
| 162 | + :stmt "INSERT INTO languages VALUES(@id, @name, @iso_639_1, @word_regex, @text_splitting_regex)"}))) |
| 163 | + |
| 164 | +(defn lang-delete |
| 165 | + [lang] |
| 166 | + (let [id (lang :id)] |
| 167 | + (sql {:op :run |
| 168 | + :params [id] |
| 169 | + :stmt "DELETE FROM languages WHERE id=?"}))) |
112 | 170 |
|
113 | 171 | ;; -- DB: Settings -------------------------------------------------------------
|
114 | 172 | ;; Settings are handled in JSON. For better or worse: ¯\_(ツ)_/¯
|
115 | 173 | ;; All settings updates/inserts need to be jsonified.
|
116 | 174 |
|
117 |
| - |
118 | 175 | (defn- settings->json
|
119 | 176 | [s]
|
120 | 177 | (-> s clj->js js/JSON.stringify))
|
|
133 | 190 | sometimes handle side-effectful things, so we diff the new settings
|
134 | 191 | against the old, and run 'hooks' based on what changed."
|
135 | 192 | [new-settings-from-fe]
|
136 |
| - (let [old-settings (settings-get) |
| 193 | + (let [old-settings (settings-get) |
137 | 194 | ;; normalize new-settings to look like it came from the db before we diff it.
|
138 |
| - new-settings (-> new-settings-from-fe settings->json settings->edn) |
139 |
| - [old-diff new-diff both] (data/diff new-settings old-settings)] |
| 195 | + new-settings (-> new-settings-from-fe settings->json settings->edn) |
| 196 | + [_ new-diff _] (data/diff new-settings old-settings)] |
140 | 197 | (cond
|
141 | 198 | (contains? new-diff "page-size")
|
142 | 199 | (sql {:op :run :params [] :stmt "UPDATE articles SET current_page = 0"})
|
143 | 200 |
|
144 |
| - :else nil |
145 |
| - ))) |
| 201 | + :else nil))) |
146 | 202 |
|
147 | 203 | (defn settings-update
|
148 | 204 | [settings]
|
149 |
| - (settings-hook! settings) |
150 |
| - (sql {:op :run |
151 |
| - :stmt "UPDATE settings SET user = ? WHERE settings_id = 1" |
152 |
| - :params [(settings->json settings)]})) |
| 205 | + (settings-hook! settings) |
| 206 | + (sql {:op :run |
| 207 | + :stmt "UPDATE settings SET user = ? WHERE settings_id = 1" |
| 208 | + :params [(settings->json settings)]})) |
153 | 209 |
|
154 | 210 | (defn settings-init
|
155 | 211 | "If there are no rows in the settings table, initialize it."
|
|
205 | 261 | :params [word-id]
|
206 | 262 | :op :get})]
|
207 | 263 | (swap! words-out conj res)))
|
208 |
| - (assoc article :word-data @words-out :total-pages (js/Math.ceil total-pages))) |
209 |
| - ) |
| 264 | + (assoc article :word-data @words-out :total-pages (js/Math.ceil total-pages)))) |
210 | 265 |
|
211 | 266 | (defn article-insert
|
212 | 267 | "Creates a new article. Requirements:
|
213 | 268 | -> words for article are already in words table.
|
214 |
| - -> words from article have been re-queries |
215 |
| - -> requiriesed words' ids have been made into a delimited string with $." |
| 269 | + -> words from article have been queried |
| 270 | + -> queried words' ids have been made into a delimited string with $." |
216 | 271 | [{:keys [article title source word_ids language]}]
|
217 | 272 | (sql {:op :run
|
218 | 273 | :params [article word_ids title source (js/Date.now) language]
|
|
264 | 319 | "Before inserting an article, we need to get the id for each word in the db
|
265 | 320 | then we can build a delimited string that will get stored under the `word_ids` column"
|
266 | 321 | [{:keys [article language]}]
|
267 |
| - (let [article-str-vec (u/split-article article) |
| 322 | + (let [split-regex (lang-get-text-split-regex language) |
| 323 | + article-str-vec (u/split-article article split-regex) |
268 | 324 | word-ids (atom [])
|
269 | 325 | query "SELECT id FROM words WHERE slug = ? AND name = ? AND language = ?"]
|
270 | 326 | (doseq [word article-str-vec]
|
|
280 | 336 | The sql placeholder is a string with many question marks because we are doing a bulk insert?
|
281 | 337 | "
|
282 | 338 | [{:keys [article language]}]
|
283 |
| - (let [words (u/split-article article) |
| 339 | + (let [split-regex (lang-get-text-split-regex language) |
| 340 | + word-regex (lang-get-word-regex language) |
| 341 | + words (u/split-article article split-regex) |
284 | 342 | placeholders (str/join ", " (map (fn [_] "(?, ?, ?, ?)") words)) ;; this is annoying
|
285 | 343 | params (->> words
|
286 | 344 | (map (fn [word]
|
287 | 345 | ;; THIS is the param list!
|
288 | 346 | (vector
|
289 | 347 | word
|
290 | 348 | (u/slug-word word)
|
291 |
| - (bool->int (not (u/word? word))) |
| 349 | + (bool->int (not (u/word? word word-regex))) |
292 | 350 | language)))
|
293 | 351 | flatten
|
294 | 352 | (apply array))
|
|
302 | 360 | (sql {:op :run
|
303 | 361 | :params phrase
|
304 | 362 | :stmt "INSERT INTO phrases VALUES (@id, @word_ids, @name, @slug, @comfort, @translation, @first_word_slug, @last_word_slug, @language)
|
305 |
| - ON CONFLICT(name, language) DO UPDATE SET comfort=@comfort, name=@name, translation=@translation" |
306 |
| - })) |
| 363 | + ON CONFLICT(name, language) DO UPDATE SET comfort=@comfort, name=@name, translation=@translation"})) |
307 | 364 |
|
308 | 365 | (defn phrase-get
|
309 | 366 | [id]
|
|
343 | 400 | article
|
344 | 401 | (assoc article :word-data @new-word-data))))
|
345 | 402 |
|
346 |
| - |
347 |
| - |
348 | 403 | ;; ----------------------------------------------------------------------------------------
|
349 | 404 | ;; Seed fns
|
350 | 405 |
|
351 |
| - |
352 | 406 | (defn seed-article
|
353 | 407 | []
|
354 |
| - (let [ |
355 |
| - data {:article (read-sample-file "fr_compte2.txt") :title "Compte, Ch 2", :source "..", :language "fr"} |
| 408 | + (let [data {:article (read-sample-file "fr_compte2.txt") :title "Compte, Ch 2", :source "..", :language "fr"} |
356 | 409 | _ (words-insert data)
|
357 | 410 | word-ids-str (words-get-ids-for-article data)
|
358 |
| - inserted-article (article-insert (merge data {:word_ids word-ids-str}))]) |
359 |
| - ) |
| 411 | + inserted-article (article-insert (merge data {:word_ids word-ids-str}))])) |
360 | 412 |
|
361 | 413 | (defn run-seeds
|
362 | 414 |
|
|
368 | 420 | (when no-content-yet
|
369 | 421 | (seed-article))))
|
370 | 422 |
|
371 |
| - |
372 | 423 | ;; Wipe / Init -----------------------------------------------------------------------------
|
373 | 424 |
|
374 | 425 | (defn wipe!
|
|
377 | 428 | (.exec db "DROP TABLE words;
|
378 | 429 | DROP TABLE articles;
|
379 | 430 | DROP TABLE phrases;
|
| 431 | + DROP TABLE languages; |
380 | 432 | DROP TABLE settings;" #(println %))
|
381 | 433 | (.relaunch app)
|
382 |
| - (.quit app) |
383 |
| - ) |
384 |
| - |
| 434 | + (.quit app)) |
385 | 435 |
|
386 | 436 | (defn init
|
387 | 437 | []
|
|
390 | 440 | (when err
|
391 | 441 | (throw (js/Error. (str "Failed db" err))))))
|
392 | 442 | (settings-init)
|
393 |
| - (when u/debug? (run-seeds)) |
394 |
| - ) |
| 443 | + (langs-init) |
| 444 | + |
| 445 | + (when u/debug? (run-seeds))) |
395 | 446 |
|
396 | 447 | ;; FIXME: when do I run "db.close()"?
|
0 commit comments