Hi,

Some time ago I created a complete parser for SELECT statements in SQLite
(based on the grammar found in the source code). Sample code below!

Understanding how the Lemon parser works, creating the abstract syntax tree
(AST), and implementing the parser classes turned out to be extraordinary
hard - even though I do have experience with Bison and Flex.

This library is indeed a piece of art, and my company has little use of
it. I'm wondering if there is a commercial interest for this C++ library.
Please let me know.

** SAMPLE CODE **

select_parser parser = select_parser::create_parser(sql);

// Visitor pattern to allow arbitrary operations on the abstract syntax
tree (AST).
select_node_visitor snv;
parser.accept(&snv);
cout << "Result columns: " << snv.get_result_columns().size() << endl;
cout << "Group columns: " << snv.get_group_by_columns().size() << endl;

// Pretty-printed SQL suitable for editors. (I used it myself together with
Scintilla.)
// E.g.
// "select x,y,foo(x) as f  order by f"
// -->
// "SELECT x, y, foo(x) ORDER BY f"
//
sql_printer printer;
parser.accept(&printer);
cout << "Pretty-printed SQL: " << printer.get_sql() << endl;

// Possible to create arbitrary operations on SELECT statements.
my_select_injector injector;
parser.accept(&injector);

// E.g.
injector.toggle_sort_order(); // DESC <-> ASC
injector.make_distinct_result(); // SELECT DISTINCT ...
injector.add_row_limit(3); // ... LIMIT 3

// Or maybe just...
injector.perform_crazy_complex_sql_analysis();

Reply via email to