crossbind
GitHub
← すべてのライブラリ

SQLite

組み込みのリレーショナルデータベース。

インストール

ブラウザーで動きます。Node.js とエッジの実行環境も同じパッケージを使います。ビルド対象:wasm32、シングルスレッドとマルチスレッド。

shell
npm install @crossbind/port-sqlite3-wasm@beta
crossbind.config.js
import sqlite3Wasm from '@crossbind/port-sqlite3-wasm/crossbind.config.js';
 
export default {
dependencies: [sqlite3Wasm],
paths: { config: import.meta.url },
};
main.js
import { initNative, sqlite3_libversion } from '@crossbind/port-sqlite3/sqlite3.h';
 
await initNative();

crossbind 本体はバンドラーのプラグインと一緒に入ります。Vite、Webpack、Rspack、Rsbuild、Rollup、esbuild、Bun、Next.js についてはバンドラープラグインを参照してください。

ブラウザーで動く SQLite と、その横で動く独自の C++

これらのアプリは、crossbind でコンパイルした SQLite 3.54.1 を、小さな C++ のラッパーと並べて実行します。FTS4 にない BM25 のランキング関数、B-tree やテーブルスキャンと速度を比べる R*Tree、そして開いたデータベースファイルを読み取り専用で調べるエクスプローラーです。 初回の実行時に 1.6 MB の WebAssembly を一度だけダウンロードします。このページのすべてのアプリがそれを共有し、何もアップロードされません。

アプリ 02

50,000 個の点に、矩形の中にあるものを問い合わせる

点の上をドラッグして矩形を描いてください。SQLite の R*Tree が空間インデックスを使って点を見つけます。続いて同じクエリを、x の B-tree インデックスに対して、さらにインデックスのないテーブルに対して実行し、それぞれの時間をこのタブで測って、SQLite が選んだ実行計画と並べて表示します。

モジュールは生成した同じ点で 3 つのテーブルを埋めます。ページも同じ xorshift32 の数列から点を描くので、やり取りするのは答えだけです。

点を読み込んでから、その上をドラッグして矩形を描いてください。
コードを表示
src/native/spatial_index.h
// src/native/spatial_index.h (excerpt)
create virtual table pts using rtree_i32(id, minx, maxx, miny, maxy);
create table indexed(id integer primary key, x integer not null, y integer not null);
create index indexed_x on indexed(x);
create table plain(id integer primary key, x integer not null, y integer not null);
 
-- a point is a box of size zero
insert into pts values (?1, ?2, ?2, ?3, ?3);
 
select count(*) from pts where minx >= ?1 and maxx <= ?3 and miny >= ?2 and maxy <= ?4;
select count(*) from indexed where x between ?1 and ?3 and y between ?2 and ?4;
select count(*) from plain where x between ?1 and ?3 and y between ?2 and ?4;
main.js
const m = await initNative();
const space = await new m.SpatialIndex();
await space.generate(50000, 2463534242); // xorshift32 points in [0, 1000)²
 
await space.count(400, 400, 599, 599, 'rtree'); // 2018
await space.plan('rtree'); // SCAN pts VIRTUAL TABLE INDEX 2:D0B1D2B3
await space.measure(400, 400, 599, 599, 'scan'); // ms per query, timed in C++
アプリ 03

どんな SQLite ファイルも、外部に送らずに開く

.sqlite や .db のファイル、GeoPackage、MBTiles のタイルセットを開くと、そのテーブルを表示し、SQL を実行して結果を CSV に書き出せます。SQLite はそれを読み取り専用の immutable データベースとして開くので、ファイルに書き込むことはありません。WAL モードのファイルも、付随する -wal と -shm のファイルなしで開けます。また、10 万件の API リクエストのログを生成し、SQLite の percentile() で p95 レイテンシを求めることもできます。

手元のファイルはこのタブの中にとどまります。モジュールのメモリ上のファイルシステムにコピーされ、アップロードされることはありません。別の -wal ファイルにまだ残っている変更は、表示される内容に含まれません。

