Skip to main content

Module pg_sql

Module pg_sql 

Source
Expand description

Hyperdrive H4 (#3321, ADR 0799 G): a literal Postgres statement’s SQL checked against Postgres’ own parser, when the project supplies a schema.sql.

Bynk does not own the Postgres schema, so there is nothing to prepare a statement against, as bynk.d1.sql_invalid does for D1 (ADR 0763). What can be checked without one is the text:

  • bynk.postgres.sql_invalid (a warning): the statement does not parse with sqlparser’s Postgres dialect, or holds more than one command (a prepared statement takes one, so the driver would refuse it);
  • bynk.postgres.sql_param_count (an error): the statement’s highest $n differs from the number of parameters the call passes.

sqlparser is a pure-Rust parser and is not Postgres’ own, so it can refuse valid Postgres (a newer or rarer form). That is why the syntax finding is a warning: an advisory check must never block a program that runs. The parameter count is exact (it is a scanner of this file’s own, below, pinned by tests to Postgres’ rules), so a mismatch is an error. libpg_query (pg_query) would be exact on syntax too, but needs libclang to build.

The check is off unless schema.sql is at the project root: its presence is the project saying it keeps a Postgres schema Bynk may take an interest in. The parser has no C, so it builds on every target, wasm32 included.

Enums§

PgSqlProblem
What is wrong with a statement’s text.

Functions§

check_statement
The problems with the literal statement sql, given the number of parameters the call passes when the list is written out (None when it is not, which leaves the count unchecked).
placeholder_count
The number of parameters a statement uses: its highest $n, which is how many Postgres asks the call for. A $n inside a string literal ('$9', E'…'), a quoted identifier, a comment or a dollar-quoted body is text, not a parameter.