Implement variadic and/or/+/*
This commit is contained in:
parent
5449c23ede
commit
0052aade7c
2 changed files with 41 additions and 19 deletions
|
|
@ -458,10 +458,30 @@
|
|||
(format-dsl x (assoc opts :nested? true))
|
||||
|
||||
(sequential? x)
|
||||
(let [op (first x)]
|
||||
(let [op (first x)
|
||||
op-ignore-nil #{:and :or}
|
||||
op-variadic #{:and :or :+ :*}]
|
||||
(if (keyword? op)
|
||||
(cond (infix-ops op)
|
||||
(let [[_ a b] x
|
||||
(if (op-variadic op) ; no aliases here, no special semantics
|
||||
(let [x (if (op-ignore-nil op) (remove nil? x) x)
|
||||
[sqls params]
|
||||
(reduce (fn [[sql params] [sql' & params']]
|
||||
[(conj sql sql')
|
||||
(if params' (into params params') params)])
|
||||
[[] []]
|
||||
(map #(format-expr % {:nested? true})
|
||||
(rest x)))]
|
||||
(into [(cond-> (str/join (str " " (sql-kw op) " ") sqls)
|
||||
nested?
|
||||
(as-> s (str "(" s ")")))]
|
||||
params))
|
||||
(let [[_ a b & y] x
|
||||
_ (when (seq y)
|
||||
(throw (ex-info (str "only binary "
|
||||
op
|
||||
"is supported")
|
||||
{:expr x})))
|
||||
[s1 & p1] (format-expr a {:nested? true})
|
||||
[s2 & p2] (format-expr b {:nested? true})
|
||||
op (get infix-aliases op op)]
|
||||
|
|
@ -478,7 +498,7 @@
|
|||
(as-> s (str "(" s ")")))
|
||||
(vector)
|
||||
(into p1)
|
||||
(into p2))))
|
||||
(into p2)))))
|
||||
(special-syntax op)
|
||||
(let [formatter (special-syntax op)]
|
||||
(formatter (rest x)))
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@
|
|||
(sut/format-expr [:+ :id 1])))
|
||||
(is (= ["? + (? + quux)" 1 1]
|
||||
(sut/format-expr [:+ 1 [:+ 1 :quux]])))
|
||||
(is (= ["? + ? + quux" 1 1]
|
||||
(sut/format-expr [:+ 1 1 :quux])))
|
||||
(is (= ["FOO(BAR(? + G(abc)), F(?, quux))" 2 1]
|
||||
(sut/format-expr [:foo [:bar [:+ 2 [:g :abc]]] [:f 1 :quux]])))
|
||||
(is (= ["id"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue