Skip to content

TabularisDB/sql-splitter

Repository files navigation

@tabularis/sql-splitter

Dialect-aware SQL statement splitter, tokenizer, and classifier — the first-party engine that powers Tabularis.

It splits a SQL script into individual statements without a full parser, honouring the quoting, comment, and delimiter rules of each dialect, and classifies each statement (does it return a result set? is it explainable?). It has no runtime dependencies.

Install

npm install @tabularis/sql-splitter
# or: pnpm add @tabularis/sql-splitter

Quick start

import { splitStatements, splitQueries } from "@tabularis/sql-splitter";

const sql = `
  SELECT 1;
  INSERT INTO t (a) VALUES (2);
`;

// Full metadata per statement.
for (const stmt of splitStatements(sql, "postgres")) {
  console.log(stmt.text, stmt.returnsResultSet, stmt.range);
}

// Just the statement texts.
const queries = splitQueries(sql, "mysql"); // string[]

dialect is optional and accepts a Dialect | string; unknown values fall back to the generic preset (a plain undefined defaults to postgres), so plugin-manifest values can flow in without extra validation.

Supported dialects

postgres · mysql · mssql · sqlite · oracle · generic

Each preset encodes the lexical quirks that matter for splitting, including:

  • PostgreSQL — dollar quoting ($tag$ … $tag$), E'…' escape strings, nested block comments.
  • MySQL / MariaDB — backtick identifiers, \ string escapes, DELIMITER changes, executable comments (/*! … */), and -- requiring a trailing space.
  • SQL Server[bracket] identifiers and the GO batch separator.
  • SQLite — backtick and bracket identifiers.
  • Oracle — PL/SQL blocks, the / terminator, and q'[ … ]' / nq'…' alternative quoting.

API

Splitting

  • splitStatements(sql, dialect?)Statement[] — per-statement metadata.
  • splitQueries(sql, dialect?)string[] — statement texts only.
  • dialectOptions(dialect)DialectOptions — the resolved lexer preset for a Dialect.

A Statement carries text, a range ({ start, end } as JS UTF-16 code-unit offsets into the original source), and the classifier flags isSelect, returnsResultSet, isExplainable.

Classification

Single-statement helpers, usable independently of splitting:

  • isSelect(query) — leading keyword is SELECT.
  • returnsResultSet(query) — statement produces rows (SELECT, WITH, SHOW, VALUES, PRAGMA, …).
  • isExplainable(query) — statement can be prefixed with EXPLAIN.
  • stripLeadingComments(query) — drop leading line/block comments and whitespace.

Types

Dialect, Statement, StatementRange, Token, TokenKind, QuoteRule, DialectOptions are all exported.

License

Apache-2.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages