Collect writing db querys for running once in MysqlRedPage
Add classmethod to MysqlRed for executing collected querys
This commit is contained in:
54
mysqlred.py
54
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
|
||||
@@ -73,6 +79,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 ):
|
||||
@@ -80,12 +103,15 @@ class MysqlRedPage( MysqlRed ):
|
||||
MySQL-db Interface for handling querys for RedPages
|
||||
"""
|
||||
|
||||
# Class variables for storing cached querys
|
||||
__cached_update = []
|
||||
# Class variables for storing cached querys
|
||||
__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 ) \
|
||||
VALUES ( ?, ?, ?, ? );'
|
||||
( page_id, page_title, rev_id, status ) \
|
||||
VALUES ( ?, ?, ?, ? );'
|
||||
|
||||
def __init__( self, page_id ):
|
||||
"""
|
||||
@@ -131,12 +157,12 @@ class MysqlRedPage( MysqlRed ):
|
||||
@param int status Page parsing status
|
||||
"""
|
||||
|
||||
__cached_insert_data.apend( ( self.__page_id, page_title,
|
||||
rev_id, status ) )
|
||||
type( self ).__cached_insert_data.apend( ( self.__page_id, page_title,
|
||||
rev_id, status ) )
|
||||
|
||||
# Manualy construct self.data dict
|
||||
self.data = { 'page_id' : self.__page_id, 'rev_id' : rev_id,
|
||||
'page_title' : page_title, 'status' : status }
|
||||
self.data = { 'page_id': self.__page_id, 'rev_id': rev_id,
|
||||
'page_title': page_title, 'status': status }
|
||||
|
||||
def update_page( self, rev_id=None, page_title=None, status=0 ):
|
||||
"""
|
||||
@@ -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