← Back to Demos & Examples

38_zero_ffi_sqlite_and_orm.vk

Raw .vk
// examples/38_zero_ffi_sqlite_and_orm.vk
// Demonstrates Veko's pure-Veko Zero-FFI SQLite 3 engine (`"SQLite format 3\0"` 4096-byte
// slotted B-tree pages) and homoiconic ORM / query DSL (`db/sqlite`) across VM, Wasm, and Native.

use [sq = db/sqlite]

main = fn [] any [
	mut db = sq/open ':memory:'

	// 1. Parameterized SQL DDL, UNIQUE index, transaction rollback, and aggregates
	r1 = sq/exec mut db 'CREATE TABLE invoices (id INTEGER PRIMARY KEY, customer TEXT, cents INTEGER, status TEXT)' []
	if err? r1 [ return r1 ]
	r2 = sq/exec mut db 'CREATE UNIQUE INDEX idx_inv_cust ON invoices (customer)' []
	if err? r2 [ return r2 ]

	_ = sq/exec mut db 'INSERT INTO invoices (customer, cents, status) VALUES (?, ?, ?)' ['Acme Corp' 12500 'paid']
	_ = sq/exec mut db 'INSERT INTO invoices (customer, cents, status) VALUES (?, ?, ?)' ['Veko Labs' 28000 'paid']
	_ = sq/exec mut db 'INSERT INTO invoices (customer, cents, status) VALUES (?, ?, ?)' ['North Star' 4900 'draft']

	// Verify BEGIN / ROLLBACK restores exact B-tree snapshot
	_ = sq/exec mut db 'BEGIN' []
	_ = sq/exec mut db 'DELETE FROM invoices WHERE status = ?' ['paid']
	_ = sq/exec mut db 'ROLLBACK' []

	agg_rows = sq/query-sql db 'SELECT COUNT(*) AS n, SUM(cents) AS total FROM invoices WHERE status = ?' ['paid']
	if err? agg_rows or (len agg_rows) != 1 [ return (err 'aggregate query failed') ]
	agg = agg_rows[0]
	if agg.n != 2 or agg.total != 40500 [
		return (err `unexpected SQL aggregate: {mold agg}`)
	]

	// 2. Homoiconic Veko ORM (`define-table`, `migrate`, `insert`, `find-by-id`, `q`)
	deployments = sq/define-table 'deployments' (quote [
		id     i64 .pk
		region str .index
		env    str
		ok     bool
	])
	rm = sq/migrate mut db deployments
	if err? rm [ return rm ]

	id1 = sq/insert mut db deployments { region = 'ca-central-1', env = 'prod', ok = true }
	id2 = sq/insert mut db deployments { region = 'eu-west-1', env = 'prod', ok = true }
	_   = sq/insert mut db deployments { region = 'us-east-1', env = 'staging', ok = false }
	if err? id1 or err? id2 [ return (err 'orm insert failed') ]

	prod_rows = sq/q db (quote [
		from deployments
		where [env == 'prod']
		order-by [region .asc]
		limit 10
	])
	if err? prod_rows [ return prod_rows ]
	if (len prod_rows) != 2 or prod_rows[0].region != 'ca-central-1' or prod_rows[1].region != 'eu-west-1' [
		return (err `unexpected ORM query rows: {mold prod_rows}`)
	]

	// 3. Round-trip genuine 4096-byte SQLite 3 binary image ("SQLite format 3\0")
	wire = sq/to-bytes db
	if (len wire) < 4096 or ((len wire) % 4096) != 0 or (bytes/to-str wire[0..15]) != 'SQLite format 3' [
		return (err 'invalid SQLite 3 binary image header or page alignment')
	]
	reopened = sq/from-bytes wire
	if err? reopened [ return reopened ]
	dep1 = sq/find-by-id reopened deployments id1
	if err? dep1 or dep1.region != 'ca-central-1' [
		return (err 'SQLite 3 B-tree round-trip verification failed')
	]

	println `[Example 38 OK] sqlite_pages={(len wire) / 4096} | paid_cents={agg.total} | orm_prod={len prod_rows} | region0={dep1.region}`
	0
]

main