项目作者: VeryCrazyDog

项目描述 :
Parser for MySQL statements
高级语言: TypeScript
项目地址: git://github.com/VeryCrazyDog/mysql-parser.git
创建时间: 2020-04-05T15:02:08Z
项目社区:https://github.com/VeryCrazyDog/mysql-parser

开源协议:MIT License

下载


@verycrazydog/mysql-parser

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.

@verycrazydog/mysql-parser"">Version on npm
Supported Node.js version
Build status

Install

  1. npm install @verycrazydog/mysql-parser

Usage

Split into an array of MySQL statement, one statement per array item

  1. const mysqlParser = require('@verycrazydog/mysql-parser')
  2. const splitResult = mysqlParser.split(`
  3. -- Comment is removed
  4. SELECT 1;
  5. DELIMITER ;;
  6. SELECT 2;;
  7. DELIMITER ;
  8. SELECT 3;
  9. `)
  10. // Print [ 'SELECT 1', 'SELECT 2', 'SELECT 3' ]
  11. console.log(splitResult)

Split into an array of MySQL statement, allow multiple statements per array item

  1. const mysqlParser = require('@verycrazydog/mysql-parser')
  2. const splitResult = mysqlParser.split(`
  3. SELECT 1;
  4. SELECT 2;
  5. DELIMITER $$
  6. SELECT 3$$
  7. DELIMITER ;
  8. SELECT 4;
  9. `, { multipleStatements: true })
  10. // Print ["SELECT 1;\nSELECT 2;\nSELECT 3;\nSELECT 4;"]
  11. console.log(JSON.stringify(splitResult))

Split into an array of MySQL statement, retaining comments

  1. const mysqlParser = require('@verycrazydog/mysql-parser')
  2. const splitResult = mysqlParser.split([
  3. '-- Comment is retained',
  4. 'SELECT 1;',
  5. 'SELECT 2;'
  6. ].join('\n'), { retainComments: true })
  7. // Print [ '-- Comment is retained\nSELECT 1', 'SELECT 2' ]
  8. console.log(splitResult)

A more extensive example

  1. const util = require('util')
  2. const mysql = require('mysql')
  3. const mysqlParser = require('@verycrazydog/mysql-parser')
  4. // Try change this to see the effect of `multipleStatements` option
  5. const ENABLE_MULTI_STATEMENT = true
  6. ;(async () => {
  7. const rawConn = mysql.createConnection({
  8. host: 'my_host',
  9. user: 'my_username',
  10. password: 'my_password',
  11. multipleStatements: ENABLE_MULTI_STATEMENT
  12. })
  13. const conn = {
  14. raw: rawConn,
  15. connect: util.promisify(rawConn.connect).bind(rawConn),
  16. query: util.promisify(rawConn.query).bind(rawConn),
  17. end: util.promisify(rawConn.end).bind(rawConn)
  18. }
  19. const sqlFile = `
  20. SELECT 'Hello world!' message FROM dual;
  21. DELIMITER $$
  22. SELECT 'DELIMITER is supported!' message FROM dual$$
  23. DELIMITER ; SELECT 'Same as MySQL client, this will not print out' message FROM dual;
  24. /*! SELECT "'/*!' style comment is executed!" message FROM dual */;
  25. -- multipleStatements option allows statements that can be separated by
  26. /* semicolon combined together in one, allowing to send to server in one */
  27. ## batch, reducing execution time on high latency network
  28. SELECT 'Goodbye world!' message FROM dual;
  29. `
  30. const sqls = mysqlParser.split(sqlFile, { multipleStatements: ENABLE_MULTI_STATEMENT })
  31. let queryCount = 0
  32. await conn.connect()
  33. try {
  34. sqls.forEach(async sql => {
  35. const queryResults = await conn.query(sql)
  36. queryCount++
  37. queryResults.forEach(queryResult => {
  38. if (!(queryResult instanceof Array)) {
  39. queryResult = [queryResult]
  40. }
  41. queryResult.forEach(row => console.log(row.message))
  42. })
  43. })
  44. } finally {
  45. await conn.end()
  46. }
  47. // Print:
  48. // Hello world!
  49. // DELIMITER is supported!
  50. // '/*!' style comment is executed!
  51. // Goodbye world!
  52. // Done! Query count: 1
  53. console.log('Done! Query count:', queryCount)
  54. })()

Limitation

Some limitations of this module which are currently not addressed:

  • MySQL client will return DELIMITER cannot contain a backslash character if backslash
    is used such as DELIMITER \\, however this module will not throw any error and will
    ignore the DELIMITER line.
  • MySQL client support \g and \G as delimiter, however this module will not treat
    them as delimiter.
  • MySQL client will return DELIMITER must be followed by a ‘delimiter’ character or string
    if there is nothing specified after keyword DELIMITER, however this module will not throw
    any error and will ignore the DELIMITER line.

License

This module is licensed under the MIT License.

Acknowledge

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:

  • exec-sql: Execute MySQL SQL files in directories.
  • execsql: Execute you *.sql file which contains multiple sql statements.
  • sql-ast: Parse the output of mysqldump into an AST.
  • sql-parser: A SQL parser written in pure JS.