サンプルか手元の SQLite ファイルを開くと、そのテーブルを表示し、クエリを実行できます。
コードを表示
src/native/db_explorer.h
// src/native/db_explorer.h (excerpt)
// "file:/memfs/…/name.sqlite?immutable=1": no writes, no locks, no -wal or -shm needed
explicit DbExplorer(const std::string& path)
: file(path), db(uri(path), SQLITE_OPEN_READONLY | SQLITE_OPEN_URI) {
// a runaway query stops after 10 s instead of holding the worker
sqlite3_progress_handler(db.get(), 1000, stopAtDeadline, this);
sql::exec(db.get(), "select count(*) from sqlite_schema"); // "file is not a database"
}
 
// a compacted copy for the download
sql::Statement vacuum = sql::prepare(db.get(), "vacuum into ?1");
sql::bind(vacuum.get(), 1, path);
sql::step(db.get(), vacuum.get());
main.js
const m = await initNative();
const [path] = await m.autoMountFiles([file], await m.getRandomPath('/memfs'));
const db = await new m.DbExplorer(path);
 
JSON.parse(await db.tables()); // [{ name, type, rows, columns: [...] }]
JSON.parse(await db.query('select * from requests limit 5', 200));
await db.csv('select * from requests'); // RFC 4180 text
await db.saveAs('/memfs/sqliteapps/copy.sqlite'); // then m.getFileBytes(...)

サンプル

それぞれ、小さな C++ のヘッダーファイルと、それを呼び出す JavaScript、そしてサイトのビルド時に出力された内容です。JavaScript のみのタブは、C++ ファイルなしで SQLite 自身のヘッダーファイルを使って同じことをします。最後の 1 つは、WASI 向けにビルドした完全な C++ プログラムです。

プリペアドステートメントで挿入と検索を行う

SQLite を使うコードの多くの裏にある呼び出し:sqlite3_prepare_v2、sqlite3_bind_text、sqlite3_step、sqlite3_column_*。値はバインドされたパラメーターとして渡るので、「parser's」のアポストロフィは SQL ではなくデータとして扱われます。

src/native/notes.h
#pragma once
 
#include <sqlite3.h>
 
#include <memory>
#include <stdexcept>
#include <string>
 
// Notes in an in-memory SQLite database. Values reach SQL only as bound parameters (?1), never by
// pasting them into the statement, so a quote inside a note is data, not SQL.
class Notes {
public:
Notes() {
if (sqlite3_open(":memory:", &db) != SQLITE_OK) throw std::runtime_error("cannot open the database");
run("create table notes(id integer primary key, body text not null)");
}
 
~Notes() { sqlite3_close(db); }
 
static std::string version() { return sqlite3_libversion(); }
 
// Inserts one note and returns its id.
int add(const std::string& body) {
Statement insert = prepare("insert into notes(body) values (?1)");
sqlite3_bind_text(insert.get(), 1, body.c_str(), static_cast<int>(body.size()), SQLITE_TRANSIENT);
if (sqlite3_step(insert.get()) != SQLITE_DONE) throw std::runtime_error(sqlite3_errmsg(db));
return static_cast<int>(sqlite3_last_insert_rowid(db));
}
 
int count() {
Statement query = prepare("select count(*) from notes");
return sqlite3_step(query.get()) == SQLITE_ROW ? sqlite3_column_int(query.get(), 0) : 0;
}
 
// Every note containing `word`, oldest first, one "id: body" line each.
std::string find(const std::string& word) {
Statement query = prepare("select id, body from notes where body like '%' || ?1 || '%' order by id");
sqlite3_bind_text(query.get(), 1, word.c_str(), static_cast<int>(word.size()), SQLITE_TRANSIENT);
std::string lines;
int result;
while ((result = sqlite3_step(query.get())) == SQLITE_ROW) {
const int id = sqlite3_column_int(query.get(), 0);
const char* body = reinterpret_cast<const char*>(sqlite3_column_text(query.get(), 1));
lines += (lines.empty() ? "" : "\n") + std::to_string(id) + ": " + body;
}
if (result != SQLITE_DONE) throw std::runtime_error(sqlite3_errmsg(db));
return lines;
}
 
private:
// Finalizes the statement however the method returns.
using Statement = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
 
Statement prepare(const char* sql) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db, sql, -1, &statement, nullptr) != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
return Statement(statement, sqlite3_finalize);
}
 
void run(const char* sql) {
char* message = nullptr;
if (sqlite3_exec(db, sql, nullptr, nullptr, &message) == SQLITE_OK) return;
const std::string reason = message ? message : "unknown error";
sqlite3_free(message);
throw std::runtime_error(reason);
}
 
