I'm planning on passing in multiple f-string based templates into another general method that iterates over databases and tables.
My code:
@staticmethod
def query1_template():
query = '''
SELECT
'{db}' as db_name,
'{tbl}' as tbl_name,
'{config[db][tbl]['definition']}' as definition
'''
return query
def build(self, query, config):
etl = ''
for i, db in enumerate(self.datasets):
for j, tbl in enumerate(self.datasets.get(db)):
if i > 0 or j > 0:
etl += 'UNION ALL'
etl += query
Current output:
SELECT
'{db}' as db_name,
'{tbl}' as tbl_name
UNION ALL
SELECT ...
My expected output should be having the {db}
and {tbl}
values actually filled out.
I've tried etl += query.format(db, tbl)
as well but keep getting a key error for db.
Is there a way for this logic to work?
In the query1_template()
method:
query = '''
SELECT
'{db}' as db_name,
'{tbl}' as tbl_name,
'{definition}' as definition
'''
In the build()
method:
etl += query.format(db=db, tbl=tbl, definition=config[db][tbl]['definition']