SQL Query Splitter

4.12.0 · active · verified Wed Apr 22

dbgate-query-splitter is a utility library designed to efficiently break down long SQL queries into individual statements. Currently at version 4.12.0, it is actively maintained with a focus on high performance and a zero-dependency footprint. The library supports a wide array of SQL dialects including MySQL, PostgreSQL, SQLite, Microsoft SQL Server, and Oracle, handling complex syntax elements like comments, dollar strings, GO separators, custom delimiters, and `SET SQLTERMINATOR`. A key differentiator is its robust streaming support for Node.js environments, allowing processing of large SQL files without loading them entirely into memory. It also provides an option to return rich metadata, including line and column numbers, for each split statement, which is useful for tooling and error reporting.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates splitting a multi-statement SQL string using MSSQL dialect options and retrieving rich position information for each statement.

import { splitQuery, mssqlSplitterOptions } from 'dbgate-query-splitter';

const complexSqlQuery = `
-- This is a comment
SELECT * FROM Users WHERE isActive = 1;
GO

-- Another statement block
DECLARE @myVar INT = 10;
SELECT @myVar AS Result;
GO

/*
  Multi-line comment
  followed by an empty statement
*/
SELECT COUNT(*) FROM Products;
`;

// Split the query, returning rich information including positions for MS SQL Server dialect
const statements = splitQuery(complexSqlQuery, {
  ...mssqlSplitterOptions,
  returnRichInfo: true,
});

statements.forEach(stmt => {
  console.log(`Statement:\n'${stmt.text}'\n  Start: { line: ${stmt.start.line}, column: ${stmt.start.column} }\n  End:   { line: ${stmt.end.line}, column: ${stmt.end.column} }\n`);
});

/* Expected output (simplified):
Statement:
'SELECT * FROM Users WHERE isActive = 1;'...
Statement:
'DECLARE @myVar INT = 10;\nSELECT @myVar AS Result;'...
Statement:
'SELECT COUNT(*) FROM Products;'...
*/

view raw JSON →