sqlite3* db = nullptr;
};
main.js
import { initNative, Notes } from './native/notes.h';
 
await initNative();
const notes = await new Notes();
for (const body of ['buy milk', 'write the README', "fix the parser's bug"]) await notes.add(body);
console.log(await Notes.version(), await notes.count());
console.log(await notes.find('the'));
出力初回は 1.6 MB をダウンロード
3.54.1 3
2: write the README
3: fix the parser's bug

一括処理を 1 つのトランザクションで適用する

BEGIN の後、upsert(ON CONFLICT DO UPDATE)のプリペアドステートメント 1 つを行ごとにリセットして再バインドし、最後に COMMIT します。CHECK 制約に違反する行があるとラッパーは ROLLBACK し、それより前の行も取り消されます。

src/native/ledger.h
#pragma once
 
#include <sqlite3.h>
 
#include <memory>
#include <stdexcept>
#include <string>
 
// Account balances updated in batches. A batch is one transaction: BEGIN, one prepared upsert that
// is reset and re-bound for every row, then COMMIT. When a row fails, ROLLBACK undoes the rows
// before it too, so a batch is applied completely or not at all.
class Ledger {
public:
Ledger() {
if (sqlite3_open(":memory:", &db) != SQLITE_OK) throw std::runtime_error("cannot open the database");
run("create table balances(account text primary key, total integer not null check (total >= 0))");
}
 
~Ledger() { sqlite3_close(db); }
 
// `rows` holds one "account,amount" line per entry; returns how many rows were applied.
int apply(const std::string& rows) {
run("begin");
try {
const int applied = upsertEach(rows);
run("commit");
return applied;
} catch (...) {
sqlite3_exec(db, "rollback", nullptr, nullptr, nullptr);
throw;
}
}
 
// "account total" pairs in account order.
std::string balances() {
Statement query = prepare("select account, total from balances order by account");
std::string out;
while (sqlite3_step(query.get()) == SQLITE_ROW) {
out += (out.empty() ? "" : ", ") + std::string(reinterpret_cast<const char*>(sqlite3_column_text(query.get(), 0))) + " " +
std::to_string(sqlite3_column_int64(query.get(), 1));
}
return out;
}
 
private:
int upsertEach(const std::string& rows) {
Statement upsert = prepare(
"insert into balances(account, total) values (?1, ?2) "
"on conflict(account) do update set total = total + excluded.total");
int applied = 0;
for (size_t start = 0; start < rows.size();) {
size_t end = rows.find('\n', start);
if (end == std::string::npos) end = rows.size();
const std::string row = rows.substr(start, end - start);
start = end + 1;
const size_t comma = row.find(',');
if (comma == std::string::npos) throw std::invalid_argument("expected account,amount but got: " + row);
sqlite3_bind_text(upsert.get(), 1, row.data(), static_cast<int>(comma), SQLITE_TRANSIENT);
sqlite3_bind_int64(upsert.get(), 2, std::stoll(row.substr(comma + 1)));
if (sqlite3_step(upsert.get()) != SQLITE_DONE) throw std::runtime_error(sqlite3_errmsg(db));
sqlite3_reset(upsert.get());
applied += 1;
}
return applied;
}
 
using Statement = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
 
Statement prepare(const char* sql) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db, sql, -1, &statement, nullptr) != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
return Statement(statement, sqlite3_finalize);
}
 
void run(const char* sql) {
char* message = nullptr;
if (sqlite3_exec(db, sql, nullptr, nullptr, &message) == SQLITE_OK) return;
const std::string reason = message ? message : "unknown error";
sqlite3_free(message);
throw std::runtime_error(reason);
}
 
sqlite3* db = nullptr;
};
main.js
import { initNative, Ledger } from './native/ledger.h';
 
