From 53f53ddb8bf81e2ab5fff8537e46f86a33150a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Wed, 16 Sep 2015 21:26:55 +0200 Subject: [PATCH] Implement cached querys in MysqlRedFam --- mysqlred.py | 54 ++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/mysqlred.py b/mysqlred.py index c8b23a2..63cb10f 100644 --- a/mysqlred.py +++ b/mysqlred.py @@ -88,14 +88,19 @@ class MysqlRed: cursor = cls.connection.cursor() # Execute insert query - cursor.execute( cls.__insert_query, cls.__cached_insert_data ) + if cls.__cached_insert_data: + cursor.execute( cls.__insert_query, cls.__cached_insert_data ) + cls.__cached_insert_data = [] # Execute update query # Use executemany since update could not be reduced to one query - cursor.executemany( cls.__update_query, cls.__cached_update_data ) + if cls.__cached_update_data: + cursor.executemany( cls.__update_query, cls.__cached_update_data ) + cls.__cached_update_data = [] # Commit db changes - cls.connection.commit() + if cls.__cached_insert_data or cls.__cached_update_data: + cls.connection.commit() class MysqlRedPage( MysqlRed ): @@ -186,6 +191,17 @@ class MysqlRedFam( MysqlRed ): """ MySQL-db Interface for handling querys for RedFams """ + # Class variables for storing cached querys + __cached_update_data = [] + __update_query = 'UPDATE `red_families` \ + SET `red_page_id` = ?, `heading` = ?, `beginning` = ?, \ + `ending` = ?, `status`= ? WHERE `fam_hash` = ?;' + __cached_insert_data = [] + __insert_query = 'INSERT INTO `red_families` \ + ( fam_hash, red_page_id, beginning, ending, status, \ + heading, article0, article1, article2, article3, \ + article4, article5, article6, article7 ) \ + VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );' def __init__( self, fam_hash ): """ @@ -223,13 +239,6 @@ class MysqlRedFam( MysqlRed ): def add_fam( self, articlesList, heading, red_page_id, beginning, ending=None, status=0 ): - cursor = type( self ).connection.cursor() - - query = 'INSERT INTO `red_families` \ - ( fam_hash, red_page_id, beginning, ending, status, heading, \ - article0, article1, article2, article3, \ - article4, article5, article6, article7 ) \ - VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );' data = [ self.__fam_hash, red_page_id, beginning, ending, status, heading ] @@ -241,11 +250,14 @@ class MysqlRedFam( MysqlRed ): data = tuple( data ) - cursor.execute( query, data) + type( self ).__cached_insert_data.append( data ) - type( self ).connection.commit() - - self.data = self.get_fam() + # Manualy construct self.data dict + data_keys = ( 'fam_hash', 'red_page_id', 'beginning', 'ending', + 'status', 'heading', 'article0', 'article1', 'article2', + 'article3', 'article4', 'article5', 'article6', + 'article7' ) + self.data = dict( zip( data_keys, data ) ) def update_fam( self, red_page_id, heading, beginning, ending, status ): """ @@ -257,14 +269,6 @@ class MysqlRedFam( MysqlRed ): @param int status red_fam status """ - cursor = type( self ).connection.cursor() - - query = 'UPDATE `red_families` \ - SET `red_page_id` = ?, `heading` = ?, `beginning` = ?, \ - `ending` = ?, `status`= ? WHERE `fam_hash` = ?;' - data = ( red_page_id, heading, beginning, - ending, status, self.__fam_hash ) - - cursor.execute( query, data) - - type( self ).connection.commit() + type( self ).__cached_update_data.append( ( red_page_id, heading, + beginning, ending, status, + self.__fam_hash ) )