Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
ts-mysql-parser
Advanced tools
Readme
A standalone, grammar-complete MySQL parser.
yarn add ts-mysql-parser
# or
npm install ts-mysql-parser
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'
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 a query.
parser.parse('SELECT id FROM users')
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
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
Split the text into multiple statements, optionally specifying the line break and delimiter.
parser.splitStatements(`SELECT * from users; SELECT * FROM posts`, '\n', ';')
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
Check if the given text is a MySQL keyword.
parser.isKeyword('TIME') // true
Check if the given text is a MySQL reserved keyword.
parser.isReservedKeyword('TIME') // false
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.
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
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.
stevenmiller888.github.io · GitHub @stevenmiller888 · Twitter @stevenmiller888
FAQs
A standalone, grammar-complete MySQL parser.
The npm package ts-mysql-parser receives a total of 1,081 weekly downloads. As such, ts-mysql-parser popularity was classified as popular.
We found that ts-mysql-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.