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 [#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 [#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. * 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. * 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 * 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). > 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 `:alter-table` can accept either a single table name or
a sequence that begins with a table name and is followed 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]}) :drop-column [:if-exists :ident]})
["ALTER TABLE fruit DROP COLUMN IF EXISTS ident"] ["ALTER TABLE fruit DROP COLUMN IF EXISTS ident"]
user=> (sql/format {:alter-table :fruit user=> (sql/format {:alter-table :fruit
:modify-column [:id :int :unsigned nil]}) :alter-column [:id :int :unsigned nil]})
["ALTER TABLE fruit MODIFY COLUMN id INT UNSIGNED NULL"] ["ALTER TABLE fruit ALTER COLUMN id INT UNSIGNED NULL"]
user=> (sql/format {:alter-table :fruit user=> (sql/format {:alter-table :fruit
:rename-column [:look :appearance]}) :rename-column [:look :appearance]})
["ALTER TABLE fruit RENAME COLUMN look TO 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 [:id :int [:not nil]]}
{:add-column [:name [:varchar 32]]} {:add-column [:name [:varchar 32]]}
{:drop-column :ident} {: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, MODIFY 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 user=> (sql/format {:alter-table [:fruit
{:add-column [:id :int [:not nil] :if-not-exists]} {:add-column [:id :int [:not nil] :if-not-exists]}
{:drop-column [:if-exists :ident]}]}) {:drop-column [:if-exists :ident]}]})
["ALTER TABLE fruit ADD COLUMN IF NOT EXISTS id INT NOT NULL, 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 both accept a column description (as a sequence of simple
expressions); `:drop-column` accepts a single column name, expressions); `:drop-column` accepts a single column name,
and `:rename-column` accepts a sequence with two column and `:rename-column` accepts a sequence with two column
names: the "from" and the "to" names. 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 ### add-index, drop-index
Used with `:alter-table`, Used with `:alter-table`,

View file

@ -21,7 +21,7 @@ The code examples herein assume:
insert-into values insert-into values
create-table with-columns create-view create-extension create-table with-columns create-view create-extension
add-column alter-table add-index 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 drop-table drop-column drop-index drop-extension
upsert returning on-conflict on-constraint upsert returning on-conflict on-constraint
do-update-set do-nothing]]) do-update-set do-nothing]])
@ -361,11 +361,11 @@ user=> (-> (alter-table :fruit)
(drop-column :skin) (drop-column :skin)
sql/format) sql/format)
["ALTER TABLE fruit DROP COLUMN skin"] ["ALTER TABLE fruit DROP COLUMN skin"]
;; alter table modify column: ;; alter table alter column:
user=> (-> (alter-table :fruit) user=> (-> (alter-table :fruit)
(modify-column :name [:varchar 64] [:not nil]) (alter-column :name [:varchar 64] [:not nil])
sql/format) 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: ;; alter table rename column:
user=> (-> (alter-table :fruit) user=> (-> (alter-table :fruit)
(rename-column :cost :price) (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 HoneySQL supports out of the box which are formatted
as special syntactic forms. 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 ## array

View file

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

View file

@ -179,11 +179,22 @@
[& args] [& args]
(generic-1 :drop-column 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 (defn modify-column
"Like add-column, accepts any number of SQL elements "Like add-column, accepts any number of SQL elements
that describe the new column definition: 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] [& col-elems]
(generic :modify-column col-elems)) (generic :modify-column col-elems))