Skip to content

SQL(select) parser written with jison. parse SQL into abstract syntax tree(AST) and stringify back to SQL. sql grammar follows https://dev.mysql.com/doc/refman/5.7/en/select.html

License

Notifications You must be signed in to change notification settings

picorana/js-sql-parser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-sql-parser

parse / stringify sql (select grammar) in js.

Build Status NPM Version NPM Downloads

sql grammar follows https://dev.mysql.com/doc/refman/5.7/en/select.html

news

  • Fix bug using ' & " for column alias? since v1.4.1 #40, #44
  • Fix bug tableFactor alias since v1.3.0 #34
  • Add support for "`" quoted alias since v1.2.2. #33
  • Fix bug stringify keyword having since v1.2.1. #29
  • Typo 'refrence' has been fixed to 'reference' since v1.2.0.

for more changes see CHANGELOG

commonjs usage

npm install --save js-sql-parser

const parser = require('js-sql-parser');
const ast = parser.parse('select * from dual');

console.log(JSON.stringify(ast, null, 2));

ast.value.selectItems.value[0].value = 'foo';
ast.value.from.value[0].value.value.value = 'bar';

console.log(parser.stringify(ast));
// SELECT foo FROM bar

script tag

<script src="./dist/parser/sqlParser.js"><script/>

var sqlParser = window.sqlParser;
var ast = sqlParser.parse('select * from dual');
var sql = sqlParser.stringify(ast);

AMD supported

...

unsupported grammar currently

  • Hexadecimal Literals as x'01af' X'01af', but 0x01af is supported.
  • keyword COLLATE.
  • parammarker: keyword PREPARE / EXECUTE / DEALLOCATE
  • variable: keyword SET / CREATE PROCEDURE / CREATE FUNCTION
  • identifier expr: ODBC escape syntax
  • matchexpr: Full-Text Search Functions. // to support
  • intervalexpr: Date INTERVAL keyword. // to support
  • into outfile: INTO OUTFILE keyword. // to support

TODO

  • ${value} like value place holder support.

Build

  • Run npm run build to build the distributable.

LICENSE

MIT

About

SQL(select) parser written with jison. parse SQL into abstract syntax tree(AST) and stringify back to SQL. sql grammar follows https://dev.mysql.com/doc/refman/5.7/en/select.html

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Yacc 58.6%
  • JavaScript 41.2%
  • Makefile 0.2%