This commit is contained in:
Sean Corfield 2022-07-29 15:40:45 -07:00
parent 5be96ba6e3
commit f0ada59fff
6 changed files with 32 additions and 13 deletions

View file

@ -6,6 +6,7 @@
* Address [#414](https://github.com/seancorfield/honeysql/issues/414) by providing an example of `ORDER BY` with a `CASE` expression.
* Address [#412](https://github.com/seancorfield/honeysql/issues/412) by documenting options in a separate page and reorganizing the ToC structure.
* Address [#409](https://github.com/seancorfield/honeysql/issues/409) by making docstring check for public helpers conditional.
* Fix [#406](https://github.com/seancorfield/honeysql/issues/406) by adding `:alter-column` (which produces `MODIFY COLUMN` when the MySQL dialect is selected) and deprecating `:modify-column`.
* Address [#401](https://github.com/seancorfield/honeysql/issues/401) by adding `register-dialect!` and `get-dialect`, and also making `add-clause-before`, `strop`, and `upper-case` public so that new dialects are easier to construct.
* 2.2.891 -- 2022-04-23

View file

@ -32,7 +32,7 @@ See [Column Descriptors in Special Syntax](special-syntax.md#column-descriptors)
> Google BigQuery support: `[:bigquery/array :string]` as a column type produces `ARRAY<STRING>` and `[:bigquery/struct col1-spec col2-spec]` as a column type produces `STRUCT<col1, col2>` (where `colN-spec` is a vector specifying a named column).
## alter-table, add-column, drop-column, modify-column, rename-column
## alter-table, add-column, drop-column, alter-column, modify-column, rename-column
`:alter-table` can accept either a single table name or
a sequence that begins with a table name and is followed
@ -55,8 +55,8 @@ user=> (sql/format {:alter-table :fruit
:drop-column [:if-exists :ident]})
["ALTER TABLE fruit DROP COLUMN IF EXISTS ident"]
user=> (sql/format {:alter-table :fruit
:modify-column [:id :int :unsigned nil]})
["ALTER TABLE fruit MODIFY COLUMN id INT UNSIGNED NULL"]
:alter-column [:id :int :unsigned nil]})
["ALTER TABLE fruit ALTER COLUMN id INT UNSIGNED NULL"]
user=> (sql/format {:alter-table :fruit
:rename-column [:look :appearance]})
["ALTER TABLE fruit RENAME COLUMN look TO appearance"]
@ -75,20 +75,22 @@ user=> (sql/format {:alter-table [:fruit
{:add-column [:id :int [:not nil]]}
{:add-column [:name [:varchar 32]]}
{:drop-column :ident}
{:modify-column [:appearance :text]}]})
["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, ADD COLUMN name VARCHAR(32), DROP COLUMN ident, MODIFY COLUMN appearance TEXT"]
{:alter-column [:appearance :text]}]})
["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, ADD COLUMN name VARCHAR(32), DROP COLUMN ident, ALTER COLUMN appearance TEXT"]
user=> (sql/format {:alter-table [:fruit
{:add-column [:id :int [:not nil] :if-not-exists]}
{:drop-column [:if-exists :ident]}]})
["ALTER TABLE fruit ADD COLUMN IF NOT EXISTS id INT NOT NULL, DROP COLUMN IF EXISTS ident"]
```
As can be seen above, `:add-column` and `:modify-column`
As can be seen above, `:add-column` and `:alter-column`
both accept a column description (as a sequence of simple
expressions); `:drop-column` accepts a single column name,
and `:rename-column` accepts a sequence with two column
names: the "from" and the "to" names.
> Note: `:modify-column` is MySQL-specific and should be considered legacy and deprecated. `:alter-column` will produce `MODIFY COLUMN` when the MySQL dialect is selected.
### add-index, drop-index
Used with `:alter-table`,

View file

@ -21,7 +21,7 @@ The code examples herein assume:
insert-into values
create-table with-columns create-view create-extension
add-column alter-table add-index
modify-column rename-column rename-table
alter-column rename-column rename-table
drop-table drop-column drop-index drop-extension
upsert returning on-conflict on-constraint
do-update-set do-nothing]])
@ -361,11 +361,11 @@ user=> (-> (alter-table :fruit)
(drop-column :skin)
sql/format)
["ALTER TABLE fruit DROP COLUMN skin"]
;; alter table modify column:
;; alter table alter column:
user=> (-> (alter-table :fruit)
(modify-column :name [:varchar 64] [:not nil])
(alter-column :name [:varchar 64] [:not nil])
sql/format)
["ALTER TABLE fruit MODIFY COLUMN name VARCHAR(64) NOT NULL"]
["ALTER TABLE fruit ALTER COLUMN name VARCHAR(64) NOT NULL"]
;; alter table rename column:
user=> (-> (alter-table :fruit)
(rename-column :cost :price)

View file

@ -4,7 +4,7 @@ This section lists the function-like expressions that
HoneySQL supports out of the box which are formatted
as special syntactic forms.
The first group are used for SQL expressions. The second (last group) are used primarily in column definitions (as part of `:with-columns` and `:add-column` / `:modify-column`).
The first group are used for SQL expressions. The second (last group) are used primarily in column definitions (as part of `:with-columns` and `:add-column` / `:alter-column`).
## array

View file

@ -42,7 +42,8 @@
(def ^:private default-clause-order
"The (default) order for known clauses. Can have items added and removed."
[;; DDL comes first (these don't really have a precedence):
:alter-table :add-column :drop-column :modify-column :rename-column
:alter-table :add-column :drop-column
:alter-column :modify-column :rename-column
:add-index :drop-index :rename-table
:create-table :create-table-as :with-columns
:create-view :create-materialized-view :create-extension
@ -943,6 +944,10 @@
(atom {:alter-table #'format-alter-table
:add-column #'format-add-item
:drop-column #'format-drop-items
:alter-column (fn [k spec]
(format-add-item
(if (mysql?) :modify-column k)
spec))
:modify-column #'format-add-item
:rename-column #'format-rename-item
;; so :add-index works with both [:index] and [:unique]

View file

@ -179,11 +179,22 @@
[& args]
(generic-1 :drop-column args))
(defn alter-column
"Like add-column, accepts any number of SQL elements
that describe the new column definition:
(alter-column :name [:varchar 64] [:not nil])"
[& col-elems]
(generic :alter-column col-elems))
(defn modify-column
"Like add-column, accepts any number of SQL elements
that describe the new column definition:
(modify-column :name [:varchar 64] [:not nil])"
(modify-column :name [:varchar 64] [:not nil])
MySQL-specific, deprecated. Use `alter-column` and
specify the MySQL dialect to get `MODIFY COLUMN`."
[& col-elems]
(generic :modify-column col-elems))