Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Improve unicode support for MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jorinvo committed Nov 16, 2019
1 parent 79a2ac6 commit 44aa690
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Let users manually insert data using Google Sheets while having the power of all

googlesheets-sql-sync uses [JDBC](https://github.com/clojure/java.jdbc) and bundles the PostgreSQL and MySQL drivers.
Additional drivers can be added any time.
If you would like to add support for SQLite or any other SQL database, open an issue and it can probably be added in no time.
If you also would like to have support for SQLite, open an issue and it can probably be added in no time.


## Assumptions and simplifications
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject googlesheets-sql-sync "0.6.0"
(defproject googlesheets-sql-sync "0.6.1"
:description "Keep your SQL database in sync with Google Sheets"
:url "https://github.com/jorinvo/googlesheets-sql-sync"
:license {:name "MIT"
Expand Down
20 changes: 15 additions & 5 deletions src/googlesheets_sql_sync/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
(comment
(escape "hey \"you\"!" "\""))

(defn- create-table [db headers table]
(defn- create-table [db headers table is-mysql]
(log/info "Creating table")
(let [cols (->> headers
(map #(str % " text"))
(string/join ", "))
s (str "create table " table " ( " cols " )")]
s (str "create table " table " ( " cols " )" (when is-mysql " character set=utf8mb4"))]
(jdbc/execute! db s)))

(defn- get-headers-or-drop
Expand Down Expand Up @@ -85,10 +85,20 @@
(log/info "Writing" (count rows) "rows to table")
(jdbc/insert-multi! db table headers rows))

(defn- get-db-config [config target]
(let [db (get-in config [:targets (keyword target)])
dbtype (keyword (:dbtype db))]
(if (= dbtype :mysql)
(merge {"useUnicode" "yes"
"characterEncoding" "UTF-8"}
db)
db)))

(defn update-table [config sheet]
(let [target (-> sheet :sheet :target)
db (get-in config [:targets (keyword target)])
identifier-quoting (identifier-quoting-map (keyword (:dbtype db)))
db (get-db-config config target)
dbtype (keyword (:dbtype db))
identifier-quoting (identifier-quoting-map dbtype)
table (-> sheet :sheet :table)
rows (:rows sheet)
headers (->> rows
Expand All @@ -106,7 +116,7 @@
(do
(check-header-conflicts headers old-headers)
(clear-table db table))
(create-table db escaped-headers table))
(create-table db escaped-headers table (= dbtype :mysql)))
(write-rows db table escaped-headers data)
(catch Exception e (throw-db-err target table e))))
sheet)

0 comments on commit 44aa690

Please sign in to comment.