await initNative();
const ledger = await new Ledger();
const accounts = ['alice', 'bob', 'carol'];
const rows = Array.from({ length: 10000 }, (_, i) => `${accounts[i % 3]},${(i % 7) + 1}`);
console.log(await ledger.apply(rows.join('\n')), 'rows applied');
console.log(await ledger.balances());
try {
await ledger.apply('alice,5\nbob,-1000000');
} catch (error) {
// wasm builds add the C++ type in front of the message and keep the text in cppMessage.
console.log('rolled back:', error.cppMessage ?? error.message);
}
console.log(await ledger.balances());
出力初回は 1.6 MB をダウンロード
10000 rows applied
alice 13333, bob 13330, carol 13331
rolled back: CHECK constraint failed: total >= 0
alice 13333, bob 13330, carol 13331

JSON ドキュメントを SQL で検索する

JSON を届いたままの形で保存し、SQL で問い合わせます。->> はフィールドを読み、json_each は配列を行に変え、json_group_object と json_group_array は JSON の答えを組み立てます。

src/native/order_log.h
#pragma once
 
#include <sqlite3.h>
 
#include <memory>
#include <stdexcept>
#include <string>
 
// Orders stored as JSON documents and queried with SQLite's JSON functions: ->> reads a field,
// json_each turns an array into rows, and json_group_object / json_group_array build the answer
// as JSON again. A CHECK on json_valid() rejects anything that is not JSON.
class OrderLog {
public:
OrderLog() {
if (sqlite3_open(":memory:", &db) != SQLITE_OK) throw std::runtime_error("cannot open the database");
run("create table orders(id integer primary key, doc text not null check (json_valid(doc)))");
}
 
~OrderLog() { sqlite3_close(db); }
 
void add(const std::string& json) {
Statement insert = prepare("insert into orders(doc) values (?1)");
sqlite3_bind_text(insert.get(), 1, json.c_str(), static_cast<int>(json.size()), SQLITE_TRANSIENT);
if (sqlite3_step(insert.get()) != SQLITE_DONE) throw std::runtime_error(sqlite3_errmsg(db));
}
 
// What each customer spent: {"customer": total, ...}
std::string totals() {
return value(
"select json_group_object(customer, total order by customer) from ("
" select doc ->> 'customer' as customer, sum((item.value ->> 'qty') * (item.value ->> 'price')) as total"
" from orders, json_each(orders.doc, '$.items') as item group by customer)");
}
 
// Units sold per product, best seller first: {"sku": units, ...}
std::string unitsSold() {
return value(
"select json_group_object(sku, units order by units desc, sku) from ("
" select item.value ->> 'sku' as sku, sum(item.value ->> 'qty') as units"
" from orders, json_each(orders.doc, '$.items') as item group by sku)");
}
 
// The customers who ordered `sku`, as a JSON array.
std::string buyersOf(const std::string& sku) {
return value(
"select json_group_array(distinct doc ->> 'customer' order by doc ->> 'customer')"
" from orders, json_each(orders.doc, '$.items') as item where item.value ->> 'sku' = ?1",
sku);
}
 
private:
// The first column of the first row, with `parameter` bound to ?1.
std::string value(const char* sql, const std::string& parameter = "") {
Statement query = prepare(sql);
if (sqlite3_bind_parameter_count(query.get()) > 0) {
sqlite3_bind_text(query.get(), 1, parameter.c_str(), static_cast<int>(parameter.size()), SQLITE_TRANSIENT);
}
if (sqlite3_step(query.get()) != SQLITE_ROW) throw std::runtime_error(sqlite3_errmsg(db));
const unsigned char* text = sqlite3_column_text(query.get(), 0);
return text ? reinterpret_cast<const char*>(text) : "null";
}
 
using Statement = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
 
Statement prepare(const char* sql) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db, sql, -1, &statement, nullptr) != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
return Statement(statement, sqlite3_finalize);
}
 
void run(const char* sql) {
char* message = nullptr;
if (sqlite3_exec(db, sql, nullptr, nullptr, &message) == SQLITE_OK) return;
const std::string reason = message ? message : "unknown error";
sqlite3_free(message);
throw std::runtime_error(reason);
}
 
sqlite3* db = nullptr;
};
main.js
import { initNative, OrderLog } from './native/order_log.h';
 
await initNative();
const orders = await new OrderLog();
await orders.add('{"customer":"ada","items":[{"sku":"pen","qty":2,"price":1.5},{"sku":"ink","qty":1,"price":4}]}');
await orders.add('{"customer":"linus","items":[{"sku":"pen","qty":10,"price":1.5}]}');
await orders.add('{"customer":"ada","items":[{"sku":"pad","qty":3,"price":2.25}]}');
console.log(await orders.totals());
console.log(await orders.unitsSold());
console.log(await orders.buyersOf('pen'));
出力初回は 1.6 MB をダウンロード
{"ada":13.75,"linus":15.0}
{"pen":12,"pad":3,"ink":1}
["ada","linus"]

