Parse an SQL query into a JavaScript object

3,322 views
Skip to first unread message

João Rodrigues

unread,
Jul 15, 2014, 1:25:06 PM7/15/14
to nod...@googlegroups.com

I want to create a parser, possibly recursive to use in my GIS application that work on top of OpenLayers 2.

I have a query like this:

name = 1 OR name = 2 OR name in(3,4)

The query is written in SQL style.

I want an output like this (as an array of expressions and operators):

var rules = [
    {
        property:'name',
        operator: 'equal',
        value: 1
    },
    'OR',
    {
        property:'name',
        operator: 'equal',
        value: 2
    },
    'OR',
    {
        property:'name',
        operator: 'in',
        value: [3,4]
    }
];

The main problem here is haven't been able to parse the string. I have been looking for libraries that could do it automatically.

I found JISON, but it seems that I have to write the whole grammar.

I wonder if there is a library that parses this kind of SQL string into a JavaScript object/array, for future manipulation.

The purpose is to insert these data in an SLD rule of a layer in OpenLayers 2.

As a matter of fact, it should also be able to parse nested queries.

Thank you.

Matt

unread,
Jul 15, 2014, 2:35:13 PM7/15/14
to nod...@googlegroups.com
You'll need to write a parser. There are tools to help, e.g. http://pegjs.majda.cz/documentation


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0a27c552-6cd5-432a-92fe-f567e853f86d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

João Rodrigues

unread,
Jul 16, 2014, 9:03:28 PM7/16/14
to nod...@googlegroups.com
Terça-feira, 15 de Julho de 2014 19:35:13 UTC+1, Matt Sergeant escreveu:
You'll need to write a parser. There are tools to help, e.g. http://pegjs.majda.cz/documentation


Hi Matt.

I was already writing the parser with JISON, but I'm glad you answered my question, because PEG.js is way easier. :)

Thank you very much!!

João

Daniel Alberto Martinez

unread,
Jul 17, 2014, 10:30:01 AM7/17/14
to nod...@googlegroups.com
Just include the word 'OR' as a key:value into your object and do not insert it into the array as a new element.
I means var rules = [..., {type:'OR', property:'name', operator:'equal', value:[1,2]}  ,...];
That will allow you to eval something like  for(var idx in rules){ switch rules[idx].type;   case 'OR' foo; case 'AND' bar;  default ...}

Kevin Swiber

unread,
Jul 17, 2014, 11:26:32 AM7/17/14
to nod...@googlegroups.com
Hey there,

As part of a previous project, I wrote a SQL parser using Jison.  It's actually a subset/fork of SQL that works over any queryable backend (database, Web API, etc.).  There's currently support for MongoDB and Usergrid, a backend-as-a-service.  This project was spawned out of another, and I still need to migrate over drivers for CouchDB and Salesforce.


Calypso also has ORM-like functionality.  The query language it uses is called CaQL (https://github.com/kevinswiber/calypso#calypso-query-language).  I plan on breaking out the query parsing parts into a separate library, as I actually need it for a separate project, but if you want to get at the parsing guts today, check here: https://github.com/kevinswiber/calypso/tree/master/compiling .  Here's a railroad diagram of the syntax, but it might be slightly out of date: http://kevinswiber.github.io/surface/diagram.html

If you take this route, let me know if you have any questions.  Working with Jison was a lot of fun!

Cheers,

-- 
Kevin Swiber
Projects: https://github.com/kevinswiber
Twitter: @kevinswiber

Simon

unread,
Jul 17, 2014, 11:27:36 AM7/17/14
to nod...@googlegroups.com
What about this: https://github.com/dsferruzza/simpleSqlParser

I've come across a few in the past but they're often not feature-complete and can't parse JOIN and subqueries and such. I think one of the use cases I've seen was to polyfill WebSQL by using IndexedDB in the browser, but I can't find that project now.
Reply all
Reply to author
Forward
0 new messages