Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neondatabase/semicolons
Take a string with multiple Postgres SQL statements, separated by semicolons, and split it into its constituent statements
https://github.com/neondatabase/semicolons
comments parse postgres postgresql semicolons split sql
Last synced: 2 months ago
JSON representation
Take a string with multiple Postgres SQL statements, separated by semicolons, and split it into its constituent statements
- Host: GitHub
- URL: https://github.com/neondatabase/semicolons
- Owner: neondatabase
- License: mit
- Created: 2023-12-04T17:32:58.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-08T12:17:03.000Z (8 months ago)
- Last Synced: 2024-10-12T21:49:43.885Z (3 months ago)
- Topics: comments, parse, postgres, postgresql, semicolons, split, sql
- Language: TypeScript
- Homepage: https://neon.tech/blog/bringing-psqls-d-to-your-web-browser
- Size: 140 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## semicolons
This package exists to take a string containing multiple Postgres SQL statements, separated by semicolons, and split it into its constituent statements.
This isn't trivial, because semicolons may occur in double-quoted identifiers, ordinary strings, 'escape' strings, continuation strings, dollar-quoted strings, single-line comments, and (nestable) multi-line comments, and in all these cases they do not separate statements.
Sticky RegExps are used liberally.
Developed for and [used by Neon's SQL Editor](https://neon.tech/blog/bringing-psqls-d-to-your-web-browser). Kick the tyres there or at https://semicolons.pages.dev/.
### Installation
```sh
npm install postgres-semicolons
```### Usage
The exported functions have comprehensive [TSDoc](https://tsdoc.org/) comments in [`index.ts`](index.ts).
An example:
```javascript
import * as semicolons from 'postgres-semicolons';const sql = `BEGIN; /*/* SELECT 1; */ SELECT 2; */; SELECT ';'';'; SELECT $x$;$x$; -- COMMIT;`;
const standardConformingStrings = true;
const splits = semicolons.parseSplits(sql, standardConformingStrings);
const queries = semicolons.nonEmptyStatements(sql, splits.positions);console.log(queries); // -> [ 'BEGIN', "SELECT ';'';'", 'SELECT $x$;$x$' ]
```### License
The code is [MIT licensed](LICENSE).