CREATE VIRTUAL TABLE … USING fts4 で作る全文検索インデックスを MATCH で検索し、snippet() で抜粋を切り出します。語幹処理、フレーズ、前方一致、NOT、NEAR も使えます。このビルドには FTS3 と FTS4 がありますが、FTS5 はありません。

src/native/search_index.h
#pragma once
 
#include <sqlite3.h>
 
#include <memory>
#include <stdexcept>
#include <string>
 
// Full-text search with SQLite's FTS4 module (this build has FTS3 and FTS4, not FTS5). The porter
// tokenizer reduces English words to their stems, so "parse" also finds "parses" and "parsing".
// MATCH takes FTS query syntax: "a phrase", prefix*, NOT, OR and NEAR/n.
class SearchIndex {
public:
SearchIndex() {
if (sqlite3_open(":memory:", &db) != SQLITE_OK) throw std::runtime_error("cannot open the database");
run("create virtual table docs using fts4(title, body, tokenize=porter)");
}
 
~SearchIndex() { sqlite3_close(db); }
 
void add(const std::string& title, const std::string& body) {
Statement insert = prepare("insert into docs(title, body) values (?1, ?2)");
sqlite3_bind_text(insert.get(), 1, title.c_str(), static_cast<int>(title.size()), SQLITE_TRANSIENT);
sqlite3_bind_text(insert.get(), 2, body.c_str(), static_cast<int>(body.size()), SQLITE_TRANSIENT);
if (sqlite3_step(insert.get()) != SQLITE_DONE) throw std::runtime_error(sqlite3_errmsg(db));
}
 
// A snippet of every matching document, in the order they were added, with the hits in
// [brackets]; the snippets are joined by " | ".
std::string search(const std::string& query) {
Statement select = prepare("select snippet(docs, '[', ']', '…', -1, 8) from docs where docs match ?1 order by docid");
sqlite3_bind_text(select.get(), 1, query.c_str(), static_cast<int>(query.size()), SQLITE_TRANSIENT);
std::string out;
int result;
while ((result = sqlite3_step(select.get())) == SQLITE_ROW) {
out += (out.empty() ? "" : " | ") + std::string(reinterpret_cast<const char*>(sqlite3_column_text(select.get(), 0)));
}
if (result != SQLITE_DONE) throw std::runtime_error(sqlite3_errmsg(db));
return out;
}
 
private:
using Statement = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
 
Statement prepare(const char* sql) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db, sql, -1, &statement, nullptr) != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
return Statement(statement, sqlite3_finalize);
}
 
void run(const char* sql) {
char* message = nullptr;
if (sqlite3_exec(db, sql, nullptr, nullptr, &message) == SQLITE_OK) return;
const std::string reason = message ? message : "unknown error";
sqlite3_free(message);
throw std::runtime_error(reason);
}
 
sqlite3* db = nullptr;
};
main.js
import { initNative, SearchIndex } from './native/search_index.h';
 
await initNative();
const index = await new SearchIndex();
await index.add('SQLite in the browser', 'SQLite runs inside the browser tab as WebAssembly; queries never leave the page.');
await index.add('OpenSSL certificates', 'Generate a key, a certificate signing request and a self-signed certificate offline.');
await index.add('curl URL parser', 'libcurl parses and normalizes URLs exactly like the curl command line tool.');
await index.add('Parsing XML with expat', 'expat is a stream-oriented parser: it reports each start tag, end tag and text run as it reads.');
for (const query of ['parse', '"signing request"', 'brows*', 'parser NOT expat', 'tag NEAR/2 text']) {
console.log(`${query} -> ${await index.search(query)}`);
}
出力初回は 1.6 MB をダウンロード
parse -> libcurl [parses] and normalizes URLs exactly like the… | [Parsing] XML with expat
"signing request" -> …key, a certificate [signing] [request] and a self…
brows* -> SQLite in the [browser]
parser NOT expat -> curl URL [parser]
tag NEAR/2 text -> …start tag, end [tag] and [text] run as…

データベースをバイト列に保存し、開き直す

sqlite3_serialize はデータベースをそのファイルのバイト列としてコピーし、sqlite3_deserialize はそうしたバイト列をデータベースとして開きます。データベース全体をダウンロード、アップロード、キャッシュする方法です。コピーの内容は、続いて sqlite_schema と pragma_table_info で一覧にします。

src/native/snapshot.h
#pragma once
 
#include <sqlite3.h>
 
#include <memory>
#include <stdexcept>
#include <string>
 
// A whole database as bytes and back. sqlite3_serialize copies out the bytes SQLite would store in
// a database file, and sqlite3_deserialize opens such bytes as a database: in a browser, that is how
// a database is downloaded, uploaded, kept in IndexedDB or sent over the network. Bytes cross the
// binding as a byte string, one UTF-16 code unit (0-255) per byte.
class Snapshot {
public:
Snapshot() {
if (sqlite3_open(":memory:", &db) != SQLITE_OK) throw std::runtime_error("cannot open the database");
}
 
~Snapshot() { sqlite3_close(db); }
 
void exec(const std::string& sql) {
char* message = nullptr;
if (sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &message) == SQLITE_OK) return;
const std::string reason = message ? message : "unknown error";
sqlite3_free(message);
throw std::runtime_error(reason);
}
 
// The database file's bytes.
std::u16string save() {
sqlite3_int64 size = 0;
unsigned char* data = sqlite3_serialize(db, "main", &size, 0);
if (!data) throw std::runtime_error("sqlite3_serialize could not copy the database");
std::u16string bytes(static_cast<size_t>(size), u'\0');
for (size_t i = 0; i < bytes.size(); ++i) bytes[i] = data[i];
sqlite3_free(data);
return bytes;
}
 
// Replaces this database with the one in `bytes`.
void load(const std::u16string& bytes) {
auto* data = static_cast<unsigned char*>(sqlite3_malloc64(bytes.size()));
if (!data) throw std::runtime_error("out of memory");
for (size_t i = 0; i < bytes.size(); ++i) {
if (bytes[i] > 0xFF) {
sqlite3_free(data);
throw std::invalid_argument("not a byte string");
}
data[i] = static_cast<unsigned char>(bytes[i]);
}
// SQLite owns `data` from here on, and frees it even when this call fails.
const int flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
if (sqlite3_deserialize(db, "main", data, bytes.size(), bytes.size(), flags) != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
}
 
// Each table as "name(column TYPE, ...): N rows", one per line.
std::string describe() {
Statement tables = prepare("select name from sqlite_schema where type = 'table' and name not like 'sqlite_%' order by name");
std::string out;
while (sqlite3_step(tables.get()) == SQLITE_ROW) {
const std::string table = reinterpret_cast<const char*>(sqlite3_column_text(tables.get(), 0));
out += (out.empty() ? "" : "\n") + table + "(" + columnsOf(table) + "): " + std::to_string(rowsIn(table)) + " rows";
}
return out;
}
 
private:
std::string columnsOf(const std::string& table) {
Statement columns = prepare("select name, type from pragma_table_info(?1)");
sqlite3_bind_text(columns.get(), 1, table.c_str(), static_cast<int>(table.size()), SQLITE_TRANSIENT);
std::string out;
while (sqlite3_step(columns.get()) == SQLITE_ROW) {
out += (out.empty() ? "" : ", ") + std::string(reinterpret_cast<const char*>(sqlite3_column_text(columns.get(), 0))) + " " +
reinterpret_cast<const char*>(sqlite3_column_text(columns.get(), 1));
}
return out;
}
 
long long rowsIn(const std::string& table) {
// A table name cannot be a bound parameter, so it is quoted as an identifier instead.
std::string quoted = "\"";
for (char c : table) quoted += c == '"' ? std::string("\"\"") : std::string(1, c);
Statement count = prepare(("select count(*) from " + quoted + "\"").c_str());
return sqlite3_step(count.get()) == SQLITE_ROW ? sqlite3_column_int64(count.get(), 0) : 0;
}
 
using Statement = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
 
Statement prepare(const char* sql) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db, sql, -1, &statement, nullptr) != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
return Statement(statement, sqlite3_finalize);
}
 
