SQL Query Splitter
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
-
TypeError: Cannot destructure property 'splitQueryStream' of 'dbgate-query-splitter__WEBPACK_IMPORTED_MODULE_0__' as it is undefined.
cause Attempting to import `splitQueryStream` directly from the main `dbgate-query-splitter` package using ESM syntax, but it's located in a subpath.fixChange your import statement to `import { splitQueryStream } from 'dbgate-query-splitter/lib/splitQueryStream';` -
TypeError: splitQuery is not a function
cause Incorrect CommonJS `require` syntax when trying to access named exports, or attempting to use `require` on an ESM-only package (less likely for this package which supports both).fixFor CommonJS, use `const { splitQuery } = require('dbgate-query-splitter');`. For ESM, ensure `import { splitQuery } from 'dbgate-query-splitter';` is used in an ESM context.
Warnings
- breaking The API for `splitQueryStream` was simplified in version 4.9.0. Explicitly piping a `byline` stream is no longer required and should be removed from your code.
- gotcha When using `splitQueryStream`, ensure you are importing it from `dbgate-query-splitter/lib/splitQueryStream` and not directly from the main package. This specific import path is necessary for the streaming functionality.
- gotcha The `splitQuery` function requires a second argument specifying the dialect options. Forgetting this argument or passing `null`/`undefined` will result in incorrect splitting or runtime errors.
Install
-
npm install dbgate-query-splitter -
yarn add dbgate-query-splitter -
pnpm add dbgate-query-splitter
Imports
- splitQuery
const splitQuery = require('dbgate-query-splitter').splitQuery;import { splitQuery } from 'dbgate-query-splitter'; - mysqlSplitterOptions
import mysqlSplitterOptions from 'dbgate-query-splitter/mysqlSplitterOptions';
import { mysqlSplitterOptions } from 'dbgate-query-splitter'; - splitQueryStream
import { splitQueryStream } from 'dbgate-query-splitter';import { splitQueryStream } from 'dbgate-query-splitter/lib/splitQueryStream';
Quickstart
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;'...
*/