Implementation:2giosangmitom Sqmeow nvim sql split
| 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/ENDblocks, package specs and bodies, trigger bodies, andq'[...]'alternative quoting. - Scylla/SurrealDB:
BEGIN BATCH/APPLYandBEGIN TRANSACTION/COMMITare 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", ... }]