Skip to content

Use locale agnostic clojure.string/lower-case #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/toucan/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
(model-symb->ns 'CardFavorite) -> 'my-project.models.card-favorite"
[symb]
{:pre [(symbol? symb)]}
(symbol (str (models/root-namespace) \. (s/lower-case (s/replace (name symb) #"([a-z])([A-Z])" "$1-$2")))))
(symbol (str (models/root-namespace) \. (u/lower-case (s/replace (name symb) #"([a-z])([A-Z])" "$1-$2")))))

(defn- resolve-model-from-symbol
"Resolve the model associated with SYMB, calling `require` on its namespace if needed.
Expand Down Expand Up @@ -271,7 +271,11 @@
"Compile `honeysql-from` and call `jdbc/query` against the application database. Options are passed along to
`jdbc/query`."
[honeysql-form & {:as options}]
(jdbc/query (connection) (honeysql->sql honeysql-form) options))
(jdbc/query (connection)
(honeysql->sql honeysql-form)
;; FIXME: This has already been fixed in `clojure.java.jdbc`, so
;; this option can be removed when using >= 0.7.10.
(into options {:identifiers u/lower-case})))

(defn reducible-query
"Compile `honeysql-from` and call `jdbc/reducible-query` against the application database. Options are passed along
Expand Down
11 changes: 10 additions & 1 deletion src/toucan/util.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns toucan.util
"Utility functions used by other Toucan modules."
(:require [clojure.string :as s]))
(:require [clojure.string :as s])
(:import java.util.Locale))

(defn keyword->qualified-name
"Return keyword K as a string, including its namespace, if any (unlike `name`).
Expand All @@ -9,3 +10,11 @@
[k]
(when k
(s/replace (str k) #"^:" "")))

(defn lower-case
"Locale-agnostic version of `clojure.string/lower-case`.
`clojure.string/lower-case` uses the default locale in conversions, turning
`ID` into `ıd`, in the Turkish locale. This function always uses the
`Locale/US` locale."
[^CharSequence s]
(.. s toString (toLowerCase (Locale/US))))
23 changes: 21 additions & 2 deletions test/toucan/db_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns toucan.db-test
(:require [expectations :refer :all]
(:require [clojure.java.jdbc :as jdbc]
[expectations :refer :all]
[toucan
[db :as db]
[test-setup :as test]]
Expand All @@ -8,7 +9,8 @@
[category :refer [Category]]
[phone-number :refer [PhoneNumber]]
[user :refer [User]]
[venue :refer [Venue]]]))
[venue :refer [Venue]]])
(:import java.util.Locale))

;; Test overriding quoting-style
(expect
Expand Down Expand Up @@ -162,6 +164,23 @@
:order-by [:id]
:limit 1}))

;; Test that identifiers are correctly lower cased in Turkish locale (#59)
(expect
:id
(let [connection (db/connection)
original-locale (Locale/getDefault)]
(try
(Locale/setDefault (Locale/forLanguageTag "tr"))
(jdbc/execute! connection "DROP TABLE IF EXISTS heroes")
(jdbc/execute! connection "CREATE TABLE heroes (\"ID\" SERIAL PRIMARY KEY, \"NAME\" VARCHAR(256))")
(jdbc/execute! connection "INSERT INTO heroes (\"NAME\") VALUES ('Batman')")
(let [first-row (first (db/query {:select [:ID] :from [:heroes]}))]
;; If `db/query` (jdbc) uses `clojure.string/lower-case`, `:ID` will be converted to `:ıd` in Turkish locale
(first (keys first-row)))
(finally
(jdbc/execute! connection "DROP TABLE IF EXISTS heroes")
(Locale/setDefault original-locale)))))

(defn- transduce-to-set
"Process `reducible-query-result` using a transducer that puts the rows from the resultset into a set"
[reducible-query-result]
Expand Down