Skip to main content

sqlrite/
lib.rs

1//! SQLRite — a small SQLite clone written in Rust, as a library.
2//!
3//! The REPL binary (`src/main.rs`) uses this library. The Tauri desktop
4//! app under `desktop/src-tauri/` uses it too. Future consumers — a
5//! `Connection` API split, a WASM build, a C FFI shim — all grow out of
6//! this same surface.
7//!
8//! **Scope right now.** The library surfaces two layers:
9//!
10//! **1. High-level public API (Phase 5a).** The shape most callers
11//! want — stable, documented, and the same surface that the C FFI
12//! shim (Phase 5b) and every language SDK (Python / Node / Go /
13//! WASM) binds against:
14//!
15//! - [`Connection`] — open a file, in-memory DB, or read-only view
16//! - [`Statement`] — prepared SQL with typed row iteration
17//! - [`Rows`] / [`Row`] / [`OwnedRow`] — streaming typed result rows
18//! - [`FromValue`] — pluggable row-to-Rust conversion (`i64`,
19//!   `f64`, `String`, `bool`, `Option<T>`, plus raw `Value`)
20//!
21//! ```no_run
22//! use sqlrite::Connection;
23//! let mut conn = Connection::open("foo.sqlrite")?;
24//! conn.execute("INSERT INTO users (name) VALUES ('alice')")?;
25//! let mut stmt = conn.prepare("SELECT id, name FROM users")?;
26//! let mut rows = stmt.query()?;
27//! while let Some(row) = rows.next()? {
28//!     let (id, name): (i64, String) = (row.get(0)?, row.get(1)?);
29//!     println!("{id}: {name}");
30//! }
31//! # Ok::<(), sqlrite::SQLRiteError>(())
32//! ```
33//!
34//! **2. Lower-level engine surface.** Accessible via `sqlrite::sql::…`
35//! for the REPL, the Tauri desktop app, and the engine's own tests:
36//!
37//! - [`Database`] — the in-memory state owning all tables
38//! - [`process_command`] — parse + execute one SQL statement (returns
39//!   the rendered status string the REPL prints)
40//! - [`open_database`] / [`open_database_read_only`] / [`save_database`] —
41//!   file I/O primitives (shared-lock read-only variant added in Phase 4e)
42//! - [`AccessMode`] — the enum driving exclusive vs shared lock acquisition
43//! - [`Result`] / [`SQLRiteError`] — the error surface
44//!
45//! Lower-level modules (`sql::pager`, `sql::executor`, etc.) remain
46//! accessible through `sqlrite::sql::…` for tests and tooling, but
47//! aren't considered public API — their shapes will change as Phase 4
48//! (WAL + locks) and Phase 5 (cursor / lazy-load) land.
49
50#[macro_use]
51extern crate prettytable;
52
53// `sqlrite::ask` is always available — its `schema` submodule (the
54// CREATE TABLE dump used to ground the LLM's prompt) is pure-engine
55// and useful even for builds that don't enable the `ask` feature.
56// The `ConnectionAskExt` trait + `ask` / `ask_with_database`
57// helpers inside the module are gated under the feature, since
58// they pull in `sqlrite-ask`. The WASM SDK uses
59// `sqlrite::ask::schema::dump_schema_for_database` to introspect a
60// browser-side `Connection` without needing the HTTP transport.
61pub mod ask;
62pub mod connection;
63pub mod error;
64pub mod sql;
65
66// Phase 5a public API.
67pub use connection::{Connection, FromValue, OwnedRow, Row, Rows, Statement};
68
69// Phase 7g.2: re-export the `Connection::ask` extension trait + the
70// public `sqlrite_ask` types at the crate root when the `ask`
71// feature is on. Lets callers write `use sqlrite::{Connection,
72// ConnectionAskExt, ask::AskConfig};` rather than dragging in
73// `sqlrite-ask` as a separate dep just to reach `AskConfig`. Only
74// available with the `ask` feature.
75#[cfg(feature = "ask")]
76pub use ask::ConnectionAskExt;
77
78// Underlying types useful to public-API callers (Value for typed
79// comparisons / untyped `row.get::<Value>(0)` access).
80pub use sql::db::table::Value;
81
82// Lower-level engine surface — public for the REPL, Tauri app, and
83// the engine's own tests. Not part of the stable public contract;
84// layered above these for most users is `Connection`.
85pub use error::{Result, SQLRiteError};
86pub use sql::db::database::Database;
87pub use sql::pager::{
88    AccessMode, MASTER_TABLE_NAME, open_database, open_database_read_only, open_database_with_mode,
89    save_database,
90};
91pub use sql::process_command;
92
93// Re-export sqlparser so downstream crates (the Tauri desktop app, the
94// eventual WASM bindings) can reach into the AST without pulling a
95// second copy as a direct dep. Using `pub extern crate` so it's
96// namespaced under `sqlrite::sqlparser::…`.
97pub use ::sqlparser;