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 split

From Leeroopedia
Knowledge Sources
Domains SQL_Parsing, Query_Execution
Last Updated 2026-10-16 14:00 GMT

Overview

Concrete tool for splitting SQL buffers into individual statements with dialect-aware boundary detection provided by the sqmeow-db crate.

Description

The sql::split function splits a SQL buffer into statements at top-level semicolons, respecting quoted strings, identifiers, comments, dollar-quoting, routine bodies, and dialect-specific batches. It is a boundary detector, not a SQL parser: invalid fragments are still returned for the database to diagnose. Statement line ranges are zero-based.

Dialect-specific behavior:

  • MSSQL: Uses a T-SQL batch parser (mssql::batches) instead of semicolon splitting.
  • PostgreSQL: Supports dollar-quoting ($$...$$, $tag$...$tag$), nested block comments, and escape strings (E'...').
  • MySQL: Reads backslash escapes in strings; # is a line comment.
  • Oracle: Tracks DECLARE/BEGIN/END blocks, package specs and bodies, trigger bodies, and q'[...]' alternative quoting.
  • Scylla/SurrealDB: BEGIN BATCH / APPLY and BEGIN TRANSACTION / COMMIT are single statements; // line comments; function bodies.

Companion functions:

  • sql::split_lines — Redis: one command per non-comment line.
  • sql::split_documents — MongoDB: Extended JSON documents by brace/bracket depth.
  • sql::statement_at — Returns the statement containing a cursor line, or the nearest preceding one.

Usage

Use this function when executing SQL from a Neovim buffer: split the buffer text, optionally select the statement at the cursor, and pass selected statements to Backend::execute_results.

Code Reference

Source Location

  • Repository: sqmeow.nvim
  • File: core/sqmeow-db/src/sql.rs
  • Lines: L296-638 (split), L206-220 (split_lines), L227-289 (split_documents), L48-61 (statement_at)

Signature

pub struct Statement {
    pub sql: String,
    pub start_line: usize,
    pub end_line: usize,
}

pub fn split(input: &str, dialect: Dialect) -> Vec<Statement>;
pub fn split_lines(input: &str) -> Vec<Statement>;
pub fn split_documents(input: &str) -> Vec<Statement>;
pub fn statement_at(statements: &[Statement], line: usize) -> Option<&Statement>;

Import

use sqmeow_db::sql;

I/O Contract

Inputs

Name Type Required Description
input &str Yes /// Full SQL buffer text
dialect Dialect Yes /// Selects splitting rules (MSSQL uses batch parser)
line usize No /// (statement_at) Zero-based cursor line

Outputs

Name Type Description
Vec<Statement> Vec /// Statements with sql text and zero-based start/end line ranges

Usage Examples

Splitting a multi-statement buffer

let statements = sql::split("select 1; select 2", Dialect::Postgres);
// → [Statement { sql: "select 1", start_line: 0, end_line: 0 },
//    Statement { sql: "select 2", start_line: 0, end_line: 0 }]

Selecting the statement at a cursor line

let statements = sql::split("select 1;\n\nselect\n  2;\n", Dialect::Postgres);
let at_cursor = sql::statement_at(&statements, 2);
// → Some(Statement { sql: "select\n  2", ... })

Redis command splitting

let cmds = sql::split_lines("SET k v\n\n# note\nGET k");
// → [Statement { sql: "SET k v", ... }, Statement { sql: "GET k", ... }]

Related Pages

Implements Principle

Requires Environment

Page Connections

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