diff --git a/CHANGELOG.md b/CHANGELOG.md index a5d0cce..99728b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changes +* 2.4.next in progress + * Address [#456](https://github.com/seancorfield/honeysql/issues/456) by allowing `format` to handling expressions (via `format-expr`) as well as statements (via `format-dsl` per previous versions). + * 2.4.969 -- 2023-01-14 * Fix [#454](https://github.com/seancorfield/honeysql/issues/454) by allowing `-` to be variadic. * Address [#452](https://github.com/seancorfield/honeysql/pull/452) by adding `:replace-into` to the core SQL supported, instead of just for the MySQL and SQLite dialects (so the latter is not needed yet). diff --git a/build.clj b/build.clj index 0af8ef9..3b27a23 100644 --- a/build.clj +++ b/build.clj @@ -34,7 +34,7 @@ :main 'clojure.main :main-args (:main-opts combined)}) {:keys [exit]} (b/process cmds)] - (when-not (zero? exit) (throw "Task failed")))) + (when-not (zero? exit) (throw (ex-info "Task failed" {}))))) (defn eastwood "Run Eastwood." [opts] (run-task [:eastwood]) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index c522c13..25f2a00 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -1655,18 +1655,23 @@ This is the primary API for HoneySQL and handles dialects, quoting, and named parameters. + If the data DSL is a hash map, it will be treated as a SQL statement + and formatted via `format-dsl`, otherwise it will be treated as a SQL + expression and formatted via `format-expr`. + `format` accepts options as either a single hash map argument or as named arguments (alternating keys and values). If you are using Clojure 1.11 (or later) you can mix'n'match, providing some options as named arguments followed by other options in a hash map." ([data] (format data {})) ([data opts] - (let [cache (:cache opts) - dialect? (contains? opts :dialect) - dialect (when dialect? (get @dialects (check-dialect (:dialect opts)))) - numbered (if (contains? opts :numbered) - (:numbered opts) - @default-numbered)] + (let [cache (:cache opts) + dialect? (contains? opts :dialect) + dialect (when dialect? (get @dialects (check-dialect (:dialect opts)))) + numbered (if (contains? opts :numbered) + (:numbered opts) + @default-numbered) + formatter (if (map? data) #'format-dsl #'format-expr)] (binding [*dialect* (if dialect? dialect @default-dialect) *caching* cache *checking* (if (contains? opts :checking) @@ -1694,9 +1699,9 @@ *params* (:params opts) *values-default-columns* (:values-default-columns opts)] (if cache - (->> (through-opts opts cache data (fn [_] (format-dsl data (dissoc opts :cache)))) + (->> (through-opts opts cache data (fn [_] (formatter data (dissoc opts :cache)))) (mapv #(unwrap % opts))) - (mapv #(unwrap % opts) (format-dsl data opts)))))) + (mapv #(unwrap % opts) (formatter data opts)))))) ([data k v & {:as opts}] (format data (assoc opts k v)))) (defn set-dialect! diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index bd60621..39e6426 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -1058,3 +1058,7 @@ ORDER BY id = ? DESC (deftest issue-434-case-quoting (is (= ["SELECT ARRAY (SELECT \"oid\" FROM \"pg_proc\" WHERE \"proname\" LIKE 'bytea%')"] (sut/format {:select [[[:'ARRAY {:select :oid :from :pg_proc :where [:like :proname [:inline "bytea%"]]}]]]} :quoted true)))) + +(deftest issue-456-format-expr + (is (= ["`x` + ?" 1] + (sut/format [:+ :x 1] {:dialect :mysql}))))