npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wsporto/ts-mysql-parser

v0.4.0

Published

A standalone, grammar-complete MySQL parser.

Downloads

132

Readme

This is a fork of @stevenmiller888's ts-mysql-parser.

A standalone, grammar-complete MySQL parser.

Alt Text

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

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

Usage

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 to run in (e.g. SqlMode.AnsiQuotes)
  • charsets: the MySQL server character sets to support (e.g. [ '_utf8' ])

.parse()

Parse a query.

parser.parse('SELECT id FROM users')

.getQueryType()

Get the query type of the statement.

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.

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.

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

.getStatementAtOffset()

Get the MySQL statement at the given offset.

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.

parser.isKeyword('TIME') // true

.isReservedKeyword()

Check if the given text is a MySQL reserved keyword.

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:

$ 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:

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

Architecture

This project is built on 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

License

MIT