Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
dt-sql-parser
Advanced tools
English | 简体中文
dt-sql-parser is a SQL Parser project built with ANTLR4, and it's mainly for the BigData domain. The ANTLR4 generated the basic Parser, Visitor, and Listener, so it's easy to complete the syntax validation, tokenizer, traverse the AST, and so on features.
Besides, it provides some helper methods, like split SQL, and filter the --
and /**/
types of comments in SQL.
Supported SQL:
Tips: This project is the default for Javascript language, also you can try to compile it to other languages if you need.
We have provided a monaco-sql-languages package, you can integrate with monaco-editor
easily.
// use npm
npm i dt-sql-parser --save
// use yarn
yarn add dt-sql-parser
First, we need to import the Parser object from dt-sql-parser
, the different language needs
different Parser, so if you need to handle the Flink SQL, you can import the FlinkSQL Parser.
The below is a GenericSQL Parser example:
import { GenericSQL } from 'dt-sql-parser';
const parser = new GenericSQL();
const correctSql = 'select id,name from user1;';
const errors = parser.validate(correctSql);
console.log(errors);
Output:
/*
[]
*/
Validate failed:
const incorrectSql = 'selec id,name from user1;'
const errors = parser.validate(incorrectSql);
console.log(errors);
Output:
/*
[
{
endCol: 5,
endLine: 1,
startCol: 0,
startLine: 1,
message: "mismatched input 'SELEC' expecting {<EOF>, 'ALTER', 'ANALYZE', 'CALL', 'CHANGE', 'CHECK', 'CREATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXPLAIN', 'GET', 'GRANT', 'INSERT', 'KILL', 'LOAD', 'LOCK', 'OPTIMIZE', 'PURGE', 'RELEASE', 'RENAME', 'REPLACE', 'RESIGNAL', 'REVOKE', 'SELECT', 'SET', 'SHOW', 'SIGNAL', 'UNLOCK', 'UPDATE', 'USE', 'BEGIN', 'BINLOG', 'CACHE', 'CHECKSUM', 'COMMIT', 'DEALLOCATE', 'DO', 'FLUSH', 'HANDLER', 'HELP', 'INSTALL', 'PREPARE', 'REPAIR', 'RESET', 'ROLLBACK', 'SAVEPOINT', 'START', 'STOP', 'TRUNCATE', 'UNINSTALL', 'XA', 'EXECUTE', 'SHUTDOWN', '--', '(', ';'}"
}
]
*/
We instanced a Parser object, and use the validate method to check the SQL syntax, if failed returns an array object includes error message.
Get all tokens by the Parser:
import { GenericSQL } from 'dt-sql-parser';
const parser = new GenericSQL()
const sql = 'select id,name,sex from user1;'
const tokens = parser.getAllTokens(sql)
console.log(tokens)
/*
[
{
channel: 0
column: 0
line: 1
source: [SqlLexer, InputStream]
start: 0
stop: 5
tokenIndex: -1
type: 137
_text: null
text: "SELECT"
},
...
]
*/
Traverse the tree node by the Visitor:
import { GenericSQL, SqlParserVisitor } from 'dt-sql-parser';
const parser = new GenericSQL()
const sql = `select id,name from user1;`
// parseTree
const tree = parser.parse(sql)
class MyVisitor extends SqlParserVisitor {
// overwrite visitTableName
visitTableName(ctx) {
let tableName = ctx.getText().toLowerCase()
console.log('TableName', tableName)
}
// overwrite visitSelectElements
visitSelectElements(ctx) {
let selectElements = ctx.getText().toLowerCase()
console.log('SelectElements', selectElements)
}
}
const visitor = new MyVisitor()
visitor.visit(tree)
/*
SelectElements id,name
TableName user1
*/
Tips: The node's method name can be found in the Visitor file under the corresponding SQL directory
Access the specified node in the AST by the Listener
import { GenericSQL, SqlParserListener } from 'dt-sql-parser';
const parser = new GenericSQL();
const sql = 'select id,name from user1;'
// parseTree
const tree = parser.parse(sql)
class MyListener extends SqlParserListener {
enterTableName(ctx) {
let tableName = ctx.getText().toLowerCase()
console.log('TableName', tableName)
}
enterSelectElements(ctx) {
let selectElements = ctx.getText().toLowerCase()
console.log('SelectElements', selectElements)
}
}
const listenTableName = new MyListener();
parser.listen(listenTableName, tree);
/*
SelectElements id,name
TableName user1
*/
Tips: The node's method name can be found in the Listener file under the corresponding SQL directory
Clear the comments and spaces before and after
import { cleanSql } from 'dt-sql-parser';
const sql = `-- comment comment
select id,name from user1; `
const cleanedSql = cleanSql(sql)
console.log(cleanedSql)
/*
select id,name from user1;
*/
When the SQL text is very big, you can think about to split it by ;
, and handle it by each line.
import { splitSql } from 'dt-sql-parser';
const sql = `select id,name from user1;
select id,name from user2;`
const sqlList = splitSql(sql)
console.log(sqlList)
/*
["select id,name from user1;", "\nselect id,name from user2;"]
*/
Parse the input and convert the AST to a List-like
tree string.
FAQs
SQL Parsers for BigData, built with antlr4
The npm package dt-sql-parser receives a total of 4,429 weekly downloads. As such, dt-sql-parser popularity was classified as popular.
We found that dt-sql-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.