Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2giosangmitom Sqmeow nvim sql plan

From Leeroopedia
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 separate SELECT read-back for MySQL (by key or LAST_INSERT_ID()). CQL adds IF 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)

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment