Liking cljdoc? Tell your friends :D

datahike.pg.sql

SQL → Datahike Datalog translator.

Parses SQL strings using JSqlParser and translates the AST into Datahike Datalog queries that can be executed by datahike.api/q.

The core mapping: attribute namespace prefixes become virtual table names. :person/name → table 'person', column 'name' :person/age → table 'person', column 'age'

Main entry points: (parse-sql sql schema) → {:type :select :query {...} :args [...]} | {:type :insert :tx-data [...]} | {:type :system :result QueryResult} | {:type :error :message str}

SQL → Datahike Datalog translator.

Parses SQL strings using JSqlParser and translates the AST into Datahike
Datalog queries that can be executed by `datahike.api/q`.

The core mapping: attribute namespace prefixes become virtual table names.
  :person/name  → table 'person', column 'name'
  :person/age   → table 'person', column 'age'

Main entry points:
  (parse-sql sql schema)  → {:type :select :query {...} :args [...]}
                           | {:type :insert :tx-data [...]}
                           | {:type :system :result QueryResult}
                           | {:type :error :message str}
raw docstring

*ast-cache*clj

Server-wide JSqlParser AST cache, keyed on the preprocessed SQL string. Tests can rebind to an isolated map; nil disables caching.

Server-wide JSqlParser AST cache, keyed on the preprocessed SQL
string. Tests can rebind to an isolated map; nil disables caching.
sourceraw docstring

*catalog-cache*clj

Tests can rebind this to an isolated java.util.Map to keep their data from polluting the global cache (or vice versa). Nil disables caching entirely. Defaults to the server-wide cache.

Tests can rebind this to an isolated `java.util.Map` to keep their
data from polluting the global cache (or vice versa). Nil disables
caching entirely. Defaults to the server-wide cache.
sourceraw docstring

*parse-cache*clj

Server-wide cache for parse-sql results. Tests can rebind to an isolated map; nil disables caching entirely.

Server-wide cache for parse-sql results. Tests can rebind to an
isolated map; nil disables caching entirely.
sourceraw docstring

->ParamRefclj

source

add-clause!clj

source

coerce-insert-valueclj

source

col-var!clj

source

collect-varsclj

source

enrich-db-with-catalogsclj

(enrich-db-with-catalogs db schema catalog-names)

Materialise the given catalog tables' schema + data on top of db, returning the enriched db (its :schema carries the catalog attrs). Returns db unchanged when catalog-names is empty.

Cached in the server-wide LRU by [user-schema-hash sorted-names]; the cache is DDL-invalidated (invalidate-catalog-cache!). Callable at BOTH parse time (to translate against the catalog schema) and execute time (so a prepared catalog statement re-resolves fresh catalog rows against the current db instead of a stale parse-time snapshot — real PG re-plans on catalog change). schema is db's user schema.

Materialise the given catalog tables' schema + data on top of `db`,
returning the enriched db (its `:schema` carries the catalog attrs).
Returns `db` unchanged when `catalog-names` is empty.

Cached in the server-wide LRU by [user-schema-hash sorted-names]; the
cache is DDL-invalidated (invalidate-catalog-cache!). Callable at BOTH
parse time (to translate against the catalog schema) and execute time
(so a prepared catalog statement re-resolves fresh catalog rows against
the current db instead of a stale parse-time snapshot — real PG re-plans
on catalog change). `schema` is `db`'s user schema.
sourceraw docstring

entity-var!clj

source

eval-check-predicateclj

source

eval-update-exprclj

source

extract-empty-catalog-shapeclj

source

filter-array-aggclj

source

filter-array-agg-orderedclj

source

filter-array-agg-ordered-descclj

source

filter-avgclj

source

filter-avg-numericclj

source

filter-corrclj

source

filter-countclj

source

filter-count-distinctclj

source

filter-maxclj

source

filter-minclj

source

filter-modeclj

source

filter-percentile-contclj

source

filter-percentile-discclj

source

filter-stddev-sampclj

source

filter-sumclj

source

filter-sum-numericclj

source

filter-variance-sampclj

