From 3ce47b92f2acb391d0af9108c4a5609d5422b394 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 23 Sep 2020 22:52:57 -0700 Subject: [PATCH] Implement :inline syntax; allow select call without alias --- src/honey/sql.cljc | 45 +++++++++++++++++++++++++++------------- test/honey/sql_test.cljc | 9 ++++---- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 9cfcb66..8d12e5c 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -101,13 +101,15 @@ (defn- format-entity-alias [x] (cond (sequential? x) - (str (let [s (first x)] - (if (map? s) - (throw (ex-info "selectable cannot be statement!" - {:selectable s})) - (format-entity s))) - #_" AS " " " - (format-entity (second x) {:aliased? true})) + (let [s (first x) + pair? (< 1 (count x))] + (when (map? s) + (throw (ex-info "selectable cannot be statement!" + {:selectable s}))) + (cond-> (format-entity s) + pair? + (str #_" AS " " " + (format-entity (second x) {:aliased? true})))) :else (format-entity x))) @@ -117,16 +119,20 @@ (format-dsl x {:nested? true}) (sequential? x) - (let [s (first x) - a (second x) + (let [s (first x) + pair? (< 1 (count x)) + a (second x) [sql & params] (if (map? s) (format-dsl s {:nested? true}) (format-expr s)) - [sql' & params'] (if (sequential? a) - (let [[sql params] (format-expr-list a {:aliased? true})] - (into [(str/join " " sql)] params)) - (format-selectable-dsl a {:aliased? true}))] - (-> [(str sql (if as? " AS " " ") sql')] + [sql' & params'] (when pair? + (if (sequential? a) + (let [[sql params] (format-expr-list a {:aliased? true})] + (into [(str/join " " sql)] params)) + (format-selectable-dsl a {:aliased? true})))] + (-> [(cond-> sql + pair? + (str (if as? " AS " " ") sql'))] (into params) (into params'))) @@ -401,6 +407,14 @@ (into (vals infix-aliases)) (->> (into #{} (map keyword))))) +(defn- sqlize-value [x] + (cond + (nil? x) "NULL" + (string? x) x ; I feel this should be 'single-quoted' but 1.x does not + (symbol? x) (name x) + (keyword? x) (name x) + :else (str x))) + (def ^:private special-syntax {:array (fn [[arr]] @@ -419,6 +433,9 @@ (fn [[x type]] (let [[sql & params] (format-expr x)] (into [(str "CAST(" sql " AS " (sql-kw type) ")")] params))) + :inline + (fn [[x]] + [(sqlize-value x)]) :interval (fn [[n units]] (let [[sql & params] (format-expr n)] diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index ce0f1d2..3f290e3 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -291,11 +291,10 @@ (format {:dialect :mysql}))))) (deftest inlined-values-are-stringified-correctly - (is nil "inline unimplemented") - #_(is (= ["SELECT foo, bar, NULL"] - (format {:select [(honeysql.core/inline "foo") - (honeysql.core/inline :bar) - (honeysql.core/inline nil)]})))) + (is (= ["SELECT foo, bar, NULL"] + (format {:select [[[:inline "foo"]] + [[:inline :bar]] + [[:inline nil]]]})))) ;; Make sure if Locale is Turkish we're not generating queries like İNNER JOIN (dot over the I) because ;; `string/upper-case` is converting things to upper-case using the default Locale. Generated query should be the same