dbgate/packages/query-splitter
2021-12-12 11:56:48 +01:00
..
src better current query UX 2021-12-12 11:56:48 +01:00
.gitignore query-splitter renamamed 2021-06-02 21:02:28 +02:00
jest.config.js query-splitter renamamed 2021-06-02 21:02:28 +02:00
package.json upgraded typescript 2021-09-28 12:34:54 +02:00
README.md query-splitter: documentatin for streaming support 2021-10-28 11:24:23 +02:00
tsconfig.json query-splitter renamamed 2021-06-02 21:02:28 +02:00

NPM version

dbgate-query-splitter

Splits long SQL query into into particular statements. Designed to have zero dependencies and to be fast. Also supports nodejs-streams.

Supports following SQL dialects:

  • MySQL
  • PostgreSQL
  • SQLite
  • Microsoft SQL Server

Usage

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

const output = splitQuery('SELECT * FROM `table1`;SELECT * FROM `table2`;', mysqlSplitterOptions);

// output is ['SELECT * FROM `table1`', 'SELECT * FROM `table2`']

Streaming support in nodejs

Function splitQueryStream accepts input stream and query options. Result is object stream, each object for one splitted query. Tokens must not be divided into more input chunks. This can be accomplished eg. when input stream emits one chunk per line (eg. using byline module)

const { mysqlSplitterOptions, mssqlSplitterOptions, postgreSplitterOptions } = require('dbgate-query-splitter');
const { splitQueryStream } = require('dbgate-query-splitter/lib/splitQueryStream');
const fs = require('fs');
const byline = require('byline');

const fileStream = fs.createReadStream('INPUT_FILE_NAME', 'utf-8');
const lineStream = byline(fileStream);
const splittedStream = splitQueryStream(lineStream, mysqlSplitterOptions);

Contributing

Please run tests before pushing any changes.

yarn test

Supported syntax

  • Comments
  • Dollar strings (PostgreSQL)
  • GO separators (MS SQL)
  • Custom delimiter, setby DELIMITER keyword (MySQL)