To define a table, transact a document into XTDB:
{:xt/id :xtdb.sql/person-schema
:xtdb.sql/table-name "person"
:xtdb.sql/table-query '{:find [id name homeworld]
:where [[id :name name]
[id :homeworld homeworld]]}
:xtdb.sql/table-columns '{id :keyword, name :varchar, homeworld :varchar}}
A SQL table is a schema that maps XTDB attributes to SQL table columns.
Any document in XTDB that matches the :xtdb.sql/table-query
in the schema document is eligible to be returned via the SQL query.
SQL table column names are mapped from the symbols used by the :xtdb.sql/table-query
query backing the table and referred to by :xtdb.sql/table-columns
.
Note that in the case where symbols are prefixed with ?
, then ?
is stripped for the SQL column name.
For example with the following XTDB transaction operation:
[::xt/put {:xt/id :ivan :name "Ivan" :homeworld "Earth"}]
Get a connection and query as such:
(require '[xtdb.calcite])
(defn query [node q]
(with-open [conn (xtdb.calcite/jdbc-connection node)]
(let [stmt (.createStatement conn)]
(->> q (.executeQuery stmt) resultset-seq))))
(query "SELECT PERSON.NAME FROM PERSON")
Note that using JDBC PreparedStatements for queries will be faster.