From 9971d853fbb852bd91eb3fc76a030f683ce6f53a Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 10 Apr 2018 16:56:50 -0700 Subject: [PATCH] Make use of voussoirkit.sqlhelpers for the insert filler. --- timesearch/tsdb.py | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/timesearch/tsdb.py b/timesearch/tsdb.py index 2f1c6af..e0d68f7 100644 --- a/timesearch/tsdb.py +++ b/timesearch/tsdb.py @@ -8,6 +8,7 @@ from . import exceptions from . import pushshift from voussoirkit import pathclass +from voussoirkit import sqlhelpers # For backwards compatibility reasons, this list of format strings will help @@ -317,7 +318,7 @@ class TSDB: 'replaced_at': replaced_at, } cur = self.sql.cursor() - (qmarks, bindings) = binding_filler(SQL_EDITS_COLUMNS, postdata, require_all=True) + (qmarks, bindings) = sqlhelpers.insert_filler(SQL_EDITS_COLUMNS, postdata) query = 'INSERT INTO %s VALUES(%s)' % (table, qmarks) cur.execute(query, bindings) @@ -358,7 +359,7 @@ class TSDB: 'augmented_at': None, 'augmented_count': None, } - (qmarks, bindings) = binding_filler(SQL_SUBMISSION_COLUMNS, postdata, require_all=True) + (qmarks, bindings) = sqlhelpers.insert_filler(SQL_SUBMISSION_COLUMNS, postdata) query = 'INSERT INTO submissions VALUES(%s)' % qmarks cur.execute(query, bindings) @@ -414,7 +415,7 @@ class TSDB: 'distinguish': comment.distinguished, 'textlen': len(comment.body), } - (qmarks, bindings) = binding_filler(SQL_COMMENT_COLUMNS, postdata, require_all=True) + (qmarks, bindings) = sqlhelpers.insert_filler(SQL_COMMENT_COLUMNS, postdata) query = 'INSERT INTO comments VALUES(%s)' % qmarks cur.execute(query, bindings) @@ -439,25 +440,6 @@ class TSDB: return existing_entry is None -def binding_filler(column_names, values, require_all=True): - ''' - Manually aligning question marks and bindings is annoying. - Given the table's column names and a dictionary of {column: value}, - return the question marks and the list of bindings in the right order. - ''' - values = values.copy() - for column in column_names: - if column in values: - continue - if require_all: - raise ValueError('Missing column "%s"' % column) - else: - values.setdefault(column, None) - qmarks = '?' * len(column_names) - qmarks = ', '.join(qmarks) - bindings = [values[column] for column in column_names] - return (qmarks, bindings) - def name_from_path(filepath): ''' In order to support usage like