如何在Node.js中解析/标记化SQL语句 [英] How to parse / tokenize an SQL statement in Node.js
问题描述
我正在寻找一种解析/标记化Node.js应用程序中的SQL语句的方法,以便:
I'm looking for a way to parse / tokenize SQL statement within a Node.js application, in order to:
- 对 ISO/IEC 9075 标准或此处.
- 验证SQL语法.
- 找出查询将要执行的操作(例如,读取还是写入?).
- Tokenize all the "basics" SQL keywords defined in the ISO/IEC 9075 standard or here.
- Validate the SQL syntax.
- Find out what the query is gonna do (e.g. read or write?).
您有什么解决方法或建议偷看吗?
Do you have any solution or advises peeps?
链接:要验证SQL语句的任何Javascript/Jquery库? /a>
Linked: Any Javascript/Jquery Library To validate SQL statment?
我已经进行了研究,发现了一些方法:
I've done research and I found out some ways to do it:
使用现有的node.js库
我做了一个 Google搜索,但我没有找到可以使用的共识且流行的库.我找到了那些:
I did a Google search and I didn't found a consensual and popular library to use. I found those ones:
- 简单SQL解析器( npm 一个>)
- 仅支持SELECT,INSERT,UPDATE和DELETE
- 路上有一个 v2分支
- simple-sql-parser (22 stars on github, 16 daily download on npm)
- Supports only SELECT, INSERT, UPDATE and DELETE
- There is a v2 branch on the road
- 仅支持基本的SELECT语句
- 基于 jison
- v0.0.0-3,正在开发中……完全没有文档:)
不幸的是,这些库中没有一个是完整和可信赖的.
Unfortunately, none of those libraries seams to be complete and trustful.
基于node.js低级令牌生成器库自己做
我可以使用低级令牌生成器库来实现自己的目的,
I can do it my self with a low level tokenizer library like:
- jison ( github ,每天在 npm )上下载240次
- tokenizer ( npm 上下载10次a>)
- jison (1,457 stars on github, 240 daily downloads on npm)
- tokenizer (44 stars on github, 10 daily downloads on npm)
基于现有的Javascript代码美化器自己做
CodeMirror 是一个非常酷的Javascript库(浏览器端),可以识别SQL关键字,字符串等. 演示.
CodeMirror is a pretty cool Javascript library (browser side) that can recognize SQL keywords, strings, etc. Check ou the demo.
我可以基于CodeMirror构建一个node.js库标记器. SQL模式在github上,我也许可以对其进行修改以获取节点应用程序中的令牌.
I can build a node.js library tokenizer based on CodeMirror. The SQL mode is here on github, I can maybe adapt it to get tokens within a node application.
PS: CodeMirror 在
PS: CodeMirror have 5,046 stars on github and is well maintained.
我发现存在两个不同的问题:令牌化和语法验证(与令牌化有关).
I figured out that there are 2 distinct problems: Tokenization and Syntax validation (which is related to tokenization).
我根据 CodeMirror 的"noreferrer> SQL模式
I made myself a SQL tokenizer for Node.js based on the SQL mode of the excellent CodeMirror (5,046 stars on github, well maintained). CodeMirror's SQL mode take in charge "generic" SQL and some SQL particularities like MSSQL, MySQL, PL/SQL, Cassandra, Hive and MariaDB.
当我的项目足够成熟时,我(可能)将其在GitHub上公开并告知您.
When my project will be mature enough, I will (probably) put it public on GitHub and let you know.
关于 SQL语法验证,我发现还没有JavaScript工具(或可在JS中使用的开源项目)...
About the SQL syntax validation, I found no JavaScript tool (or open source project to adapt in JS) yet...
推荐答案
我最近发布了
sql-ast
程序包,该程序包分析SQL脚本并返回AST节点数组. (我找不到所维护的任何类似内容)I recently published the
sql-ast
package, which parses SQL scripts and returns an array of AST nodes. (I couldn't find anything similar that's maintained)目前非常受限制.我只是实现了我需要的东西.如果有时间,请请贡献.我已经尽力使代码库易于理解.
It's very limited at the moment. I've only implemented what I need from it. If you have time, please contribute. I've tried hard to make the codebase understandable.
如果您有兴趣看到它进一步发展,请给它一颗星.随着库的改进,我将更新此答案.
Give it a star if you're interested in seeing it developed further. I will update this answer as the library is improved.
这篇关于如何在Node.js中解析/标记化SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!