next-jdbc/src/next/jdbc/quoted.clj

39 lines
1 KiB
Clojure
Raw Normal View History

2023-03-31 19:36:29 +00:00
;; copyright (c) 2019-2023 Sean Corfield, all rights reserved
2019-04-01 00:29:21 +00:00
(ns next.jdbc.quoted
2019-04-21 23:13:52 +00:00
"Provides functions for use with the `:table-fn` and `:column-fn` options
that define how SQL entities should be quoted in strings constructed
from Clojure data."
(:require [clojure.string :as str]))
2019-04-01 00:29:21 +00:00
(set! *warn-on-reflection* true)
2023-03-31 19:36:29 +00:00
(defn strop
"Escape any embedded closing strop characters."
[s x e]
(str s (str/replace x (str e) (str e e)) e))
2019-04-01 00:29:21 +00:00
2023-03-31 19:36:29 +00:00
(defn ansi "ANSI \"quoting\"" [s] (strop \" s \"))
2019-04-01 00:29:21 +00:00
2023-03-31 19:36:29 +00:00
(defn mysql "MySQL `quoting`" [s] (strop \` s \`))
(defn sql-server "SQL Server [quoting]" [s] (strop \[ s \]))
2019-04-01 00:29:21 +00:00
(def oracle "Oracle \"quoting\" (ANSI)" ansi)
(def postgres "PostgreSQL \"quoting\" (ANSI)" ansi)
(defn schema
"Given a quoting function, return a new quoting function that will
process schema-qualified names by quoting each segment:
2019-04-21 23:13:52 +00:00
```clojure
(mysql (name :foo.bar)) ;=> `foo.bar`
((schema mysql) (name :foo.bar)) ;=> `foo`.`bar`
```
"
[quoting]
(fn [s]
(->> (str/split s #"\.")
(map quoting)
(str/join "."))))