Implement cached querys in MysqlRedFam
This commit is contained in:
48
mysqlred.py
48
mysqlred.py
@@ -88,13 +88,18 @@ class MysqlRed:
|
||||
cursor = cls.connection.cursor()
|
||||
|
||||
# Execute insert query
|
||||
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
|
||||
if cls.__cached_update_data:
|
||||
cursor.executemany( cls.__update_query, cls.__cached_update_data )
|
||||
cls.__cached_update_data = []
|
||||
|
||||
# Commit db changes
|
||||
if cls.__cached_insert_data or cls.__cached_update_data:
|
||||
cls.connection.commit()
|
||||
|
||||
|
||||
@@ -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 ) )
|
||||
|
||||
Reference in New Issue
Block a user