From f5e6408195a6688a08de846b69cd8865462d484f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 11 Nov 2019 19:09:40 -0500 Subject: [PATCH] WIP executemany --- bazarr/get_languages.py | 12 +++++------- libs/sqlite3worker.py | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/bazarr/get_languages.py b/bazarr/get_languages.py index c6f68e79b..4854a43c3 100644 --- a/bazarr/get_languages.py +++ b/bazarr/get_languages.py @@ -11,25 +11,23 @@ from database import database def load_language_in_db(): # Get languages list in langs tuple - langs = [{'code3': lang.alpha_3, 'code2': lang.alpha_2, 'name': lang.name} + langs = [[lang.alpha_3, lang.alpha_2, lang.name] for lang in pycountry.languages if hasattr(lang, 'alpha_2')] # Insert languages in database table - for lang in langs: - database.execute("INSERT OR IGNORE INTO table_settings_languages (code3, code2, name) VALUES (?, ?, ?)", - (lang['code3'], lang['code2'], lang['name'])) + database.execute("INSERT OR IGNORE INTO table_settings_languages (code3, code2, name) VALUES (?, ?, ?)", + langs, execute_many=True) database.execute("INSERT OR IGNORE INTO table_settings_languages (code3, code2, name) " "VALUES ('pob', 'pb', 'Brazilian Portuguese')") - langs = [{'code3b': lang.bibliographic, 'code3': lang.alpha_3} + langs = [[lang.bibliographic, lang.alpha_3] for lang in pycountry.languages if hasattr(lang, 'alpha_2') and hasattr(lang, 'bibliographic')] # Update languages in database table - for lang in langs: - database.execute("UPDATE table_settings_languages SET code3b=? WHERE code3=?", (lang['code3b'], lang['code3'])) + database.execute("UPDATE table_settings_languages SET code3b=? WHERE code3=?", langs, execute_many=True) def language_from_alpha2(lang): diff --git a/libs/sqlite3worker.py b/libs/sqlite3worker.py index 989994628..7353190dc 100644 --- a/libs/sqlite3worker.py +++ b/libs/sqlite3worker.py @@ -90,11 +90,11 @@ class Sqlite3Worker(threading.Thread): """ LOGGER.debug("run: Thread started") execute_count = 0 - for token, query, values, only_one in iter(self.sql_queue.get, None): + for token, query, values, only_one, execute_many in iter(self.sql_queue.get, None): LOGGER.debug("sql_queue: %s", self.sql_queue.qsize()) if token != self.exit_token: LOGGER.debug("run: %s, %s", query, values) - self.run_query(token, query, values, only_one) + self.run_query(token, query, values, only_one, execute_many) execute_count += 1 # Let the executes build up a little before committing to disk # to speed things up. @@ -112,7 +112,7 @@ class Sqlite3Worker(threading.Thread): self.thread_running = False return - def run_query(self, token, query, values, only_one): + def run_query(self, token, query, values, only_one=False, execute_many=False): """Run a query. Args: @@ -136,9 +136,14 @@ class Sqlite3Worker(threading.Thread): "Query returned error: %s: %s: %s", query, values, err) else: try: - self.sqlite3_cursor.execute(query, values) - if query.lower().strip().startswith(("insert", "update")): - self.results[token] = self.sqlite3_cursor.rowcount + if execute_many: + self.sqlite3_cursor.executemany(query, values) + if query.lower().strip().startswith(("insert", "update")): + self.results[token] = self.sqlite3_cursor.rowcount + else: + self.sqlite3_cursor.execute(query, values) + if query.lower().strip().startswith(("insert", "update")): + self.results[token] = self.sqlite3_cursor.rowcount except sqlite3.Error as err: self.results[token] = ( "Query returned error: %s: %s: %s" % (query, values, err)) @@ -181,7 +186,7 @@ class Sqlite3Worker(threading.Thread): if delay < 8: delay += delay - def execute(self, query, values=None, only_one=False): + def execute(self, query, values=None, only_one=False, execute_many=False): """Execute a query. Args: @@ -201,10 +206,10 @@ class Sqlite3Worker(threading.Thread): # If it's a select we queue it up with a token to mark the results # into the output queue so we know what results are ours. if query.lower().strip().startswith(("select", "insert", "update")): - self.sql_queue.put((token, query, values, only_one), timeout=5) + self.sql_queue.put((token, query, values, only_one, execute_many), timeout=5) return self.query_results(token) else: - self.sql_queue.put((token, query, values, only_one), timeout=5) + self.sql_queue.put((token, query, values, only_one, execute_many), timeout=5) def dict_factory(cursor, row):