Initial commit: zsqlite - SQLite wrapper for Zig

- SQLite 3.47.2 amalgamation compiled directly into binary
- Idiomatic Zig API (Database, Statement, errors)
- Full test suite passing
- Basic example with CRUD operations
- Zero runtime dependencies

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
reugenio 2025-12-08 16:45:28 +01:00
commit a290859182
9 changed files with 309238 additions and 0 deletions

10
.gitignore vendored Normal file
View file

@ -0,0 +1,10 @@
zig-cache/
zig-out/
.zig-cache/
*.o
*.a
*.so
*.db
*.db-journal
*.db-wal
*.db-shm

184
CLAUDE.md Normal file
View file

@ -0,0 +1,184 @@
# zsqlite - SQLite Wrapper para Zig
> **Fecha creación**: 2025-12-08
> **Versión Zig**: 0.15.2
> **Estado**: En desarrollo inicial
## Descripción del Proyecto
Wrapper idiomático de SQLite para Zig. Compila SQLite amalgamation directamente en el binario, resultando en un ejecutable único sin dependencias externas.
**Filosofía**:
- Zero dependencias runtime
- API idiomática Zig (errores, allocators, iteradores)
- Binario único y portable
- Compatible con bases de datos SQLite existentes
## Arquitectura
```
zsqlite/
├── CLAUDE.md # Este archivo
├── build.zig # Sistema de build
├── src/
│ ├── root.zig # Exports públicos
│ ├── sqlite.zig # Wrapper principal
│ ├── statement.zig # Prepared statements
│ ├── row.zig # Iterador de filas
│ └── errors.zig # Mapeo de errores SQLite
├── vendor/
│ ├── sqlite3.c # SQLite amalgamation
│ └── sqlite3.h # Headers SQLite
└── examples/
└── basic.zig # Ejemplo básico
```
## API Objetivo
```zig
const std = @import("std");
const sqlite = @import("zsqlite");
pub fn main() !void {
var db = try sqlite.open("test.db");
defer db.close();
// Ejecutar SQL directo
try db.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)");
// Prepared statement con parámetros
var stmt = try db.prepare("INSERT INTO users (name) VALUES (?)");
defer stmt.deinit();
try stmt.bind(.{ "Alice" });
try stmt.exec();
stmt.reset();
try stmt.bind(.{ "Bob" });
try stmt.exec();
// Query con iterador
var query = try db.prepare("SELECT id, name FROM users");
defer query.deinit();
while (try query.step()) |row| {
const id = row.int(0);
const name = row.text(1);
std.debug.print("User {}: {s}\n", .{ id, name });
}
}
```
## Funcionalidades Planificadas
### Fase 1 - Core (Actual)
- [x] Estructura proyecto
- [ ] Compilar SQLite amalgamation
- [ ] Abrir/cerrar bases de datos
- [ ] Ejecutar SQL simple (exec)
- [ ] Prepared statements básicos
- [ ] Bind de parámetros (int, text, blob, null)
- [ ] Iterador de resultados
### Fase 2 - Transacciones y Errores
- [ ] BEGIN/COMMIT/ROLLBACK helpers
- [ ] Mapeo completo errores SQLite
- [ ] SAVEPOINT support
### Fase 3 - Avanzado
- [ ] Blob streaming (para archivos grandes)
- [ ] User-defined functions
- [ ] Collations personalizadas
- [ ] Backup API
## SQLite Amalgamation
Usamos SQLite amalgamation (sqlite3.c + sqlite3.h) que es la forma recomendada de embeber SQLite. Es un único archivo .c con todo el código de SQLite.
**Versión objetivo**: SQLite 3.45+ (última estable)
**Flags de compilación recomendados**:
```
-DSQLITE_DQS=0 # Disable double-quoted strings
-DSQLITE_THREADSAFE=0 # Single-threaded (más rápido)
-DSQLITE_DEFAULT_MEMSTATUS=0 # Disable memory tracking
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS
-DSQLITE_OMIT_DEPRECATED
-DSQLITE_OMIT_SHARED_CACHE
-DSQLITE_USE_ALLOCA
-DSQLITE_ENABLE_FTS5 # Full-text search
-DSQLITE_ENABLE_JSON1 # JSON functions
```
## Compilación
```bash
# Descargar SQLite amalgamation (una sola vez)
cd vendor
curl -O https://sqlite.org/2024/sqlite-amalgamation-3450000.zip
unzip sqlite-amalgamation-3450000.zip
mv sqlite-amalgamation-3450000/* .
rm -rf sqlite-amalgamation-3450000*
# Compilar
zig build
# Ejecutar tests
zig build test
# Ejecutar ejemplo
zig build example
```
## Diferencias con otros wrappers
| Característica | zsqlite | zig-sqlite | zqlite.zig |
|----------------|---------|------------|------------|
| Compila SQLite | Sí | No (linkea) | Sí |
| API idiomática | Sí | Parcial | Sí |
| Zero alloc option | Planificado | No | No |
| Zig 0.15 | Sí | ? | ? |
## Uso en simifactu-zig (futuro)
Este wrapper está diseñado para ser drop-in replacement del uso de SQLite en simifactu-fyne, soportando:
- Foreign keys
- Transactions
- Prepared statements
- BLOB storage
- JSON functions
---
## Equipo y Metodología
### Normas de Trabajo
**IMPORTANTE**: Todas las normas de trabajo están en:
```
/mnt/cello2/arno/re/recode/TEAM_STANDARDS/
```
### Control de Versiones
```bash
# Remote
git remote: git@git.reugenio.com:reugenio/zsqlite.git
# Branches
main # Código estable
develop # Desarrollo activo
```
### Zig Path
```bash
ZIG=/mnt/cello2/arno/re/recode/zig/zig-0.15.2/zig-x86_64-linux-0.15.2/zig
```
---
## Notas de Desarrollo
*Se irán añadiendo conforme avance el proyecto*

72
build.zig Normal file
View file

