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.
@verycrazydog/mysql-parser
Advanced tools
Changelog
[1.2.0] - 2020-05-25
.d.ts
declaration files.retainComments
in .split()
API.Readme
A parser for MySQL statements. The current goal is to provide a solution to the missing DELIMITER syntax support in Node.js module mysql with the aim to support MySQL dump file and common usage scenario.
npm install @verycrazydog/mysql-parser
Split into an array of MySQL statement, one statement per array item
const mysqlParser = require('@verycrazydog/mysql-parser')
const splitResult = mysqlParser.split(`
-- Comment is removed
SELECT 1;
DELIMITER ;;
SELECT 2;;
DELIMITER ;
SELECT 3;
`)
// Print [ 'SELECT 1', 'SELECT 2', 'SELECT 3' ]
console.log(splitResult)
Split into an array of MySQL statement, allow multiple statements per array item
const mysqlParser = require('@verycrazydog/mysql-parser')
const splitResult = mysqlParser.split(`
SELECT 1;
SELECT 2;
DELIMITER $$
SELECT 3$$
DELIMITER ;
SELECT 4;
`, { multipleStatements: true })
// Print ["SELECT 1;\nSELECT 2;\nSELECT 3;\nSELECT 4;"]
console.log(JSON.stringify(splitResult))
Split into an array of MySQL statement, retaining comments
const mysqlParser = require('@verycrazydog/mysql-parser')
const splitResult = mysqlParser.split([
'-- Comment is retained',
'SELECT 1;',
'SELECT 2;'
].join('\n'), { retainComments: true })
// Print [ '-- Comment is retained\nSELECT 1', 'SELECT 2' ]
console.log(splitResult)
A more extensive example
const util = require('util')
const mysql = require('mysql')
const mysqlParser = require('@verycrazydog/mysql-parser')
// Try change this to see the effect of `multipleStatements` option
const ENABLE_MULTI_STATEMENT = true
;(async () => {
const rawConn = mysql.createConnection({
host: 'my_host',
user: 'my_username',
password: 'my_password',
multipleStatements: ENABLE_MULTI_STATEMENT
})
const conn = {
raw: rawConn,
connect: util.promisify(rawConn.connect).bind(rawConn),
query: util.promisify(rawConn.query).bind(rawConn),
end: util.promisify(rawConn.end).bind(rawConn)
}
const sqlFile = `
SELECT 'Hello world!' message FROM dual;
DELIMITER $$
SELECT 'DELIMITER is supported!' message FROM dual$$
DELIMITER ; SELECT 'Same as MySQL client, this will not print out' message FROM dual;
/*! SELECT "'/*!' style comment is executed!" message FROM dual */;
-- multipleStatements option allows statements that can be separated by
/* semicolon combined together in one, allowing to send to server in one */
## batch, reducing execution time on high latency network
SELECT 'Goodbye world!' message FROM dual;
`
const sqls = mysqlParser.split(sqlFile, { multipleStatements: ENABLE_MULTI_STATEMENT })
let queryCount = 0
await conn.connect()
try {
sqls.forEach(async sql => {
const queryResults = await conn.query(sql)
queryCount++
queryResults.forEach(queryResult => {
if (!(queryResult instanceof Array)) {
queryResult = [queryResult]
}
queryResult.forEach(row => console.log(row.message))
})
})
} finally {
await conn.end()
}
// Print:
// Hello world!
// DELIMITER is supported!
// '/*!' style comment is executed!
// Goodbye world!
// Done! Query count: 1
console.log('Done! Query count:', queryCount)
})()
Some limitations of this module which are currently not addressed:
DELIMITER \\
, however this module will not throw any error and will
ignore the DELIMITER
line.\g
and \G
as delimiter, however this module will not treat
them as delimiter.DELIMITER
, however this module will not throw
any error and will ignore the DELIMITER
line.This module is licensed under the MIT License.
This module was built by referencing the following materials:
Below are some modules found to be related to the goal of this module during development:
FAQs
Parser for MySQL statements
The npm package @verycrazydog/mysql-parser receives a total of 102 weekly downloads. As such, @verycrazydog/mysql-parser popularity was classified as not popular.
We found that @verycrazydog/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.