From ac136dab08f1725eab564be03b83145c23ea5b25 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 09:58:01 +0300 Subject: [PATCH 1/7] Pass strings to strop Converting Character->String involves additional allocations. Passing literal strings avoids that. --- src/honey/sql.cljc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index b9c0f53..ad9c24e 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -110,16 +110,16 @@ (reduce-kv (fn [m k v] (assoc m k (assoc v :dialect k))) {} - {:ansi {:quote #(strop \" % \")} - :sqlserver {:quote #(strop \[ % \])} - :mysql {:quote #(strop \` % \`) + {:ansi {:quote #(strop "\"" % "\"")} + :sqlserver {:quote #(strop "[" % "]")} + :mysql {:quote #(strop "`" % "`") :clause-order-fn #(add-clause-before % :set :where)} - :nrql {:quote #(strop \` % \`) + :nrql {:quote #(strop "`" % "`") :col-fn #(if (keyword? %) (subs (str %) 1) (str %)) :parts-fn vector} - :oracle {:quote #(strop \" % \") :as false} - :xtdb {:quote #(strop \" % \") + :oracle {:quote #(strop "\"" % "\"") :as false} + :xtdb {:quote #(strop "\"" % "\"") :col-fn #(if (keyword? %) (subs (str %) 1) (str %)) :parts-fn #(str/split % #"\.")}}))) From 803584dc7c4310c5ab16b097aa2536e391ed2262 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 09:58:54 +0300 Subject: [PATCH 2/7] More efficient check for suspicious entities re-find sets up the regex machinery which is unnecessary here. str/includes? of a single character is more efficient. --- src/honey/sql.cljc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index ad9c24e..c390feb 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -161,8 +161,8 @@ (def ^:no-doc ^:dynamic *escape-?* true) ;; suspicious entity names: -(def ^:private suspicious #";") -(defn- suspicious? [s] (boolean (re-find suspicious s))) +(def ^:private suspicious ";") +(defn- suspicious? [s] (str/includes? s suspicious)) (defn- suspicious-entity-check [entity] (when-not *allow-suspicious-entities* (when (suspicious? entity) From bfd7eb2141a650ace5b1a181c94b69abf7033177 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 09:59:47 +0300 Subject: [PATCH 3/7] Use non-capturing match group in alphanumeric regex If the group is non-capturing, Clojure will not form match groups with `re-groups`. We only use this regex as a predicate and don't need those. --- src/honey/sql.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index c390feb..23326ea 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -272,7 +272,7 @@ * the whole entity is numeric (with optional underscores), or * the first character is alphabetic (or underscore) and the rest is alphanumeric (or underscore)." - #"^([0-9_]+|[A-Za-z_][A-Za-z0-9_]*)$") + #"^(?:[0-9_]+|[A-Za-z_][A-Za-z0-9_]*)$") (defn format-entity "Given a simple SQL entity (a keyword or symbol -- or string), From 15f73f9442cc63dfad55e5f0cce1f6d463e51077 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 11:19:17 +0300 Subject: [PATCH 4/7] Replace first character checks with starts-with? --- src/honey/sql.cljc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 23326ea..bfdb902 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -280,7 +280,7 @@ Handles quoting, splitting at / or ., replacing - with _ etc." [e & [{:keys [aliased drop-ns]}]] - (let [e (if (and aliased (keyword? e) (= \' (first (name e)))) + (let [e (if (and aliased (keyword? e) (str/starts-with? (name e) "'")) ;; #497 quoted alias support (should behave like string) (subs (name e) 1) e) @@ -350,7 +350,7 @@ (let [n (cond-> (name k) *escape-?* (str/replace "?" "??"))] - (if (= \' (first n)) + (if (str/starts-with? n "'") (let [ident (subs n 1) ident-l (str/lower-case ident)] (binding [*quoted* (when-not (contains? #{"array"} ident-l) *quoted*)] @@ -419,13 +419,16 @@ ;; rather than name/namespace, we want to allow ;; for multiple / in the %fun.call case so that ;; qualified column names can be used: - (let [c (cond-> (str x) (keyword? x) (subs 1))] - (cond (= \% (first c)) + (let [c (if (keyword? x) + #?(:clj (str (.sym ^clojure.lang.Keyword x)) ;; Omits leading colon + :default (subs (str x) 1)) + (str x))] + (cond (str/starts-with? c "%") (let [[f & args] (str/split (subs c 1) #"\.")] [(str (format-fn-name f) "(" (join ", " (map #(format-entity (keyword %) opts)) args) ")")]) - (= \? (first c)) + (str/starts-with? c "?") (let [k (keyword (subs c 1))] (cond *inline* [(sqlize-value (param-value k))] @@ -433,7 +436,7 @@ (->numbered-param k) :else ["?" (->param k)])) - (= \' (first c)) + (str/starts-with? c "'") (do (reset! *formatted-column* true) [(subs c 1)]) From 18a511b1c9f6a30e14ed63b292fa7d0fafbe8ddc Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 12:53:40 +0300 Subject: [PATCH 5/7] Add transducer arity to reduce-sql --- src/honey/sql.cljc | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index bfdb902..f5ca8c3 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -659,11 +659,16 @@ :else (format-expr x))) -(defn- reduce-sql [xs] - (reduce (fn [[sql params] [sql' & params']] - [(conj sql sql') (if params' (into params params') params)]) - [[] []] - xs)) +(defn- reduce-sql + ([xs] (reduce-sql identity xs)) + ([xform xs] + (transduce xform + (fn + ([res] res) + ([[sql params] [sql' & params']] + [(conj sql sql') (if params' (into params params') params)])) + [[] []] + xs))) ;; primary clauses @@ -733,7 +738,7 @@ (throw (ex-info (str "format-expr-list expects a sequence of expressions, found: " (type exprs)) {:exprs exprs}))) - (reduce-sql (map #(format-expr % opts) exprs))) + (reduce-sql (map #(format-expr % opts)) exprs)) (comment (format-expr-list :?tags) @@ -753,7 +758,7 @@ (cond-> prefix qualifier (str " " qualifier)) qualifier)] (if (sequential? xs) - (let [[sqls params] (reduce-sql (map #(format-selectable-dsl % {:as as}) xs))] + (let [[sqls params] (reduce-sql (map #(format-selectable-dsl % {:as as})) xs)] (when-not (= :none *checking*) (when (empty? xs) (throw (ex-info (str prefix " empty column list is illegal") @@ -851,8 +856,8 @@ ;; according to docs, CTE should _always_ be wrapped: (cond-> [(str sql " " (as-fn with) " " (str "(" sql' ")"))] params (into params) - params' (into params'))))) - xs))] + params' (into params')))))) + xs)] (into [(str (sql-kw k) " " (join ", " sqls))] params))) (defn- format-selector [k xs] @@ -880,7 +885,7 @@ [table]) [sql & params] (format-dsl statement) [t-sql & t-params] (format-entity-alias table) - [c-sqls c-params] (reduce-sql (map #'format-entity-alias cols))] + [c-sqls c-params] (reduce-sql (map #'format-entity-alias) cols)] (-> [(str (sql-kw k) " " t-sql " " (cond (seq cols) @@ -898,7 +903,7 @@ (sequential? (second table)) (let [[table cols] table [t-sql & t-params] (format-entity-alias table) - [c-sqls c-params] (reduce-sql (map #'format-entity-alias cols))] + [c-sqls c-params] (reduce-sql (map #'format-entity-alias) cols)] (-> [(str (sql-kw k) " " t-sql " (" (join ", " c-sqls) @@ -1974,7 +1979,7 @@ :else ; args is empty and not a special case []) [sqls params] - (reduce-sql (map #(format-expr % {:nested true}) args))] + (reduce-sql (map #(format-expr % {:nested true})) args)] (when-not (pos? (count sqls)) (throw (ex-info (str "no operands found for " op') {:expr expr}))) From 4c75db9a95f504e3ff8350a80a19697efe26b30b Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 12:57:44 +0300 Subject: [PATCH 6/7] Miscellaneous improvements --- src/honey/sql.cljc | 56 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index f5ca8c3..b8a1411 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -691,7 +691,9 @@ * [:overlay :foo :*placing :?subs :*from 3 :*for 4] * [:trim :*leading-from :bar]" [args & [opts]] - (loop [exprs (map #(format-expr % opts) (remove inline-kw? args)) + (loop [exprs (keep #(when-not (inline-kw? %) + (format-expr % opts)) + args) args args prev-in false result []] @@ -933,29 +935,33 @@ (defn- format-join [k clauses] (let [[sqls params] - (reduce (fn [[sqls params] [j e]] - (let [[sql-j & params-j] - (format-selects-common - (sql-kw (if (= :join k) :inner-join k)) - true - [j]) - sqls (conj sqls sql-j)] - (if (and (sequential? e) (= :using (first e))) - (let [[u-sqls u-params] - (reduce-sql (map #'format-entity-alias (rest e)))] - [(conj sqls - "USING" - (str "(" - (join ", " u-sqls) - ")")) - (-> params (into params-j) (into u-params))]) - (let [[sql & params'] (when e (format-expr e))] - [(cond-> sqls e (conj "ON" sql)) - (-> params - (into params-j) - (into params'))])))) - [[] []] - (partition-all 2 clauses))] + (transduce + (partition-all 2) + (fn + ([res] res) + ([[sqls params] [j e]] + (let [[sql-j & params-j] + (format-selects-common + (sql-kw (if (= :join k) :inner-join k)) + true + [j]) + sqls (conj sqls sql-j)] + (if (and (sequential? e) (= :using (first e))) + (let [[u-sqls u-params] + (reduce-sql (map #'format-entity-alias) (rest e))] + [(conj sqls + "USING" + (str "(" + (join ", " u-sqls) + ")")) + (-> params (into params-j) (into u-params))]) + (let [[sql & params'] (when e (format-expr e))] + [(cond-> sqls e (conj "ON" sql)) + (-> params + (into params-j) + (into params'))]))))) + [[] []] + clauses)] (into [(join " " sqls)] params))) (def ^:private join-by-aliases @@ -1969,7 +1975,7 @@ (defn- format-infix-expr [op' op expr nested] (let [args (cond->> (rest expr) (contains? @op-ignore-nil op) - (remove nil?)) + (filterv some?)) args (cond (seq args) args (= :and op) From 2fb4df6bdda734b8e8f25ab526f47027ddffa4e6 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Fri, 27 Sep 2024 13:06:40 +0300 Subject: [PATCH 7/7] Remove misplaced type hint --- src/honey/sql/util.cljc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/honey/sql/util.cljc b/src/honey/sql/util.cljc index 8572319..1e621ad 100644 --- a/src/honey/sql/util.cljc +++ b/src/honey/sql/util.cljc @@ -7,7 +7,6 @@ "More efficient implementation of `clojure.core/str` because it has more non-variadic arities. Optimization is Clojure-only, on other platforms it reverts back to `clojure.core/str`." - {:tag String} (^String [] "") (^String [^Object a] #?(:clj (if (nil? a) "" (.toString a))