| Author of jinjasql. The whole point of jinjasql is to dynamically build queries. There comes a time when you need the power of sql, and an ORM gets in the way. Think unions, sql functions, group by, more complex sql. Think dynamically generated sql queries based on some data structure. In such cases, you have to keep track of how many variables you are capturing in the query, and then manually bind them. Jinjasql is just a query generator. At the end of the day, it doesn't actually execute the query. You take the query it generates, and the array of parameters it gives - and then execute it using traditional means using bind parameters. This approach gives you the power of a template language to generate the query. You can create reusable macros, conditionals and other features that a template language provides. |
I just prototyped a very expressive reporting API that takes database driven configurations for the reporting columns (dimensions and metrics), validates inputs and allows very expressive filtering (including partial string matches, etc.) in Flask and SQLAlchemy.
Did not use the ORM part, but rather the query API: http://docs.sqlalchemy.org/en/latest/orm/query.html
Queries included a with statement, the main query had 4 nested subqueries (4 level cascade basically) for ranking, filtering and ordering.
The prototype supports queries like this: Give me best performing shows and their stats in my top 5 countries by the number of viewers I have in each of these top countries, but I want shows not broken down by country, but instead by season and episode, including counts of uniques at each level. (Sorry don't have a copy of the resulting SQL on this computer, but it's fairly long.)
Queries I designed up front, and reverse engineered into being dynamically generated by SQLAlchemy query API. However, by not having a fixed template, I can simply update my reporting table configurations and not have to worry about updating the templates or the query structure.
When the user requests something that does not have top dimension and topN results, I can just omit that part of the query and build valid syntax without it.
All-in-all, the entire API is less than 1k lines of python.