# ts-mysql-parser

> A standalone, grammar-complete MySQL parser.

Latest version **0.2.0** (published 2020-04-06) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ts-mysql-parser
pnpm add ts-mysql-parser
yarn add ts-mysql-parser
bun add ts-mysql-parser
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-04-06 |
| First published | 2020-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.13 |
| Dependencies | 2 |
| Unpacked size | 7 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 73 |
| Author | Steven Miller |
| Maintainers | stevenmiller888 |
| Keywords | typescript, mysql-parser, sql-parser, parser, mysql, sql |

## Links

- npm: https://www.npmjs.com/package/ts-mysql-parser
- Repository: https://github.com/stevenmiller888/ts-mysql-parser
- npm.io page: https://npm.io/package/ts-mysql-parser

## Dependencies (2)

- [antlr4ts](https://npm.io/package/antlr4ts.md) ^0.5.0-alpha.3
- [typescript](https://npm.io/package/typescript.md) ^3.8.3

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2020-04-06
- 0.1.3 — 2020-03-16
- 0.1.2 — 2020-03-08
- 0.1.1 — 2020-03-08
- 0.1.0 — 2020-03-08
- 0.0.4 — 2020-03-07
- 0.0.3 — 2020-03-07
- 0.0.2 — 2020-03-07
- 0.0.1 — 2020-03-07

## README

# ts-mysql-parser

![Alt Text](https://github.com/stevenmiller888/ts-mysql-parser/workflows/CI/badge.svg)

> A standalone, grammar-complete MySQL parser.

![Alt Text](https://github.com/stevenmiller888/ts-mysql-parser/raw/master/.github/code.png)

## Features

- Covers 100% of the MySQL grammar
- Supports all versions of MySQL
- Supports multiple statements
- Supports MySQL mode and character sets
- Custom lexer and parser listeners

## Installation

```shell
yarn add ts-mysql-parser
# or
npm install ts-mysql-parser
```

## Usage

```typescript
import MySQLParser, { SqlMode, MySQLQueryType } from 'ts-mysql-parser'

const parser = new MySQLParser({
  version: '5.7.7',
  mode: SqlMode.AnsiQuotes
})

const result = parser.parse('SELECT id FROM users')

const queryType = parser.getQueryType(result)
console.log(queryType === MySQLQueryType.QtSelect) // true

const tableRef = parser.getNodeAtOffset(result, 18)
console.log(tableRef) // table 'users'

const columnRef = parser.getNodeAtOffset(result, 7)
console.log(columnRef) // column 'id'
```

## API

### new MySQLParser(options)

Create a new instance of MySQLParser.

The available options are:

- `version`: the MySQL server version (e.g. `'5.7.7'`)
- `mode`: the MySQL server [mode](https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html) to run in (e.g. `SqlMode.AnsiQuotes`)
- `charsets`: the MySQL server [character sets](https://dev.mysql.com/doc/refman/8.0/en/charset-configuration.html) to support (e.g. `[ '_utf8' ]`)

#### .parse()

Parse a query.

```typescript
parser.parse('SELECT id FROM users')
```

#### .getQueryType()

Get the query type of the statement.

```typescript
const result = parser.parse('SELECT id FROM users')
const queryType = parser.getQueryType(parseResult)
console.log(queryType === MySQLQueryType.QtSelect) // true
```

#### .getNodeAtOffset()

Get a node in the parse tree at the given offset.

```typescript
const result = parser.parse('SELECT id FROM users')
const node = parser.getNodeAtOffset(parseResult, 18)
console.log(node) // "users" table
```

#### .splitStatements()

Split the text into multiple statements, optionally specifying the line break and delimiter.

```typescript
parser.splitStatements(`SELECT * from users; SELECT * FROM posts`, '\n', ';')
```

#### .getStatementAtOffset()

Get the MySQL statement at the given offset.

```typescript
const statements = parser.splitStatements(`SELECT * from users; SELECT * FROM posts`, '\n', ';')
const statement = parser.getStatementAtOffset(statements, 30)
console.log(statement) // SELECT * FROM posts
```

#### .isKeyword()

Check if the given text is a MySQL keyword.

```typescript
parser.isKeyword('TIME') // true
```

#### .isReservedKeyword()

Check if the given text is a MySQL reserved keyword.

```typescript
parser.isReservedKeyword('TIME') // false
```

## Using Custom Listeners

You can use your own custom listeners to hook into the parse tree. See `examples/custom-parser-listener.ts` for an example of how to do this.

## Development

When the MySQL grammar changes, we merge in updates to the grammar files, and re-build the lexer and parser by running:

```shell
$ yarn build-parser
```

Afterwards, we need to add the following to the top of `src/grammar/MySQLLexer.ts`, `src/grammar/MySQLParser.ts`, and `src/grammar/MySQLParserListener.ts`:

```typescript
/* eslint-disable */
// @ts-nocheck
```

## Architecture

This project is built on [Antlr4](https://github.com/antlr/antlr4) with the MySQL grammar extracted from MySQL workbench. The grammar itself was kept mostly unchanged, aside from Typescript-specific rule predicates. This allows for easy updating as new versions of MySQL are released.

The `MySQLBaseLexer` class represents a superclass to the lexer class and customizes lexer functionality, such as emitting multiple tokens per rule. Similarly, the `MySQLBaseParser` class represents a superclass to the parser class and customizes parser functionality. These superclasses allow us to change the MySQL version, mode, and character sets at runtime.

## Related

- [ts-antlr4-scanner](https://github.com/stevenmiller888/ts-antlr4-scanner) - A scanner for antlr4-based lexers
- [ts-mysql-analyzer](https://github.com/stevenmiller888/ts-mysql-analyzer) - A MySQL query analyzer
- [ts-mysql-schema](https://github.com/stevenmiller888/ts-mysql-schema) - A schema extractor for MySQL
- [ts-mysql-uri](https://github.com/stevenmiller888/ts-mysql-uri) - Parse a MySQL connection URI

## License

[MIT](https://tldrlegal.com/license/mit-license)

---

> [stevenmiller888.github.io](https://stevenmiller888.github.io) &nbsp;&middot;&nbsp;
> GitHub [@stevenmiller888](https://github.com/stevenmiller888) &nbsp;&middot;&nbsp;
> Twitter [@stevenmiller888](https://twitter.com/stevenmiller888)

---
_Source: https://npm.io/package/ts-mysql-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
