Sakura Lang

Query SQLite with bound parameters

Keep SQL text literal and interpolate values as parameters, then decode typed rows.

Keep SQL text literal and interpolate values as parameters, then decode typed rows.

find_notes returns List<Note>, which determines the row schema for Sql.query. Each sql"…" interpolation becomes a bound parameter; neither string concatenation nor a trust bypass is needed. SqlError propagates from the database operations.

Tests install Sql.memory, a real fresh in-memory SQLite database, so no filesystem permission or persistent database is needed. One test proves SQL-looking input matches only its literal value. Another runs a transaction that writes and then fails on an absent table, checks SqlError, and proves the first write rolled back. Only the tests run. For a persistent database, query sakura doc std::sql::sqlite and supply an appropriately confined filesystem handler and manifest permissions.

Source project

Run it

From the repository root, with this checkout's sakura on PATH:

cd compiler/examples/sqlite_query
sakura check --json
sakura test --json

Source and tests

src/main.sak includes all required imports and tests.

import std/sql.{Sql, SqlError}

type Note = { id: Int, title: String }

fn create_notes() -> Unit ! {Sql, Fail<SqlError>} {
  Sql.migrate([sql"create table notes (id integer primary key, title text not null)"])
}

fn add_note(title: String) -> Unit ! {Sql, Fail<SqlError>} {
  let _ = Sql.execute(sql"insert into notes (title) values ({title})")
}

fn find_notes(title: String) -> List<Note> ! {Sql, Fail<SqlError>} {
  Sql.query(sql"select id, title from notes where title = {title} order by id")
}

test "SQLite binds interpolated values, including SQL-looking text" {
  with Sql.memory() {
    create_notes()
    add_note("tea")
    let literal = "x' OR 1=1 --"
    expect_eq(find_notes(literal), [])
    add_note(literal)
    expect_eq(find_notes(literal), [Note { id: 2, title: literal }])
    expect_eq(find_notes("tea"), [Note { id: 1, title: "tea" }])
  }
}

test "a failing transaction rolls back earlier writes" {
  with Sql.memory() {
    create_notes()
    expect_fails_with(
      fn() {
        Sql.transaction(fn() {
          add_note("rolled back")
          let _ = Sql.execute(sql"insert into absent_table (title) values ({"bad"})")
        })
      },
      fn(e) {
        match e {
          SqlError { .. } => true,
        }
      },
    )
    expect_eq(find_notes("rolled back"), [])
  }
}

Manifest and permissions

sakura.toml names the package and grants what the program may use.

[package]
name = "sqlite_query"
version = "0.1.0"
edition = "2026"

[permissions]

How this recipe is checked

Every project runs offline in the repository's tests (cargo test -p sakura_cli --test examples), against the compiler in the same checkout, on the JavaScript backend. A recipe that talks to an HTTP API or a model replays a cassette in its tests: a committed file of recorded requests and responses. These cassettes are synthetic, so no network access or API key is needed. Running a project needs Node 26, whose built-in SQLite the SQLite recipe uses.

To adapt a recipe, build the CLI from the same checkout and run sakura --version and sakura prompt first. Sakura Lang is pre-1.0: read STABILITY.md before depending on an API. The initial API baseline is compiler revision 5cae349516c21714d90d36e87f1cfd6debe980c6. An installed skill's frontmatter records its compiler version and build revision. These teaching examples are separate from the agent evaluation tasks.

Each page is generated from its example's recipe.md, sakura.toml, src/main.sak and fixture files. Edit those sources, not the rendered pages, then run cargo run -p agent_docs from compiler/ to regenerate the cookbook and the skill reference.

Edit on GitHub

Last updated

On this page