bindgen_bundled_version.rs
Overview
This Rust source file provides raw FFI (Foreign Function Interface) bindings to the SQLite3 C API. It is automatically generated by rust-bindgen to allow Rust code to call SQLite3 functions, use its types, and access constants directly. The file exposes low-level SQLite3 functionality including database connection management, SQL statement preparation and execution, virtual file system (VFS) interfaces, virtual tables, memory management, concurrency control via mutexes, full-text search (FTS5) APIs, and change set/session management.
The file primarily consists of:
extern "C"blocks declaring SQLite3 functions.Definitions of SQLite3 C structs as Rust
#[repr(C)]structs.Constant definitions for SQLite3 error codes, flags, and options.
Type aliases matching SQLite3 integral types.
Function pointer typedefs representing callbacks used by SQLite3 extensions.
This file serves as the foundational low-level SQLite3 API interface for higher-level Rust wrappers and modules, enabling direct interoperability with the SQLite3 engine.
Key Constants
The file defines numerous SQLite3 constants representing:
Version information (e.g., SQLITE_VERSION, SQLITE_VERSION_NUMBER, SQLITE_SOURCE_ID).
SQLite3 result and error codes (e.g.,
SQLITE_OK, SQLITE_ERROR,SQLITE_ROW,SQLITE_DONE).Open flags for database connections (SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, etc.).
File control flags (SQLITE_FCNTL_*).
Locking states (SQLITE_LOCK_NONE, SQLITE_LOCK_SHARED, etc.).
Configuration options (SQLITE_CONFIG_*).
Limits for SQL statements (SQLITE_LIMIT_*).
Data types (SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, etc.).
Trace and status flags.
Virtual table flags and operations.
These constants are used throughout the SQLite3 API to control behavior and report statuses.
Primary Structs and Types
sqlite3
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct sqlite3 {
_unused: [u8; 0],
}
Represents an opaque SQLite3 database connection handle. It is used in most database-related function calls.
sqlite3_stmt
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct sqlite3_stmt {
_unused: [u8; 0],
}
Represents a prepared SQL statement object. Used for compiling and executing SQL queries.
sqlite3_file and sqlite3_io_methods
These structs define the interface for SQLite3's virtual file system abstraction.
sqlite3_filerepresents an open file handle.sqlite3_io_methodscontains function pointers for file operations like read, write, sync, lock, and unlocking.
This abstraction allows SQLite3 to be ported to different platforms by implementing these methods.
sqlite3_mutex and sqlite3_mutex_methods
Represents mutex objects used internally by SQLite3 to provide thread safety and concurrency control.
sqlite3_vfs
Defines the interface for SQLite3's Virtual File System layer, including methods for file operations, randomness, time, system calls, and dynamic loading.
sqlite3_value and sqlite3_context
Opaque types representing SQL values and user-defined function contexts respectively, used in creating and handling custom SQL functions.
fts5_api, Fts5Context, and related structs
These structs provide the API and context for SQLite3's Full-Text Search (FTS5) extension. They allow tokenizing, querying, and managing FTS5 virtual tables and extension functions.
Important Functions and Their Usage
sqlite3_auto_extension and sqlite3_cancel_auto_extension
pub fn sqlite3_auto_extension(
xEntryPoint: Option<unsafe extern "C" fn(db: *mut sqlite3, pzErrMsg: *mut *mut c_char, _: *const sqlite3_api_routines) -> c_int>,
) -> c_int;
pub fn sqlite3_cancel_auto_extension(
xEntryPoint: Option<unsafe extern "C" fn(db: *mut sqlite3, pzErrMsg: *mut *mut c_char, _: *const sqlite3_api_routines) -> c_int>,
) -> c_int;
Purpose: Register or unregister automatic extensions that SQLite initializes on each database connection.
Parameters:
xEntryPoint: Optional pointer to an extension initialization function.
Returns:
SQLITE_OKon success or an error code otherwise.Usage: Used to add or remove extensions that are automatically loaded when SQLite initializes a new database connection.
sqlite3_open and sqlite3_open_v2
pub fn sqlite3_open(filename: *const c_char, ppDb: *mut *mut sqlite3) -> c_int;
pub fn sqlite3_open_v2(filename: *const c_char, ppDb: *mut *mut sqlite3, flags: c_int, zVfs: *const c_char) -> c_int;
Purpose: Open a new SQLite database connection.
Parameters:
filename: Path to the database file.ppDb: Pointer to receive the database handle.flags: Open flags (forsqlite3_open_v2), controlling read/write mode, creation flags, etc.zVfs: Optional name of the VFS module to use.
Returns:
SQLITE_OKon success or an error code otherwise.Usage: Enables establishing a database connection for subsequent SQL operations.
sqlite3_prepare_v2 and sqlite3_prepare_v3
pub fn sqlite3_prepare_v2(
db: *mut sqlite3,
zSql: *const c_char,
nByte: c_int,
ppStmt: *mut *mut sqlite3_stmt,
pzTail: *mut *const c_char,
) -> c_int;
pub fn sqlite3_prepare_v3(
db: *mut sqlite3,
zSql: *const c_char,
nByte: c_int,
prepFlags: c_uint,
ppStmt: *mut *mut sqlite3_stmt,
pzTail: *mut *const c_char,
) -> c_int;
Purpose: Compile an SQL statement into a prepared statement object.
Parameters:
db: The database handle.zSql: SQL query string.nByte: Length of the SQL string.ppStmt: Pointer to receive the prepared statement.pzTail: Pointer to unused portion of the SQL.prepFlags: Preparation flags (forsqlite3_prepare_v3).
Returns:
SQLITE_OKon success or an error code otherwise.Usage: Prepare statements for execution with binding parameters and stepping through results.
sqlite3_step
pub fn sqlite3_step(pStmt: *mut sqlite3_stmt) -> c_int;
Purpose: Execute a prepared statement or advance to the next result row.
Parameters:
pStmt- prepared statement pointer.Returns: One of
SQLITE_ROW(result row available) orSQLITE_DONE(execution complete).Usage: Drives the execution of SQL queries, typically in a loop to iterate over result rows.
sqlite3_finalize
pub fn sqlite3_finalize(pStmt: *mut sqlite3_stmt) -> c_int;
Purpose: Delete a prepared statement and release resources.
Parameters:
pStmt- prepared statement pointer.Returns:
SQLITE_OKon success or error code.Usage: Clean up statements after execution to avoid resource leaks.
sqlite3_bind_* functions
Functions like sqlite3_bind_int, sqlite3_bind_text, sqlite3_bind_blob, etc., bind Rust data to SQL statement parameters by index.
Example:
pub fn sqlite3_bind_int(stmt: *mut sqlite3_stmt, index: c_int, value: c_int) -> c_int;
Purpose: Bind an integer value to a prepared statement parameter.
Parameters:
stmt: Prepared statement pointer.index: Parameter index (1-based).value: Integer value to bind.
Returns:
SQLITE_OKor error code.Usage: Used before
sqlite3_stepto supply values for parameterized queries.
sqlite3_column_* functions
Functions like sqlite3_column_int, sqlite3_column_text, etc., retrieve column values from the current row of a result set.
Example:
pub fn sqlite3_column_text(stmt: *mut sqlite3_stmt, col: c_int) -> *const c_uchar;
Purpose: Get text from a result column.
Parameters:
stmt: Prepared statement pointer.col: Column index (0-based).
Returns: Pointer to UTF-8 text.
Usage: Called after
sqlite3_stepreturnsSQLITE_ROWto access column data.
sqlite3_exec
pub fn sqlite3_exec(
db: *mut sqlite3,
sql: *const c_char,
callback: Option<unsafe extern "C" fn(*mut c_void, c_int, *mut *mut c_char, *mut *mut c_char) -> c_int>,
arg: *mut c_void,
errmsg: *mut *mut c_char,
) -> c_int;
Purpose: Convenience function to execute SQL without preparing statements.
Parameters:
db: Database handle.sql: SQL string.callback: Optional callback for each row.arg: Pointer passed to callback.errmsg: Pointer to error message string.
Returns: Result code.
Usage: Simple execution of SQL commands with optional row processing.
sqlite3_create_function_v2
Allows creation of custom SQL scalar or aggregate functions.
pub fn sqlite3_create_function_v2(
db: *mut sqlite3,
zFunctionName: *const c_char,
nArg: c_int,
eTextRep: c_int,
pApp: *mut c_void,
xFunc: Option<unsafe extern "C" fn(*mut sqlite3_context, c_int, *mut *mut sqlite3_value)>,
xStep: Option<unsafe extern "C" fn(*mut sqlite3_context, c_int, *mut *mut sqlite3_value)>,
xFinal: Option<unsafe extern "C" fn(*mut sqlite3_context)>,
xDestroy: Option<unsafe extern "C" fn(*mut c_void)>,
) -> c_int;
Purpose: Register new SQL functions implemented in Rust/C.
Parameters: Function name, argument count, text encoding, function callbacks for scalar/aggregate processing, and destructor.
Usage: Extend SQLite3 with custom functions.
sqlite3_vfs and VFS Interface Functions
The sqlite3_vfs struct and related functions (sqlite3_vfs_find, sqlite3_vfs_register, sqlite3_vfs_unregister) allow SQLite3 to interact with different underlying file systems or OS abstractions.
Usage: Implement custom VFS modules or register existing ones.
sqlite3_backup API
Functions like sqlite3_backup_init, sqlite3_backup_step, and sqlite3_backup_finish provide API to copy data between databases incrementally.
sqlite3_mutex API
Mutex allocation, deallocation, and locking functions manage concurrency control in SQLite.
Full-Text Search (FTS5) Extension API
Types:
Fts5Context,Fts5ExtensionApi,Fts5Tokenizer,fts5_tokenizer_v2.Functions to create and manage tokenizers, extension functions, and query processing.
Enables indexing and querying text data efficiently.
Change Set and Session APIs
sqlite3_session,sqlite3_changeset_iter,sqlite3_changegroup, andsqlite3_rebaserstructs.Functions to create, apply, iterate, and manage change sets for synchronization and replication.
Support streaming APIs for incremental change application.
Interactions with Other System Parts
This file exposes the core SQLite3 API for use by higher-level Rust modules managing database operations, custom extensions, virtual tables, full-text search features, and synchronization mechanisms. It interoperates primarily with:
Rust wrappers providing safe, idiomatic interfaces.
FTS5 modules for text indexing and search.
Custom VFS implementations for platform-specific storage.
Change tracking and session modules for data synchronization.
Important Implementation Details
All functions are declared as
unsafe extern "C"to indicate FFI calls to native SQLite3 C library functions.Opaque structs with zero-sized arrays represent C opaque pointers.
Constants provide compile-time definitions matching SQLite3's C header constants.
Function pointers model SQLite3 callbacks for extensions, virtual tables, and custom functions.
The design adheres closely to the SQLite3 C API specification, ensuring compatibility.
Selected Usage Examples
Opening a Database and Executing a Simple Query
use std::ffi::CString;
unsafe {
let db_filename = CString::new("test.db").unwrap();
let mut db: *mut sqlite3 = std::ptr::null_mut();
let rc = sqlite3_open(db_filename.as_ptr(), &mut db);
if rc != SQLITE_OK {
// Handle open error
}
let sql = CString::new("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT);").unwrap();
let rc = sqlite3_exec(db, sql.as_ptr(), None, std::ptr::null_mut(), std::ptr::null_mut());
if rc != SQLITE_OK {
// Handle exec error
}
sqlite3_close(db);
}
Preparing and Executing a Parameterized Statement
use std::ffi::CString;
unsafe {
let sql = CString::new("INSERT INTO users(name) VALUES(?1);").unwrap();
let mut stmt: *mut sqlite3_stmt = std::ptr::null_mut();
let rc = sqlite3_prepare_v2(db, sql.as_ptr(), -1, &mut stmt, std::ptr::null_mut());
if rc == SQLITE_OK {
let name = CString::new("Alice").unwrap();
sqlite3_bind_text(stmt, 1, name.as_ptr(), -1, None);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
}
Mermaid Flowchart Diagram of Main Functions
flowchart TD
A[sqlite3_open / sqlite3_open_v2] --> B[sqlite3_prepare_v2 / sqlite3_prepare_v3]
B --> C[sqlite3_bind_* functions]
C --> D[sqlite3_step]
D -->|SQLITE_ROW| E[Retrieve data with sqlite3_column_*]
D -->|SQLITE_DONE| F[sqlite3_finalize]
A --> G[sqlite3_exec]
A --> H[sqlite3_close]
I[sqlite3_auto_extension] --> A
J[sqlite3_create_function_v2] --> B
K[sqlite3_vfs_register / sqlite3_vfs_unregister] --> A
This diagram depicts the typical flow for opening a database, preparing and executing SQL statements, binding parameters, retrieving results, and finalizing resources. It also shows registration of auto-extensions, custom functions, and virtual file systems.
Reference to Related Topics
For detailed usage and explanation of SQLite3 APIs, error codes, virtual tables, mutexes, and full-text search, see: