diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 9e52a52..9a939aa 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -116,6 +116,10 @@ in the example above, but allow things like `CHECK` for a constraint, `FOREIGN KEY` (with a column name), `REFERENCES` (with a pair of column names). See [Clause Descriptors in Special Syntax](special-syntax.md#clause-descriptors) for more details. +## create-table-as, create-view, and others + +## create-extension + ## create-view `:create-view` accepts a single view name: diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 152999d..a499eff 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -39,7 +39,7 @@ [;; DDL comes first (these don't really have a precedence): :alter-table :add-column :drop-column :modify-column :rename-column :add-index :drop-index :rename-table - :create-table :with-columns :create-view :drop-table + :create-table :create-table-as :with-columns :create-view :drop-table :create-extension :drop-extension ;; then SQL clauses in priority order: :nest :with :with-recursive :intersect :union :union-all :except :except-all @@ -53,7 +53,8 @@ :window :partition-by :order-by :limit :offset :for :values :on-conflict :on-constraint :do-nothing :do-update-set - :returning]) + :returning + :with-data]) (defn- add-clause-before "Low-level helper just to insert a new clause." @@ -599,6 +600,12 @@ i (when as (sql-kw as))]))])) +(defn- format-with-data [k data] + [(str/join " " (remove nil? + [(sql-kw :with) + (when-not data (sql-kw :no)) + (sql-kw :data)]))]) + (defn- format-drop-table [k params] (let [tables (if (sequential? params) params [params]) @@ -655,6 +662,7 @@ :drop-index #'format-selector :rename-table (fn [_ x] (format-selector :rename-to x)) :create-table (fn [_ x] (format-create :table x nil)) + :create-table-as (fn [_ x] (format-create :table x :as)) :create-extension (fn [_ x] (format-create :extension x nil)) :with-columns #'format-table-columns :create-view (fn [_ x] (format-create :view x :as)) @@ -702,7 +710,8 @@ :on-constraint #'format-selector :do-nothing (fn [k _] (vector (sql-kw k))) :do-update-set #'format-do-update-set - :returning #'format-selects})) + :returning #'format-selects + :with-data #'format-with-data})) (assert (= (set @base-clause-order) (set @current-clause-order) diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index 0177e50..23457b3 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -156,23 +156,27 @@ flag to trigger IF NOT EXISTS in the SQL: (create-table :foo) - (create-table :foo :if-not-exists) - - That second argument can be truthy value but using - that keyword is recommended for clarity." + (create-table :foo :if-not-exists)" {:arglists '([table] [table if-not-exists])} [& args] (generic :create-table args)) +(defn create-table-as + "Accepts a table name to create and optionally a + flag to trigger IF NOT EXISTS in the SQL: + + (create-table-as :foo) + (create-table-as :foo :if-not-exists)" + {:arglists '([table] [table if-not-exists])} + [& args] + (generic :create-table-as args)) + (defn create-extension "Accepts an extension name to create and optionally a flag to trigger IF NOT EXISTS in the SQL: (create-extension :postgis) - (create-extension :postgis :if-not-exists) - - That second argument can be truthy value but using - that keyword is recommended for clarity." + (create-extension :postgis :if-not-exists)" {:arglists '([extension] [extension if-not-exists])} [& args] (generic :create-extension args)) @@ -548,6 +552,12 @@ [& cols] (generic :returning cols)) +(defn with-data + "Accepts a Boolean determining WITH DATA vs WITH NO DATA." + {:arglists '([data?])} + [& args] + (generic-1 :with-data args)) + ;; helpers that produce non-clause expressions -- must be listed below: (defn composite "Accepts any number of SQL expressions and produces