Merge #974 + #987: dialect-aware Sql read-only guard + read-only execution (13 bypasses closed), provider-semantics key matching, trigger-safe SQL Server rowcount
This commit is contained in:
+65
-27
@@ -137,21 +137,30 @@ point the tag at a latest-row view.
|
||||
|
||||
Both value-bearing fields — `whereValue` here and `keyValue` on the `KeyValue` model — may be
|
||||
authored as a JSON string, number or boolean, and are captured as text. They are **bound as
|
||||
parameters** either way, and matched back as text (the key cell is stringified), so a key column
|
||||
that holds an INTEGER station id can be authored either `"7"` or `7`. A JSON object, array or `null`
|
||||
is not a value and is refused. On **PostgreSQL** a string bind is sent untyped (`NpgsqlDbType.Unknown`,
|
||||
`PostgresDialect.ConfigureParameter`, STAB-24) so the server infers its type from the compared column;
|
||||
bound as `text`, a `"7"` against an `integer` key fails with `operator does not exist: integer = text`.
|
||||
parameters** either way, so a key column that holds an INTEGER station id can be authored either `"7"` or
|
||||
`7`. A JSON object, array or `null` is not a value and is refused. On **PostgreSQL** a string bind is sent
|
||||
untyped (`NpgsqlDbType.Unknown`, `PostgresDialect.ConfigureParameter`, STAB-24) so the server infers its type
|
||||
from the compared column; bound as `text`, a `"7"` against an `integer` key fails with
|
||||
`operator does not exist: integer = text`. (They were read asymmetrically until 2026-08-10, so a numerically
|
||||
authored `keyValue` refused the tag while the identically-bound `whereValue` was served — and the deploy gate
|
||||
reported the key as *missing* when it was right there in the blob.)
|
||||
|
||||
⚠️ **Known limitation — key normalisation.** Row slice-back matches the returned key cell by its *text* form
|
||||
(`Convert.ToString`, ordinal), while the server now matches by the column's inferred type. An authored key the
|
||||
server treats as equal but whose text differs therefore selects a row that the driver then cannot find, and the
|
||||
tag reads **no-row**: `"007"` against an `integer` column (returns `7`), `"1.50"` against `numeric` (returns
|
||||
`1.5`), and an upper-case GUID against a `uuid` column (returned lower-case). Author keys in the column's
|
||||
canonical text form. SQL Server behaves the same way.
|
||||
(They were read asymmetrically until 2026-08-10, so a numerically
|
||||
authored `keyValue` refused the tag while the identically-bound `whereValue` was served — and the
|
||||
deploy gate reported the key as *missing* when it was right there in the blob.)
|
||||
**Key-value rows are attributed by the server's own equality (#987).** The server picks the rows
|
||||
(`WHERE key IN (…)`) with the column's type and collation, and the driver attributes each returned row back to
|
||||
its tag by the same semantics (`SqlKeyMatcher`) — not by an ordinal compare of the stringified cell, which used
|
||||
to leave a row the server returned attributed to no tag, reading `BadNoData` forever while the driver stayed
|
||||
Healthy. Per row, the first stage that matches wins:
|
||||
|
||||
1. **Exact** text — always first, so on a case-*sensitive* column `pump1` and `PUMP1` stay two keys.
|
||||
2. **Representational** — a typed compare for numeric, Guid and boolean cells (`"007"` against an `integer`
|
||||
7, `"1.50"` against `numeric` 1.5, an upper-case GUID against a `uuid` / `uniqueidentifier`) and
|
||||
trailing-space-insensitive text (a `CHAR(n)` key padded to `"PUMP1 "`).
|
||||
3. **Collation** — case-insensitive, then accent / width / kana-insensitive: only rows a case- or
|
||||
accent-insensitive collation returned (SQL Server's and MySQL's defaults) ever reach this stage.
|
||||
|
||||
A row equal to several authored keys at its stage is each of theirs; a key that then holds more than one row is
|
||||
the duplicate-key `BadConfigurationError` — on a case-insensitive column holding both `PUMP1` and `pump1`, the
|
||||
key `pump1` really is ambiguous. A text cell is never compared numerically (a `varchar` `"007"` is not `"7"`).
|
||||
|
||||
### `Query` — a named read-only query (#753)
|
||||
|
||||
@@ -174,7 +183,9 @@ row:
|
||||
```
|
||||
|
||||
- **One execution per query per poll**, however many tags read it.
|
||||
- **`rowKey` picks the row** (matched as text against that result column, like `keyValue`). Without a
|
||||
- **`rowKey` picks the row** — matched against that result column exactly or representationally (an
|
||||
`integer` 7 for `"007"`, any GUID casing, `CHAR` padding; #987), but never case-insensitively: the server
|
||||
did not compare these, so case is the data's own. Without a
|
||||
`rowKey` the query must return exactly one row. No matching row ⇒ `BadNoData`; two matching rows (or
|
||||
two rows with no `rowKey`) ⇒ `BadConfigurationError` — never "last row wins".
|
||||
- **Result column names** (`column`, `rowKey.column`, `timestampColumn`) match exactly first, then by a
|
||||
@@ -188,14 +199,35 @@ row:
|
||||
values are JSON scalars or `null`. Write the provider's own marker in the SQL: `@active` on SqlServer,
|
||||
Postgres and MySql, `:p_active` on Oracle, a positional `?` on Odbc (bound in authored order). The
|
||||
factory refuses a declared parameter whose marker the statement never uses.
|
||||
- **Read-only, twice over.** `SqlReadOnlyStatement` refuses at construction (and in the AdminUI form)
|
||||
anything but a single `SELECT`/`WITH` statement, and any of `INSERT UPDATE DELETE MERGE UPSERT DROP
|
||||
- **Read-only, in layers (#974).** `SqlReadOnlyStatement` refuses at construction (and in the AdminUI
|
||||
form) anything but a single `SELECT`/`WITH` statement, and any of `INSERT UPDATE DELETE MERGE UPSERT DROP
|
||||
ALTER CREATE TRUNCATE EXEC EXECUTE CALL GRANT REVOKE DENY INTO COPY LOCK RENAME` outside a literal or
|
||||
comment — so `SELECT … INTO`, `FOR UPDATE` and a data-modifying CTE are refused too. Behind that guard
|
||||
the statement runs **inside a transaction the driver always rolls back**, so a side effect the guard
|
||||
cannot see (a function that writes) is never committed. A non-transactional side effect (an e-mail
|
||||
procedure, a sequence, a call out to another system) is outside both — which is why a read-only
|
||||
driver **must** connect with a `SELECT`-only login (see [Database login](#database-login-required)).
|
||||
comment — so `SELECT … INTO`, `FOR UPDATE` and a data-modifying CTE are refused too.
|
||||
- **The guard lexes the statement as the driver's provider does.** It used to have one lexer for five
|
||||
dialects, and where a provider ends a literal earlier than that lexer did, live SQL hid inside a
|
||||
"literal": `SELECT '\'' ; COMMIT; DELETE …` on MySQL (backslash escape) and `SELECT $$'$$; COMMIT; DROP …`
|
||||
on PostgreSQL (dollar quote) passed as one `SELECT`, and the embedded `COMMIT` ended the rolled-back
|
||||
transaction before the write (reproduced live). Each provider now has its own lexicon, and what dialects
|
||||
disagree on is **refused**, not modelled: a backslash inside a MySQL / PostgreSQL string literal (spell it
|
||||
`CHR(92)` / `CHAR(92)`), PostgreSQL dollar quoting, Oracle `q'…'` quoting, MySQL `/*! … */` executable
|
||||
comments, a nested `/* /* */` comment, MySQL `--` without a following space, a line comment ended by a bare
|
||||
carriage return. T-SQL's `]]` inside `[ … ]` and MySQL's `#` comments are modelled. An **Odbc** driver's
|
||||
backend is unknown, so its statement must lex identically under every dialect (so must a statement checked
|
||||
without a provider). Changing the driver form's **Provider** re-validates the queries.
|
||||
- **It executes in a read-only transaction where the provider has one** — always rolled back regardless:
|
||||
PostgreSQL and Oracle `SET TRANSACTION READ ONLY`, MySQL a read-only **session** plus `START TRANSACTION
|
||||
READ ONLY` (a smuggled `COMMIT` ends a transaction, not a session; the session is restored afterwards). A
|
||||
function that writes is then refused by the server (the read fails) instead of writing and being rolled
|
||||
back. SQL Server and Odbc get the plain always-rolled-back transaction.
|
||||
- **PostgreSQL also refuses a second statement at the protocol level.** The driver sends a named query with
|
||||
positional `$n` parameters (rewritten from your `@name` markers), which makes Npgsql pass the text unsplit
|
||||
as one extended-protocol statement; the server rejects anything after a `;` itself. Oracle (ODP.NET)
|
||||
refuses a second statement anyway. SQL Server and MySQL execute a multi-statement batch — there the
|
||||
per-dialect guard is the line, and MySQL's read-only session is defence in depth that hidden text could
|
||||
undo.
|
||||
- None of this replaces the grant: a non-transactional side effect (an e-mail procedure, a call out to
|
||||
another system, an autonomous transaction) is outside every layer — which is why a read-only driver
|
||||
**must** connect with a `SELECT`-only login (see [Database login](#database-login-required)).
|
||||
- A tag naming a query the driver config does not declare is **skipped** at Initialize (it reads
|
||||
`BadNodeIdUnknown`, and `/hosts` names the missing query). One unusable `queries` entry **fails the
|
||||
driver** instead, because every tag that names it would otherwise go dark unexplained.
|
||||
@@ -420,7 +452,7 @@ both and is rolled back (`BadConfigurationError`).
|
||||
|---|---|
|
||||
| Committed | `Good` |
|
||||
| `Update` (or a non-MySQL `Upsert`) touched no row — the key is absent; nothing is inserted | `BadNoData` |
|
||||
| More rows than the mode allows — the key is not unique; rolled back | `BadConfigurationError` |
|
||||
| More rows than the mode allows — the key is not unique; rolled back. The count is the **target statement's own**: on SQL Server the driver reads `@@ROWCOUNT` right after it (an `OUTPUT` parameter), because SqlClient's command-level count also sums trigger DML — an audit trigger that inserts a history row used to make every single-row write "touch 2 rows" and roll back (#987). PostgreSQL, MySQL and Oracle already report the statement alone. Odbc reports whatever its backend's driver does (an ODBC link to SQL Server keeps the trigger-summed count). | `BadConfigurationError` |
|
||||
| The provider could not report a row count; rolled back | `BadNotSupported` |
|
||||
| Unique / primary-key violation — e.g. an `Insert` of a key that exists (SQLSTATE 23505, SQL Server 2627/2601, MySQL 1062, ORA-00001) | `BadEntryExists` |
|
||||
| Another integrity rule — NOT NULL, foreign key, CHECK (SQLSTATE class 23, SQL Server 515/547, ORA-01400/02290/02291) — or a data exception: overflow, truncation, an unconvertible value (SQLSTATE class 22, SQL Server 8115/8152/245, ORA-01438/12899/01722) | `BadOutOfRange` |
|
||||
@@ -447,7 +479,12 @@ value. Writes never change driver health. Discovery advertises `Operate` only fo
|
||||
not serve is `SqlWriteModeUnsupported`. The AdminUI tag editor shows the same parser message.
|
||||
- Live gate: `SqlWriteModesLiveTests` runs the same suite (Update+stamp, absent key, Insert and its
|
||||
duplicate, NOT NULL, Upsert insert-then-update, Procedure, unknown procedure) against SQL Server,
|
||||
PostgreSQL, MySQL and Oracle; `OdbcReadTests` covers Odbc's Update/Insert.
|
||||
PostgreSQL, MySQL and Oracle; `OdbcReadTests` covers Odbc's Update/Insert;
|
||||
`SqlServerTriggerWriteLiveTests` proves Update / Insert / Upsert commit on a table whose audit trigger writes
|
||||
two rows per changed row (#987). `NamedQueryBypassLiveTests` proves, per provider, that each smuggled-write
|
||||
string is a working bypass the old way, that the guard refuses it, and that with the guard bypassed the
|
||||
write still does not land (PostgreSQL, MySQL) — plus the MySQL session restore and an Oracle read under
|
||||
`SET TRANSACTION READ ONLY`.
|
||||
|
||||
## Database login (required)
|
||||
|
||||
@@ -455,9 +492,10 @@ The driver's own guards are not a substitute for the database's grants:
|
||||
|
||||
- **A read-only driver (`allowWrites` off) must connect with a `SELECT`-only login** — `SELECT` on
|
||||
the tables and views its tags and named queries read, and nothing else. The named-query guard is a
|
||||
keyword check plus an always-rolled-back transaction; neither can stop a **non-transactional** side
|
||||
effect — a sequence `nextval`, a function that sends mail or calls out, autonomous transactions,
|
||||
DDL that auto-commits on some backends. Only the grant can.
|
||||
per-dialect keyword check plus an always-rolled-back (read-only where the provider has one) transaction;
|
||||
none of that can stop a **non-transactional** side effect — a function that sends mail or calls out,
|
||||
autonomous transactions — and on SQL Server and MySQL a statement that got past the lexer would run as a
|
||||
batch. Only the grant is a boundary.
|
||||
- **A writable driver must connect with a login limited to its write targets** — `SELECT` as above,
|
||||
plus exactly what its modes need on exactly the tables / procedures its writable tags name:
|
||||
`UPDATE` (Update), `INSERT` (Insert), `INSERT` + `UPDATE` (Upsert; SQL Server's `MERGE` also needs
|
||||
|
||||
Reference in New Issue
Block a user