@ -0,0 +1,72 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// SQLite compile flags for optimal embedded use
const sqlite_flags: []const []const u8 = &.{
"-DSQLITE_DQS=0", // Disable double-quoted strings as identifiers
"-DSQLITE_THREADSAFE=0", // Single-threaded for speed
"-DSQLITE_DEFAULT_MEMSTATUS=0", // Disable memory usage tracking
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1", // Normal WAL sync
"-DSQLITE_LIKE_DOESNT_MATCH_BLOBS", // LIKE doesn't match BLOBs
"-DSQLITE_OMIT_DEPRECATED", // Remove deprecated APIs
"-DSQLITE_OMIT_SHARED_CACHE", // No shared cache
"-DSQLITE_ENABLE_FTS5", // Full-text search v5
"-DSQLITE_ENABLE_JSON1", // JSON functions
"-DSQLITE_ENABLE_RTREE", // R-Tree for geospatial
"-DSQLITE_OMIT_LOAD_EXTENSION", // No dynamic extensions
};
// zsqlite module - includes SQLite C compilation
const zsqlite_mod = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
zsqlite_mod.addIncludePath(b.path("vendor"));
zsqlite_mod.addCSourceFile(.{
.file = b.path("vendor/sqlite3.c"),
.flags = sqlite_flags,
});
// Tests
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
unit_tests.root_module.addIncludePath(b.path("vendor"));
unit_tests.root_module.addCSourceFile(.{
.file = b.path("vendor/sqlite3.c"),
.flags = sqlite_flags,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
// Example: basic
const basic_exe = b.addExecutable(.{
.name = "basic",
.root_module = b.createModule(.{
.root_source_file = b.path("examples/basic.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "zsqlite", .module = zsqlite_mod },
},
}),
});
b.installArtifact(basic_exe);
const run_basic = b.addRunArtifact(basic_exe);
run_basic.step.dependOn(b.getInstallStep());
const basic_step = b.step("basic", "Run basic example");
basic_step.dependOn(&run_basic.step);
}

153
examples/basic.zig Normal file
View file

@ -0,0 +1,153 @@
//! Basic example demonstrating zsqlite usage
//!
//! This example shows:
//! - Opening a database
//! - Creating tables
//! - Inserting data with prepared statements
//! - Querying data
//! - Transactions
const std = @import("std");
const sqlite = @import("zsqlite");
pub fn main() !void {
std.debug.print("zsqlite basic example\n", .{});
std.debug.print("SQLite version: {s}\n\n", .{sqlite.version()});
// Open an in-memory database
var db = try sqlite.openMemory();
defer db.close();
// Enable foreign keys (recommended)
try db.setForeignKeys(true);
// Create tables
try db.exec(
\\CREATE TABLE users (
\\ id INTEGER PRIMARY KEY,
\\ name TEXT NOT NULL,
\\ email TEXT UNIQUE,
\\ created_at TEXT DEFAULT CURRENT_TIMESTAMP
\\);
\\
\\CREATE TABLE posts (
\\ id INTEGER PRIMARY KEY,
\\ user_id INTEGER NOT NULL REFERENCES users(id),
\\ title TEXT NOT NULL,
\\ content TEXT
\\);
);
std.debug.print("Tables created successfully\n\n", .{});
// Insert users with prepared statement
{
var stmt = try db.prepare("INSERT INTO users (name, email) VALUES (?, ?)");
defer stmt.finalize();
const users = [_][2][]const u8{
.{ "Alice", "alice@example.com" },
.{ "Bob", "bob@example.com" },
.{ "Charlie", "charlie@example.com" },
};
for (users) |user| {
try stmt.bindText(1, user[0]);
try stmt.bindText(2, user[1]);
_ = try stmt.step();
try stmt.reset();
try stmt.clearBindings();
}
std.debug.print("Inserted {} users\n", .{db.totalChanges()});
}
// Insert posts
{
var stmt = try db.prepare("INSERT INTO posts (user_id, title, content) VALUES (?, ?, ?)");
defer stmt.finalize();
try stmt.bindInt(1, 1); // Alice
try stmt.bindText(2, "Hello World");
try stmt.bindText(3, "This is my first post!");
_ = try stmt.step();
try stmt.reset();
try stmt.clearBindings();
try stmt.bindInt(1, 1); // Alice
try stmt.bindText(2, "Zig is awesome");
try stmt.bindNull(3); // No content
_ = try stmt.step();
try stmt.reset();
try stmt.clearBindings();
try stmt.bindInt(1, 2); // Bob
try stmt.bindText(2, "SQLite rocks");
try stmt.bindText(3, "Zero dependencies!");
_ = try stmt.step();
}
std.debug.print("Inserted posts\n\n", .{});
// Query users
std.debug.print("All users:\n", .{});
std.debug.print("-" ** 50 ++ "\n", .{});
{
var stmt = try db.prepare("SELECT id, name, email FROM users ORDER BY name");
defer stmt.finalize();
while (try stmt.step()) {
const id = stmt.columnInt(0);
const name = stmt.columnText(1) orelse "(null)";
const email = stmt.columnText(2) orelse "(null)";
std.debug.print(" [{d}] {s} <{s}>\n", .{ id, name, email });
}
}
// Query posts with JOIN
std.debug.print("\nPosts with authors:\n", .{});
std.debug.print("-" ** 50 ++ "\n", .{});
{
var stmt = try db.prepare(
\\SELECT p.title, u.name, p.content
\\FROM posts p
\\JOIN users u ON p.user_id = u.id
\\ORDER BY p.id
);
defer stmt.finalize();
while (try stmt.step()) {
const title = stmt.columnText(0) orelse "(null)";
const author = stmt.columnText(1) orelse "(null)";
const content = stmt.columnText(2) orelse "(no content)";
std.debug.print(" \"{s}\" by {s}\n", .{ title, author });
std.debug.print(" {s}\n", .{content});
}
}
// Demonstrate transaction rollback
std.debug.print("\nTransaction demo:\n", .{});
std.debug.print("-" ** 50 ++ "\n", .{});
{
// Count before
var count_stmt = try db.prepare("SELECT COUNT(*) FROM users");
defer count_stmt.finalize();
_ = try count_stmt.step();
const count_before = count_stmt.columnInt(0);
std.debug.print(" Users before transaction: {d}\n", .{count_before});
// Start transaction and insert, then rollback
try db.begin();
try db.exec("INSERT INTO users (name, email) VALUES ('Temporary', 'temp@example.com')");
try db.rollback();
// Count after rollback
try count_stmt.reset();
_ = try count_stmt.step();
const count_after = count_stmt.columnInt(0);
std.debug.print(" Users after rollback: {d}\n", .{count_after});
std.debug.print(" Transaction was rolled back successfully!\n", .{});
}
std.debug.print("\nDone!\n", .{});
}

662
src/root.zig Normal file
View file

@ -0,0 +1,662 @@
//! zsqlite - SQLite wrapper for Zig
//!
//! A lightweight, idiomatic Zig wrapper around SQLite that compiles the
//! SQLite amalgamation directly into your binary for zero runtime dependencies.
//!
//! ## Quick Start
//!
//! ```zig
//! const sqlite = @import("zsqlite");
//!
//! pub fn main() !void {
//! var db = try sqlite.open(":memory:");
//! defer db.close();
//!
//! try db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
//!
//! var stmt = try db.prepare("INSERT INTO users (name) VALUES (?)");
//! defer stmt.finalize();
//!
//! try stmt.bindText(1, "Alice");
//! _ = try stmt.step();
//! }
//! ```
const std = @import("std");
const c = @cImport({
@cInclude("sqlite3.h");
});
/// SQLite error codes mapped to Zig errors
pub const Error = error{
/// Generic error
SqliteError,
/// Internal logic error in SQLite
InternalError,
/// Access permission denied
PermissionDenied,
/// Callback routine requested an abort
Abort,
/// The database file is locked
Busy,
/// A table in the database is locked
Locked,
/// A malloc() failed
OutOfMemory,
/// Attempt to write a readonly database
ReadOnly,
/// Operation terminated by sqlite3_interrupt()
Interrupt,
/// Some kind of disk I/O error occurred
IoError,
/// The database disk image is malformed
Corrupt,
/// Unknown opcode in sqlite3_file_control()
NotFound,
/// Insertion failed because database is full
Full,
/// Unable to open the database file
CantOpen,
/// Database lock protocol error
Protocol,
/// Internal use only
Empty,
/// The database schema changed
Schema,
/// String or BLOB exceeds size limit
TooBig,
/// Abort due to constraint violation
Constraint,
/// Data type mismatch
Mismatch,
/// Library used incorrectly
Misuse,
/// Uses OS features not supported on host
NoLfs,
/// Authorization denied
Auth,
/// Not used
Format,
/// Parameter out of range
Range,
/// File opened that is not a database file
NotADatabase,
/// Notifications from sqlite3_log()
Notice,
/// Warnings from sqlite3_log()
Warning,
/// sqlite3_step() has another row ready
Row,
/// sqlite3_step() has finished executing
Done,
};
/// Converts a SQLite result code to a Zig error
fn resultToError(result: c_int) Error {
return switch (result) {
c.SQLITE_ERROR => Error.SqliteError,
c.SQLITE_INTERNAL => Error.InternalError,
c.SQLITE_PERM => Error.PermissionDenied,
c.SQLITE_ABORT => Error.Abort,
c.SQLITE_BUSY => Error.Busy,
c.SQLITE_LOCKED => Error.Locked,
c.SQLITE_NOMEM => Error.OutOfMemory,
c.SQLITE_READONLY => Error.ReadOnly,
c.SQLITE_INTERRUPT => Error.Interrupt,
c.SQLITE_IOERR => Error.IoError,
c.SQLITE_CORRUPT => Error.Corrupt,
c.SQLITE_NOTFOUND => Error.NotFound,
c.SQLITE_FULL => Error.Full,
c.SQLITE_CANTOPEN => Error.CantOpen,
c.SQLITE_PROTOCOL => Error.Protocol,
c.SQLITE_EMPTY => Error.Empty,
c.SQLITE_SCHEMA => Error.Schema,
c.SQLITE_TOOBIG => Error.TooBig,
c.SQLITE_CONSTRAINT => Error.Constraint,
c.SQLITE_MISMATCH => Error.Mismatch,
c.SQLITE_MISUSE => Error.Misuse,
c.SQLITE_NOLFS => Error.NoLfs,
c.SQLITE_AUTH => Error.Auth,
c.SQLITE_FORMAT => Error.Format,
c.SQLITE_RANGE => Error.Range,
c.SQLITE_NOTADB => Error.NotADatabase,
c.SQLITE_NOTICE => Error.Notice,
c.SQLITE_WARNING => Error.Warning,
c.SQLITE_ROW => Error.Row,
c.SQLITE_DONE => Error.Done,
else => Error.SqliteError,
};
}
/// SQLite database connection
pub const Database = struct {
handle: ?*c.sqlite3,
const Self = @This();
/// Opens a database connection.
///
/// Use ":memory:" for an in-memory database, or a file path for persistent storage.
pub fn open(path: [:0]const u8) Error!Self {
var handle: ?*c.sqlite3 = null;
const result = c.sqlite3_open(path.ptr, &handle);
if (result != c.SQLITE_OK) {
if (handle) |h| {
_ = c.sqlite3_close(h);
}
return resultToError(result);
}
return .{ .handle = handle };
}
/// Opens a database with specific flags.
pub fn openWithFlags(path: [:0]const u8, flags: OpenFlags) Error!Self {
var handle: ?*c.sqlite3 = null;
const result = c.sqlite3_open_v2(path.ptr, &handle, flags.toInt(), null);
if (result != c.SQLITE_OK) {
if (handle) |h| {
_ = c.sqlite3_close(h);
}
return resultToError(result);
}
return .{ .handle = handle };
}
/// Closes the database connection.
pub fn close(self: *Self) void {
if (self.handle) |h| {
_ = c.sqlite3_close(h);
self.handle = null;
}
}
/// Executes SQL statement(s) without returning results.
///
/// Suitable for CREATE, INSERT, UPDATE, DELETE, etc.
pub fn exec(self: *Self, sql: [:0]const u8) Error!void {
const result = c.sqlite3_exec(self.handle, sql.ptr, null, null, null);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
/// Executes SQL with runtime-known string (copies to add null terminator).
pub fn execAlloc(self: *Self, allocator: std.mem.Allocator, sql: []const u8) !void {
const sql_z = try allocator.dupeZ(u8, sql);
defer allocator.free(sql_z);
try self.exec(sql_z);
}
/// Prepares a SQL statement for execution.
pub fn prepare(self: *Self, sql: [:0]const u8) Error!Statement {
var stmt: ?*c.sqlite3_stmt = null;
const result = c.sqlite3_prepare_v2(self.handle, sql.ptr, @intCast(sql.len + 1), &stmt, null);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
return .{ .handle = stmt, .db = self };
}
/// Prepares a SQL statement with runtime-known string.
pub fn prepareAlloc(self: *Self, allocator: std.mem.Allocator, sql: []const u8) !Statement {
const sql_z = try allocator.dupeZ(u8, sql);
defer allocator.free(sql_z);
return self.prepare(sql_z);
}
/// Returns the rowid of the most recent successful INSERT.
pub fn lastInsertRowId(self: *Self) i64 {
return c.sqlite3_last_insert_rowid(self.handle);
}
/// Returns the number of rows modified by the most recent statement.
pub fn changes(self: *Self) i32 {
return c.sqlite3_changes(self.handle);
}
/// Returns the total number of rows modified since connection opened.
pub fn totalChanges(self: *Self) i32 {
return c.sqlite3_total_changes(self.handle);
}
/// Returns the most recent error message.
pub fn errorMessage(self: *Self) ?[]const u8 {
const msg = c.sqlite3_errmsg(self.handle);
if (msg) |m| {
return std.mem.span(m);
}
return null;
}
/// Enables or disables foreign key constraints.
pub fn setForeignKeys(self: *Self, enabled: bool) Error!void {
const sql: [:0]const u8 = if (enabled) "PRAGMA foreign_keys = ON" else "PRAGMA foreign_keys = OFF";
try self.exec(sql);
}
/// Begins a transaction.
pub fn begin(self: *Self) Error!void {
try self.exec("BEGIN");
}
/// Begins an immediate transaction (acquires write lock immediately).
pub fn beginImmediate(self: *Self) Error!void {
try self.exec("BEGIN IMMEDIATE");
}
/// Commits the current transaction.
pub fn commit(self: *Self) Error!void {
try self.exec("COMMIT");
}
/// Rolls back the current transaction.
pub fn rollback(self: *Self) Error!void {
try self.exec("ROLLBACK");
}
};
/// Flags for opening a database
pub const OpenFlags = struct {
read_only: bool = false,
read_write: bool = true,
create: bool = true,
uri: bool = false,
memory: bool = false,
no_mutex: bool = false,
full_mutex: bool = false,
fn toInt(self: OpenFlags) c_int {
var flags: c_int = 0;
if (self.read_only) flags |= c.SQLITE_OPEN_READONLY;
if (self.read_write) flags |= c.SQLITE_OPEN_READWRITE;
if (self.create) flags |= c.SQLITE_OPEN_CREATE;
if (self.uri) flags |= c.SQLITE_OPEN_URI;
if (self.memory) flags |= c.SQLITE_OPEN_MEMORY;
if (self.no_mutex) flags |= c.SQLITE_OPEN_NOMUTEX;
if (self.full_mutex) flags |= c.SQLITE_OPEN_FULLMUTEX;
return flags;
}
};
/// Prepared SQL statement
pub const Statement = struct {
handle: ?*c.sqlite3_stmt,
db: *Database,
const Self = @This();
/// Finalizes (destroys) the statement.
pub fn finalize(self: *Self) void {
if (self.handle) |h| {
_ = c.sqlite3_finalize(h);
self.handle = null;
}
}
/// Resets the statement for re-execution.
pub fn reset(self: *Self) Error!void {
const result = c.sqlite3_reset(self.handle);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
/// Clears all parameter bindings.
pub fn clearBindings(self: *Self) Error!void {
const result = c.sqlite3_clear_bindings(self.handle);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
// ========================================================================
// Bind parameters (1-indexed as per SQLite convention)
// ========================================================================
/// Binds NULL to a parameter.
pub fn bindNull(self: *Self, index: u32) Error!void {
const result = c.sqlite3_bind_null(self.handle, @intCast(index));
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
/// Binds an integer to a parameter.
pub fn bindInt(self: *Self, index: u32, value: i64) Error!void {
const result = c.sqlite3_bind_int64(self.handle, @intCast(index), value);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
/// Binds a float to a parameter.
pub fn bindFloat(self: *Self, index: u32, value: f64) Error!void {
const result = c.sqlite3_bind_double(self.handle, @intCast(index), value);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
/// Binds text to a parameter.
/// The text is copied by SQLite (SQLITE_TRANSIENT).
pub fn bindText(self: *Self, index: u32, value: []const u8) Error!void {
const result = c.sqlite3_bind_text(
self.handle,
@intCast(index),
value.ptr,
@intCast(value.len),
c.SQLITE_TRANSIENT,
);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
/// Binds a blob to a parameter.
/// The blob is copied by SQLite (SQLITE_TRANSIENT).
pub fn bindBlob(self: *Self, index: u32, value: []const u8) Error!void {
const result = c.sqlite3_bind_blob(
self.handle,
@intCast(index),
value.ptr,
@intCast(value.len),
c.SQLITE_TRANSIENT,
);
if (result != c.SQLITE_OK) {
return resultToError(result);
}
}
// ========================================================================
// Execution
// ========================================================================
/// Executes one step of the statement.
///
/// Returns true if there's a row available (for SELECT statements).
/// Returns false when done (SQLITE_DONE).
pub fn step(self: *Self) Error!bool {
const result = c.sqlite3_step(self.handle);
return switch (result) {
c.SQLITE_ROW => true,
c.SQLITE_DONE => false,
else => resultToError(result),
};
}
// ========================================================================
// Column access (0-indexed as per SQLite convention)
// ========================================================================
/// Returns the number of columns in the result set.
pub fn columnCount(self: *Self) u32 {
return @intCast(c.sqlite3_column_count(self.handle));
}
/// Returns the name of a column.
pub fn columnName(self: *Self, index: u32) ?[]const u8 {
const name = c.sqlite3_column_name(self.handle, @intCast(index));
if (name) |n| {
return std.mem.span(n);
}
return null;
}
/// Returns the type of a column value.
pub fn columnType(self: *Self, index: u32) ColumnType {
const col_type = c.sqlite3_column_type(self.handle, @intCast(index));
return switch (col_type) {
c.SQLITE_INTEGER => .integer,
c.SQLITE_FLOAT => .float,
c.SQLITE_TEXT => .text,
c.SQLITE_BLOB => .blob,
c.SQLITE_NULL => .null_value,
else => .null_value,
};
}
/// Returns an integer column value.
pub fn columnInt(self: *Self, index: u32) i64 {
return c.sqlite3_column_int64(self.handle, @intCast(index));
}
/// Returns a float column value.
pub fn columnFloat(self: *Self, index: u32) f64 {
return c.sqlite3_column_double(self.handle, @intCast(index));
}
/// Returns a text column value.
pub fn columnText(self: *Self, index: u32) ?[]const u8 {
const len = c.sqlite3_column_bytes(self.handle, @intCast(index));
const text = c.sqlite3_column_text(self.handle, @intCast(index));
if (text) |t| {
return t[0..@intCast(len)];
}
return null;
}
/// Returns a blob column value.
pub fn columnBlob(self: *Self, index: u32) ?[]const u8 {
const len = c.sqlite3_column_bytes(self.handle, @intCast(index));
const blob = c.sqlite3_column_blob(self.handle, @intCast(index));
if (blob) |b| {
const ptr: [*]const u8 = @ptrCast(b);
return ptr[0..@intCast(len)];
}
return null;
}
/// Returns true if the column is NULL.
pub fn columnIsNull(self: *Self, index: u32) bool {
return self.columnType(index) == .null_value;
}
};
/// Column data types
pub const ColumnType = enum {
integer,
float,
text,
blob,
null_value,
};
// ============================================================================
// Convenience functions
// ============================================================================
/// Opens a database connection.
pub fn open(path: [:0]const u8) Error!Database {
return Database.open(path);
}
/// Opens an in-memory database.
pub fn openMemory() Error!Database {
return Database.open(":memory:");
}
/// Returns the SQLite version string.
pub fn version() []const u8 {
return std.mem.span(c.sqlite3_libversion());
}
/// Returns the SQLite version number.
pub fn versionNumber() i32 {
return c.sqlite3_libversion_number();
}
// ============================================================================
// Tests
// ============================================================================
test "version" {
const ver = version();
try std.testing.expect(ver.len > 0);
// Should start with "3."
try std.testing.expectEqualStrings("3.", ver[0..2]);
}
test "open and close memory database" {
var db = try openMemory();
defer db.close();
try std.testing.expect(db.handle != null);
}
test "create table and insert" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)");
try db.exec("INSERT INTO test (name) VALUES ('hello')");
try std.testing.expectEqual(@as(i64, 1), db.lastInsertRowId());
try std.testing.expectEqual(@as(i32, 1), db.changes());
}
test "prepared statement with binding" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
var stmt = try db.prepare("INSERT INTO users (name, age) VALUES (?, ?)");
defer stmt.finalize();
try stmt.bindText(1, "Alice");
try stmt.bindInt(2, 30);
_ = try stmt.step();
try stmt.reset();
try stmt.clearBindings();
try stmt.bindText(1, "Bob");
try stmt.bindInt(2, 25);
_ = try stmt.step();
try std.testing.expectEqual(@as(i64, 2), db.lastInsertRowId());
}
test "select query" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
try db.exec("INSERT INTO users (name) VALUES ('Alice'), ('Bob')");
var stmt = try db.prepare("SELECT id, name FROM users ORDER BY id");
defer stmt.finalize();
// First row
try std.testing.expect(try stmt.step());
try std.testing.expectEqual(@as(i64, 1), stmt.columnInt(0));
try std.testing.expectEqualStrings("Alice", stmt.columnText(1).?);
// Second row
try std.testing.expect(try stmt.step());
try std.testing.expectEqual(@as(i64, 2), stmt.columnInt(0));
try std.testing.expectEqualStrings("Bob", stmt.columnText(1).?);
// No more rows
try std.testing.expect(!try stmt.step());
}
test "transaction commit" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE test (x INTEGER)");
try db.begin();
try db.exec("INSERT INTO test VALUES (1)");
try db.exec("INSERT INTO test VALUES (2)");
try db.commit();
var stmt = try db.prepare("SELECT COUNT(*) FROM test");
defer stmt.finalize();
_ = try stmt.step();
try std.testing.expectEqual(@as(i64, 2), stmt.columnInt(0));
}
test "transaction rollback" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE test (x INTEGER)");
try db.exec("INSERT INTO test VALUES (1)");
try db.begin();
try db.exec("INSERT INTO test VALUES (2)");
try db.rollback();
var stmt = try db.prepare("SELECT COUNT(*) FROM test");
defer stmt.finalize();
_ = try stmt.step();
try std.testing.expectEqual(@as(i64, 1), stmt.columnInt(0));
}
test "foreign keys" {
var db = try openMemory();
defer db.close();
try db.setForeignKeys(true);
try db.exec(
\\CREATE TABLE parent (id INTEGER PRIMARY KEY);
\\CREATE TABLE child (id INTEGER PRIMARY KEY, parent_id INTEGER REFERENCES parent(id));
);
try db.exec("INSERT INTO parent VALUES (1)");
try db.exec("INSERT INTO child VALUES (1, 1)");
// This should fail due to FK constraint
const result = db.exec("INSERT INTO child VALUES (2, 999)");
try std.testing.expectError(Error.Constraint, result);
}
test "null values" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE test (x INTEGER, y TEXT)");
var stmt = try db.prepare("INSERT INTO test VALUES (?, ?)");
defer stmt.finalize();
try stmt.bindInt(1, 42);
try stmt.bindNull(2);
_ = try stmt.step();
var query = try db.prepare("SELECT x, y FROM test");
defer query.finalize();
_ = try query.step();
try std.testing.expectEqual(@as(i64, 42), query.columnInt(0));
try std.testing.expect(query.columnIsNull(1));
}
test "blob data" {
var db = try openMemory();
defer db.close();
try db.exec("CREATE TABLE test (data BLOB)");
const blob_data = [_]u8{ 0x00, 0x01, 0x02, 0xFF, 0xFE };
var stmt = try db.prepare("INSERT INTO test VALUES (?)");
defer stmt.finalize();
try stmt.bindBlob(1, &blob_data);
_ = try stmt.step();
var query = try db.prepare("SELECT data FROM test");
defer query.finalize();
_ = try query.step();
const result = query.columnBlob(0).?;
try std.testing.expectEqualSlices(u8, &blob_data, result);
}

33362
vendor/shell.c vendored Normal file

File diff suppressed because it is too large Load diff

260493
vendor/sqlite3.c vendored Normal file

File diff suppressed because it is too large Load diff

13583
vendor/sqlite3.h vendored Normal file

File diff suppressed because it is too large Load diff

719
vendor/sqlite3ext.h vendored Normal file
View file

@ -0,0 +1,719 @@
/*
** 2006 June 7
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the SQLite interface for use by
** shared libraries that want to be imported as extensions into
** an SQLite instance. Shared libraries that intend to be loaded
** as extensions by SQLite should #include this file instead of
** sqlite3.h.
*/
#ifndef SQLITE3EXT_H
#define SQLITE3EXT_H
#include "sqlite3.h"
/*
** The following structure holds pointers to all of the SQLite API
** routines.
**
** WARNING: In order to maintain backwards compatibility, add new
** interfaces to the end of this structure only. If you insert new
** interfaces in the middle of this structure, then older different
** versions of SQLite will not be able to load each other's shared
** libraries!
*/
struct sqlite3_api_routines {
void * (*aggregate_context)(sqlite3_context*,int nBytes);
int (*aggregate_count)(sqlite3_context*);
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
int (*bind_double)(sqlite3_stmt*,int,double);
int (*bind_int)(sqlite3_stmt*,int,int);
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
int (*bind_null)(sqlite3_stmt*,int);
int (*bind_parameter_count)(sqlite3_stmt*);
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
const char * (*bind_parameter_name)(sqlite3_stmt*,int);
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
int (*busy_timeout)(sqlite3*,int ms);
int (*changes)(sqlite3*);
int (*close)(sqlite3*);
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const char*));
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const void*));
const void * (*column_blob)(sqlite3_stmt*,int iCol);
int (*column_bytes)(sqlite3_stmt*,int iCol);
int (*column_bytes16)(sqlite3_stmt*,int iCol);
int (*column_count)(sqlite3_stmt*pStmt);
const char * (*column_database_name)(sqlite3_stmt*,int);
const void * (*column_database_name16)(sqlite3_stmt*,int);
const char * (*column_decltype)(sqlite3_stmt*,int i);
const void * (*column_decltype16)(sqlite3_stmt*,int);
double (*column_double)(sqlite3_stmt*,int iCol);
int (*column_int)(sqlite3_stmt*,int iCol);
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
const char * (*column_name)(sqlite3_stmt*,int);
const void * (*column_name16)(sqlite3_stmt*,int);
const char * (*column_origin_name)(sqlite3_stmt*,int);
const void * (*column_origin_name16)(sqlite3_stmt*,int);
const char * (*column_table_name)(sqlite3_stmt*,int);
const void * (*column_table_name16)(sqlite3_stmt*,int);
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
const void * (*column_text16)(sqlite3_stmt*,int iCol);
int (*column_type)(sqlite3_stmt*,int iCol);
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
int (*complete)(const char*sql);
int (*complete16)(const void*sql);
int (*create_collation)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_collation16)(sqlite3*,const void*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_function)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_function16)(sqlite3*,const void*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
int (*data_count)(sqlite3_stmt*pStmt);
sqlite3 * (*db_handle)(sqlite3_stmt*);
int (*declare_vtab)(sqlite3*,const char*);
int (*enable_shared_cache)(int);
int (*errcode)(sqlite3*db);
const char * (*errmsg)(sqlite3*);
const void * (*errmsg16)(sqlite3*);
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
int (*expired)(sqlite3_stmt*);
int (*finalize)(sqlite3_stmt*pStmt);
void (*free)(void*);
void (*free_table)(char**result);
int (*get_autocommit)(sqlite3*);
void * (*get_auxdata)(sqlite3_context*,int);
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
int (*global_recover)(void);
void (*interruptx)(sqlite3*);
sqlite_int64 (*last_insert_rowid)(sqlite3*);
const char * (*libversion)(void);
int (*libversion_number)(void);
void *(*malloc)(int);
char * (*mprintf)(const char*,...);
int (*open)(const char*,sqlite3**);
int (*open16)(const void*,sqlite3**);
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
void *(*realloc)(void*,int);
int (*reset)(sqlite3_stmt*pStmt);
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_double)(sqlite3_context*,double);
void (*result_error)(sqlite3_context*,const char*,int);
void (*result_error16)(sqlite3_context*,const void*,int);
void (*result_int)(sqlite3_context*,int);
void (*result_int64)(sqlite3_context*,sqlite_int64);
void (*result_null)(sqlite3_context*);
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_value)(sqlite3_context*,sqlite3_value*);
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
const char*,const char*),void*);
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
char * (*xsnprintf)(int,char*,const char*,...);
int (*step)(sqlite3_stmt*);
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
char const**,char const**,int*,int*,int*);
void (*thread_cleanup)(void);
int (*total_changes)(sqlite3*);
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
sqlite_int64),void*);
void * (*user_data)(sqlite3_context*);
const void * (*value_blob)(sqlite3_value*);
int (*value_bytes)(sqlite3_value*);
int (*value_bytes16)(sqlite3_value*);
double (*value_double)(sqlite3_value*);
int (*value_int)(sqlite3_value*);
sqlite_int64 (*value_int64)(sqlite3_value*);
int (*value_numeric_type)(sqlite3_value*);
const unsigned char * (*value_text)(sqlite3_value*);
const void * (*value_text16)(sqlite3_value*);
const void * (*value_text16be)(sqlite3_value*);
const void * (*value_text16le)(sqlite3_value*);
int (*value_type)(sqlite3_value*);
char *(*vmprintf)(const char*,va_list);
/* Added ??? */
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
/* Added by 3.3.13 */
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
int (*clear_bindings)(sqlite3_stmt*);
/* Added by 3.4.1 */
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
void (*xDestroy)(void *));
/* Added by 3.5.0 */
int (*bind_zeroblob)(sqlite3_stmt*,int,int);
int (*blob_bytes)(sqlite3_blob*);
int (*blob_close)(sqlite3_blob*);
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
int,sqlite3_blob**);
int (*blob_read)(sqlite3_blob*,void*,int,int);
int (*blob_write)(sqlite3_blob*,const void*,int,int);
int (*create_collation_v2)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*),
void(*)(void*));
int (*file_control)(sqlite3*,const char*,int,void*);
sqlite3_int64 (*memory_highwater)(int);
sqlite3_int64 (*memory_used)(void);
sqlite3_mutex *(*mutex_alloc)(int);
void (*mutex_enter)(sqlite3_mutex*);
void (*mutex_free)(sqlite3_mutex*);
void (*mutex_leave)(sqlite3_mutex*);
int (*mutex_try)(sqlite3_mutex*);
int (*open_v2)(const char*,sqlite3**,int,const char*);
int (*release_memory)(int);
void (*result_error_nomem)(sqlite3_context*);
void (*result_error_toobig)(sqlite3_context*);
int (*sleep)(int);
void (*soft_heap_limit)(int);
sqlite3_vfs *(*vfs_find)(const char*);
int (*vfs_register)(sqlite3_vfs*,int);
int (*vfs_unregister)(sqlite3_vfs*);
int (*xthreadsafe)(void);
void (*result_zeroblob)(sqlite3_context*,int);
void (*result_error_code)(sqlite3_context*,int);
int (*test_control)(int, ...);
void (*randomness)(int,void*);
sqlite3 *(*context_db_handle)(sqlite3_context*);
int (*extended_result_codes)(sqlite3*,int);
int (*limit)(sqlite3*,int,int);
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
const char *(*sql)(sqlite3_stmt*);
int (*status)(int,int*,int*,int);
int (*backup_finish)(sqlite3_backup*);
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
int (*backup_pagecount)(sqlite3_backup*);
int (*backup_remaining)(sqlite3_backup*);
int (*backup_step)(sqlite3_backup*,int);
const char *(*compileoption_get)(int);
int (*compileoption_used)(const char*);
int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void(*xDestroy)(void*));
int (*db_config)(sqlite3*,int,...);
sqlite3_mutex *(*db_mutex)(sqlite3*);
int (*db_status)(sqlite3*,int,int*,int*,int);
int (*extended_errcode)(sqlite3*);
void (*log)(int,const char*,...);
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
const char *(*sourceid)(void);
int (*stmt_status)(sqlite3_stmt*,int,int);
int (*strnicmp)(const char*,const char*,int);
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
int (*wal_autocheckpoint)(sqlite3*,int);
int (*wal_checkpoint)(sqlite3*,const char*);
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
int (*vtab_config)(sqlite3*,int op,...);
int (*vtab_on_conflict)(sqlite3*);
/* Version 3.7.16 and later */
int (*close_v2)(sqlite3*);
const char *(*db_filename)(sqlite3*,const char*);
int (*db_readonly)(sqlite3*,const char*);
int (*db_release_memory)(sqlite3*);
const char *(*errstr)(int);
int (*stmt_busy)(sqlite3_stmt*);
int (*stmt_readonly)(sqlite3_stmt*);
int (*stricmp)(const char*,const char*);
int (*uri_boolean)(const char*,const char*,int);
sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
const char *(*uri_parameter)(const char*,const char*);
char *(*xvsnprintf)(int,char*,const char*,va_list);
int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
/* Version 3.8.7 and later */
int (*auto_extension)(void(*)(void));
int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
void(*)(void*));
int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
void(*)(void*),unsigned char);
int (*cancel_auto_extension)(void(*)(void));
int (*load_extension)(sqlite3*,const char*,const char*,char**);
void *(*malloc64)(sqlite3_uint64);
sqlite3_uint64 (*msize)(void*);
void *(*realloc64)(void*,sqlite3_uint64);
void (*reset_auto_extension)(void);
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
void(*)(void*));
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
void(*)(void*), unsigned char);
int (*strglob)(const char*,const char*);
/* Version 3.8.11 and later */
sqlite3_value *(*value_dup)(const sqlite3_value*);
void (*value_free)(sqlite3_value*);
int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
/* Version 3.9.0 and later */
unsigned int (*value_subtype)(sqlite3_value*);
void (*result_subtype)(sqlite3_context*,unsigned int);
/* Version 3.10.0 and later */
int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
int (*strlike)(const char*,const char*,unsigned int);
int (*db_cacheflush)(sqlite3*);
/* Version 3.12.0 and later */
int (*system_errno)(sqlite3*);
/* Version 3.14.0 and later */
int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
char *(*expanded_sql)(sqlite3_stmt*);
/* Version 3.18.0 and later */
void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
/* Version 3.20.0 and later */
int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
sqlite3_stmt**,const char**);
int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
sqlite3_stmt**,const void**);
int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
void *(*value_pointer)(sqlite3_value*,const char*);
int (*vtab_nochange)(sqlite3_context*);
int (*value_nochange)(sqlite3_value*);
const char *(*vtab_collation)(sqlite3_index_info*,int);
/* Version 3.24.0 and later */
int (*keyword_count)(void);
int (*keyword_name)(int,const char**,int*);
int (*keyword_check)(const char*,int);
sqlite3_str *(*str_new)(sqlite3*);
char *(*str_finish)(sqlite3_str*);
void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
void (*str_append)(sqlite3_str*, const char *zIn, int N);
void (*str_appendall)(sqlite3_str*, const char *zIn);
void (*str_appendchar)(sqlite3_str*, int N, char C);
void (*str_reset)(sqlite3_str*);
int (*str_errcode)(sqlite3_str*);
int (*str_length)(sqlite3_str*);
char *(*str_value)(sqlite3_str*);
/* Version 3.25.0 and later */
int (*create_window_function)(sqlite3*,const char*,int,int,void*,
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void (*xValue)(sqlite3_context*),
void (*xInv)(sqlite3_context*,int,sqlite3_value**),
void(*xDestroy)(void*));
/* Version 3.26.0 and later */
const char *(*normalized_sql)(sqlite3_stmt*);
/* Version 3.28.0 and later */
int (*stmt_isexplain)(sqlite3_stmt*);
int (*value_frombind)(sqlite3_value*);
/* Version 3.30.0 and later */
int (*drop_modules)(sqlite3*,const char**);
/* Version 3.31.0 and later */
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
const char *(*uri_key)(const char*,int);
const char *(*filename_database)(const char*);
const char *(*filename_journal)(const char*);
const char *(*filename_wal)(const char*);
/* Version 3.32.0 and later */
const char *(*create_filename)(const char*,const char*,const char*,
int,const char**);
void (*free_filename)(const char*);
sqlite3_file *(*database_file_object)(const char*);
/* Version 3.34.0 and later */
int (*txn_state)(sqlite3*,const char*);
/* Version 3.36.1 and later */
sqlite3_int64 (*changes64)(sqlite3*);
sqlite3_int64 (*total_changes64)(sqlite3*);
/* Version 3.37.0 and later */
int (*autovacuum_pages)(sqlite3*,
unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
void*, void(*)(void*));
/* Version 3.38.0 and later */
int (*error_offset)(sqlite3*);
int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
int (*vtab_distinct)(sqlite3_index_info*);
int (*vtab_in)(sqlite3_index_info*,int,int);
int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
/* Version 3.39.0 and later */
int (*deserialize)(sqlite3*,const char*,unsigned char*,
sqlite3_int64,sqlite3_int64,unsigned);
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
unsigned int);
const char *(*db_name)(sqlite3*,int);
/* Version 3.40.0 and later */
int (*value_encoding)(sqlite3_value*);
/* Version 3.41.0 and later */
int (*is_interrupted)(sqlite3*);
/* Version 3.43.0 and later */
int (*stmt_explain)(sqlite3_stmt*,int);
/* Version 3.44.0 and later */
void *(*get_clientdata)(sqlite3*,const char*);
int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
};
/*
** This is the function signature used for all extension entry points. It
** is also defined in the file "loadext.c".
*/
typedef int (*sqlite3_loadext_entry)(
sqlite3 *db, /* Handle to the database. */
char **pzErrMsg, /* Used to set error string on failure. */
const sqlite3_api_routines *pThunk /* Extension API function pointers. */
);
/*
** The following macros redefine the API routines so that they are
** redirected through the global sqlite3_api structure.
**
** This header file is also used by the loadext.c source file
** (part of the main SQLite library - not an extension) so that
** it can get access to the sqlite3_api_routines structure
** definition. But the main library does not want to redefine
** the API. So the redefinition macros are only valid if the
** SQLITE_CORE macros is undefined.
*/
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
#define sqlite3_aggregate_context sqlite3_api->aggregate_context
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_aggregate_count sqlite3_api->aggregate_count
#endif
#define sqlite3_bind_blob sqlite3_api->bind_blob
#define sqlite3_bind_double sqlite3_api->bind_double
#define sqlite3_bind_int sqlite3_api->bind_int
#define sqlite3_bind_int64 sqlite3_api->bind_int64
#define sqlite3_bind_null sqlite3_api->bind_null
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
#define sqlite3_bind_text sqlite3_api->bind_text
#define sqlite3_bind_text16 sqlite3_api->bind_text16
#define sqlite3_bind_value sqlite3_api->bind_value
#define sqlite3_busy_handler sqlite3_api->busy_handler
#define sqlite3_busy_timeout sqlite3_api->busy_timeout
#define sqlite3_changes sqlite3_api->changes
#define sqlite3_close sqlite3_api->close
#define sqlite3_collation_needed sqlite3_api->collation_needed
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
#define sqlite3_column_blob sqlite3_api->column_blob
#define sqlite3_column_bytes sqlite3_api->column_bytes
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
#define sqlite3_column_count sqlite3_api->column_count
#define sqlite3_column_database_name sqlite3_api->column_database_name
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
#define sqlite3_column_decltype sqlite3_api->column_decltype
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
#define sqlite3_column_double sqlite3_api->column_double
#define sqlite3_column_int sqlite3_api->column_int
#define sqlite3_column_int64 sqlite3_api->column_int64
#define sqlite3_column_name sqlite3_api->column_name
#define sqlite3_column_name16 sqlite3_api->column_name16
#define sqlite3_column_origin_name sqlite3_api->column_origin_name
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
#define sqlite3_column_table_name sqlite3_api->column_table_name
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
#define sqlite3_column_text sqlite3_api->column_text
#define sqlite3_column_text16 sqlite3_api->column_text16
#define sqlite3_column_type sqlite3_api->column_type
#define sqlite3_column_value sqlite3_api->column_value
#define sqlite3_commit_hook sqlite3_api->commit_hook
#define sqlite3_complete sqlite3_api->complete
#define sqlite3_complete16 sqlite3_api->complete16
#define sqlite3_create_collation sqlite3_api->create_collation
#define sqlite3_create_collation16 sqlite3_api->create_collation16
#define sqlite3_create_function sqlite3_api->create_function
#define sqlite3_create_function16 sqlite3_api->create_function16
#define sqlite3_create_module sqlite3_api->create_module
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
#define sqlite3_data_count sqlite3_api->data_count
#define sqlite3_db_handle sqlite3_api->db_handle
#define sqlite3_declare_vtab sqlite3_api->declare_vtab
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
#define sqlite3_errcode sqlite3_api->errcode
#define sqlite3_errmsg sqlite3_api->errmsg
#define sqlite3_errmsg16 sqlite3_api->errmsg16
#define sqlite3_exec sqlite3_api->exec
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_expired sqlite3_api->expired
#endif
#define sqlite3_finalize sqlite3_api->finalize
#define sqlite3_free sqlite3_api->free
#define sqlite3_free_table sqlite3_api->free_table
#define sqlite3_get_autocommit sqlite3_api->get_autocommit
#define sqlite3_get_auxdata sqlite3_api->get_auxdata
#define sqlite3_get_table sqlite3_api->get_table
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_global_recover sqlite3_api->global_recover
#endif
#define sqlite3_interrupt sqlite3_api->interruptx
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
#define sqlite3_libversion sqlite3_api->libversion
#define sqlite3_libversion_number sqlite3_api->libversion_number
#define sqlite3_malloc sqlite3_api->malloc
#define sqlite3_mprintf sqlite3_api->mprintf
#define sqlite3_open sqlite3_api->open
#define sqlite3_open16 sqlite3_api->open16
#define sqlite3_prepare sqlite3_api->prepare
#define sqlite3_prepare16 sqlite3_api->prepare16
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_profile sqlite3_api->profile
#define sqlite3_progress_handler sqlite3_api->progress_handler
#define sqlite3_realloc sqlite3_api->realloc
#define sqlite3_reset sqlite3_api->reset
#define sqlite3_result_blob sqlite3_api->result_blob
#define sqlite3_result_double sqlite3_api->result_double
#define sqlite3_result_error sqlite3_api->result_error
#define sqlite3_result_error16 sqlite3_api->result_error16
#define sqlite3_result_int sqlite3_api->result_int
#define sqlite3_result_int64 sqlite3_api->result_int64
#define sqlite3_result_null sqlite3_api->result_null
#define sqlite3_result_text sqlite3_api->result_text
#define sqlite3_result_text16 sqlite3_api->result_text16
#define sqlite3_result_text16be sqlite3_api->result_text16be
#define sqlite3_result_text16le sqlite3_api->result_text16le
#define sqlite3_result_value sqlite3_api->result_value
#define sqlite3_rollback_hook sqlite3_api->rollback_hook
#define sqlite3_set_authorizer sqlite3_api->set_authorizer
#define sqlite3_set_auxdata sqlite3_api->set_auxdata
#define sqlite3_snprintf sqlite3_api->xsnprintf
#define sqlite3_step sqlite3_api->step
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
#define sqlite3_total_changes sqlite3_api->total_changes
#define sqlite3_trace sqlite3_api->trace
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
#endif
#define sqlite3_update_hook sqlite3_api->update_hook
#define sqlite3_user_data sqlite3_api->user_data
#define sqlite3_value_blob sqlite3_api->value_blob
#define sqlite3_value_bytes sqlite3_api->value_bytes
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
#define sqlite3_value_double sqlite3_api->value_double
#define sqlite3_value_int sqlite3_api->value_int
#define sqlite3_value_int64 sqlite3_api->value_int64
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
#define sqlite3_value_text sqlite3_api->value_text
#define sqlite3_value_text16 sqlite3_api->value_text16
#define sqlite3_value_text16be sqlite3_api->value_text16be
#define sqlite3_value_text16le sqlite3_api->value_text16le
#define sqlite3_value_type sqlite3_api->value_type
#define sqlite3_vmprintf sqlite3_api->vmprintf
#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
#define sqlite3_overload_function sqlite3_api->overload_function
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_clear_bindings sqlite3_api->clear_bindings
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
#define sqlite3_blob_bytes sqlite3_api->blob_bytes
#define sqlite3_blob_close sqlite3_api->blob_close
#define sqlite3_blob_open sqlite3_api->blob_open
#define sqlite3_blob_read sqlite3_api->blob_read
#define sqlite3_blob_write sqlite3_api->blob_write
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
#define sqlite3_file_control sqlite3_api->file_control
#define sqlite3_memory_highwater sqlite3_api->memory_highwater
#define sqlite3_memory_used sqlite3_api->memory_used
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
#define sqlite3_mutex_enter sqlite3_api->mutex_enter
#define sqlite3_mutex_free sqlite3_api->mutex_free
#define sqlite3_mutex_leave sqlite3_api->mutex_leave
#define sqlite3_mutex_try sqlite3_api->mutex_try
#define sqlite3_open_v2 sqlite3_api->open_v2
#define sqlite3_release_memory sqlite3_api->release_memory
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
#define sqlite3_sleep sqlite3_api->sleep
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
#define sqlite3_vfs_find sqlite3_api->vfs_find
#define sqlite3_vfs_register sqlite3_api->vfs_register
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
#define sqlite3_threadsafe sqlite3_api->xthreadsafe
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
#define sqlite3_result_error_code sqlite3_api->result_error_code
#define sqlite3_test_control sqlite3_api->test_control
#define sqlite3_randomness sqlite3_api->randomness
#define sqlite3_context_db_handle sqlite3_api->context_db_handle
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
#define sqlite3_limit sqlite3_api->limit
#define sqlite3_next_stmt sqlite3_api->next_stmt
#define sqlite3_sql sqlite3_api->sql
#define sqlite3_status sqlite3_api->status
#define sqlite3_backup_finish sqlite3_api->backup_finish
#define sqlite3_backup_init sqlite3_api->backup_init
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
#define sqlite3_backup_remaining sqlite3_api->backup_remaining
#define sqlite3_backup_step sqlite3_api->backup_step
#define sqlite3_compileoption_get sqlite3_api->compileoption_get
#define sqlite3_compileoption_used sqlite3_api->compileoption_used
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
#define sqlite3_db_config sqlite3_api->db_config
#define sqlite3_db_mutex sqlite3_api->db_mutex
#define sqlite3_db_status sqlite3_api->db_status
#define sqlite3_extended_errcode sqlite3_api->extended_errcode
#define sqlite3_log sqlite3_api->log
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
#define sqlite3_sourceid sqlite3_api->sourceid
#define sqlite3_stmt_status sqlite3_api->stmt_status
#define sqlite3_strnicmp sqlite3_api->strnicmp
#define sqlite3_unlock_notify sqlite3_api->unlock_notify
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
#define sqlite3_wal_hook sqlite3_api->wal_hook
#define sqlite3_blob_reopen sqlite3_api->blob_reopen
#define sqlite3_vtab_config sqlite3_api->vtab_config
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
/* Version 3.7.16 and later */
#define sqlite3_close_v2 sqlite3_api->close_v2
#define sqlite3_db_filename sqlite3_api->db_filename
#define sqlite3_db_readonly sqlite3_api->db_readonly
#define sqlite3_db_release_memory sqlite3_api->db_release_memory
#define sqlite3_errstr sqlite3_api->errstr
#define sqlite3_stmt_busy sqlite3_api->stmt_busy
#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
#define sqlite3_stricmp sqlite3_api->stricmp
#define sqlite3_uri_boolean sqlite3_api->uri_boolean
#define sqlite3_uri_int64 sqlite3_api->uri_int64
#define sqlite3_uri_parameter sqlite3_api->uri_parameter
#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
/* Version 3.8.7 and later */
#define sqlite3_auto_extension sqlite3_api->auto_extension
#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
#define sqlite3_bind_text64 sqlite3_api->bind_text64
#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
#define sqlite3_load_extension sqlite3_api->load_extension
#define sqlite3_malloc64 sqlite3_api->malloc64
#define sqlite3_msize sqlite3_api->msize
#define sqlite3_realloc64 sqlite3_api->realloc64
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
#define sqlite3_result_blob64 sqlite3_api->result_blob64
#define sqlite3_result_text64 sqlite3_api->result_text64
#define sqlite3_strglob sqlite3_api->strglob
/* Version 3.8.11 and later */
#define sqlite3_value_dup sqlite3_api->value_dup
#define sqlite3_value_free sqlite3_api->value_free
#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
/* Version 3.9.0 and later */
#define sqlite3_value_subtype sqlite3_api->value_subtype
#define sqlite3_result_subtype sqlite3_api->result_subtype
/* Version 3.10.0 and later */
#define sqlite3_status64 sqlite3_api->status64
#define sqlite3_strlike sqlite3_api->strlike
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
/* Version 3.12.0 and later */
#define sqlite3_system_errno sqlite3_api->system_errno
/* Version 3.14.0 and later */
#define sqlite3_trace_v2 sqlite3_api->trace_v2
#define sqlite3_expanded_sql sqlite3_api->expanded_sql
/* Version 3.18.0 and later */
#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
/* Version 3.20.0 and later */
#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
#define sqlite3_bind_pointer sqlite3_api->bind_pointer
#define sqlite3_result_pointer sqlite3_api->result_pointer
#define sqlite3_value_pointer sqlite3_api->value_pointer
/* Version 3.22.0 and later */
#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
#define sqlite3_value_nochange sqlite3_api->value_nochange
#define sqlite3_vtab_collation sqlite3_api->vtab_collation
/* Version 3.24.0 and later */
#define sqlite3_keyword_count sqlite3_api->keyword_count
#define sqlite3_keyword_name sqlite3_api->keyword_name
#define sqlite3_keyword_check sqlite3_api->keyword_check
#define sqlite3_str_new sqlite3_api->str_new
#define sqlite3_str_finish sqlite3_api->str_finish
#define sqlite3_str_appendf sqlite3_api->str_appendf
#define sqlite3_str_vappendf sqlite3_api->str_vappendf
#define sqlite3_str_append sqlite3_api->str_append
#define sqlite3_str_appendall sqlite3_api->str_appendall
#define sqlite3_str_appendchar sqlite3_api->str_appendchar
#define sqlite3_str_reset sqlite3_api->str_reset
#define sqlite3_str_errcode sqlite3_api->str_errcode
#define sqlite3_str_length sqlite3_api->str_length
#define sqlite3_str_value sqlite3_api->str_value
/* Version 3.25.0 and later */
#define sqlite3_create_window_function sqlite3_api->create_window_function
/* Version 3.26.0 and later */
#define sqlite3_normalized_sql sqlite3_api->normalized_sql
/* Version 3.28.0 and later */
#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
#define sqlite3_value_frombind sqlite3_api->value_frombind
/* Version 3.30.0 and later */
#define sqlite3_drop_modules sqlite3_api->drop_modules
/* Version 3.31.0 and later */
#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
#define sqlite3_uri_key sqlite3_api->uri_key
#define sqlite3_filename_database sqlite3_api->filename_database
#define sqlite3_filename_journal sqlite3_api->filename_journal
#define sqlite3_filename_wal sqlite3_api->filename_wal
/* Version 3.32.0 and later */
#define sqlite3_create_filename sqlite3_api->create_filename
#define sqlite3_free_filename sqlite3_api->free_filename
#define sqlite3_database_file_object sqlite3_api->database_file_object
/* Version 3.34.0 and later */
#define sqlite3_txn_state sqlite3_api->txn_state
/* Version 3.36.1 and later */
#define sqlite3_changes64 sqlite3_api->changes64
#define sqlite3_total_changes64 sqlite3_api->total_changes64
/* Version 3.37.0 and later */
#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
/* Version 3.38.0 and later */
#define sqlite3_error_offset sqlite3_api->error_offset
#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
#define sqlite3_vtab_in sqlite3_api->vtab_in
#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
/* Version 3.39.0 and later */
#ifndef SQLITE_OMIT_DESERIALIZE
#define sqlite3_deserialize sqlite3_api->deserialize
#define sqlite3_serialize sqlite3_api->serialize
#endif
#define sqlite3_db_name sqlite3_api->db_name
/* Version 3.40.0 and later */
#define sqlite3_value_encoding sqlite3_api->value_encoding
/* Version 3.41.0 and later */
#define sqlite3_is_interrupted sqlite3_api->is_interrupted
/* Version 3.43.0 and later */
#define sqlite3_stmt_explain sqlite3_api->stmt_explain
/* Version 3.44.0 and later */
#define sqlite3_get_clientdata sqlite3_api->get_clientdata
#define sqlite3_set_clientdata sqlite3_api->set_clientdata
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
/* This case when the file really is being compiled as a loadable
** extension */
# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
# define SQLITE_EXTENSION_INIT3 \
extern const sqlite3_api_routines *sqlite3_api;
#else
/* This case when the file is being statically linked into the
** application */
# define SQLITE_EXTENSION_INIT1 /*no-op*/
# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
# define SQLITE_EXTENSION_INIT3 /*no-op*/
#endif
#endif /* SQLITE3EXT_H */