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
@ -555,9 +560,14 @@
(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
(not (map? expr))) ;; a string arranged as `with expr as ident`
(format-expr expr)
(format-dsl expr))]
;; according to docs, CTE should _always_ be wrapped: ;; according to docs, CTE should _always_ be wrapped:
(cond-> [(str sql " " (as-fn with) " " (str "(" sql' ")"))] (cond-> [(if (and (clickhouse?) (not (map? expr)))
(str sql' " AS " sql)
(str sql " " (as-fn with) " " (str "(" sql' ")")))]
params (into params) params (into params)
params' (into params')))) params' (into params'))))
xs))] xs))]