I am looking to provide a Web API similar to Facebook’s FQL interface.
https://developers.facebook.com/docs/reference/fql/
My idea is to create a mapping of real SQL tables to fake ones, with nice friendly names etc, and then apply a condition to the query submitted to restrict access where needed.
My problem comes in find/replacing the real table/column names with the fake ones. Obvioiusly a find replace is a bit lame – and could be worked around.
My plan is to “read” the posted query, interpret it into an object graph of sorts, then recreate the query swapping in the relevant names and appending my mandatory conditions for the table in question.
Does anyone have any ideas on a better way to do this OR some tips on creating an object graph of the query? I have tried traditional lexer/parser techniques but I am struggling to make the code obviously readable. Is there something I could use pre-made?
You might want to look into creating your own Abstract Syntax Tree which you use to interpret the request sent to you. If you’re able to build a complete syntax tree with this (see what expression trees can do for LINQ) you must be able to build something that translates your AST to the FQL you need.
There’s a lot to find on how to create your own AST using parser generators etc. It depends on how much time you want to invest in this and how extensible you want everything to be.