I'm trying to use pyparsing to build a little not-quite-sql parser (I don't have from clauses, I don't have any joins, etc). I've been basing my work today on the simpleSQL.py example script included with pyparsing. I'm trying to add the "GROUP BY" and "ORDER BY" clauses to the parser, but trying to match them no matter which comes before the other.
I'm using the Each class, and it seems to be matching them, but it does not set the result name from within the Each class. I am either not doing something right, or something is happening I'm not clear on.
Again, from the simpleSQL.py example, I removed the fromToken, so my selectStmt now looks like:
# define the grammar
selectStmt << ( selectToken +
columnNameList.setResultsName('columns') +
Optional( CaselessKeyword('where') + whereExpression, "" ).setResultsName('where') +
Each( [ Optional( CaselessKeyword('group by') + columnNameList, "" ).setResultsName('group'),
Optional( CaselessKeyword('order by') + columnNameList, "" ).setResultsName('order') ] ) )
I get the result:
SELECT a WHERE a=b and c = d GROUP BY c, e ORDER By d ->
tokens = ['select', ['a'], 'where', ['a', '=', 'b'], 'and', ['c', '=', 'd'], 'group by', ['c', 'e'], 'order by', ['d']]
SELECT a WHERE a=b and c =d ORDER BY z, y GROUP BY c ->
tokens = ['select', ['a'], 'where', ['a', '=', 'b'], 'and', ['c', '=', 'd'], 'order by', ['z', 'y'], 'group by', ['c']]
So, it looks like it's parsing the group and order clauses in either order, but does not assign the results name (to 'order' and 'group'). I can see this being completely dumb and I'm missing something, or I can see it being that I'm going about finding those clauses in any order (and more clauses in the future, like LIMIT) completely wrong.
Any help would be greatly appreciated! LOVING pyparsing so far.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)