From 0f24df5ee080b83f80d80b7019fa1c6aa9cc80c8 Mon Sep 17 00:00:00 2001 From: Dave Della Costa Date: Thu, 5 Mar 2015 00:20:42 +0900 Subject: [PATCH] supports extended INSERT INTO...SELECT syntax allowing specifying columns to insert into explicitly --- src/honeysql/format.clj | 8 +++++++- test/honeysql/format_test.clj | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index f3fa8ae..1eb5859 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -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)) ")")) diff --git a/test/honeysql/format_test.clj b/test/honeysql/format_test.clj index 19483ef..d276535 100644 --- a/test/honeysql/format_test.clj +++ b/test/honeysql/format_test.clj @@ -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")))