Add docstrings for DDL helpers
This commit is contained in:
parent
63add4df3b
commit
ea4e120252
1 changed files with 91 additions and 8 deletions
|
|
@ -48,46 +48,121 @@
|
||||||
;; for every clause, there is a public helper
|
;; for every clause, there is a public helper
|
||||||
|
|
||||||
(defn alter-table
|
(defn alter-table
|
||||||
|
"Alter table takes a SQL entity (the name of the
|
||||||
|
table to modify) and any number of optional SQL
|
||||||
|
clauses to be applied in a single statement.
|
||||||
|
|
||||||
|
(alter-table :foo (add-column :id :int nil))
|
||||||
|
|
||||||
|
If only the SQL entity is provided, the result
|
||||||
|
needs to be combined with another SQL clause to
|
||||||
|
modify the table.
|
||||||
|
|
||||||
|
(-> (alter-table :foo) (add-column :id :int nil))"
|
||||||
|
{:arglists '([table & clauses])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic :alter-table args))
|
(generic :alter-table args))
|
||||||
|
|
||||||
(defn add-column
|
(defn add-column
|
||||||
[& args]
|
"Add a single column to a table (see `alter-table`).
|
||||||
(generic :add-column args))
|
|
||||||
|
Accepts any number of SQL elements that describe
|
||||||
|
a column:
|
||||||
|
|
||||||
|
(add-column :name [:varchar 32] [:not nil])"
|
||||||
|
[& col-elems]
|
||||||
|
(generic :add-column col-elems))
|
||||||
|
|
||||||
(defn drop-column
|
(defn drop-column
|
||||||
|
"Takes a single column name (use with `alter-table`).
|
||||||
|
|
||||||
|
(alter-table :foo (drop-column :bar))"
|
||||||
|
{:arglists '([col])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic-1 :drop-column args))
|
(generic-1 :drop-column args))
|
||||||
|
|
||||||
(defn modify-column
|
(defn modify-column
|
||||||
[& args]
|
"Like add-column, accepts any number of SQL elements
|
||||||
(generic :modify-column args))
|
that describe the new column definition:
|
||||||
|
|
||||||
|
(modify-column :name [:varchar 64] [:not nil])"
|
||||||
|
[& col-elems]
|
||||||
|
(generic :modify-column col-elems))
|
||||||
|
|
||||||
(defn rename-column
|
(defn rename-column
|
||||||
|
"Accepts two column names: the original name and the
|
||||||
|
new name to which it should be renamed:
|
||||||
|
|
||||||
|
(rename-column :name :full-name)"
|
||||||
|
{:arglists '([old-col new-col])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic :rename-column args))
|
(generic :rename-column args))
|
||||||
|
|
||||||
(defn add-index
|
(defn add-index
|
||||||
|
"Like add-column, this accepts any number of SQL
|
||||||
|
elements that describe a new index to be added:
|
||||||
|
|
||||||
|
(add-index :unique :name-key :first-name :last-name)
|
||||||
|
|
||||||
|
Produces: UNIQUE name_key(first_name, last_name)"
|
||||||
|
{:arglist '([& index-elems])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic :add-index args))
|
(generic :add-index args))
|
||||||
|
|
||||||
(defn drop-index
|
(defn drop-index
|
||||||
|
"Like drop-table, accepts a single index name:
|
||||||
|
|
||||||
|
(drop-index :name-key)"
|
||||||
[& args]
|
[& args]
|
||||||
(generic-1 :drop-index args))
|
(generic-1 :drop-index args))
|
||||||
|
|
||||||
(defn rename-table
|
(defn rename-table
|
||||||
|
"Accepts a single table name and, despite its name,
|
||||||
|
actually means RENAME TO:
|
||||||
|
|
||||||
|
(alter-table :foo (rename-table :bar))
|
||||||
|
|
||||||
|
Produces: ALTER TABLE foo RENAME TO bar"
|
||||||
|
{:arglist '([new-table])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic-1 :rename-table args))
|
(generic-1 :rename-table args))
|
||||||
|
|
||||||
(defn create-table
|
(defn create-table
|
||||||
|
"Accepts a table name to create and optionally a
|
||||||
|
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."
|
||||||
|
{:arglists '([table] [table if-not-exists])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic :create-table args))
|
(generic :create-table args))
|
||||||
|
|
||||||
(defn create-extension
|
(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."
|
||||||
|
{:arglists '([extension] [extension if-not-exists])}
|
||||||
[& args]
|
[& args]
|
||||||
(generic :create-extension args))
|
(generic :create-extension args))
|
||||||
|
|
||||||
(defn with-columns
|
(defn with-columns
|
||||||
|
"Accepts any number of column descriptions. Each
|
||||||
|
column description is a sequence of SQL elements
|
||||||
|
that specify the name and the attributes.
|
||||||
|
|
||||||
|
Can also accept a single argument which is a
|
||||||
|
collection of column descriptions (mostly for
|
||||||
|
compatibility with nilenso/honeysql-postgres
|
||||||
|
which used to be needed for DDL)."
|
||||||
|
{:arglists '([& col-specs] [col-spec-coll])}
|
||||||
[& args]
|
[& args]
|
||||||
;; special case so (with-columns [[:col-1 :definition] [:col-2 :definition]])
|
;; special case so (with-columns [[:col-1 :definition] [:col-2 :definition]])
|
||||||
;; also works in addition to (with-columns [:col-1 :definition] [:col-2 :definition])
|
;; also works in addition to (with-columns [:col-1 :definition] [:col-2 :definition])
|
||||||
|
|
@ -99,16 +174,24 @@
|
||||||
(generic :with-columns args)))
|
(generic :with-columns args)))
|
||||||
|
|
||||||
(defn create-view
|
(defn create-view
|
||||||
|
"Accepts a single view name to create.
|
||||||
|
|
||||||
|
(-> (create-view :cities)
|
||||||
|
(select :*) (from :city))"
|
||||||
[& args]
|
[& args]
|
||||||
(generic-1 :create-view args))
|
(generic-1 :create-view args))
|
||||||
|
|
||||||
(defn drop-table
|
(defn drop-table
|
||||||
[& args]
|
"Accepts one or more table names to drop.
|
||||||
(generic :drop-table args))
|
|
||||||
|
(drop-table :foo)"
|
||||||
|
[& tables]
|
||||||
|
(generic :drop-table tables))
|
||||||
|
|
||||||
(defn drop-extension
|
(defn drop-extension
|
||||||
[& args]
|
"Accepts one or more extension names to drop."
|
||||||
(generic :drop-extension args))
|
[& extensions]
|
||||||
|
(generic :drop-extension extensions))
|
||||||
|
|
||||||
(defn nest
|
(defn nest
|
||||||
[& args]
|
[& args]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue