Commit 5f5f2fdb

5f5f2fdbf572Merged

Richard Ramsden <[email protected]> · 6 months ago · 3 files · +3 added

Description

examples(ffi/sqlite): toy blog on sqlite3

Demonstrates the FFI surface against a real C library:

  • open / close
  • DDL via sqlite3_exec
  • INSERT followed by sqlite3_last_insert_rowid
  • prepare_v2 / step / finalize for SELECTs with joins
  • :ptr out-params (sqlite3 **, sqlite3_stmt **) read back via ffi_buffer + ffi_read_ptr

sqlite3_lib.rb is a focused bindings module; blog.rb is the demo itself (in-memory db, posts/tags/comments many-to-many). README covers the per-distro libsqlite3-dev install line.

Strings are inlined into SQL with a single-quote escape rather than bound — sqlite3_bind_text needs the SQLITE_TRANSIENT (void *)-1 sentinel which the MVP FFI can't construct as a :ptr.

Details

Commit
5f5f2fdbf572
Parent
e4e8f4ed9d7c

examples/ffi/sqlite/README.mdaddedView file

1+# SQLite3 FFI demo — toy blog
2+
3+A self-contained blog (posts, tags, comments) running on top of
4+SQLite3 via Spinel's FFI. Demonstrates:
5+
6+- Open / close a database
7+- DDL via `sqlite3_exec`
8+- INSERT with `last_insert_rowid` follow-up
9+- Many-to-many joins (post_tags)
10+- SELECT with `prepare_v2` / `step` / `finalize` and `column_int` /
11+ `column_text`
12+- `:ptr` out-params (`sqlite3 **`, `sqlite3_stmt **`) read back via
13+ `ffi_buffer` + `ffi_read_ptr`
14+
15+## Setup
16+
17+You need SQLite3 development headers and a linker stub:
18+
19+```sh
20+# Debian / Ubuntu / WSL
21+sudo apt install libsqlite3-dev
22+
23+# Fedora
24+sudo dnf install sqlite-devel
25+
26+# macOS (Homebrew)
27+brew install sqlite
28+```
29+
30+Spinel's `ffi_lib "sqlite3"` becomes `-lsqlite3` at link time, so the
31+runtime library (`libsqlite3.so`/`.dylib`) needs to be on the loader's
32+search path.
33+
34+## Build & run
35+
36+From the repo root:
37+
38+```sh
39+./spinel examples/ffi/sqlite/blog.rb
40+./blog
41+```
42+
43+Expected output:
44+
45+```
46+Posts (with comment + tag counts):
47+ [1] Spinel learns to call C (2 comments, 3 tags)
48+ [2] Tasting o'tea (1 comments, 2 tags)
49+ [3] Hashes vs SymIntHash (1 comments, 2 tags)
50+
51+Posts tagged 'spinel':
52+ - Spinel learns to call C
53+ - Hashes vs SymIntHash
54+
55+Comments on the FFI post:
56+ alice: Wonderful — does it work with libcurl too?
57+ bob: I tried sqlite3 myself; the bindings are tidy.
58+```
59+
60+## Files
61+
62+- `sqlite3_lib.rb` — minimal FFI bindings + a `Sql.q` helper that
63+ escapes single quotes for inline string literals.
64+- `blog.rb` — the demo program.
65+
66+## Notes
67+
68+The demo uses inline-quoted SQL with `Sql.q` for escaping, not bound
69+parameters. The reason: `sqlite3_bind_text`'s last argument is a
70+destructor callback (the `SQLITE_TRANSIENT` sentinel is `(void *)-1`),
71+and the MVP FFI can't yet construct a `:ptr` from a sentinel integer.
72+For real applications you'd write a tiny C helper that wraps `bind_text`
73+with `SQLITE_TRANSIENT` baked in and link it alongside.