Last active
February 16, 2021 17:58
-
-
Save realgenekim/3fb9d2dccde9234743ec392b600055be to your computer and use it in GitHub Desktop.
Example of getting UTF-8 working with next.jdbc with MySQL, to enable Unicode and emojis, along with using using "REPLACE" instead of "INSERT" to enable upserts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns db.db2 | |
(:require | |
[next.jdbc :as j] | |
[next.jdbc.sql :as jsql] | |
[next.jdbc.sql.builder :as jb] | |
[clojure.spec.alpha :as s])) | |
(def mysql-db {:dbtype "mysql" | |
:dbname "twitter" | |
:host "127.0.0.1" | |
:user "xxx" | |
:password "xxx" | |
:useUnicode "yes" | |
:characterEncoding "UTF-8"}) | |
(def ds (j/get-datasource mysql-db)) | |
(defn insert-tweets-with-conn! | |
[conn tweets] | |
(let [ks [:id :tweet :hash256] | |
rows (map #((apply juxt ks) %) tweets) | |
;_ (println rows) | |
q (jb/for-insert-multi :tweets ks rows {}) | |
;_ (println q) | |
qr (clojure.string/replace (first q) #"^INSERT" "REPLACE") | |
;_ (println qr) | |
qnew (assoc-in q [0] qr) | |
_ (println qnew)] | |
(j/execute! conn qnew))) | |
(def insert-tweets! (partial insert-tweets-with-conn! ds)) | |
(commment | |
(insert-tweets! [{:id 1235 :tweet "abc🎉" :hash256 "abcxxx"}]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@seancorfield also wrote — I think this is what every dev (and DBA) should know about modern realities of strings and their storage in databases