Skip to content

Commit

Permalink
Named and pyformat keys now have a numeric suffix
Browse files Browse the repository at this point in the history
Instead of generating a long random suffix, we now maintain a running number across all the bind parameters.
This means that a clause like
will generate a query like
  • Loading branch information
Sripathi Krishnan committed May 27, 2020
1 parent ccaefce commit b6bfb1a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions jinjasql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def bind_in_clause(value):
return clause

def _bind_param(already_bound, key, value):
new_key = key
new_key = "%s#%s" % (key, random.getrandbits(128))
_thread_local.param_index += 1
new_key = "%s_%s" % (key, _thread_local.param_index)
already_bound[new_key] = value

param_style = _thread_local.param_style
Expand All @@ -132,14 +132,12 @@ def _bind_param(already_bound, key, value):
elif param_style == 'format':
return "%s"
elif param_style == 'numeric':
_thread_local.param_index += 1
return ":%s" % _thread_local.param_index
elif param_style == 'named':
return ":%s" % new_key
elif param_style == 'pyformat':
return "%%(%s)s" % new_key
elif param_style == 'asyncpg':
_thread_local.param_index += 1
return "$%s" % _thread_local.param_index
else:
raise AssertionError("Invalid param_style - %s" % param_style)
Expand Down
8 changes: 4 additions & 4 deletions tests/yaml/macros.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ expected_sql:
numeric: >
select * from timesheet
where day in (:1,:2,:3,:4,:5)
numeric: >
named: >
select * from timesheet
where day in (:1,:2,:3,:4,:5)
numeric: >
where day in (:inclause_1,:inclause_2,:inclause_3,:inclause_4,:inclause_5)
pyformat: >
select * from timesheet
where day in (:1,:2,:3,:4,:5)
where day in (%(inclause_1)s,%(inclause_2)s,%(inclause_3)s,%(inclause_4)s,%(inclause_5)s)
---
name: test_macros
template: >
Expand Down

0 comments on commit b6bfb1a

Please sign in to comment.