4 ms·
There are a lot of examples about SQL in comments. In the SQL case you want something like: def process_template(template: Template) -> tuple[str, tuple]:
by rfoo 1y ago
There are a lot of examples about SQL in comments. In the SQL case you want something like:
def process_template(template: Template) -> tuple[str, tuple]:
sql_parts = []
args = []
for item in template:
if isinstance(item, str):
sql_parts.append(item)
else:
sql_parts.append("?")
args.append(process_value(item.value))
return "".join(sql_parts), tuple(args)
(of course it would be more nuanced, but I hope you get the point)
- pansa2 1y agoYes that makes sense, thanks. Also, my comment was about the amount of boilerplate required, but that can be vastly reduced by writing `process_template` in a more functional style instead of the highly-imperative (Golang-like?) style used in the article. The first `process_template` example is just: def process_template(template: Template) -> str: return ''.join(interleave_longest(template.strings, map(process_value, template.values))) And the second is something like: def process_template(template: Template) -> tuple[str, tuple]: return ( ''.join(interleave_longest(template.strings, ['?'] * len(template.values))), map(process_value, template.values) )