Skip to content

Commit e85ef6d

Browse files
author
Eric Tsai
committed
existing display options use safe SQL
1 parent 7d59920 commit e85ef6d

1 file changed

Lines changed: 14 additions & 50 deletions

File tree

src/election_anomaly/db_routines/__init__.py

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -308,49 +308,6 @@ def get_name_field(element):
308308
return field
309309

310310

311-
# def get_input_options(session, input):
312-
# """Returns a list of response options based on the input"""
313-
# # input comes as a pythonic (snake case) input, need to
314-
# # change to match DB element naming format
315-
# name_parts = input.split('_')
316-
# search_str = "".join([name_part.capitalize() for name_part in name_parts])
317-
318-
# if search_str in ['BallotMeasureContest', 'CandidateContest','BallotMeasureSelection','CandidateContest' ]:
319-
# print(f'Options not available for {input}')
320-
# return None
321-
# elif search_str in ['Election',
322-
# 'Office', 'Party', 'ReportingUnit']:
323-
# column_name = 'Name'
324-
# table_search = True
325-
# elif search_str in ['CountItemStatus', 'CountItemType', 'ElectionType',
326-
# 'IdentifierType', 'ReportingUnitType']:
327-
# column_name = 'Txt'
328-
# table_search = True
329-
# elif search_str == 'Candidate':
330-
# column_name = 'BallotName'
331-
# table_search = True
332-
# else:
333-
# search_str = search_str.lower()
334-
# table_search = False
335-
336-
# connection = session.bind.raw_connection()
337-
# cursor = connection.cursor()
338-
# if table_search:
339-
# q1 = sql.SQL('SELECT {column_name} FROM {search_str};').format(
340-
# column_name=sql.Identifier(column_name),search_str=sql.Identifier(search_str)
341-
# )
342-
# cursor.execute(q1)
343-
# result = cursor.fetchall()
344-
# else:
345-
# q2 = sql.SQL(
346-
# 'SELECT "Name" FROM "ReportingUnit" ru JOIN "ReportingUnitType" rut on ru."ReportingUnitType_Id" = rut."Id" WHERE rut."Txt" = %s'
347-
# )
348-
# cursor.execute(q2,[search_str])
349-
# result = cursor.fetchall()
350-
# connection.close()
351-
# return [r[0] for r in result]
352-
353-
354311
def insert_to_cdf_db(engine, df, element, sep='\t', encoding='iso-8859-1', timestamp=None) -> str:
355312
"""Inserts any new records in <df> into <element>; if <element> has a timestamp column
356313
it must be specified in <timestamp>; <df> must have columns matching <element>, except Id and <timestamp> if any"""
@@ -731,15 +688,22 @@ def get_input_options(session, input, verbose):
731688
table_search = False
732689

733690
if not verbose:
691+
connection = session.bind.raw_connection()
692+
cursor = connection.cursor()
734693
if table_search:
735-
result = session.execute(f'SELECT "{column_name}" FROM "{search_str}";')
736-
return [r[0] for r in result]
694+
q1 = sql.SQL('SELECT {column_name} FROM {search_str};').format(
695+
column_name=sql.Identifier(column_name),search_str=sql.Identifier(search_str)
696+
)
697+
cursor.execute(q1)
698+
result = cursor.fetchall()
737699
else:
738-
result = session.execute(f' \
739-
SELECT "Name" FROM "ReportingUnit" ru \
740-
JOIN "ReportingUnitType" rut on ru."ReportingUnitType_Id" = rut."Id" \
741-
WHERE rut."Txt" = \'{search_str}\'')
742-
return [r[0] for r in result]
700+
q2 = sql.SQL(
701+
'SELECT "Name" FROM "ReportingUnit" ru JOIN "ReportingUnitType" rut on ru."ReportingUnitType_Id" = rut."Id" WHERE rut."Txt" = %s'
702+
)
703+
cursor.execute(q2,[search_str])
704+
result = cursor.fetchall()
705+
connection.close()
706+
return [r[0] for r in result]
743707
else:
744708
# jurisction result are handled differently than the rest of the flow because
745709
# it's the first selection made

0 commit comments

Comments
 (0)