Skip to content

Commit 2246242

Browse files
committed
Merge pull request #1200 from josenavas/queue-issue
Using regex rather than directly check for indices in the sql args
2 parents ca084a6 + 9d3a86d commit 2246242

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

qiita_db/sql_connection.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
from itertools import chain
8282
from tempfile import mktemp
8383
from datetime import date, time, datetime
84+
import re
8485

8586
from psycopg2 import (connect, ProgrammingError, Error as PostgresError,
8687
OperationalError)
@@ -154,6 +155,8 @@ class SQLConnectionHandler(object):
154155
'admin_with_database': '_admin_args',
155156
'admin_without_database': '_admin_nodb_args'}
156157

158+
_regex = re.compile("{(\d+)}")
159+
157160
"""Encapsulates the DB connection with the Postgres DB
158161
159162
Parameters
@@ -362,11 +365,12 @@ def execute_queue(self, queue):
362365
if sql_args is not None:
363366
for pos, arg in enumerate(sql_args):
364367
# check if previous results needed and replace
365-
if isinstance(arg, str) and \
366-
arg[0] == "{" and arg[-1] == "}":
367-
result_pos = int(arg[1:-1])
368-
sql_args[pos] = results[result_pos]
369-
clear_res = True
368+
if isinstance(arg, str):
369+
result = self._regex.search(arg)
370+
if result:
371+
result_pos = int(result.group(1))
372+
sql_args[pos] = results[result_pos]
373+
clear_res = True
370374
# wipe out results if needed and reset clear_res
371375
if clear_res:
372376
results = []

qiita_db/test/test_sql_connection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ def test_run_queue_placeholders(self):
9393
None, None, None]]
9494
self.assertEqual(obs, exp)
9595

96+
def test_run_queue_placeholders_regex(self):
97+
self.conn_handler.create_queue("toy_queue")
98+
self.conn_handler.add_to_queue(
99+
"toy_queue", "INSERT INTO qiita.qiita_user (email, name, password,"
100+
"phone) VALUES (%s, %s, %s, %s) RETURNING email",
101+
['insert@foo.bar', '', 'pass', '111-111-11112'])
102+
self.conn_handler.add_to_queue(
103+
"toy_queue", "UPDATE qiita.qiita_user SET user_level_id = 1, "
104+
"phone = '222-222-2221' WHERE email = %s AND name = %s",
105+
['{0}', ''])
106+
obs = self.conn_handler.execute_queue("toy_queue")
107+
self.assertEqual(obs, [])
108+
obs = self.conn_handler.execute_fetchall(
109+
"SELECT * from qiita.qiita_user WHERE email = %s",
110+
['insert@foo.bar'])
111+
exp = [['insert@foo.bar', 1, 'pass', '', None, None, '222-222-2221',
112+
None, None, None]]
113+
self.assertEqual(obs, exp)
114+
96115
def test_queue_fail(self):
97116
"""Fail if no results data exists for substitution"""
98117
self.conn_handler = SQLConnectionHandler()

0 commit comments

Comments
 (0)