diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 7e28425..ded5eff 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) @@ -392,7 +393,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 6a9fef9..1a7ee40 100644 --- a/test/honeysql/format_test.clj +++ b/test/honeysql/format_test.clj @@ -25,3 +25,11 @@ (is (= (format-clause (first {:with-recursive [[:query {:select [:foo] :from [:bar]}]]}) nil) "WITH RECURSIVE query AS SELECT foo FROM bar"))) + +(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")))