2021-06-03 05:41:59 +00:00
|
|
|
[![NPM version](https://img.shields.io/npm/v/dbgate-query-splitter.svg)](https://www.npmjs.com/package/dbgate-query-splitter)
|
|
|
|
|
2021-10-28 09:24:23 +00:00
|
|
|
# dbgate-query-splitter
|
2021-06-03 05:40:35 +00:00
|
|
|
|
2021-10-28 09:24:23 +00:00
|
|
|
Splits long SQL query into into particular statements. Designed to have zero dependencies and to be fast. Also supports nodejs-streams.
|
2021-06-03 05:40:35 +00:00
|
|
|
|
|
|
|
Supports following SQL dialects:
|
2021-10-28 09:24:23 +00:00
|
|
|
|
|
|
|
- MySQL
|
|
|
|
- PostgreSQL
|
|
|
|
- SQLite
|
|
|
|
- Microsoft SQL Server
|
2021-06-03 05:40:35 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```js
|
|
|
|
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`']
|
2021-10-28 09:24:23 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## 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)
|
|
|
|
|
|
|
|
```js
|
|
|
|
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);
|
2021-06-03 05:40:35 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Contributing
|
2021-10-28 09:24:23 +00:00
|
|
|
|
2021-06-03 05:40:35 +00:00
|
|
|
Please run tests before pushing any changes.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
yarn test
|
|
|
|
```
|
|
|
|
|
|
|
|
## Supported syntax
|
2021-10-28 09:24:23 +00:00
|
|
|
|
|
|
|
- Comments
|
|
|
|
- Dollar strings (PostgreSQL)
|
|
|
|
- GO separators (MS SQL)
|
|
|
|
- Custom delimiter, setby DELIMITER keyword (MySQL)
|