From 6b015400ed3ec668aeb7fd27fda7219510c20991 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 14 Jan 2023 14:58:13 -0800 Subject: [PATCH] fix #445 #453 --- CHANGELOG.md | 1 + doc/clause-reference.md | 42 ++++++++++++++++++++++++++++++- test/honey/sql/postgres_test.cljc | 18 +++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c415ea5..ba1a09c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * 2.4.next in progress * Address [#447](https://github.com/seancorfield/honeysql/issues/447) by updating GitHub Actions and dependencies. + * Address [#445](https://github.com/seancorfield/honeysql/issues/445) and [#453](https://github.com/seancorfield/honeysql/issues/453) by adding key/constraint examples to `CREATE TABLE` docs. * 2.4.962 -- 2022-12-17 * Fix `set-options!` (only `:checking` worked in 2.4.947). diff --git a/doc/clause-reference.md b/doc/clause-reference.md index e2b735a..76c9ff2 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -168,7 +168,7 @@ was specified so nothing is parameterized. In addition, everything except the first element of a column description will be uppercased (mostly to give the appearance of separating the column name from the SQL keywords) -- except for keywords -that with `'` which will be transcribed into the SQL exactly +that start with `'` which will be transcribed into the SQL exactly as-is, with no case or character conversion at all. This "escape hatch" is intended to allow for SQL dialects that are case sensitive and/or have other unusual syntax constraints. @@ -178,6 +178,46 @@ in the example above, that allow things like `CHECK` for a constraint, `FOREIGN KEY` (with a column name), `REFERENCES` (with a pair of column names). See [Column Descriptors in Special Syntax](special-syntax.md#column-descriptors) for more details. +For example: + +```clojure +user=> (-> {:create-table :foo + :with-columns + [[:a :int] + [:b :int] + [[:primary-key :a :b]]]} + (sql/format)) +["CREATE TABLE foo (a INT, b INT, PRIMARY KEY(a, b))"] +``` + +or: + +```clojure +user=> (-> {:create-table [:bar] + :with-columns + [[:a :integer] + [:b :integer] + [[:constraint :foo_natural_key] :unique [:composite :a :b]]]} + (sql/format)) +["CREATE TABLE bar (a INTEGER, b INTEGER, CONSTRAINT foo_natural_key UNIQUE (a, b))"] +``` + +or a mix of column constraints and table constraints: + +```clojure +user=> (-> '{create-table quux + with-columns + ((a integer (constraint a_pos) (check (> a 0))) + (b integer) + ((constraint a_bigger) (check (< b a))))} + (sql/format {:pretty true})) +[" +CREATE TABLE quux +(a INTEGER CONSTRAINT a_pos CHECK(a > 0), b INTEGER, CONSTRAINT a_bigger CHECK(b < a)) +"] +``` + + ## create-table-as `:create-table-as` can accept a single table name or a sequence diff --git a/test/honey/sql/postgres_test.cljc b/test/honey/sql/postgres_test.cljc index 803117a..120a13e 100644 --- a/test/honey/sql/postgres_test.cljc +++ b/test/honey/sql/postgres_test.cljc @@ -405,3 +405,21 @@ (is (= ["DROP EXTENSION \"uuid-ossp\""] (-> (drop-extension :uuid-ossp) (sql/format {:quoted true})))))) + +(deftest issue-453-constraint + (testing "standalone constraint" + (is (= ["CREATE TABLE bar (a INTEGER, b INTEGER, CONSTRAINT foo_natural_key UNIQUE (a, b))"] + (-> {:create-table [:bar] + :with-columns + [[:a :integer] + [:b :integer] + [[:constraint :foo_natural_key] :unique [:composite :a :b]]]} + (sql/format))))) + (testing "inline constraint" + (is (= ["CREATE TABLE foo (a INTEGER CONSTRAINT a_pos CHECK(a > 0), b INTEGER, CONSTRAINT a_bigger CHECK(b < a))"] + (-> '{create-table foo + with-columns + ((a integer (constraint a_pos) (check (> a 0))) + (b integer) + ((constraint a_bigger) (check (< b a))))} + (sql/format))))))