Implementation:2giosangmitom Sqmeow nvim sql plan
| Knowledge Sources | |
|---|---|
| Domains | Editing, SQL, Database |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
Concrete tool for generating dialect-specific DML statements from staged result edits provided by the sqmeow-db crate.
Description
The sql_plan function plans changes to the tables a result came from, generating DML in a fixed order: deletes → inserts → updates. It uses a Planner struct that holds the dialect, result, source, and table metadata.
DML generation:
- DELETE: Deletes from the first editable table, locating the row by its key columns.
- INSERT: Inserts into the single table the result shows. Uses
RETURNING *for PostgreSQL/SQLite/DuckDB,OUTPUT INSERTED.*for MSSQL, and a separateSELECTread-back for MySQL (by key orLAST_INSERT_ID()). CQL addsIF NOT EXISTS. - UPDATE: Updates the table that owns each changed column, locating the row by key. Value literals are quoted per dialect; SQL expressions (like
now()) are inserted as-is.
The planner validates that rows exist (check_row), columns are editable (check_column), and the result has a Source::Tables provenance. Results without source metadata return a "not editable" error.
Usage
Called by Backend::plan (which delegates to this function by default) when the user stages edits and requests a plan for review. The generated statements are shown to the user before applying.
Code Reference
Source Location
- Repository: sqmeow.nvim
- File: core/sqmeow-db/src/edit/sql.rs
- Lines: L12-34 (sql_plan), L36-120+ (Planner)
Signature
pub fn sql_plan(
dialect: Dialect,
result: &ResultSet,
changes: &Changes,
) -> Result<Vec<String>>;
Import
use sqmeow_db::edit::sql_plan;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| dialect | Dialect | Yes /// SQL dialect for quoting and clause syntax | |
| result | &ResultSet | Yes /// Retained result with Source::Tables provenance | |
| changes | &Changes | Yes /// Staged edits: updates, deletes, inserts |
Outputs
| Name | Type | Description |
|---|---|---|
| Ok | Vec<String> /// DML statements in order: deletes → inserts → updates | |
| Err | Error /// Not editable, row out of range, column not editable |
Usage Examples
Planning a mix of edits
let changes = Changes {
deletes: vec![3],
inserts: vec![vec![(1, Value::Text("New".into()))]],
updates: vec![(0, vec![(1, Value::Sql("now()".into()))])],
};
let statements = sql_plan(Dialect::Postgres, &result, &changes)?;
// → [
// "DELETE FROM \"people\" WHERE \"id\" = $1",
// "INSERT INTO \"people\" (\"name\") VALUES ($1) RETURNING *",
// "UPDATE \"people\" SET \"name\" = now() WHERE \"id\" = $1"
// ]
Related Pages
Implements Principle
Requires Environment
- (None — string generation)