Whole-statement SQLite formatter for the river style, as a library and a stdin CLI.
select size
, species
, hits
from bugshields
where end_time is null
order by ts desc
limit 1
;moon add jonesmelton/sqlbrookmoonx jonesmelton/sqlbrook/cmd/sqlbrook < query.sql
moon install jonesmelton/sqlbrook/cmd/sqlbrook///|
test "format lays out a select" {
let r = @sqlbrook.format("SELECT a, COUNT(*) AS n FROM t GROUP BY a;")
inspect(
r.output,
content=(
#| select a
#| , count(*)
#| as n
#| from t
#|group by a
#| ;
#|
),
)
assert_false(r.had_passthrough())
}///|
test "unsupported statements pass through and are reported" {
let r = @sqlbrook.format("select a from t;\nvacuum;")
inspect(
r.output,
content=(
#|select a
#| from t
#| ;
#|
#|vacuum;
#|
),
)
debug_inspect(
r.skips,
content=(
#|[{ kind: "vacuum", line: 2 }]
),
)
}///|
test "PostgreSQL syntax raises LexError" {
try @sqlbrook.format("select a::int from t;") catch {
err =>
inspect(
err,
content="'::' is not SQLite syntax (PostgreSQL cast) at offset 8; dialect is SQLite",
)
} noraise {
_ => fail("expected LexError")
}
}///|
test "formatting preserves the token stream" {
let src = "select a,b from t where x=1;"
assert_eq(
@sqlbrook.tokenize(@sqlbrook.format(src).output),
@sqlbrook.tokenize(src),
)
}sqlbrook < query.sql # format
sqlbrook --check < query.sql # exit 1 if not already formatted; writes nothing
sqlbrook --man # the full contract: scope, exit codes, embedded queries| Exit code | Meaning |
|---|---|
| 0 | Fully formatted, nothing passed through |
| 1 | --check found the input unformatted |
| 2 | Usage error, invalid UTF-8, or input that is not lexable SQLite SQL (stdout empty) |
| 3 | A statement passed through unformatted (stdout still holds the whole output) |
| Command | What it checks |
|---|---|
| moon test --target all | Unit, snapshot and property tests, the corpus invariants, and the examples in this README |
| moon run scripts/cli_test.mbtx | Builds the wasm CLI and runs its exit-code contract with moon-cram |
| moon run scripts/cli_test.mbtx -- <bin> | Runs the backend-independent part of that contract against another binary, such as the OCaml build or a native build |
| moon run scripts/oracle.mbtx -- <ocaml-bin> [--fuzz] | Compares stdout, stderr and exit status with the OCaml binary over the corpus, or over 9000 generated inputs |
| moon run scripts/sync_corpus.mbtx -- <ocaml-repo> | Refreshes internal/corpus/ from the OCaml repository's examples |
test {
let r = @sqlbrook.format("select a, b from t;")
inspect(
r.output,
content=(
#|select a
#| , b
#| from t
#| ;
#|
),
)
assert_false(r.had_passthrough())
}test {
debug_inspect(
@lexer.tokenize("Order By x"),
content=(
#|[Keyword("order by"), Ident("x")]
),
)
}Install
Download zipWhole-statement SQLite formatter for the river style, as a library and a stdin CLI.