sqlite3* db = nullptr;
};
main.js
import { initNative, Snapshot } from './native/snapshot.h';
 
await initNative();
const db = await new Snapshot();
await db.exec("create table readings(sensor text, celsius real); insert into readings values ('attic', 21.5), ('cellar', 12.25), ('garden', 17.0)");
const image = await db.save();
console.log(`${image.length} B, starts with "${image.slice(0, 15)}"`);
 
const copy = await new Snapshot();
await copy.load(image);
console.log(await copy.describe());
出力初回は 1.6 MB をダウンロード
8192 B, starts with "SQLite format 3"
readings(sensor TEXT, celsius REAL): 3 rows

SQL のコマンドラインツール

WASI には JavaScript のバインディングはありません。プログラムは SQLite をリンクする main() で、1 つの .wasm にビルドして wasmtime で実行します。これはカレントディレクトリにあるデータベースファイルに対して SQL を実行し、sqlite3 シェルと同じ形で行を出力するので、書き出したファイルはどの SQLite ツールでも開けます。

crossbind.config.js
import sqlite3Wasi from '@crossbind/port-sqlite3-wasi/crossbind.config.js';
 
export default {
general: { name: 'sqlite-tool' },
dependencies: [sqlite3Wasi],
paths: { config: import.meta.url },
};
src/native/main.cpp
// A command-line SQL runner for WASI:
// sqlite-tool <database> "<sql>" runs the SQL against the database file
// sqlite-tool <database> reads the SQL from standard input
// Result rows print as the sqlite3 shell prints them by default, columns joined by |, and a
// summary line follows.
#include <sqlite3.h>
 
#include <cstdio>
#include <string>
#include <vector>
 
namespace {
 
std::string readAll(FILE* file) {
std::string text;
std::vector<char> buffer(64 * 1024);
size_t read = 0;
while ((read = std::fread(buffer.data(), 1, buffer.size(), file)) > 0) text.append(buffer.data(), read);
return text;
}
 
int fail(sqlite3* db, const char* what) {
std::fprintf(stderr, "%s: %s\n", what, sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
 
} // namespace
 
int main(int argc, char** argv) {
if (argc < 2 || argc > 3) {
std::fprintf(stderr, "usage: sqlite-tool <database> [sql] (without sql, reads it from standard input)\n");
return 2;
}
const std::string sql = argc == 3 ? argv[2] : readAll(stdin);
sqlite3* db = nullptr;
if (sqlite3_open(argv[1], &db) != SQLITE_OK) return fail(db, argv[1]);
int statements = 0;
const char* rest = sql.c_str();
const char* end = rest + sql.size();
while (rest < end) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db, rest, static_cast<int>(end - rest), &statement, &rest) != SQLITE_OK) return fail(db, "sql");
if (!statement) continue;
statements += 1;
int result;
while ((result = sqlite3_step(statement)) == SQLITE_ROW) {
for (int column = 0; column < sqlite3_column_count(statement); column += 1) {
const unsigned char* text = sqlite3_column_text(statement, column);
std::printf("%s%s", column ? "|" : "", text ? reinterpret_cast<const char*>(text) : "");
}
std::printf("\n");
}
if (result != SQLITE_DONE) {
std::fprintf(stderr, "sql: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(statement);
sqlite3_close(db);
return 1;
}
sqlite3_finalize(statement);
}
std::printf("sqlite %s, %s: %d statement%s, %d rows changed\n", sqlite3_libversion(), argv[1], statements, statements == 1 ? "" : "s",
sqlite3_total_changes(db));
sqlite3_close(db);
return 0;
}
shell
npx crossbind build -p wasi -e wasi -b release
wasmtime run --dir=. .crossbind/build/sqlite-tool-wasi-wasm32-st-release.wasm shop.db < shop.sql
wasmtime run --dir=. .crossbind/build/sqlite-tool-wasi-wasm32-st-release.wasm shop.db "select customer, sum(qty * price) from orders group by customer order by 2 desc"
output
sqlite 3.54.1, shop.db: 3 statements, 4 rows changed
linus|15.0
ada|13.75
sqlite 3.54.1, shop.db: 1 statement, 0 rows changed
入力すると、ガイドのすべてのページとセクションを検索します。
↑↓ 移動↵ 開くesc 閉じる