supports extended INSERT INTO...SELECT syntax allowing specifying columns to insert into explicitly

This commit is contained in:
Dave Della Costa 2015-03-05 00:20:42 +09:00
parent 17145ea549
commit 0f24df5ee0
2 changed files with 15 additions and 1 deletions

View file

@ -46,6 +46,7 @@
(defn- undasherize [s]
(string/replace s "-" "_"))
(defn quote-identifier [x & {:keys [style split] :or {split true}}]
(let [qf (if style
(quote-fns style)
@ -369,7 +370,12 @@
(str "OFFSET " (to-sql offset)))
(defmethod format-clause :insert-into [[_ table] _]
(str "INSERT INTO " (to-sql table)))
(if (and (sequential? table) (sequential? (first table)))
(str "INSERT INTO "
(to-sql (ffirst table))
" (" (comma-join (map to-sql (second (first table)))) ") "
(to-sql (second table)))
(str "INSERT INTO " (to-sql table))))
(defmethod format-clause :columns [[_ fields] _]
(str "(" (comma-join (map to-sql fields)) ")"))

View file

@ -17,3 +17,11 @@
'foo "foo"
:foo-bar "foo_bar")
(is (= (quote-identifier "*" :style :ansi) "*")))
(deftest insert-into
(is (= (format-clause (first {:insert-into :foo}) nil)
"INSERT INTO foo"))
(is (= (format-clause (first {:insert-into [:foo {:select [:bar] :from [:baz]}]}) nil)
"INSERT INTO foo SELECT bar FROM baz"))
(is (= (format-clause (first {:insert-into [[:foo [:a :b :c]] {:select [:d :e :f] :from [:baz]}]}) nil)
"INSERT INTO foo (a, b, c) SELECT d, e, f FROM baz")))