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_password = config.db_password
|
||||||
db_name = config.db_username + jogobot.db_namesuffix
|
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 ):
|
def __init__( self ):
|
||||||
"""
|
"""
|
||||||
Opens a connection to MySQL-DB
|
Opens a connection to MySQL-DB
|
||||||
@@ -74,18 +80,38 @@ class MysqlRed:
|
|||||||
|
|
||||||
type( self ).connection.close()
|
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 ):
|
class MysqlRedPage( MysqlRed ):
|
||||||
"""
|
"""
|
||||||
MySQL-db Interface for handling querys for RedPages
|
MySQL-db Interface for handling querys for RedPages
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Class variables for storing cached querys
|
# 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 = []
|
__cached_insert_data = []
|
||||||
__insert_query = 'INSERT INTO `red_pages` \
|
__insert_query = 'INSERT INTO `red_pages` \
|
||||||
( page_id, page_title, rev_id, status ) \
|
( page_id, page_title, rev_id, status ) \
|
||||||
VALUES ( ?, ?, ?, ? );'
|
VALUES ( ?, ?, ?, ? );'
|
||||||
|
|
||||||
def __init__( self, page_id ):
|
def __init__( self, page_id ):
|
||||||
"""
|
"""
|
||||||
@@ -131,12 +157,12 @@ class MysqlRedPage( MysqlRed ):
|
|||||||
@param int status Page parsing status
|
@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 ) )
|
rev_id, status ) )
|
||||||
|
|
||||||
# Manualy construct self.data dict
|
# Manualy construct self.data dict
|
||||||
self.data = { 'page_id' : self.__page_id, 'rev_id' : rev_id,
|
self.data = { 'page_id': self.__page_id, 'rev_id': rev_id,
|
||||||
'page_title' : page_title, 'status' : status }
|
'page_title': page_title, 'status': status }
|
||||||
|
|
||||||
def update_page( self, rev_id=None, page_title=None, status=0 ):
|
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
|
@param int status Page parsing status
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cursor = type( self ).connection.cursor()
|
|
||||||
|
|
||||||
if not page_title:
|
if not page_title:
|
||||||
page_title = self.data[ 'page_title' ]
|
page_title = self.data[ 'page_title' ]
|
||||||
if not rev_id:
|
if not rev_id:
|
||||||
rev_id = self.data[ 'rev_id' ]
|
rev_id = self.data[ 'rev_id' ]
|
||||||
|
|
||||||
query = 'UPDATE `red_pages` \
|
type( self ).__cached_update_data.append( ( page_title, rev_id,
|
||||||
SET `page_title` = ?, `rev_id` = ?, `status`= ? \
|
status, self.__page_id ) )
|
||||||
WHERE `page_id` = ?;'
|
|
||||||
data = ( page_title, rev_id, status, self.__page_id )
|
|
||||||
|
|
||||||
cursor.execute( query, data)
|
|
||||||
|
|
||||||
type( self ).connection.commit()
|
|
||||||
|
|
||||||
|
|
||||||
class MysqlRedFam( MysqlRed ):
|
class MysqlRedFam( MysqlRed ):
|
||||||
|
|||||||
Reference in New Issue
Block a user