hyperdb_api/lib.rs
1// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Pure Rust API for Hyper database.
5//!
6//! This crate provides a safe, idiomatic Rust interface for working with
7//! Hyper database files (.hyper). It is a pure-Rust implementation using
8//! the `PostgreSQL` wire protocol with Hyper-specific extensions.
9//!
10//! # Architecture
11//!
12//! This is a layered API built from four crates:
13//! - `hyper-types` — Type definitions with `LittleEndian` encoding
14//! - `hyper-protocol` — Wire protocol with `HyperBinary` COPY support
15//! - `hyper-client` — Sync/async TCP and gRPC clients
16//! - `hyperdb-api` — High-level API (this crate)
17//!
18//! Optional companion crates:
19//! - `sea-query-hyperdb` — `HyperDB` SQL dialect backend for `sea-query`
20//! - `hyperdb-api-salesforce` — Salesforce Data Cloud OAuth authentication
21//! - `hyperdb-api-derive` — Proc-macro `#[derive(FromRow)]` (re-exported by this crate)
22//!
23//! # Quick Start
24//!
25//! ```no_run
26//! use hyperdb_api::{HyperProcess, Connection, CreateMode, Result};
27//!
28//! fn main() -> Result<()> {
29//! let hyper = HyperProcess::new(None, None)?;
30//! let conn = Connection::new(&hyper, "example.hyper", CreateMode::CreateIfNotExists)?;
31//!
32//! conn.execute_command("CREATE TABLE test (id INT, name TEXT)")?;
33//! conn.execute_command("INSERT INTO test VALUES (1, 'Hello')")?;
34//!
35//! let mut result = conn.execute_query("SELECT * FROM test")?;
36//! while let Some(chunk) = result.next_chunk()? {
37//! for row in &chunk {
38//! let id: Option<i32> = row.get(0);
39//! let name: Option<String> = row.get(1);
40//! println!("id: {:?}, name: {:?}", id, name);
41//! }
42//! }
43//! Ok(())
44//! }
45//! ```
46//!
47//! # Lifetime Safety
48//!
49//! The API uses lifetime annotations to provide compile-time guarantees that
50//! resources are used correctly. All dependent types ([`Inserter`],
51//! [`Catalog`], [`Rowset`], [`Transaction`]) carry a `'conn` lifetime
52//! parameter tying them to the [`Connection`] they borrow:
53//!
54//! ```text
55//! Connection (owns underlying client)
56//! ├── Inserter<'conn>
57//! │ └── CopyInWriter<'conn>
58//! ├── Catalog<'conn>
59//! ├── Rowset<'conn>
60//! └── Transaction<'conn>
61//! ```
62//!
63//! This is a **simple hierarchical design**, not a complex lifetime web:
64//! - **Single root owner**: `Connection` owns the underlying client
65//! - **Simple borrows**: All dependent types borrow `&'conn Connection`
66//! - **No circular references**: `Inserter` doesn't reference `Catalog`, etc.
67//! - **Single lifetime parameter**: Just one `'conn` — no multi-lifetime bounds
68//!
69//! The Rust borrow checker enforces that you cannot drop or move a `Connection`
70//! while any dependent type holds a reference to it:
71//!
72//! ```compile_fail
73//! # use hyperdb_api::{Connection, Inserter, CreateMode};
74//! # fn example() -> hyperdb_api::Result<()> {
75//! let conn = Connection::connect("localhost:7483", "test.hyper", CreateMode::CreateIfNotExists)?;
76//! let inserter = Inserter::new(&conn, /* ... */)?;
77//! drop(conn); // ERROR: cannot move `conn` because it is borrowed by `inserter`
78//! # Ok(())
79//! # }
80//! ```
81//!
82//! The `execute(self)` method on [`Inserter`] takes ownership (`self`), which
83//! automatically ends the borrow when the insert completes — no manual cleanup
84//! needed.
85//!
86//! # Key Types
87//!
88//! - [`Connection`] / [`AsyncConnection`] — Sync and async database connections
89//! - [`HyperProcess`] — Manage a local `hyperd` server process
90//! - [`Inserter`] / [`MappedInserter`] / [`AsyncInserter`] — Bulk row insertion (`HyperBinary` COPY)
91//! - [`ArrowInserter`] / [`AsyncArrowInserter`] — Arrow `RecordBatch` insertion
92//! - [`Catalog`] — Schema/table introspection
93//! - [`TableDefinition`] — Define table schemas
94//! - [`Transaction`] / [`AsyncTransaction`] — RAII transaction guards
95//!
96//! # Public Modules
97//!
98//! - [`copy`] — CSV/text export and import via COPY protocol
99//! - [`pool`] — Async connection pooling (deadpool-based)
100//! - [`grpc`] — gRPC transport types for Arrow IPC queries
101//!
102//! # Bulk Data Loading
103//!
104//! Several inserter APIs are available depending on your data format and runtime model:
105//! - [`Inserter`] / [`MappedInserter`] — Sync `HyperBinary` row-by-row
106//! - [`AsyncInserter`] — Async `HyperBinary` row-by-row (mirrors [`Inserter`])
107//! - [`ArrowInserter`] — Sync Arrow IPC (batch or streaming `RecordBatch`)
108//! - [`AsyncArrowInserter`] — Async Arrow IPC
109//! - [`copy`] module — CSV/TSV/delimited text import & export
110//!
111//! # Authentication
112//!
113//! The client supports multiple authentication methods (Trust, Cleartext, MD5, SCRAM-SHA-256):
114//!
115//! ```no_run
116//! use hyperdb_api::{Connection, CreateMode, Result};
117//!
118//! fn main() -> Result<()> {
119//! let conn = Connection::connect_with_auth(
120//! "localhost:7483",
121//! "example.hyper",
122//! CreateMode::CreateIfNotExists,
123//! "myuser",
124//! "mypassword",
125//! )?;
126//! Ok(())
127//! }
128//! ```
129
130#![warn(missing_docs, rust_2018_idioms, clippy::all)]
131
132mod arrow_inserter;
133/// Semantic version of this crate, resolved at compile time from
134/// `Cargo.toml`. Used by downstream tools (notably `hyperdb-mcp`) to
135/// surface the library version in their own status output without
136/// duplicating the version string.
137pub const VERSION: &str = env!("CARGO_PKG_VERSION");
138
139mod arrow_reader;
140mod arrow_result;
141mod async_arrow_inserter;
142mod async_connection;
143mod async_connection_builder;
144mod async_inserter;
145mod async_prepared;
146mod async_result;
147mod async_transaction;
148mod async_transport;
149mod catalog;
150mod connection;
151mod connection_builder;
152/// CSV/text export and import via COPY protocol.
153pub mod copy;
154mod data_format;
155mod error;
156mod inserter;
157mod names;
158mod params;
159/// Connection pooling support.
160pub mod pool;
161mod prepared;
162mod process;
163mod query_result;
164pub(crate) mod query_stats;
165mod result;
166mod row_accessor;
167mod server_version;
168mod table_definition;
169mod transaction;
170mod transport;
171
172mod grpc_connection;
173#[cfg(kani)]
174mod proofs;
175
176pub use arrow_inserter::ArrowInserter;
177pub use arrow_reader::ArrowReader;
178pub use arrow_result::{
179 parse_arrow_ipc, ArrowChunk, ArrowRow, ArrowRowset, ChunkSource, FromArrowValue,
180};
181pub use async_arrow_inserter::{AsyncArrowInserter, AsyncArrowInserterOwned};
182pub use async_connection::AsyncConnection;
183pub use async_connection_builder::AsyncConnectionBuilder;
184pub use async_inserter::AsyncInserter;
185pub use async_prepared::{AsyncPreparedStatement, AsyncPreparedStatementOwned};
186pub use async_result::AsyncRowset;
187pub use catalog::Catalog;
188pub use connection::{Connection, CreateMode, ScalarValue};
189pub use connection_builder::ConnectionBuilder;
190pub use error::{ColumnErrorKind, Error, Result};
191pub use params::ToSqlParam;
192pub use prepared::PreparedStatement;
193// Re-export Notice for callback registrants. ErrorKind is intentionally
194// NOT re-exported — callers match directly on the flat `Error` enum.
195pub use async_transaction::AsyncTransaction;
196pub use hyperdb_api_core::client::{Notice, NoticeReceiver};
197pub use inserter::{ChunkSender, ColumnMapping, InsertChunk, Inserter, IntoValue, MappedInserter};
198pub use names::{
199 escape_name, escape_sql_path, escape_string_literal, DatabaseName, Name, SchemaName, TableName,
200};
201pub use process::{HyperProcess, ListenMode, Parameters, TransportMode};
202pub use query_stats::{LogFileStatsProvider, QueryStats, QueryStatsProvider};
203pub use result::{FromRow, ResultColumn, ResultSchema, Row, RowIterator, RowValue, Rowset};
204pub use row_accessor::RowAccessor;
205
206// Re-export the `#[derive(FromRow)]` proc-macro from the companion
207// crate so callers don't need to add `hyperdb-api-derive` as a direct
208// dependency. Same pattern as serde / thiserror.
209pub use hyperdb_api_derive::FromRow;
210pub use server_version::ServerVersion;
211pub use table_definition::{ColumnDefinition, Persistence, TableDefinition};
212pub use transaction::Transaction;
213
214// Re-export types from hyperdb-api-core's types layer.
215pub use hyperdb_api_core::types::{
216 Date, Geography, Interval, Nullability, Numeric, OffsetTimestamp, Oid, SqlType, Time,
217 Timestamp, Type,
218};
219
220/// Re-export of `GeoError` from hyperdb-api-core::types.
221pub use hyperdb_api_core::types::GeoError;
222
223/// Re-export of the PostgreSQL OID constants. Access as `hyperdb_api::oids::INT4` etc.
224pub use hyperdb_api_core::types::oids;
225
226// Re-export gRPC types (always available)
227pub mod grpc {
228 //! gRPC transport types for Hyper database access.
229 //!
230 //! This module provides two ways to use gRPC:
231 //!
232 //! 1. **Unified Connection** (recommended): Use `Connection::connect()` with an
233 //! `https://` or `http://` URL - transport is auto-detected.
234 //!
235 //! 2. **Direct gRPC**: Use `GrpcConnection` or `GrpcConnectionAsync` for
236 //! explicit gRPC access with full control over transfer modes and async.
237 //!
238 //! # Transfer Modes
239 //!
240 //! - `TransferMode::Sync` - All results in one response (simple, 100s timeout)
241 //! - `TransferMode::Async` - Header only, fetch results via `GetQueryResult`
242 //! - `TransferMode::Adaptive` - First chunk inline, rest streamed (default, recommended)
243
244 // Re-export connection types from grpc_connection module
245 pub use crate::grpc_connection::{GrpcConnection, GrpcConnectionAsync};
246
247 // Re-export types from hyperdb_api_core::client::grpc
248 pub use hyperdb_api_core::client::grpc::{
249 GrpcClient, GrpcClientSync, GrpcConfig, GrpcError, GrpcQueryResult, GrpcResultChunk,
250 TransferMode,
251 };
252}
253
254/// Macro for creating table definitions with a fluent syntax.
255///
256/// This macro simplifies the common pattern of creating table definitions
257/// with multiple columns by providing a more compact syntax.
258///
259/// # Syntax
260///
261/// ```text
262/// table! {
263/// "table_name" {
264/// "column_name": SqlType::type_name(), NULLABLE | NOT_NULL,
265/// // ... more columns
266/// }
267/// }
268/// ```
269///
270/// # Example
271///
272/// ```no_run
273/// # use hyperdb_api::{table, TableDefinition, SqlType, Result};
274/// # fn example() -> Result<()> {
275/// let orders = table! {
276/// "Orders" {
277/// "Address ID": SqlType::small_int(), NOT_NULL,
278/// "Customer ID": SqlType::text(), NOT_NULL,
279/// "Order Date": SqlType::date(), NOT_NULL,
280/// "Order ID": SqlType::text(), NOT_NULL,
281/// "Ship Date": SqlType::date(), NULLABLE,
282/// "Ship Mode": SqlType::text(), NULLABLE,
283/// }
284/// };
285///
286/// // Equivalent to:
287/// let orders_manual = TableDefinition::new("Orders")
288/// .add_required_column("Address ID", SqlType::small_int())
289/// .add_required_column("Customer ID", SqlType::text())
290/// .add_required_column("Order Date", SqlType::date())
291/// .add_required_column("Order ID", SqlType::text())
292/// .add_nullable_column("Ship Date", SqlType::date())
293/// .add_nullable_column("Ship Mode", SqlType::text());
294/// # Ok(())
295/// # }
296/// ```
297#[macro_export]
298macro_rules! table {
299 // Match table with schema.table syntax
300 ($schema:literal.$table:literal {
301 $($col_name:literal: $col_type:expr, $nullability:ident),* $(,)?
302 }) => {{
303 #[allow(unused_mut)]
304 let mut table_def = $crate::TableDefinition::new($table).with_schema($schema);
305 $(
306 table_def = table!(@add_column table_def, $col_name, $col_type, $nullability);
307 )*
308 table_def
309 }};
310
311 // Match simple table name
312 ($table:literal {
313 $($col_name:literal: $col_type:expr, $nullability:ident),* $(,)?
314 }) => {{
315 #[allow(unused_mut)]
316 let mut table_def = $crate::TableDefinition::new($table);
317 $(
318 table_def = table!(@add_column table_def, $col_name, $col_type, $nullability);
319 )*
320 table_def
321 }};
322
323 // Helper to add column based on nullability
324 (@add_column $table_def:expr, $col_name:literal, $col_type:expr, NULLABLE) => {
325 $table_def.add_nullable_column($col_name, $col_type)
326 };
327 (@add_column $table_def:expr, $col_name:literal, $col_type:expr, NOT_NULL) => {
328 $table_def.add_required_column($col_name, $col_type)
329 };
330}