{"id":13672838,"url":"https://github.com/stevenmiller888/ts-mysql-parser","last_synced_at":"2025-08-21T12:30:35.864Z","repository":{"id":43981167,"uuid":"245548250","full_name":"stevenmiller888/ts-mysql-parser","owner":"stevenmiller888","description":"A standalone, grammar-complete MySQL parser.","archived":false,"fork":false,"pushed_at":"2023-01-05T09:18:21.000Z","size":1894,"stargazers_count":71,"open_issues_count":14,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-05T02:34:24.541Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevenmiller888.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-07T01:31:17.000Z","updated_at":"2024-11-05T00:34:02.000Z","dependencies_parsed_at":"2023-02-03T22:31:49.621Z","dependency_job_id":null,"html_url":"https://github.com/stevenmiller888/ts-mysql-parser","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevenmiller888%2Fts-mysql-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevenmiller888%2Fts-mysql-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevenmiller888%2Fts-mysql-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevenmiller888%2Fts-mysql-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevenmiller888","download_url":"https://codeload.github.com/stevenmiller888/ts-mysql-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229885861,"owners_count":18139382,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:01:51.088Z","updated_at":"2024-12-19T23:14:39.285Z","avatar_url":"https://github.com/stevenmiller888.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# ts-mysql-parser\n\n![Alt Text](https://github.com/stevenmiller888/ts-mysql-parser/workflows/CI/badge.svg)\n\n\u003e A standalone, grammar-complete MySQL parser.\n\n![Alt Text](https://github.com/stevenmiller888/ts-mysql-parser/raw/master/.github/code.png)\n\n## Features\n\n- Covers 100% of the MySQL grammar\n- Supports all versions of MySQL\n- Supports multiple statements\n- Supports MySQL mode and character sets\n- Custom lexer and parser listeners\n\n## Installation\n\n```shell\nyarn add ts-mysql-parser\n# or\nnpm install ts-mysql-parser\n```\n\n## Usage\n\n```typescript\nimport MySQLParser, { SqlMode, MySQLQueryType } from 'ts-mysql-parser'\n\nconst parser = new MySQLParser({\n  version: '5.7.7',\n  mode: SqlMode.AnsiQuotes\n})\n\nconst result = parser.parse('SELECT id FROM users')\n\nconst queryType = parser.getQueryType(result)\nconsole.log(queryType === MySQLQueryType.QtSelect) // true\n\nconst tableRef = parser.getNodeAtOffset(result, 18)\nconsole.log(tableRef) // table 'users'\n\nconst columnRef = parser.getNodeAtOffset(result, 7)\nconsole.log(columnRef) // column 'id'\n```\n\n## API\n\n### new MySQLParser(options)\n\nCreate a new instance of MySQLParser.\n\nThe available options are:\n\n- `version`: the MySQL server version (e.g. `'5.7.7'`)\n- `mode`: the MySQL server [mode](https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html) to run in (e.g. `SqlMode.AnsiQuotes`)\n- `charsets`: the MySQL server [character sets](https://dev.mysql.com/doc/refman/8.0/en/charset-configuration.html) to support (e.g. `[ '_utf8' ]`)\n\n#### .parse()\n\nParse a query.\n\n```typescript\nparser.parse('SELECT id FROM users')\n```\n\n#### .getQueryType()\n\nGet the query type of the statement.\n\n```typescript\nconst result = parser.parse('SELECT id FROM users')\nconst queryType = parser.getQueryType(parseResult)\nconsole.log(queryType === MySQLQueryType.QtSelect) // true\n```\n\n#### .getNodeAtOffset()\n\nGet a node in the parse tree at the given offset.\n\n```typescript\nconst result = parser.parse('SELECT id FROM users')\nconst node = parser.getNodeAtOffset(parseResult, 18)\nconsole.log(node) // \"users\" table\n```\n\n#### .splitStatements()\n\nSplit the text into multiple statements, optionally specifying the line break and delimiter.\n\n```typescript\nparser.splitStatements(`SELECT * from users; SELECT * FROM posts`, '\\n', ';')\n```\n\n#### .getStatementAtOffset()\n\nGet the MySQL statement at the given offset.\n\n```typescript\nconst statements = parser.splitStatements(`SELECT * from users; SELECT * FROM posts`, '\\n', ';')\nconst statement = parser.getStatementAtOffset(statements, 30)\nconsole.log(statement) // SELECT * FROM posts\n```\n\n#### .isKeyword()\n\nCheck if the given text is a MySQL keyword.\n\n```typescript\nparser.isKeyword('TIME') // true\n```\n\n#### .isReservedKeyword()\n\nCheck if the given text is a MySQL reserved keyword.\n\n```typescript\nparser.isReservedKeyword('TIME') // false\n```\n\n## Using Custom Listeners\n\nYou 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.\n\n## Development\n\nWhen the MySQL grammar changes, we merge in updates to the grammar files, and re-build the lexer and parser by running:\n\n```shell\n$ yarn build-parser\n```\n\nAfterwards, we need to add the following to the top of `src/grammar/MySQLLexer.ts`, `src/grammar/MySQLParser.ts`, and `src/grammar/MySQLParserListener.ts`:\n\n```typescript\n/* eslint-disable */\n// @ts-nocheck\n```\n\n## Architecture\n\nThis 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.\n\nThe `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.\n\n## Related\n\n- [ts-antlr4-scanner](https://github.com/stevenmiller888/ts-antlr4-scanner) - A scanner for antlr4-based lexers\n- [ts-mysql-analyzer](https://github.com/stevenmiller888/ts-mysql-analyzer) - A MySQL query analyzer\n- [ts-mysql-schema](https://github.com/stevenmiller888/ts-mysql-schema) - A schema extractor for MySQL\n- [ts-mysql-uri](https://github.com/stevenmiller888/ts-mysql-uri) - Parse a MySQL connection URI\n\n## License\n\n[MIT](https://tldrlegal.com/license/mit-license)\n\n---\n\n\u003e [stevenmiller888.github.io](https://stevenmiller888.github.io) \u0026nbsp;\u0026middot;\u0026nbsp;\n\u003e GitHub [@stevenmiller888](https://github.com/stevenmiller888) \u0026nbsp;\u0026middot;\u0026nbsp;\n\u003e Twitter [@stevenmiller888](https://twitter.com/stevenmiller888)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevenmiller888%2Fts-mysql-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevenmiller888%2Fts-mysql-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevenmiller888%2Fts-mysql-parser/lists"}