Allow with expr AS ident syntax in WITH clause

This commit is contained in:
Ike Mawira 2022-09-22 19:40:15 +03:00
parent e553f4f169
commit 30e554f31c

View file

@ -159,6 +159,11 @@
[] []
(= :sqlserver (:dialect *dialect*))) (= :sqlserver (:dialect *dialect*)))
(defn- clickhouse?
"Helper to detect if Clickhouse is the current dialect."
[]
(= :clickhouse (:dialect *dialect*)))
;; String.toUpperCase() or `str/upper-case` for that matter converts the ;; String.toUpperCase() or `str/upper-case` for that matter converts the
;; string to uppercase for the DEFAULT LOCALE. Normally this does what you'd ;; string to uppercase for the DEFAULT LOCALE. Normally this does what you'd
;; expect but things like `inner join` get converted to `İNNER JOİN` (dot over ;; expect but things like `inner join` get converted to `İNNER JOİN` (dot over
@ -552,15 +557,20 @@
;; or just entity, as far as I can tell... ;; or just entity, as far as I can tell...
(let [[sqls params] (let [[sqls params]
(reduce-sql (reduce-sql
(map (map
(fn [[x expr :as with]] (fn [[x expr :as with]]
(let [[sql & params] (format-with-part x) (let [[sql & params] (format-with-part x)
[sql' & params'] (format-dsl expr)] [sql' & params'] (if (and (clickhouse?) ;;in clickhouse the expression can be
;; according to docs, CTE should _always_ be wrapped: (not (map? expr))) ;; a string arranged as `with expr as ident`
(cond-> [(str sql " " (as-fn with) " " (str "(" sql' ")"))] (format-expr expr)
params (into params) (format-dsl expr))]
params' (into params')))) ;; according to docs, CTE should _always_ be wrapped:
xs))] (cond-> [(if (and (clickhouse?) (not (map? expr)))
(str sql' " AS " sql)
(str sql " " (as-fn with) " " (str "(" sql' ")")))]
params (into params)
params' (into params'))))
xs))]
(into [(str (sql-kw k) " " (str/join ", " sqls))] params))) (into [(str (sql-kw k) " " (str/join ", " sqls))] params)))
(defn- format-selector [k xs] (defn- format-selector [k xs]