sripathikrishnan / jinjasql

Template Language for SQL with Automatic Bind Parameter Extraction
MIT License
807 stars 88 forks source link

complete source code generation #19

Open jayvdb opened 5 years ago

jayvdb commented 5 years ago

I would like to use this with https://github.com/moban to convert a template containing a few custom queries with preset data into a db-specific rendered file, each enclosed in a method which receives extra params and returns the cursor and bound output cols where possible.

A little more voodoo is needed for this tool to process a string containing multiple queries, such as expanding the params in generated wrapping code. That could be done manually in jinja, but it would be a shame to do it that way as this tool is so close.

Moban allows plugins, so some extra init and per file steps can be done in the plugin to make this workable, but ideally most of the voodoo is in this project so it is reusable.

sripathikrishnan commented 4 years ago

I am not sure if there is a specific feature request described here, and the link to moban doesn't really provide any insight. Do you mind providing some additional details for us to consider?

jayvdb commented 4 years ago

Sorry, the link should have been to @moremoban

Unfortunately a long time has passed since I created this issue, and the exact use-case I had has now escaped my brain.

The main part of it that I remember was I was trying to process a python file which contained several queries in it.

I didnt want to run jinjaSQL at runtime - I wanted instead to run it at build time, to convert a template into a database specific python file.

Processing a template with multiple queries was the key problem - especially the variable binding was problematic, perhaps because it was using %s instead of {name} ... ?

clayadavis commented 3 years ago

I think I understand the idea here: applying the templates in a build step and generating rendered .sql queries, similar to Frozen-Flask generating static HTML from templates + data.

Frozen-Flask is an instructive example though in that I think this usecase would probably be best as a separate tool that uses JinjaSQL, as opposed to being built into JinjaSQL itself. This article illustrates some of the steps that could get you there, specifically the section entitled "From a template to the final SQL query."

jayvdb commented 3 years ago

.. rendered .sql queries ..

Well the key is that the input files, .sqljj2 ? ;-), would contain multiple queries, or DDL/DML. Using pyformat, like you did in apply_sql_template, probably makes this easier.