source

fresh-var!clj

source

invalidate-catalog-cache!clj

(invalidate-catalog-cache!)

Clear the server-wide enriched-db catalog cache. Called from every DDL exec branch (via invalidate-schema-cache!). The cache key is only the user-schema hash + catalog-table-names, which does NOT change when a CREATE TYPE / ENUM / DOMAIN adds a registry entity (new datoms under pre-existing idents) or when ALTER changes a column's typmod — so those would otherwise serve stale catalog rows (e.g. a 2nd composite invisible in pg_type). DDL is rare, so a full clear is the simplest correct fix.

Clear the server-wide enriched-db catalog cache. Called from every DDL
exec branch (via invalidate-schema-cache!). The cache key is only the
user-schema hash + catalog-table-names, which does NOT change when a
CREATE TYPE / ENUM / DOMAIN adds a registry *entity* (new datoms under
pre-existing idents) or when ALTER changes a column's typmod — so those
would otherwise serve stale catalog rows (e.g. a 2nd composite invisible
in pg_type). DDL is rare, so a full clear is the simplest correct fix.
sourceraw docstring

make-columns-optional!clj

source

make-ctxclj

source

materialize-arg!clj

source

nextval-marker?clj

source

null-guard-clausesclj

source

null-safeclj

source

param-ref?clj

source

ParamRefclj

source

parse-sqlclj

(parse-sql sql schema)
(parse-sql sql schema db)

Parse a SQL statement and return a translation result.

Returns one of: {:type :select :query <datalog-map> :find-aliases [...] ...} {:type :insert :tx-data [...] :count N} {:type :update :table str :ns str :assignments [...] :where-expr expr} {:type :delete :table str :ns str :where-expr expr} {:type :ddl-create :tx-data [...]} {:type :system :system-type keyword} {:type :error :message str}

Optional db parameter enables subquery execution during translation.

Three cache levels stack:

  • Result cache (*parse-cache*) — for SQL strings re-issued verbatim (pgjdbc unnamed prepared statements, ORM select-by-id).
  • Lexical INSERT-VALUES templating + AST cache — for INSERT INTO t [(cols)] VALUES (lit, …) shapes the literals are captured, the SQL is normalised to (? , …), and the resulting AST is reused across all rows of the same shape. Translation still runs per row with *bound-params* bound so JdbcParameter nodes resolve to concrete values inline (no ParamRef closure captures).
  • AST cache (*ast-cache*) — covers everything else that hits JSqlParser, repeated or not.
Parse a SQL statement and return a translation result.

Returns one of:
  {:type :select :query <datalog-map> :find-aliases [...] ...}
  {:type :insert :tx-data [...] :count N}
  {:type :update :table str :ns str :assignments [...] :where-expr expr}
  {:type :delete :table str :ns str :where-expr expr}
  {:type :ddl-create :tx-data [...]}
  {:type :system :system-type keyword}
  {:type :error :message str}

Optional db parameter enables subquery execution during translation.

Three cache levels stack:
  - **Result cache** (`*parse-cache*`) — for SQL strings re-issued
    verbatim (pgjdbc unnamed prepared statements, ORM select-by-id).
  - **Lexical INSERT-VALUES templating** + AST cache — for `INSERT
    INTO t [(cols)] VALUES (lit, …)` shapes the literals are
    captured, the SQL is normalised to `(? , …)`, and the resulting
    AST is reused across all rows of the same shape. Translation
    still runs per row with `*bound-params*` bound so JdbcParameter
    nodes resolve to concrete values inline (no ParamRef closure
    captures).
  - **AST cache** (`*ast-cache*`) — covers everything else that
    hits JSqlParser, repeated or not.
sourceraw docstring

register-catalog-table!clj

source

resolve-columnclj

source

resolve-inherited-attrclj

source

resolve-nextvals!clj

source

sql-*clj

source

sql-+clj

source

sql--clj

source

sql-divclj

source

sql-modclj

source

substitute-paramsclj

source

system-query?clj

source

translate-predicateclj

source

unregister-catalog-table!clj

source

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close