Principle:2giosangmitom Sqmeow nvim Buffer Splitting
| Knowledge Sources | |
|---|---|
| Domains | SQL_Parsing, Query_Execution |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
A statement boundary detection principle that splits multi-statement buffers into individual statements using dialect-aware lexical scanning, not full SQL parsing.
Description
Buffer Splitting is the principle of detecting statement boundaries in a SQL buffer by tracking quoting context, comments, dollar-quoting, and routine bodies, without performing full SQL parsing. The splitter is a boundary detector: invalid fragments are still returned for the database to diagnose. This allows the editor to select the statement at the cursor and the engine to execute individual or all statements from a buffer.
This principle addresses the challenge of splitting SQL text that may contain semicolons inside strings, identifiers, comments, dollar-quoted routine bodies, PL/SQL blocks, T-SQL batches, CQL batches, and SurrealQL blocks. Each dialect has different rules: PostgreSQL nests block comments and uses dollar-quoting; MySQL reads backslash escapes; Oracle tracks DECLARE/BEGIN/END nesting; MSSQL uses a T-SQL batch parser. Redis uses one-command-per-line; MongoDB uses JSON document nesting depth.
Usage
Use this principle when a database tool needs to split a multi-statement editor buffer into individual statements for selective execution. The splitter must handle all quoting and commenting conventions of each supported dialect.
Theoretical Basis
The principle uses a state machine that tracks lexical context:
- Quoting modes: Single-quote, double-quote (identifier), backtick, and SurrealQL angle-quote modes each end on their matching delimiter (doubled quotes are escapes).
- Comments: Line comments (
--,#for MySQL/SurrealDB,//for CQL/SurrealDB) and block comments (nestable for PostgreSQL). - Dollar-quoting: PostgreSQL
$$...$$and named$tag$...$tag$bodies are opaque. - Routine bodies:
BEGIN...ENDblocks (with CASE/IF/LOOP/WHILE/REPEAT nesting), Oracle DECLARE sections, trigger bodies, and package specs/bodies stay together. - Batches: CQL
BEGIN BATCH...APPLYand SurrealDBBEGIN TRANSACTION...COMMITare single statements.
# Abstract pattern (NOT real implementation):
def split(input: str, dialect: Dialect) -> list[Statement]:
if dialect == Dialect.MsSql:
return mssql_batches(input) # T-SQL batch parser
mode = Mode.Code
for char in input:
match mode:
case Mode.Code:
if char == ';': push_statement() # top-level only
elif char == "'": mode = Mode.SingleQuote
elif char == '"': mode = Mode.DoubleQuote
# ... track comments, dollar-quotes, routine bodies
case Mode.SingleQuote:
if char == "'" and not peek() == "'": mode = Mode.Code
return statements