Delay definition of db_table_prefix
db_table_prefix should be defined at init of MysqlRed and not at import to have cmdline args already parsed Otherwise it uses default family Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=79 FS#79]
This commit is contained in:
@@ -54,7 +54,7 @@ class MysqlRed:
|
|||||||
db_username = config.db_username
|
db_username = config.db_username
|
||||||
db_password = config.db_password
|
db_password = config.db_password
|
||||||
db_name = config.db_username + jogobot.config['db_suffix']
|
db_name = config.db_username + jogobot.config['db_suffix']
|
||||||
db_table_prefix = pywikibot.Site().family.dbName(pywikibot.Site().code)
|
db_table_prefix = False
|
||||||
|
|
||||||
# Class variables for storing cached querys
|
# Class variables for storing cached querys
|
||||||
_cached_update_data = []
|
_cached_update_data = []
|
||||||
@@ -69,6 +69,14 @@ class MysqlRed:
|
|||||||
@returns mysql-stream MySQL Connection
|
@returns mysql-stream MySQL Connection
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Needs to be generated after Parsing of Args (not at import time)
|
||||||
|
if not type(self).db_table_prefix:
|
||||||
|
type(self).db_table_prefix = \
|
||||||
|
pywikibot.Site().family.dbName(pywikibot.Site().code)
|
||||||
|
|
||||||
|
# Now we can setup prepared queries
|
||||||
|
self._prepare_queries()
|
||||||
|
|
||||||
# Connect to mysqldb only once
|
# Connect to mysqldb only once
|
||||||
if not type( self ).connection:
|
if not type( self ).connection:
|
||||||
|
|
||||||
@@ -89,6 +97,15 @@ class MysqlRed:
|
|||||||
|
|
||||||
type( self ).connection.close()
|
type( self ).connection.close()
|
||||||
|
|
||||||
|
def _prepare_queries( self ):
|
||||||
|
"""
|
||||||
|
Used to replace placeholders in prepared queries
|
||||||
|
"""
|
||||||
|
type(self)._update_query = type(self)._update_query.format(
|
||||||
|
prefix=type(self).db_table_prefix)
|
||||||
|
type(self)._insert_query = type(self)._insert_query.format(
|
||||||
|
prefix=type(self).db_table_prefix)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def flush( cls ):
|
def flush( cls ):
|
||||||
"""
|
"""
|
||||||
@@ -137,15 +154,14 @@ class MysqlRedPage( MysqlRed ):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Class variables for storing cached querys
|
# Class variables for storing cached querys
|
||||||
|
# '{prefix}' will be replaced during super().__init__()
|
||||||
_cached_update_data = []
|
_cached_update_data = []
|
||||||
_update_query = 'UPDATE `{pre}_red_pages` \
|
_update_query = 'UPDATE `{prefix}_red_pages` \
|
||||||
SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;'.format(
|
SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;'
|
||||||
pre=MysqlRed.db_table_prefix)
|
|
||||||
|
|
||||||
_cached_insert_data = {}
|
_cached_insert_data = {}
|
||||||
_insert_query = 'INSERT INTO `{pre}_red_pages` \
|
_insert_query = 'INSERT INTO `{prefix}_red_pages` \
|
||||||
( page_id, page_title, rev_id, status ) VALUES ( ?, ?, ?, ? );'.format(
|
( page_id, page_title, rev_id, status ) VALUES ( ?, ?, ?, ? );'
|
||||||
pre=MysqlRed.db_table_prefix)
|
|
||||||
|
|
||||||
def __init__( self, page_id ):
|
def __init__( self, page_id ):
|
||||||
"""
|
"""
|
||||||
@@ -174,8 +190,8 @@ SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;'.format(
|
|||||||
cursor = type( self ).connection.cursor(mysqldb.DictCursor)
|
cursor = type( self ).connection.cursor(mysqldb.DictCursor)
|
||||||
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
'SELECT * FROM `{pre}_red_pages` WHERE `page_id` = ?;'.format(
|
'SELECT * FROM `{prefix}_red_pages` WHERE `page_id` = ?;'.format(
|
||||||
pre=MysqlRed.db_table_prefix), ( self.__page_id, ) )
|
prefix=type(self).db_table_prefix), ( self.__page_id, ) )
|
||||||
|
|
||||||
res = cursor.fetchone()
|
res = cursor.fetchone()
|
||||||
|
|
||||||
@@ -227,17 +243,14 @@ class MysqlRedFam( MysqlRed ):
|
|||||||
|
|
||||||
# Class variables for storing cached querys
|
# Class variables for storing cached querys
|
||||||
_cached_update_data = []
|
_cached_update_data = []
|
||||||
_update_query = 'UPDATE `{pre}_red_families` \
|
_update_query = 'UPDATE `{prefix}_red_families` \
|
||||||
SET `red_page_id` = ?, `heading` = ?, `beginning` = ?, `ending` = ?, \
|
SET `red_page_id` = ?, `heading` = ?, `beginning` = ?, `ending` = ?, \
|
||||||
`status`= ? WHERE `fam_hash` = ?;'.format(
|
`status`= ? WHERE `fam_hash` = ?;'
|
||||||
pre=MysqlRed.db_table_prefix)
|
|
||||||
|
|
||||||
_cached_insert_data = {}
|
_cached_insert_data = {}
|
||||||
_insert_query = 'INSERT INTO `{pre}_red_families` \
|
_insert_query = 'INSERT INTO `{prefix}_red_families` \
|
||||||
( fam_hash, red_page_id, beginning, ending, status, heading, \
|
( fam_hash, red_page_id, beginning, ending, status, heading, \
|
||||||
article0, article1, article2, article3, article4, article5, article6, \
|
article0, article1, article2, article3, article4, article5, article6, \
|
||||||
article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'.format(
|
article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'
|
||||||
pre=MysqlRed.db_table_prefix)
|
|
||||||
|
|
||||||
def __init__( self ):
|
def __init__( self ):
|
||||||
"""
|
"""
|
||||||
@@ -261,8 +274,8 @@ article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'.format(
|
|||||||
cursor = type( self ).connection.cursor( mysqldb.DictCursor )
|
cursor = type( self ).connection.cursor( mysqldb.DictCursor )
|
||||||
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
'SELECT * FROM `{pre}_red_families` WHERE `fam_hash` = ?;'.format(
|
'SELECT * FROM `{prefix}_red_families` WHERE `fam_hash` = ?;'.
|
||||||
pre=MysqlRed.db_table_prefix), ( fam_hash, ) )
|
format( prefix=type(self).db_table_prefix), ( fam_hash, ) )
|
||||||
|
|
||||||
self.data = cursor.fetchone()
|
self.data = cursor.fetchone()
|
||||||
|
|
||||||
@@ -312,8 +325,8 @@ article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'.format(
|
|||||||
cursor = type( self ).connection.cursor( mysqldb.DictCursor )
|
cursor = type( self ).connection.cursor( mysqldb.DictCursor )
|
||||||
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
'SELECT * FROM `{pre}_red_families` WHERE `status` = ?;'.format(
|
'SELECT * FROM `{prefix}_red_families` WHERE `status` = ?;'.format(
|
||||||
pre=type( self ).db_table_prefix), ( status, ) )
|
prefix=type( self ).db_table_prefix), ( status, ) )
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
res = cursor.fetchmany( 1000 )
|
res = cursor.fetchmany( 1000 )
|
||||||
|
|||||||
Reference in New Issue
Block a user