diff --git a/CHANGELOG.md b/CHANGELOG.md index ee28da6..e4b7ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Changes * 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 [#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 * 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`. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 8a5a20a..5796972 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -526,6 +526,11 @@ user=> (sql/format {: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: ```clojure diff --git a/doc/options.md b/doc/options.md index c48b2fb..dd213ea 100644 --- a/doc/options.md +++ b/doc/options.md @@ -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`, * `: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, +* `: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, * `: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, diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index aefe7ce..7b1343f 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -138,6 +138,10 @@ ;; in entities; if someone complains about this check, an option ;; can be added to format to turn this on: (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): (def ^:private ^:dynamic *checking* @default-checking) ;; the current DSL hash map being formatted (for clause-body / contains-clause?): @@ -684,14 +688,21 @@ (conj acc k) (conj acc k v))) [] - (dissoc data ; remove the somewhat "standard" metadata: - :line :column :file - :end-line :end-column))] + (reduce dissoc + data + (into [; remove the somewhat "standard" metadata: + :line :column :file + :end-line :end-column] + *ignored-metadata*)))] (when (seq items) (str/join " " (mapv sql-kw items)))))) (comment (format-meta ^{:foo true :bar :baz} []) + + (binding [*ignored-metadata* [:bar]] + (format-meta ^{:foo true :bar :baz} [])) + (format-meta []) ) @@ -2025,6 +2036,9 @@ (f @base-clause-order) @current-clause-order) @current-clause-order) + *ignored-metadata* (if (contains? opts :ignored-metadata) + (:ignored-metadata opts) + []) *inline* (cond (contains? opts :inline) (:inline opts) (= :nrql (:dialect dialect))