Implement cached querys in MysqlRedFam

This commit is contained in:
2015-09-16 21:26:55 +02:00
parent 26f5912f88
commit 53f53ddb8b

View File

@@ -88,14 +88,19 @@ class MysqlRed:
cursor = cls.connection.cursor() cursor = cls.connection.cursor()
# Execute insert query # 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 # Execute update query
# Use executemany since update could not be reduced to one 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 # Commit db changes
cls.connection.commit() if cls.__cached_insert_data or cls.__cached_update_data:
cls.connection.commit()
class MysqlRedPage( MysqlRed ): class MysqlRedPage( MysqlRed ):
@@ -186,6 +191,17 @@ class MysqlRedFam( MysqlRed ):
""" """
MySQL-db Interface for handling querys for RedFams 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 ): def __init__( self, fam_hash ):
""" """
@@ -223,13 +239,6 @@ class MysqlRedFam( MysqlRed ):
def add_fam( self, articlesList, heading, red_page_id, def add_fam( self, articlesList, heading, red_page_id,
beginning, ending=None, status=0 ): 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, data = [ self.__fam_hash, red_page_id, beginning, ending,
status, heading ] status, heading ]
@@ -241,11 +250,14 @@ class MysqlRedFam( MysqlRed ):
data = tuple( data ) data = tuple( data )
cursor.execute( query, data) type( self ).__cached_insert_data.append( data )
type( self ).connection.commit() # Manualy construct self.data dict
data_keys = ( 'fam_hash', 'red_page_id', 'beginning', 'ending',
self.data = self.get_fam() '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 ): def update_fam( self, red_page_id, heading, beginning, ending, status ):
""" """
@@ -257,14 +269,6 @@ class MysqlRedFam( MysqlRed ):
@param int status red_fam status @param int status red_fam status
""" """
cursor = type( self ).connection.cursor() type( self ).__cached_update_data.append( ( red_page_id, heading,
beginning, ending, status,
query = 'UPDATE `red_families` \ self.__fam_hash ) )
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()