Re: tokenize and parse sql where clause

1,482 views
Skip to first unread message

mscdex

unread,
Jul 5, 2012, 5:34:02 PM7/5/12
to nodejs
On Jul 5, 2:28 pm, Alex <alex.ga...@controlgroup.com> wrote:
> I need to parse out where statements for sql in an app so that the fields
> can be properly mapped to existing database columns and the statement
> parameterized, etc.  Can anyone suggest an existing node module to
> accomplish this?  Basically, if given a string such as:

Here are two on npm that look like they're still being maintained/
actively developed:

https://github.com/langpavel/node-sqljs
https://github.com/vincent-zhao/sqlparser

Alan Gutierrez

unread,
Jul 5, 2012, 5:35:29 PM7/5/12
to nod...@googlegroups.com
On Thu, Jul 05, 2012 at 11:28:25AM -0700, Alex wrote:
> I need to parse out where statements for sql in an app so that the fields
> can be properly mapped to existing database columns and the statement
> parameterized, etc. Can anyone suggest an existing node module to
> accomplish this? Basically, if given a string such as:
>
> user.name = 'dsdsadadas' and (companyid = 1 or user.companyid = 2) and (
> true = user.isactive)
>
> I want to break it out into its expression counterparts and know that
> user.name, companyid, user.isactive and user.companyid were all column
> identifiers.

I've written an SQL parser in CoffeeScript for a relational database interface
library project of mine, Relatable. You might be able to do something with that.

https://github.com/bigeasy/relatable

There's an SQL grammar for Peg JS. Haven't used it, but looks nifty.

https://github.com/steveyen/sqld3

--
Alan Gutierrez - @bigeasy

Alex

unread,
Jul 5, 2012, 9:25:44 PM7/5/12
to nod...@googlegroups.com
Both look very interesting.  Thanks!  I wound up pulling up  https://github.com/pierrec/node-atok and it works like a charm for what I need.

Alex

unread,
Jul 5, 2012, 9:27:04 PM7/5/12
to nod...@googlegroups.com
Good to know about Peg.js.  Overkill for what I needed here, but definitely looks like an interesting project that I am going to keep tabs on for future parsing/tokenizing needs.

Thanks,
Alex

tjholowaychuk

unread,
Jul 6, 2012, 3:52:26 AM7/6/12
to nodejs
do you need to produce an ast? or just scan?

this is typically the sort of pattern I use for lexers:
https://gist.github.com/3058777
simplified as far as errors go etc of course
Reply all
Reply to author
Forward
0 new messages