catalog #
A pluggable store interface that SQL engines run over.
A B-tree database, a Parquet file and an Irmin tree can all hold tables, and
without a shared interface each would need its own engine to answer the same
SELECT. catalog is that interface. A store implements one small contract,
Catalog.Spi, which lists its tables and their columns and gives a row scan,
a point lookup and, where the store allows them, writes; every engine built
on this library then runs over that store. The design follows SQLite's
virtual tables, the foreign-data wrappers of SQL/MED and the TableProvider
trait of Apache DataFusion.
Installation #
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install catalog
Place in the stack #
catalog is the base of a pure-OCaml SQLite engine and depends on nothing
but fmt. It holds stores only: the values a store exchanges, the store
interface and the in-memory reference store. It holds no engine, no SQL
dialect and no query language.
- sql is the engine and the
sqlite3 dialect: it parses SQL text, holds the typed tables and queries an
application declares (
Sql.Table,Sql.Query), and runs both over any catalog. - btree is the SQLite file format's b-trees and records.
- sqlite runs the same
typed queries over SQLite database files with
Sqlite.run, and presents a database as aCatalog.twithSqlite.catalog.
A store that wants to be queried implements Catalog.Spi; an application
declares its tables with sql and runs its queries with sql or sqlite.
Usage #
Catalog.of_mem builds a catalog over the in-memory reference store, which
an engine reads and writes by table name and values alone:
let db =
Catalog.of_mem
[ ("users", { Catalog.Spi.Mem.columns = [ "id"; "name" ]; rows = [] }) ]
let () =
ignore (Catalog.Spi.insert db "users" Catalog.Value.[ Int 7L; Text "alice" ]);
ignore (Catalog.Spi.insert db "users" Catalog.Value.[ Int 42L; Text "bob" ]);
assert (Catalog.Spi.columns db "users" = [ "id"; "name" ]);
assert (List.length (Catalog.Spi.rows db "users") = 2)
Stores #
A store hands its rows over as a Catalog.Cursor, which decodes each column
it is asked for straight into a Catalog.Register, the mutable form of a
Catalog.Value. An engine reuses the same registers for every row, so a
scan allocates for the registers it reads into and not for each row. A store
may also answer a point lookup on an equality (Spi.index_scan), report the
best index for one (Spi.best_index_for) and give statistics for the
planner (Spi.stats).
API #
Catalog.Spiis the interface a store implements, withSpi.Memas the in-memory reference store andCatalog.of_mema catalog over it.Catalog.CursorandCatalog.Registerare how a store hands its rows to an engine.Catalog.Valueis the SQL values,Catalog.Errorthe errors, named after the sqlite3 result codes, andCatalog.Identthe case-folded names.
The .mli files under lib/ document the full interface.
Licence #
ISC. See LICENSE.md.