DDL translation: CREATE TABLE, CREATE SEQUENCE, constraint extraction.
Produces Datahike tx-data that installs schema attributes (:db/ident,
:db/valueType, :db/cardinality, :db/unique, :pg/type, and the
pgwire-specific :pg/* constraint metadata).
The predicate helpers (identity-column?, column-is-primary-key?,
column-is-unique?, column-is-not-null?, specs-contain-seq?,
column-check-expr-text, column-default-spec) walk JSqlParser's
ColumnDefinition.getColumnSpecs lists, which are raw strings — so
each predicate is a case-insensitive token matcher.
Entry points:
Helpers:
DDL translation: CREATE TABLE, CREATE SEQUENCE, constraint extraction.
Produces Datahike tx-data that installs schema attributes (`:db/ident`,
`:db/valueType`, `:db/cardinality`, `:db/unique`, `:pg/type`, and the
pgwire-specific `:pg/*` constraint metadata).
The predicate helpers (`identity-column?`, `column-is-primary-key?`,
`column-is-unique?`, `column-is-not-null?`, `specs-contain-seq?`,
`column-check-expr-text`, `column-default-spec`) walk JSqlParser's
`ColumnDefinition.getColumnSpecs` lists, which are raw strings — so
each predicate is a case-insensitive token matcher.
Entry points:
- translate-create-table — main; returns {:type :ddl :tx-data [...]}
- translate-create-sequence — CREATE SEQUENCE name [...]
Helpers:
- extract-ddl-constraints — CHECK / NOT NULL / UNIQUE / FK clauses
- extract-inherits — PostgreSQL INHERITS(column-check-expr-text col)Extract the text of an inline CHECK (…) constraint from a
ColumnDefinition's ColumnSpecs, or nil if none.
JSqlParser emits inline CHECK as two tokens: CHECK and a second
token that's the entire parenthesized expression, e.g.
["CHECK" "(x > 0)"]. We keep the inner text.
Also tolerate the tokenized form PG dialect dumps sometimes
produce — CHECK followed by (, individual tokens, ) — by
paren-matching across the remaining specs.
Extract the text of an inline `CHECK (…)` constraint from a ColumnDefinition's ColumnSpecs, or nil if none. JSqlParser emits inline CHECK as two tokens: `CHECK` and a second token that's the entire parenthesized expression, e.g. `["CHECK" "(x > 0)"]`. We keep the inner text. Also tolerate the tokenized form PG dialect dumps sometimes produce — CHECK followed by `(`, individual tokens, `)` — by paren-matching across the remaining specs.
(column-default-spec col)If the column spec list carries DEFAULT <expr>, return a map
describing the default so translate-create-table can attach it to
the schema entity. Shape:
{:kind :literal :value <long|double|string|boolean|nil>} {:kind :fn :value <canonical fn name string>} {:kind :nextval :value <sequence name string>} {:kind :unsupported :raw <original SQL text>}
The literal branch handles numbers, single-quoted strings, boolean keywords, and NULL. The :fn branch covers the stateless current-* functions PG users rely on. :nextval is the only stateful default we support and it ties into our existing sequence infrastructure. Unknown expressions land in :unsupported so CREATE TABLE can raise loudly instead of silently dropping the constraint.
Walks the ColumnSpecs as whitespace-separated tokens; tolerant of
parens (e.g. DEFAULT (now())).
If the column spec list carries `DEFAULT <expr>`, return a map
describing the default so translate-create-table can attach it to
the schema entity. Shape:
{:kind :literal :value <long|double|string|boolean|nil>}
{:kind :fn :value <canonical fn name string>}
{:kind :nextval :value <sequence name string>}
{:kind :unsupported :raw <original SQL text>}
The literal branch handles numbers, single-quoted strings, boolean
keywords, and NULL. The :fn branch covers the stateless current-*
functions PG users rely on. :nextval is the only stateful default
we support and it ties into our existing sequence infrastructure.
Unknown expressions land in :unsupported so CREATE TABLE can
raise loudly instead of silently dropping the constraint.
Walks the ColumnSpecs as whitespace-separated tokens; tolerant of
parens (e.g. `DEFAULT (now())`).(column-is-not-null? col)True when the column-spec list carries NOT NULL. PK columns are
implicitly NOT NULL in PG; callers should combine this with
column-is-primary-key? to avoid double-flagging.
True when the column-spec list carries `NOT NULL`. PK columns are implicitly NOT NULL in PG; callers should combine this with column-is-primary-key? to avoid double-flagging.
(column-is-primary-key? col)True when a column has inline PRIMARY KEY in its column specs.
True when a column has inline PRIMARY KEY in its column specs.
(column-is-unique? col)True when a column has inline UNIQUE in its column specs (not part of PRIMARY KEY, which we handle separately).
True when a column has inline UNIQUE in its column specs (not part of PRIMARY KEY, which we handle separately).
(extract-ddl-constraints ct)Collect PRIMARY KEY and UNIQUE constraints from a CreateTable into a
normalized shape. Merges column-level specs (id INT PRIMARY KEY) with
table-level indexes (PRIMARY KEY (a,b), UNIQUE (c)).
Returns a map: :pk-cols — vector of column names in the PK (empty if none) :pk-name — explicit constraint name for the PK, or nil :uniques — vector of {:cols [str...] :name str-or-nil} each an independent UNIQUE constraint Column names are the raw SQL idents (unquoted), not namespaced keywords. The caller owns the :t/col namespace mapping.
Collect PRIMARY KEY and UNIQUE constraints from a CreateTable into a
normalized shape. Merges column-level specs (`id INT PRIMARY KEY`) with
table-level indexes (`PRIMARY KEY (a,b), UNIQUE (c)`).
Returns a map:
:pk-cols — vector of column names in the PK (empty if none)
:pk-name — explicit constraint name for the PK, or nil
:uniques — vector of {:cols [str...] :name str-or-nil}
each an independent UNIQUE constraint
Column names are the raw SQL idents (unquoted), not namespaced keywords.
The caller owns the :t/col namespace mapping.(extract-inherits ct)Extract parent table name from INHERITS clause in CREATE TABLE options.
Extract parent table name from INHERITS clause in CREATE TABLE options.
(identity-column? col)Check if a JSqlParser ColumnDefinition has GENERATED BY DEFAULT AS IDENTITY or uses a SERIAL type (which implies auto-increment).
Check if a JSqlParser ColumnDefinition has GENERATED BY DEFAULT AS IDENTITY or uses a SERIAL type (which implies auto-increment).
(specs-contain-seq? specs tokens)True when the whitespace-split column-spec list contains the given token sequence contiguously, case-insensitive. Used to detect PRIMARY KEY (two tokens) and UNIQUE (one token) inline on a ColumnDefinition.
True when the whitespace-split column-spec list contains the given token sequence contiguously, case-insensitive. Used to detect PRIMARY KEY (two tokens) and UNIQUE (one token) inline on a ColumnDefinition.
(translate-create-sequence cs)Translate CREATE SEQUENCE to Datahike schema + initial entity. Sequences are stored as entities with :seq/* attributes.
Translate CREATE SEQUENCE to Datahike schema + initial entity. Sequences are stored as entities with :__seq__/* attributes.
(translate-create-table ct db)Translate a CREATE TABLE statement to Datahike schema transaction data. Also returns :table-name, :column-order, :identity-cols, and :inherits.
Translate a CREATE TABLE statement to Datahike schema transaction data. Also returns :table-name, :column-order, :identity-cols, and :inherits.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |