Collect writing db querys for running once in MysqlRedPage
Add classmethod to MysqlRed for executing collected querys
This commit is contained in:
42
mysqlred.py
42
mysqlred.py
@@ -51,6 +51,12 @@ class MysqlRed:
|
||||
db_password = config.db_password
|
||||
db_name = config.db_username + jogobot.db_namesuffix
|
||||
|
||||
# Class variables for storing cached querys
|
||||
__cached_update_data = []
|
||||
__update_query = ''
|
||||
__cached_insert_data = []
|
||||
__insert_query = ''
|
||||
|
||||
def __init__( self ):
|
||||
"""
|
||||
Opens a connection to MySQL-DB
|
||||
@@ -74,6 +80,23 @@ class MysqlRed:
|
||||
|
||||
type( self ).connection.close()
|
||||
|
||||
@classmethod
|
||||
def flush( cls ):
|
||||
"""
|
||||
Run cached querys
|
||||
"""
|
||||
cursor = cls.connection.cursor()
|
||||
|
||||
# Execute insert query
|
||||
cursor.execute( cls.__insert_query, 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 )
|
||||
|
||||
# Commit db changes
|
||||
cls.connection.commit()
|
||||
|
||||
|
||||
class MysqlRedPage( MysqlRed ):
|
||||
"""
|
||||
@@ -81,7 +104,10 @@ class MysqlRedPage( MysqlRed ):
|
||||
"""
|
||||
|
||||
# Class variables for storing cached querys
|
||||
__cached_update = []
|
||||
__cached_update_data = []
|
||||
__update_query = 'UPDATE `red_pages` \
|
||||
SET `page_title` = ?, `rev_id` = ?, `status`= ? \
|
||||
WHERE `page_id` = ?;'
|
||||
__cached_insert_data = []
|
||||
__insert_query = 'INSERT INTO `red_pages` \
|
||||
( page_id, page_title, rev_id, status ) \
|
||||
@@ -131,7 +157,7 @@ class MysqlRedPage( MysqlRed ):
|
||||
@param int status Page parsing status
|
||||
"""
|
||||
|
||||
__cached_insert_data.apend( ( self.__page_id, page_title,
|
||||
type( self ).__cached_insert_data.apend( ( self.__page_id, page_title,
|
||||
rev_id, status ) )
|
||||
|
||||
# Manualy construct self.data dict
|
||||
@@ -147,21 +173,13 @@ class MysqlRedPage( MysqlRed ):
|
||||
@param int status Page parsing status
|
||||
"""
|
||||
|
||||
cursor = type( self ).connection.cursor()
|
||||
|
||||
if not page_title:
|
||||
page_title = self.data[ 'page_title' ]
|
||||
if not rev_id:
|
||||
rev_id = self.data[ 'rev_id' ]
|
||||
|
||||
query = 'UPDATE `red_pages` \
|
||||
SET `page_title` = ?, `rev_id` = ?, `status`= ? \
|
||||
WHERE `page_id` = ?;'
|
||||
data = ( page_title, rev_id, status, self.__page_id )
|
||||
|
||||
cursor.execute( query, data)
|
||||
|
||||
type( self ).connection.commit()
|
||||
type( self ).__cached_update_data.append( ( page_title, rev_id,
|
||||
status, self.__page_id ) )
|
||||
|
||||
|
||||
class MysqlRedFam( MysqlRed ):
|
||||
|
||||
Reference in New Issue
Block a user