Database: Remove trainiling whitespace

Non functional patch. Brining the tree closer to Forrests
This commit is contained in:
Worros 2010-10-07 11:45:31 +08:00
parent 1a80509bdf
commit 791ebba895

View File

@ -5,17 +5,17 @@
Create and manage the database objects. Create and manage the database objects.
""" """
# Copyright 2008-2010, Ray E. Barker # Copyright 2008-2010, Ray E. Barker
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@ -105,7 +105,7 @@ class Database:
# Data Structures for index and foreign key creation # Data Structures for index and foreign key creation
# drop_code is an int with possible values: 0 - don't drop for bulk import # drop_code is an int with possible values: 0 - don't drop for bulk import
# 1 - drop during bulk import # 1 - drop during bulk import
# db differences: # db differences:
# - note that mysql automatically creates indexes on constrained columns when # - note that mysql automatically creates indexes on constrained columns when
# foreign keys are created, while postgres does not. Hence the much longer list # foreign keys are created, while postgres does not. Hence the much longer list
# of indexes is required for postgres. # of indexes is required for postgres.
@ -147,7 +147,7 @@ class Database:
] ]
, [ # indexes for sqlite (list index 4) , [ # indexes for sqlite (list index 4)
{'tab':'Hands', 'col':'gametypeId', 'drop':0} {'tab':'Hands', 'col':'gametypeId', 'drop':0}
, {'tab':'HandsPlayers', 'col':'handId', 'drop':0} , {'tab':'HandsPlayers', 'col':'handId', 'drop':0}
, {'tab':'HandsPlayers', 'col':'playerId', 'drop':0} , {'tab':'HandsPlayers', 'col':'playerId', 'drop':0}
, {'tab':'HandsPlayers', 'col':'tourneysPlayersId', 'drop':0} , {'tab':'HandsPlayers', 'col':'tourneysPlayersId', 'drop':0}
, {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0} , {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0}
@ -204,7 +204,7 @@ class Database:
# (fkcol is used for foreigh key name) # (fkcol is used for foreigh key name)
# mysql to list indexes: (CG - "LIST INDEXES" should work too) # mysql to list indexes: (CG - "LIST INDEXES" should work too)
# SELECT table_name, index_name, non_unique, column_name # SELECT table_name, index_name, non_unique, column_name
# FROM INFORMATION_SCHEMA.STATISTICS # FROM INFORMATION_SCHEMA.STATISTICS
# WHERE table_name = 'tbl_name' # WHERE table_name = 'tbl_name'
# AND table_schema = 'db_name' # AND table_schema = 'db_name'
@ -241,7 +241,7 @@ class Database:
# create index indexname on tablename (col); # create index indexname on tablename (col);
def __init__(self, c, sql = None, autoconnect = True): def __init__(self, c, sql = None, autoconnect = True):
#log = Configuration.get_logger("logging.conf", "db", log_dir=c.dir_log) #log = Configuration.get_logger("logging.conf", "db", log_dir=c.dir_log)
log.debug(_("Creating Database instance, sql = %s") % sql) log.debug(_("Creating Database instance, sql = %s") % sql)
self.config = c self.config = c
@ -265,11 +265,11 @@ class Database:
if autoconnect: if autoconnect:
# connect to db # connect to db
self.do_connect(c) self.do_connect(c)
if self.backend == self.PGSQL: if self.backend == self.PGSQL:
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE
#ISOLATION_LEVEL_AUTOCOMMIT = 0 #ISOLATION_LEVEL_AUTOCOMMIT = 0
#ISOLATION_LEVEL_READ_COMMITTED = 1 #ISOLATION_LEVEL_READ_COMMITTED = 1
#ISOLATION_LEVEL_SERIALIZABLE = 2 #ISOLATION_LEVEL_SERIALIZABLE = 2
@ -304,7 +304,7 @@ class Database:
def dumpDatabase(self): def dumpDatabase(self):
result="fpdb database dump\nDB version=" + str(DB_VERSION)+"\n\n" result="fpdb database dump\nDB version=" + str(DB_VERSION)+"\n\n"
tables=self.cursor.execute(self.sql.query['list_tables']) tables=self.cursor.execute(self.sql.query['list_tables'])
tables=self.cursor.fetchall() tables=self.cursor.fetchall()
for table in (u'Actions', u'Autorates', u'Backings', u'Gametypes', u'Hands', u'HandsActions', u'HandsPlayers', u'HudCache', u'Players', u'RawHands', u'RawTourneys', u'Settings', u'Sites', u'TourneyTypes', u'Tourneys', u'TourneysPlayers'): for table in (u'Actions', u'Autorates', u'Backings', u'Gametypes', u'Hands', u'HandsActions', u'HandsPlayers', u'HudCache', u'Players', u'RawHands', u'RawTourneys', u'Settings', u'Sites', u'TourneyTypes', u'Tourneys', u'TourneysPlayers'):
@ -328,7 +328,7 @@ class Database:
result+="\n" result+="\n"
return result return result
#end def dumpDatabase #end def dumpDatabase
# could be used by hud to change hud style # could be used by hud to change hud style
def set_hud_style(self, style): def set_hud_style(self, style):
self.hud_style = style self.hud_style = style
@ -368,7 +368,7 @@ class Database:
self.database = database self.database = database
self.connection = None self.connection = None
self.cursor = None self.cursor = None
if backend == Database.MYSQL_INNODB: if backend == Database.MYSQL_INNODB:
import MySQLdb import MySQLdb
if use_pool: if use_pool:
@ -544,13 +544,13 @@ class Database:
self.cursor.close() self.cursor.close()
self.connection.close() self.connection.close()
self.__connected = False self.__connected = False
def reconnect(self, due_to_error=False): def reconnect(self, due_to_error=False):
"""Reconnects the DB""" """Reconnects the DB"""
#print "started reconnect" #print "started reconnect"
self.disconnect(due_to_error) self.disconnect(due_to_error)
self.connect(self.backend, self.host, self.database, self.user, self.password) self.connect(self.backend, self.host, self.database, self.user, self.password)
def get_backend_name(self): def get_backend_name(self):
"""Returns the name of the currently used backend""" """Returns the name of the currently used backend"""
if self.backend==2: if self.backend==2:
@ -570,7 +570,7 @@ class Database:
c.execute(self.sql.query['get_table_name'], (hand_id, )) c.execute(self.sql.query['get_table_name'], (hand_id, ))
row = c.fetchone() row = c.fetchone()
return row return row
def get_table_info(self, hand_id): def get_table_info(self, hand_id):
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_table_name'], (hand_id, )) c.execute(self.sql.query['get_table_name'], (hand_id, ))
@ -591,18 +591,18 @@ class Database:
c.execute(self.sql.query['get_last_hand']) c.execute(self.sql.query['get_last_hand'])
row = c.fetchone() row = c.fetchone()
return row[0] return row[0]
def get_xml(self, hand_id): def get_xml(self, hand_id):
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_xml'], (hand_id)) c.execute(self.sql.query['get_xml'], (hand_id))
row = c.fetchone() row = c.fetchone()
return row[0] return row[0]
def get_recent_hands(self, last_hand): def get_recent_hands(self, last_hand):
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_recent_hands'], {'last_hand': last_hand}) c.execute(self.sql.query['get_recent_hands'], {'last_hand': last_hand})
return c.fetchall() return c.fetchall()
def get_hand_info(self, new_hand_id): def get_hand_info(self, new_hand_id):
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_hand_info'], new_hand_id) c.execute(self.sql.query['get_hand_info'], new_hand_id)
@ -835,7 +835,7 @@ class Database:
query = query.replace("<signed>", 'signed ') query = query.replace("<signed>", 'signed ')
else: else:
query = query.replace("<signed>", '') query = query.replace("<signed>", '')
subs = (self.hand_1day_ago, hand, hero_id, seats_min, seats_max subs = (self.hand_1day_ago, hand, hero_id, seats_min, seats_max
, hero_id, h_seats_min, h_seats_max) , hero_id, h_seats_min, h_seats_max)
c = self.get_cursor() c = self.get_cursor()
@ -864,7 +864,7 @@ class Database:
#print "DEBUG: stat_dict[%s][%s]: %s" %(playerid, name.lower(), val) #print "DEBUG: stat_dict[%s][%s]: %s" %(playerid, name.lower(), val)
stat_dict[playerid][name.lower()] += val stat_dict[playerid][name.lower()] += val
n += 1 n += 1
if n >= 10000: break # todo: don't think this is needed so set nice and high if n >= 10000: break # todo: don't think this is needed so set nice and high
# prevents infinite loop so leave for now - comment out or remove? # prevents infinite loop so leave for now - comment out or remove?
row = c.fetchone() row = c.fetchone()
else: else:
@ -874,7 +874,7 @@ class Database:
#print "session stat_dict =", stat_dict #print "session stat_dict =", stat_dict
#return stat_dict #return stat_dict
def get_player_id(self, config, siteName, playerName): def get_player_id(self, config, siteName, playerName):
c = self.connection.cursor() c = self.connection.cursor()
siteNameUtf = Charset.to_utf8(siteName) siteNameUtf = Charset.to_utf8(siteName)
@ -886,7 +886,7 @@ class Database:
return row[0] return row[0]
else: else:
return None return None
def get_player_names(self, config, site_id=None, like_player_name="%"): def get_player_names(self, config, site_id=None, like_player_name="%"):
"""Fetch player names from players. Use site_id and like_player_name if provided""" """Fetch player names from players. Use site_id and like_player_name if provided"""
@ -897,7 +897,7 @@ class Database:
c.execute(self.sql.query['get_player_names'], (p_name, site_id, site_id)) c.execute(self.sql.query['get_player_names'], (p_name, site_id, site_id))
rows = c.fetchall() rows = c.fetchall()
return rows return rows
def get_site_id(self, site): def get_site_id(self, site):
c = self.get_cursor() c = self.get_cursor()
c.execute(self.sql.query['getSiteId'], (site,)) c.execute(self.sql.query['getSiteId'], (site,))
@ -941,7 +941,7 @@ class Database:
def prepareBulkImport(self): def prepareBulkImport(self):
"""Drop some indexes/foreign keys to prepare for bulk import. """Drop some indexes/foreign keys to prepare for bulk import.
Currently keeping the standalone indexes as needed to import quickly""" Currently keeping the standalone indexes as needed to import quickly"""
stime = time() stime = time()
c = self.get_cursor() c = self.get_cursor()
@ -959,7 +959,7 @@ class Database:
"FROM information_schema.KEY_COLUMN_USAGE " + "FROM information_schema.KEY_COLUMN_USAGE " +
#"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb'
"WHERE 1=1 " + "WHERE 1=1 " +
"AND table_name = %s AND column_name = %s " + "AND table_name = %s AND column_name = %s " +
"AND referenced_table_name = %s " + "AND referenced_table_name = %s " +
"AND referenced_column_name = %s ", "AND referenced_column_name = %s ",
(fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) )
@ -976,7 +976,7 @@ class Database:
print "dropping pg fk", fk['fktab'], fk['fkcol'] print "dropping pg fk", fk['fktab'], fk['fkcol']
try: try:
# try to lock table to see if index drop will work: # try to lock table to see if index drop will work:
# hmmm, tested by commenting out rollback in grapher. lock seems to work but # hmmm, tested by commenting out rollback in grapher. lock seems to work but
# then drop still hangs :-( does work in some tests though?? # then drop still hangs :-( does work in some tests though??
# will leave code here for now pending further tests/enhancement ... # will leave code here for now pending further tests/enhancement ...
c.execute("BEGIN TRANSACTION") c.execute("BEGIN TRANSACTION")
@ -996,13 +996,13 @@ class Database:
% (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n')) % (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n'))
else: else:
return -1 return -1
for idx in self.indexes[self.backend]: for idx in self.indexes[self.backend]:
if idx['drop'] == 1: if idx['drop'] == 1:
if self.backend == self.MYSQL_INNODB: if self.backend == self.MYSQL_INNODB:
print _("dropping mysql index "), idx['tab'], idx['col'] print _("dropping mysql index "), idx['tab'], idx['col']
try: try:
# apparently nowait is not implemented in mysql so this just hangs if there are locks # apparently nowait is not implemented in mysql so this just hangs if there are locks
# preventing the index drop :-( # preventing the index drop :-(
c.execute( "alter table %s drop index %s;", (idx['tab'],idx['col']) ) c.execute( "alter table %s drop index %s;", (idx['tab'],idx['col']) )
except: except:
@ -1019,13 +1019,13 @@ class Database:
#print "after lock, status:", c.statusmessage #print "after lock, status:", c.statusmessage
try: try:
# table locked ok so index drop should work: # table locked ok so index drop should work:
#print "drop index %s_%s_idx" % (idx['tab'],idx['col']) #print "drop index %s_%s_idx" % (idx['tab'],idx['col'])
c.execute( "drop index if exists %s_%s_idx" % (idx['tab'],idx['col']) ) c.execute( "drop index if exists %s_%s_idx" % (idx['tab'],idx['col']) )
#print "dropped pg index ", idx['tab'], idx['col'] #print "dropped pg index ", idx['tab'], idx['col']
except: except:
if "does not exist" not in str(sys.exc_value): if "does not exist" not in str(sys.exc_value):
print _("warning: drop index %s_%s_idx failed: %s, continuing ...") \ print _("warning: drop index %s_%s_idx failed: %s, continuing ...") \
% (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n')) % (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n'))
c.execute("END TRANSACTION") c.execute("END TRANSACTION")
except: except:
print _("warning: index %s_%s_idx not dropped %s, continuing ...") \ print _("warning: index %s_%s_idx not dropped %s, continuing ...") \
@ -1043,7 +1043,7 @@ class Database:
def afterBulkImport(self): def afterBulkImport(self):
"""Re-create any dropped indexes/foreign keys after bulk import""" """Re-create any dropped indexes/foreign keys after bulk import"""
stime = time() stime = time()
c = self.get_cursor() c = self.get_cursor()
if self.backend == self.MYSQL_INNODB: if self.backend == self.MYSQL_INNODB:
c.execute("SET foreign_key_checks=1") c.execute("SET foreign_key_checks=1")
@ -1059,7 +1059,7 @@ class Database:
"FROM information_schema.KEY_COLUMN_USAGE " + "FROM information_schema.KEY_COLUMN_USAGE " +
#"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb'
"WHERE 1=1 " + "WHERE 1=1 " +
"AND table_name = %s AND column_name = %s " + "AND table_name = %s AND column_name = %s " +
"AND referenced_table_name = %s " + "AND referenced_table_name = %s " +
"AND referenced_column_name = %s ", "AND referenced_column_name = %s ",
(fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) )
@ -1070,8 +1070,8 @@ class Database:
else: else:
print _("creating foreign key "), fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol'] print _("creating foreign key "), fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol']
try: try:
c.execute("alter table " + fk['fktab'] + " add foreign key (" c.execute("alter table " + fk['fktab'] + " add foreign key ("
+ fk['fkcol'] + ") references " + fk['rtab'] + "(" + fk['fkcol'] + ") references " + fk['rtab'] + "("
+ fk['rcol'] + ")") + fk['rcol'] + ")")
except: except:
print _(" create foreign key failed: ") + str(sys.exc_info()) print _(" create foreign key failed: ") + str(sys.exc_info())
@ -1086,7 +1086,7 @@ class Database:
print _(" create foreign key failed: ") + str(sys.exc_info()) print _(" create foreign key failed: ") + str(sys.exc_info())
else: else:
return -1 return -1
for idx in self.indexes[self.backend]: for idx in self.indexes[self.backend]:
if idx['drop'] == 1: if idx['drop'] == 1:
if self.backend == self.MYSQL_INNODB: if self.backend == self.MYSQL_INNODB:
@ -1135,10 +1135,10 @@ class Database:
c.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key) c.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key)
self.commit() self.commit()
#end drop_referential_inegrity #end drop_referential_inegrity
def recreate_tables(self): def recreate_tables(self):
"""(Re-)creates the tables of the current DB""" """(Re-)creates the tables of the current DB"""
self.drop_tables() self.drop_tables()
self.resetPlayerIDs() self.resetPlayerIDs()
self.create_tables() self.create_tables()
@ -1189,7 +1189,7 @@ class Database:
self.rollback() self.rollback()
raise raise
#end def disconnect #end def disconnect
def drop_tables(self): def drop_tables(self):
"""Drops the fpdb tables from the current db""" """Drops the fpdb tables from the current db"""
try: try:
@ -1333,7 +1333,7 @@ class Database:
"FROM information_schema.KEY_COLUMN_USAGE " + "FROM information_schema.KEY_COLUMN_USAGE " +
#"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb'
"WHERE 1=1 " + "WHERE 1=1 " +
"AND table_name = %s AND column_name = %s " + "AND table_name = %s AND column_name = %s " +
"AND referenced_table_name = %s " + "AND referenced_table_name = %s " +
"AND referenced_column_name = %s ", "AND referenced_column_name = %s ",
(fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) )
@ -1344,8 +1344,8 @@ class Database:
else: else:
print _("creating foreign key "), fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol'] print _("creating foreign key "), fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol']
try: try:
c.execute("alter table " + fk['fktab'] + " add foreign key (" c.execute("alter table " + fk['fktab'] + " add foreign key ("
+ fk['fkcol'] + ") references " + fk['rtab'] + "(" + fk['fkcol'] + ") references " + fk['rtab'] + "("
+ fk['rcol'] + ")") + fk['rcol'] + ")")
except: except:
print _(" create foreign key failed: ") + str(sys.exc_info()) print _(" create foreign key failed: ") + str(sys.exc_info())
@ -1382,7 +1382,7 @@ class Database:
"FROM information_schema.KEY_COLUMN_USAGE " + "FROM information_schema.KEY_COLUMN_USAGE " +
#"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb'
"WHERE 1=1 " + "WHERE 1=1 " +
"AND table_name = %s AND column_name = %s " + "AND table_name = %s AND column_name = %s " +
"AND referenced_table_name = %s " + "AND referenced_table_name = %s " +
"AND referenced_column_name = %s ", "AND referenced_column_name = %s ",
(fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) )
@ -1399,7 +1399,7 @@ class Database:
print _("dropping pg foreign key"), fk['fktab'], fk['fkcol'] print _("dropping pg foreign key"), fk['fktab'], fk['fkcol']
try: try:
# try to lock table to see if index drop will work: # try to lock table to see if index drop will work:
# hmmm, tested by commenting out rollback in grapher. lock seems to work but # hmmm, tested by commenting out rollback in grapher. lock seems to work but
# then drop still hangs :-( does work in some tests though?? # then drop still hangs :-( does work in some tests though??
# will leave code here for now pending further tests/enhancement ... # will leave code here for now pending further tests/enhancement ...
c.execute("BEGIN TRANSACTION") c.execute("BEGIN TRANSACTION")
@ -1419,14 +1419,14 @@ class Database:
% (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n')) % (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n'))
else: else:
print _("Only MySQL and Postgres supported so far") print _("Only MySQL and Postgres supported so far")
if self.backend == self.PGSQL: if self.backend == self.PGSQL:
self.connection.set_isolation_level(1) # go back to normal isolation level self.connection.set_isolation_level(1) # go back to normal isolation level
#end def dropAllForeignKeys #end def dropAllForeignKeys
def fillDefaultData(self): def fillDefaultData(self):
c = self.get_cursor() c = self.get_cursor()
c.execute("INSERT INTO Settings (version) VALUES (%s);" % (DB_VERSION)) c.execute("INSERT INTO Settings (version) VALUES (%s);" % (DB_VERSION))
#Fill Sites #Fill Sites
c.execute("INSERT INTO Sites (name,code) VALUES ('Full Tilt Poker', 'FT')") c.execute("INSERT INTO Sites (name,code) VALUES ('Full Tilt Poker', 'FT')")
@ -1458,7 +1458,7 @@ class Database:
c.execute("INSERT INTO Actions (name,code) VALUES ('discards', 'D')") c.execute("INSERT INTO Actions (name,code) VALUES ('discards', 'D')")
c.execute("INSERT INTO Actions (name,code) VALUES ('bringin', 'I')") c.execute("INSERT INTO Actions (name,code) VALUES ('bringin', 'I')")
c.execute("INSERT INTO Actions (name,code) VALUES ('completes', 'P')") c.execute("INSERT INTO Actions (name,code) VALUES ('completes', 'P')")
#end def fillDefaultData #end def fillDefaultData
def rebuild_indexes(self, start=None): def rebuild_indexes(self, start=None):
@ -1487,12 +1487,12 @@ class Database:
p_id = self.get_player_id(self.config, site, self.hero[site_id]) p_id = self.get_player_id(self.config, site, self.hero[site_id])
if p_id: if p_id:
self.hero_ids[site_id] = int(p_id) self.hero_ids[site_id] = int(p_id)
if h_start is None: if h_start is None:
h_start = self.hero_hudstart_def h_start = self.hero_hudstart_def
if v_start is None: if v_start is None:
v_start = self.villain_hudstart_def v_start = self.villain_hudstart_def
if self.hero_ids == {}: if self.hero_ids == {}:
where = "WHERE hp.tourneysPlayersId IS NULL" where = "WHERE hp.tourneysPlayersId IS NULL"
else: else:
@ -1509,7 +1509,7 @@ class Database:
#print "rebuild_sql_cash:",rebuild_sql_cash #print "rebuild_sql_cash:",rebuild_sql_cash
self.get_cursor().execute(self.sql.query['clearHudCache']) self.get_cursor().execute(self.sql.query['clearHudCache'])
self.get_cursor().execute(rebuild_sql_cash) self.get_cursor().execute(rebuild_sql_cash)
if self.hero_ids == {}: if self.hero_ids == {}:
where = "WHERE hp.tourneysPlayersId >= 0" where = "WHERE hp.tourneysPlayersId >= 0"
else: else:
@ -1525,7 +1525,7 @@ class Database:
rebuild_sql_tourney = rebuild_sql_tourney.replace('<tourney_group_clause>', ",t.tourneyTypeId") rebuild_sql_tourney = rebuild_sql_tourney.replace('<tourney_group_clause>', ",t.tourneyTypeId")
rebuild_sql_tourney = rebuild_sql_tourney.replace('<where_clause>', where) rebuild_sql_tourney = rebuild_sql_tourney.replace('<where_clause>', where)
#print "rebuild_sql_tourney:",rebuild_sql_tourney #print "rebuild_sql_tourney:",rebuild_sql_tourney
self.get_cursor().execute(rebuild_sql_tourney) self.get_cursor().execute(rebuild_sql_tourney)
self.commit() self.commit()
print _("Rebuild hudcache took %.1f seconds") % (time() - stime,) print _("Rebuild hudcache took %.1f seconds") % (time() - stime,)
@ -1553,7 +1553,7 @@ class Database:
p_id = self.get_player_id(self.config, site, self.hero[site_id]) p_id = self.get_player_id(self.config, site, self.hero[site_id])
if p_id: if p_id:
self.hero_ids[site_id] = int(p_id) self.hero_ids[site_id] = int(p_id)
q = self.sql.query['get_hero_hudcache_start'].replace("<playerid_list>", str(tuple(self.hero_ids.values()))) q = self.sql.query['get_hero_hudcache_start'].replace("<playerid_list>", str(tuple(self.hero_ids.values())))
c = self.get_cursor() c = self.get_cursor()
c.execute(q) c.execute(q)
@ -1634,20 +1634,20 @@ class Database:
c = self.get_cursor() c = self.get_cursor()
c.execute(q, ( c.execute(q, (
p['tableName'], p['tableName'],
p['gameTypeId'], p['gameTypeId'],
p['siteHandNo'], p['siteHandNo'],
p['tourneyId'], p['tourneyId'],
p['startTime'], p['startTime'],
datetime.utcnow(), #importtime datetime.utcnow(), #importtime
p['seats'], p['seats'],
p['maxSeats'], p['maxSeats'],
p['texture'], p['texture'],
p['playersVpi'], p['playersVpi'],
p['boardcard1'], p['boardcard1'],
p['boardcard2'], p['boardcard2'],
p['boardcard3'], p['boardcard3'],
p['boardcard4'], p['boardcard4'],
p['boardcard5'], p['boardcard5'],
p['playersAtStreet1'], p['playersAtStreet1'],
p['playersAtStreet2'], p['playersAtStreet2'],
@ -1781,14 +1781,14 @@ class Database:
#print "DEBUG: inserts: %s" %inserts #print "DEBUG: inserts: %s" %inserts
#print "DEBUG: q: %s" % q #print "DEBUG: q: %s" % q
c = self.get_cursor() c = self.get_cursor()
if self.import_options['saveActions']: if self.import_options['saveActions']:
for r in inserts: for r in inserts:
c.execute(q, r) c.execute(q, r)
hpid[(r[0], r[1])] = self.get_last_insert_id(c) hpid[(r[0], r[1])] = self.get_last_insert_id(c)
else: else:
c.executemany(q, inserts) c.executemany(q, inserts)
return hpid return hpid
def storeHandsActions(self, hid, pids, hpid, adata, printdata = False): def storeHandsActions(self, hid, pids, hpid, adata, printdata = False):
@ -1834,12 +1834,12 @@ class Database:
update_hudcache = update_hudcache.replace('%s', self.sql.query['placeholder']) update_hudcache = update_hudcache.replace('%s', self.sql.query['placeholder'])
insert_hudcache = self.sql.query['insert_hudcache'] insert_hudcache = self.sql.query['insert_hudcache']
insert_hudcache = insert_hudcache.replace('%s', self.sql.query['placeholder']) insert_hudcache = insert_hudcache.replace('%s', self.sql.query['placeholder'])
#print "DEBUG: %s %s %s" %(hid, pids, pdata) #print "DEBUG: %s %s %s" %(hid, pids, pdata)
inserts = [] inserts = []
for p in pdata: for p in pdata:
line = [0]*85 line = [0]*85
line[0] = 1 # HDs line[0] = 1 # HDs
if pdata[p]['street0VPI']: line[1] = 1 if pdata[p]['street0VPI']: line[1] = 1
if pdata[p]['street0Aggr']: line[2] = 1 if pdata[p]['street0Aggr']: line[2] = 1
@ -1939,7 +1939,7 @@ class Database:
# Test statusmessage to see if update worked, do insert if not # Test statusmessage to see if update worked, do insert if not
# num is a cursor in sqlite # num is a cursor in sqlite
if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1")
or (self.backend == self.MYSQL_INNODB and num == 0) or (self.backend == self.MYSQL_INNODB and num == 0)
or (self.backend == self.SQLITE and num.rowcount == 0)): or (self.backend == self.SQLITE and num.rowcount == 0)):
#move the last 6 items in WHERE clause of row from the end of the array #move the last 6 items in WHERE clause of row from the end of the array
# to the beginning for the INSERT statement # to the beginning for the INSERT statement
@ -1963,7 +1963,7 @@ class Database:
def getGameTypeId(self, siteid, game): def getGameTypeId(self, siteid, game):
c = self.get_cursor() c = self.get_cursor()
#FIXME: Fixed for NL at the moment #FIXME: Fixed for NL at the moment
c.execute(self.sql.query['getGametypeNL'], (siteid, game['type'], game['category'], game['limitType'], game['currency'], c.execute(self.sql.query['getGametypeNL'], (siteid, game['type'], game['category'], game['limitType'], game['currency'],
int(Decimal(game['sb'])*100), int(Decimal(game['bb'])*100))) int(Decimal(game['sb'])*100), int(Decimal(game['bb'])*100)))
tmp = c.fetchone() tmp = c.fetchone()
if (tmp == None): if (tmp == None):
@ -1985,7 +1985,7 @@ class Database:
result = {} result = {}
if(self.pcache == None): if(self.pcache == None):
self.pcache = LambdaDict(lambda key:self.insertPlayer(key[0], key[1])) self.pcache = LambdaDict(lambda key:self.insertPlayer(key[0], key[1]))
for player in pnames: for player in pnames:
result[player] = self.pcache[(player,siteid)] result[player] = self.pcache[(player,siteid)]
# NOTE: Using the LambdaDict does the same thing as: # NOTE: Using the LambdaDict does the same thing as:
@ -2069,7 +2069,7 @@ class Database:
sendFinal = True sendFinal = True
else: else:
self.store_the_hand(h) self.store_the_hand(h)
# optional commit, could be every hand / every N hands / every time a # optional commit, could be every hand / every N hands / every time a
# commit message received?? mark flag to indicate if commits outstanding # commit message received?? mark flag to indicate if commits outstanding
if commitEachHand: if commitEachHand:
self.commit() self.commit()
@ -2115,7 +2115,7 @@ class Database:
def createTourneyType(self, hand):#note: this method is used on Hand and TourneySummary objects def createTourneyType(self, hand):#note: this method is used on Hand and TourneySummary objects
tourneyTypeId = 1 tourneyTypeId = 1
# Check if Tourney exists, and if so retrieve TTypeId : in that case, check values of the ttype # Check if Tourney exists, and if so retrieve TTypeId : in that case, check values of the ttype
cursor = self.get_cursor() cursor = self.get_cursor()
cursor.execute (self.sql.query['getTourneyTypeIdByTourneyNo'].replace('%s', self.sql.query['placeholder']), cursor.execute (self.sql.query['getTourneyTypeIdByTourneyNo'].replace('%s', self.sql.query['placeholder']),
@ -2130,14 +2130,14 @@ class Database:
# Check for an existing TTypeId that matches tourney info, if not found create it # Check for an existing TTypeId that matches tourney info, if not found create it
#print "info that we use to get TT by detail:", hand.siteId, hand.buyinCurrency, hand.buyin, hand.fee, hand.gametype['category'], hand.gametype['limitType'], hand.isKO, hand.isRebuy, hand.isAddOn, hand.speed, hand.isShootout, hand.isMatrix #print "info that we use to get TT by detail:", hand.siteId, hand.buyinCurrency, hand.buyin, hand.fee, hand.gametype['category'], hand.gametype['limitType'], hand.isKO, hand.isRebuy, hand.isAddOn, hand.speed, hand.isShootout, hand.isMatrix
#print "the query:",self.sql.query['getTourneyTypeId'].replace('%s', self.sql.query['placeholder']) #print "the query:",self.sql.query['getTourneyTypeId'].replace('%s', self.sql.query['placeholder'])
cursor.execute (self.sql.query['getTourneyTypeId'].replace('%s', self.sql.query['placeholder']), cursor.execute (self.sql.query['getTourneyTypeId'].replace('%s', self.sql.query['placeholder']),
(hand.siteId, hand.buyinCurrency, hand.buyin, hand.fee, hand.gametype['category'], (hand.siteId, hand.buyinCurrency, hand.buyin, hand.fee, hand.gametype['category'],
hand.gametype['limitType'], hand.maxseats, hand.isKO, hand.gametype['limitType'], hand.maxseats, hand.isKO,
hand.isRebuy, hand.isAddOn, hand.speed, hand.isShootout, hand.isMatrix) hand.isRebuy, hand.isAddOn, hand.speed, hand.isShootout, hand.isMatrix)
) )
result=cursor.fetchone() result=cursor.fetchone()
#print "result of fetching TT by details:",result #print "result of fetching TT by details:",result
try: try:
tourneyTypeId = result[0] tourneyTypeId = result[0]
except TypeError: #this means we need to create a new entry except TypeError: #this means we need to create a new entry
@ -2150,14 +2150,14 @@ class Database:
tourneyTypeId = self.get_last_insert_id(cursor) tourneyTypeId = self.get_last_insert_id(cursor)
return tourneyTypeId return tourneyTypeId
#end def createTourneyType #end def createTourneyType
def createOrUpdateTourney(self, hand, source):#note: this method is used on Hand and TourneySummary objects def createOrUpdateTourney(self, hand, source):#note: this method is used on Hand and TourneySummary objects
cursor = self.get_cursor() cursor = self.get_cursor()
cursor.execute (self.sql.query['getTourneyByTourneyNo'].replace('%s', self.sql.query['placeholder']), cursor.execute (self.sql.query['getTourneyByTourneyNo'].replace('%s', self.sql.query['placeholder']),
(hand.siteId, hand.tourNo)) (hand.siteId, hand.tourNo))
columnNames=[desc[0] for desc in cursor.description] columnNames=[desc[0] for desc in cursor.description]
result=cursor.fetchone() result=cursor.fetchone()
if result != None: if result != None:
if self.backend == Database.PGSQL: if self.backend == Database.PGSQL:
expectedValues = ('comment', 'tourneyname', 'matrixIdProcessed', 'totalRebuyCount', 'totalAddOnCount', expectedValues = ('comment', 'tourneyname', 'matrixIdProcessed', 'totalRebuyCount', 'totalAddOnCount',
@ -2167,7 +2167,7 @@ class Database:
'prizepool', 'startTime', 'entries', 'commentTs', 'endTime') 'prizepool', 'startTime', 'entries', 'commentTs', 'endTime')
updateDb=False updateDb=False
resultDict = dict(zip(columnNames, result)) resultDict = dict(zip(columnNames, result))
tourneyId = resultDict["id"] tourneyId = resultDict["id"]
if source=="TS": if source=="TS":
for ev in expectedValues : for ev in expectedValues :
@ -2196,7 +2196,7 @@ class Database:
tourneyId = self.get_last_insert_id(cursor) tourneyId = self.get_last_insert_id(cursor)
return tourneyId return tourneyId
#end def createOrUpdateTourney #end def createOrUpdateTourney
def createOrUpdateTourneysPlayers(self, hand, source):#note: this method is used on Hand and TourneySummary objects def createOrUpdateTourneysPlayers(self, hand, source):#note: this method is used on Hand and TourneySummary objects
tourneysPlayersIds={} tourneysPlayersIds={}
for player in hand.players: for player in hand.players:
@ -2206,7 +2206,7 @@ class Database:
playerId = hand.dbid_pids[player[1]] playerId = hand.dbid_pids[player[1]]
else: else:
raise FpdbParseError(_("invalid source in Database.createOrUpdateTourneysPlayers")) raise FpdbParseError(_("invalid source in Database.createOrUpdateTourneysPlayers"))
cursor = self.get_cursor() cursor = self.get_cursor()
cursor.execute (self.sql.query['getTourneysPlayersByIds'].replace('%s', self.sql.query['placeholder']), cursor.execute (self.sql.query['getTourneysPlayersByIds'].replace('%s', self.sql.query['placeholder']),
(hand.tourneyId, playerId)) (hand.tourneyId, playerId))
@ -2217,14 +2217,14 @@ class Database:
expectedValues = ('rank', 'winnings', 'winningsCurrency', 'rebuyCount', 'addOnCount', 'koCount') expectedValues = ('rank', 'winnings', 'winningsCurrency', 'rebuyCount', 'addOnCount', 'koCount')
updateDb=False updateDb=False
resultDict = dict(zip(columnNames, result)) resultDict = dict(zip(columnNames, result))
tourneysPlayersIds[player[1]]=result[0] tourneysPlayersIds[player[1]]=result[0]
if source=="TS": if source=="TS":
for ev in expectedValues : for ev in expectedValues :
handAttribute=ev handAttribute=ev
if ev!="winnings" and ev!="winningsCurrency": if ev!="winnings" and ev!="winningsCurrency":
handAttribute+="s" handAttribute+="s"
if getattr(hand, handAttribute)[player]==None and resultDict[ev]!=None:#DB has this value but object doesnt, so update object if getattr(hand, handAttribute)[player]==None and resultDict[ev]!=None:#DB has this value but object doesnt, so update object
setattr(hand, handAttribute, resultDict[ev][player]) setattr(hand, handAttribute, resultDict[ev][player])
elif getattr(hand, handAttribute)[player]!=None and resultDict[ev]==None:#object has this value but DB doesnt, so update DB elif getattr(hand, handAttribute)[player]!=None and resultDict[ev]==None:#object has this value but DB doesnt, so update DB
@ -2250,7 +2250,7 @@ class Database:
tourneysPlayersIds[player[1]]=self.get_last_insert_id(cursor) tourneysPlayersIds[player[1]]=self.get_last_insert_id(cursor)
return tourneysPlayersIds return tourneysPlayersIds
#end def createOrUpdateTourneysPlayers #end def createOrUpdateTourneysPlayers
def getTourneyTypesIds(self): def getTourneyTypesIds(self):
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['getTourneyTypesIds']) c.execute(self.sql.query['getTourneyTypesIds'])
@ -2262,31 +2262,31 @@ class Database:
c = self.get_cursor() c = self.get_cursor()
c.execute(self.sql.query['getTourneyInfo'], (siteName, tourneyNo)) c.execute(self.sql.query['getTourneyInfo'], (siteName, tourneyNo))
columnNames=c.description columnNames=c.description
names=[] names=[]
for column in columnNames: for column in columnNames:
names.append(column[0]) names.append(column[0])
data=c.fetchone() data=c.fetchone()
return (names,data) return (names,data)
#end def getTourneyInfo #end def getTourneyInfo
def getTourneyPlayerInfo(self, siteName, tourneyNo, playerName): def getTourneyPlayerInfo(self, siteName, tourneyNo, playerName):
c = self.get_cursor() c = self.get_cursor()
c.execute(self.sql.query['getTourneyPlayerInfo'], (siteName, tourneyNo, playerName)) c.execute(self.sql.query['getTourneyPlayerInfo'], (siteName, tourneyNo, playerName))
columnNames=c.description columnNames=c.description
names=[] names=[]
for column in columnNames: for column in columnNames:
names.append(column[0]) names.append(column[0])
data=c.fetchone() data=c.fetchone()
return (names,data) return (names,data)
#end def getTourneyPlayerInfo #end def getTourneyPlayerInfo
#end class Database #end class Database
# Class used to hold all the data needed to write a hand to the db # Class used to hold all the data needed to write a hand to the db
# mainParser() in fpdb_parse_logic.py creates one of these and then passes it to # mainParser() in fpdb_parse_logic.py creates one of these and then passes it to
# self.insert_queue_hands() # self.insert_queue_hands()
class HandToWrite: class HandToWrite:
@ -2334,7 +2334,7 @@ class HandToWrite:
print _("HandToWrite.init error: ") + str(sys.exc_info()) print _("HandToWrite.init error: ") + str(sys.exc_info())
raise raise
# end def __init__ # end def __init__
def set_all( self, config, settings, base, category, siteTourneyNo, buyin def set_all( self, config, settings, base, category, siteTourneyNo, buyin
, fee, knockout, entries, prizepool, tourneyStartTime , fee, knockout, entries, prizepool, tourneyStartTime
, isTourney, tourneyTypeId, siteID, siteHandNo , isTourney, tourneyTypeId, siteID, siteHandNo
@ -2342,7 +2342,7 @@ class HandToWrite:
, positions, antes, cardValues, cardSuits, boardValues, boardSuits , positions, antes, cardValues, cardSuits, boardValues, boardSuits
, winnings, rakes, actionTypes, allIns, actionAmounts , winnings, rakes, actionTypes, allIns, actionAmounts
, actionNos, hudImportData, maxSeats, tableName, seatNos): , actionNos, hudImportData, maxSeats, tableName, seatNos):
try: try:
self.config = config self.config = config
self.settings = settings self.settings = settings
@ -2388,7 +2388,7 @@ class HandToWrite:
def get_finished(self): def get_finished(self):
return( self.finished ) return( self.finished )
# end def get_finished # end def get_finished
def get_siteHandNo(self): def get_siteHandNo(self):
return( self.siteHandNo ) return( self.siteHandNo )
# end def get_siteHandNo # end def get_siteHandNo
@ -2406,14 +2406,14 @@ if __name__=="__main__":
# db_connection.recreate_tables() # db_connection.recreate_tables()
db_connection.dropAllIndexes() db_connection.dropAllIndexes()
db_connection.createAllIndexes() db_connection.createAllIndexes()
h = db_connection.get_last_hand() h = db_connection.get_last_hand()
print "last hand = ", h print "last hand = ", h
hero = db_connection.get_player_id(c, 'PokerStars', 'nutOmatic') hero = db_connection.get_player_id(c, 'PokerStars', 'nutOmatic')
if hero: if hero:
print _("nutOmatic is id_player = %d") % hero print _("nutOmatic is id_player = %d") % hero
# example of displaying query plan in sqlite: # example of displaying query plan in sqlite:
if db_connection.backend == 4: if db_connection.backend == 4:
print print
@ -2422,16 +2422,16 @@ if __name__=="__main__":
for row in c.fetchall(): for row in c.fetchall():
print _("query plan: "), row print _("query plan: "), row
print print
t0 = time() t0 = time()
stat_dict = db_connection.get_stats_from_hand(h, "ring") stat_dict = db_connection.get_stats_from_hand(h, "ring")
t1 = time() t1 = time()
for p in stat_dict.keys(): for p in stat_dict.keys():
print p, " ", stat_dict[p] print p, " ", stat_dict[p]
print _("cards ="), db_connection.get_cards(u'1') print _("cards ="), db_connection.get_cards(u'1')
db_connection.close_connection db_connection.close_connection
print _("get_stats took: %4.3f seconds") % (t1-t0) print _("get_stats took: %4.3f seconds") % (t1-t0)
print _("press enter to continue") print _("press enter to continue")