fix #513 with :ignored-metadata option

This commit is contained in:
Sean Corfield 2023-12-02 11:47:43 -08:00
parent f46dbc5ca7
commit 9da2ccc812
4 changed files with 24 additions and 4 deletions

View file

@ -1,8 +1,8 @@
# Changes # Changes
* 2.5.next in progress * 2.5.next in progress
* Review metadata -> SQL logic?
* Address [#515](https://github.com/seancorfield/honeysql/issues/515) in part by quoting entities that start with a digit but are otherwise alphanumeric. Note that entities that are all digits (optionally including underscores) will still not be quoted as in previous releases. * Address [#515](https://github.com/seancorfield/honeysql/issues/515) in part by quoting entities that start with a digit but are otherwise alphanumeric. Note that entities that are all digits (optionally including underscores) will still not be quoted as in previous releases.
* Address [#513](https://github.com/seancorfield/honeysql/issues/513) by ignoring `:file`, `:line`, `:column`, `:end-line`, and `:end-column` metadata keys and providing an `:ignored-metadata` option to allow additional keys to be ignored.
* 2.5.1091 -- 2023-10-28 * 2.5.1091 -- 2023-10-28
* Address [#512](https://github.com/seancorfield/honeysql/issues/512) by adding support for subqueries in the `:array` special syntax (for BigQuery and PostgreSQL). This also adds support for metadata on the `:select` value to produce `AS STRUCT` or `DISTINCT`. * Address [#512](https://github.com/seancorfield/honeysql/issues/512) by adding support for subqueries in the `:array` special syntax (for BigQuery and PostgreSQL). This also adds support for metadata on the `:select` value to produce `AS STRUCT` or `DISTINCT`.

View file

@ -526,6 +526,11 @@ user=> (sql/format {:select ^{:as :struct} [:id :name] :from :table})
["SELECT AS STRUCT id, name FROM table"] ["SELECT AS STRUCT id, name FROM table"]
``` ```
As of 2.5.next, HoneySQL ignores the following metadata: `:file`, `:line`,
`:column`, `:end-line`, and `:end-column` (2.5.1091 only ignored `:line`
and `:column`). You can ask HoneySQL to ignore other metadata by specifying
the `:ignored-metadata` option to `honey.sql/format`.
> Google BigQuery support: to provide `SELECT * EXCEPT ..` and `SELECT * REPLACE ..` syntax, HoneySQL supports a vector starting with `:*` or the symbol `*` followed by except columns and/or replace expressions as columns: > Google BigQuery support: to provide `SELECT * EXCEPT ..` and `SELECT * REPLACE ..` syntax, HoneySQL supports a vector starting with `:*` or the symbol `*` followed by except columns and/or replace expressions as columns:
```clojure ```clojure

View file

@ -18,6 +18,7 @@ All options may be omitted. The default behavior of each option is described in
* `:cache` -- an atom containing a [clojure.core.cache](https://github.com/clojure/core.cache) cache used to cache generated SQL; the default behavior is to generate SQL on each call to `format`, * `:cache` -- an atom containing a [clojure.core.cache](https://github.com/clojure/core.cache) cache used to cache generated SQL; the default behavior is to generate SQL on each call to `format`,
* `:checking` -- `:none` (default), `:basic`, or `:strict` to control the amount of lint-like checking that HoneySQL performs, * `:checking` -- `:none` (default), `:basic`, or `:strict` to control the amount of lint-like checking that HoneySQL performs,
* `:dialect` -- a keyword that identifies a dialect to be used for this specific call to `format`; the default is to use what was specified in `set-dialect!` or `:ansi` if no other dialect has been set, * `:dialect` -- a keyword that identifies a dialect to be used for this specific call to `format`; the default is to use what was specified in `set-dialect!` or `:ansi` if no other dialect has been set,
* `:ignored-metadata` -- a sequence of metadata keys that should be ignored when formatting (in addition to `:file`, `:line`, `:column`, `:end-line` and `:end-column` which are always ignored); the default is `[]` -- no additional metadata is ignored (since 2.5.next),
* `:inline` -- a Boolean indicating whether or not to inline parameter values, rather than use `?` placeholders and a sequence of parameter values; the default is `false` -- values are not inlined, * `:inline` -- a Boolean indicating whether or not to inline parameter values, rather than use `?` placeholders and a sequence of parameter values; the default is `false` -- values are not inlined,
* `:numbered` -- a Boolean indicating whether to generate numbered placeholders in the generated SQL (`$1`, `$2`, etc) or positional placeholders (`?`); the default is `false` (positional placeholders); this option was added in 2.4.962, * `:numbered` -- a Boolean indicating whether to generate numbered placeholders in the generated SQL (`$1`, `$2`, etc) or positional placeholders (`?`); the default is `false` (positional placeholders); this option was added in 2.4.962,
* `:params` -- a hash map providing values for named parameters, identified by names (keywords or symbols) that start with `?` in the DSL; the default is that any such named parameters will have `nil` values, * `:params` -- a hash map providing values for named parameters, identified by names (keywords or symbols) that start with `?` in the DSL; the default is that any such named parameters will have `nil` values,

View file

@ -138,6 +138,10 @@
;; in entities; if someone complains about this check, an option ;; in entities; if someone complains about this check, an option
;; can be added to format to turn this on: ;; can be added to format to turn this on:
(def ^:private ^:dynamic *allow-suspicious-entities* false) (def ^:private ^:dynamic *allow-suspicious-entities* false)
;; the following metadata is ignored in formatted by default:
;; :file, :line, :column, :end-line, and :end-column
;; this dynamic var can be used to add more metadata to ignore:
(def ^:private ^:dynamic *ignored-metadata* [])
;; "linting" mode (:none, :basic, :strict): ;; "linting" mode (:none, :basic, :strict):
(def ^:private ^:dynamic *checking* @default-checking) (def ^:private ^:dynamic *checking* @default-checking)
;; the current DSL hash map being formatted (for clause-body / contains-clause?): ;; the current DSL hash map being formatted (for clause-body / contains-clause?):
@ -684,14 +688,21 @@
(conj acc k) (conj acc k)
(conj acc k v))) (conj acc k v)))
[] []
(dissoc data ; remove the somewhat "standard" metadata: (reduce dissoc
data
(into [; remove the somewhat "standard" metadata:
:line :column :file :line :column :file
:end-line :end-column))] :end-line :end-column]
*ignored-metadata*)))]
(when (seq items) (when (seq items)
(str/join " " (mapv sql-kw items)))))) (str/join " " (mapv sql-kw items))))))
(comment (comment
(format-meta ^{:foo true :bar :baz} []) (format-meta ^{:foo true :bar :baz} [])
(binding [*ignored-metadata* [:bar]]
(format-meta ^{:foo true :bar :baz} []))
(format-meta []) (format-meta [])
) )
@ -2025,6 +2036,9 @@
(f @base-clause-order) (f @base-clause-order)
@current-clause-order) @current-clause-order)
@current-clause-order) @current-clause-order)
*ignored-metadata* (if (contains? opts :ignored-metadata)
(:ignored-metadata opts)
[])
*inline* (cond (contains? opts :inline) *inline* (cond (contains? opts :inline)
(:inline opts) (:inline opts)
(= :nrql (:dialect dialect)) (= :nrql (:dialect dialect))