Play on GitHub
Grammar Template
A versatile and intuitive grammar-based templating for PHP, Python, JavaScript
Grammar Template ({grammartemplate_version}):
Help
PostOp Mode
SELECT <column.select>[, <*column.select>] FROM <table.from>[, <*table.from>][ WHERE (<?required.where:EXPR>) AND (<?condition.where:EXPR>)][ WHERE <?required.where:EXPR><?!condition.where:EXPR>][ WHERE <?!required.where:EXPR><?condition.where:EXPR>][ GROUP BY <?group>[, <*group>]][ HAVING (<?required.having:EXPR>) AND (<?condition.having:EXPR>)][ HAVING <?required.having:EXPR><?!condition.having:EXPR>][ HAVING <?!required.having:EXPR><?condition.having:EXPR>][ ORDER BY <?order>[, <*order>]][ LIMIT <offset|0>, <?count>][# ** recursive sub-templates ** #][# single factor sub-template #]<:FACTOR>:=[<lhs>[ <?op> <rhs|NULL>]][# foreach term:factors as factor sub-template #]<:TERM>:=[(<factor:FACTOR>[ AND <*factor:FACTOR>])][# foreach expr:terms as term sub-template #]<:EXPR>:=[<term:TERM>[ OR <*term:TERM>]]
Data (JSON):
{ "column" : { "select" : [ "field1", "field2", "field3", "field4" ] }, "table" : { "from" : [ "tbl1", "tbl2" ] }, "condition" : { "where" : [ [ {"lhs": "field1", "op": "=", "rhs": 1}, {"lhs": "field2", "op": "=", "rhs": 2} ], [ {"lhs": "field1", "op": "=", "rhs": 1}, {"lhs": "field3", "op": "=", "rhs": 4} ] ], "having" : [ [ {"lhs": "field1", "op": "=", "rhs": 1} ], [ {"lhs": "field3", "op": "=", "rhs": 4} ] ] }, "count" : 5 }
Output: