SELECT -> UPDATE Conversion
The SQLite testkit intercepts modification attempts by capturing the SELECT representation of the rows that will be affected and letting QueryBuilder.buildUpdateQuery turn that projection into an UPDATE statement. The conversion keeps the instrumentation layer read-only while executing the same assignment logic that the repository would run.
Key transformation steps
- Normalize the conversion options so the source alias, target expression, and primary keys are always available.
- Gather the column metadata from the
SimpleSelectQuery, ensure each primary key exists, and collect the non-key columns to update. - Rebuild the select clause to match the final column ordering, extract any
WITHclause, and wrap the query in aFromClausethat references the normalized alias. - Create a
SetClausewhere each column is assigned aColumnReferencefrom the source alias and add aWHEREclause that compares the primary keys usingQueryBuilder.buildEqualityPredicate.
If there are no writable columns beyond the primary keys, buildUpdateQuery raises a descriptive error so fixture authors can expose additional select targets.
ts
const updateQuery = QueryBuilder.buildUpdateQuery(simpleSelectQuery, {
target: 'customers c',
primaryKeys: ['id'],
sourceAlias: 'src',
});Options to control the update
primaryKeysdetermines which identity columns theWHEREpredicate uses and must match columns emitted by the select list.columnslets you whitelist the writable columns; the helper normalizes duplicates and invalid entries before building theSetClause.sourceAliasdefaults tosrcbut can be overridden when the surrounding repository already names the subquery.
These knobs keep the conversion deterministic and preserve the column ordering required for triangular assignments.
Learn More
- QueryBuilder API for the full
buildUpdateQuerysignature and alias helpers. - SQLite Testkit Guide to see how repository wrappers rely on this helper for fixture verification.
- Why SQL Unit Testing Is Hard for the guiding philosophy behind focusing on reads and replaying writes via conversions.