debug move to Database.py and move last part of mainparser into Database.py

This commit is contained in:
sqlcoder 2009-07-29 06:17:51 +01:00
parent 298de5dc15
commit 9c62ecb80e
3 changed files with 263 additions and 79 deletions

View File

@ -67,8 +67,7 @@ class Database:
, {'tab':'Tourneys', 'col':'siteTourneyNo', 'drop':0}
]
, [ # indexes for postgres (list index 3)
{'tab':'Boardcards', 'col':'handId', 'drop':0}
, {'tab':'Gametypes', 'col':'siteId', 'drop':0}
{'tab':'Gametypes', 'col':'siteId', 'drop':0}
, {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09
, {'tab':'Hands', 'col':'siteHandNo', 'drop':0}
, {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0}
@ -589,16 +588,16 @@ class Database:
"""Drop some indexes/foreign keys to prepare for bulk import.
Currently keeping the standalone indexes as needed to import quickly"""
stime = time()
c = self.get_cursor()
if self.backend == self.MYSQL_INNODB:
self.get_cursor().execute("SET foreign_key_checks=0")
self.get_cursor().execute("SET autocommit=0")
c.execute("SET foreign_key_checks=0")
c.execute("SET autocommit=0")
return
if self.backend == self.PGSQL:
self.connection.set_isolation_level(0) # allow table/index operations to work
for fk in self.foreignKeys[self.backend]:
if fk['drop'] == 1:
if self.backend == self.MYSQL_INNODB:
c = self.get_cursor()
c.execute("SELECT constraint_name " +
"FROM information_schema.KEY_COLUMN_USAGE " +
#"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb'
@ -640,7 +639,7 @@ class Database:
print "Only MySQL and Postgres supported so far"
return -1
for idx in indexes[self.backend]:
for idx in self.indexes[self.backend]:
if idx['drop'] == 1:
if self.backend == self.MYSQL_INNODB:
print "dropping mysql index ", idx['tab'], idx['col']
@ -684,8 +683,8 @@ class Database:
"""Re-create any dropped indexes/foreign keys after bulk import"""
stime = time()
c = self.get_cursor()
if self.backend == self.MYSQL_INNODB:
c = self.get_cursor()
c.execute("SET foreign_key_checks=1")
c.execute("SET autocommit=1")
return
@ -728,7 +727,7 @@ class Database:
print "Only MySQL and Postgres supported so far"
return -1
for idx in indexes[self.backend]:
for idx in self.indexes[self.backend]:
if idx['drop'] == 1:
if self.backend == self.MYSQL_INNODB:
print "creating mysql index ", idx['tab'], idx['col']
@ -823,12 +822,12 @@ class Database:
"""Drops the fpdb tables from the current db"""
try:
c = self.get_cursor()
if(self.get_backend_name() == 'MySQL InnoDB'):
#Databases with FOREIGN KEY support need this switched of before you can drop tables
self.drop_referential_integrity()
# Query the DB to see what tables exist
c = self.get_cursor()
c.execute(self.sql.query['list_tables'])
for table in c:
c.execute(self.sql.query['drop_table'] + table[0])
@ -972,8 +971,64 @@ class Database:
except:
print "Error during fdb.lock_for_insert:", str(sys.exc_value)
#end def lock_for_insert
def store_the_hand(self, h):
"""Take a HandToWrite object and store it in the db"""
# Following code writes hands to database and commits (or rolls back if there is an error)
try:
result = None
if h.isTourney:
ranks = map(lambda x: 0, h.names) # create an array of 0's equal to the length of names
payin_amounts = fpdb_simple.calcPayin(len(h.names), h.buyin, h.fee)
if h.base == "hold":
result = self.tourney_holdem_omaha(
h.config, h.settings, h.base, h.category, h.siteTourneyNo, h.buyin
, h.fee, h.knockout, h.entries, h.prizepool, h.tourneyStartTime
, h.payin_amounts, h.ranks, h.tourneyTypeId, h.siteID, h.siteHandNo
, h.gametypeID, h.handStartTime, h.names, h.playerIDs, h.startCashes
, h.positions, h.cardValues, h.cardSuits, h.boardValues, h.boardSuits
, h.winnings, h.rakes, h.actionTypes, h.allIns, h.actionAmounts
, h.actionNos, h.hudImportData, h.maxSeats, h.tableName, h.seatNos)
elif h.base == "stud":
result = self.tourney_stud(
h.config, h.settings, h.base, h.category, h.siteTourneyNo
, h.buyin, h.fee, h.knockout, h.entries, h.prizepool, h.tourneyStartTime
, h.payin_amounts, h.ranks, h.tourneyTypeId, h.siteID, h.siteHandNo
, h.gametypeID, h.handStartTime, h.names, h.playerIDs, h.startCashes
, h.antes, h.cardValues, h.cardSuits, h.winnings, h.rakes, h.actionTypes
, h.allIns, h.actionAmounts, h.actionNos, h.hudImportData, h.maxSeats
, h.tableName, h.seatNos)
else:
raise fpself.simple.Fpself.rror("unrecognised category")
else:
if h.base == "hold":
result = self.ring_holdem_omaha(
h.config, h.settings, h.base, h.category, h.siteHandNo
, h.gametypeID, h.handStartTime, h.names, h.playerIDs
, h.startCashes, h.positions, h.cardValues, h.cardSuits
, h.boardValues, h.boardSuits, h.winnings, h.rakes
, h.actionTypes, h.allIns, h.actionAmounts, h.actionNos
, h.hudImportData, h.maxSeats, h.tableName, h.seatNos)
elif h.base == "stud":
result = self.ring_stud(
h.config, h.settings, h.base, h.category, h.siteHandNo, h.gametypeID
, h.handStartTime, h.names, h.playerIDs, h.startCashes, h.antes
, h.cardValues, h.cardSuits, h.winnings, h.rakes, h.actionTypes, h.allIns
, h.actionAmounts, h.actionNos, h.hudImportData, h.maxSeats, h.tableName
, h.seatNos)
else:
raise fpself.simple.Fpself.rror ("unrecognised category")
self.commit()
except:
print "Error storing hand: " + str(sys.exc_value)
self.rollback()
return result
#end def store_the_hand
def storeHands(self, backend, site_hand_no, gametype_id
,hand_start_time, names, tableName, maxSeats, hudCache
,board_values, board_suits):
@ -1514,6 +1569,157 @@ class Database:
#end def store_tourneys_players
# 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
class HandToWrite:
def __init__(self, finished = False): # db_name and game not used any more
try:
self.finished = finished
self.config = None
self.settings = None
self.base = None
self.category = None
self.siteTourneyNo = None
self.buyin = None
self.fee = None
self.knockout = None
self.entries = None
self.prizepool = None
self.tourneyStartTime = None
self.isTourney = None
self.tourneyTypeId = None
self.siteID = None
self.siteHandNo = None
self.gametypeID = None
self.handStartTime = None
self.names = None
self.playerIDs = None
self.startCashes = None
self.positions = None
self.antes = None
self.cardValues = None
self.cardSuits = None
self.boardValues = None
self.boardSuits = None
self.winnings = None
self.rakes = None
self.actionTypes = None
self.allIns = None
self.actionAmounts = None
self.actionNos = None
self.hudImportData = None
self.maxSeats = None
self.tableName = None
self.seatNos = None
except:
print "htw.init error: " + str(sys.exc_info)
raise
# end def __init__
def set_all( self, config, settings, base, category, siteTourneyNo, buyin
, fee, knockout, entries, prizepool, tourneyStartTime
, isTourney, tourneyTypeId, siteID, siteHandNo
, gametypeID, handStartTime, names, playerIDs, startCashes
, positions, antes, cardValues, cardSuits, boardValues, boardSuits
, winnings, rakes, actionTypes, allIns, actionAmounts
, actionNos, hudImportData, maxSeats, tableName, seatNos):
try:
self.config = config
self.settings = settings
self.base = base
self.category = category
self.siteTourneyNo = siteTourneyNo
self.buyin = buyin
self.fee = fee
self.knockout = knockout
self.entries = entries
self.prizepool = prizepool
self.tourneyStartTime = tourneyStartTime
self.isTourney = isTourney
self.tourneyTypeId = tourneyTypeId
self.siteID = siteID
self.siteHandNo = siteHandNo
self.gametypeID = gametypeID
self.handStartTime = handStartTime
self.names = names
self.playerIDs = playerIDs
self.startCashes = startCashes
self.positions = positions
self.antes = antes
self.cardValues = cardValues
self.cardSuits = cardSuits
self.boardValues = boardValues
self.boardSuits = boardSuits
self.winnings = winnings
self.rakes = rakes
self.actionTypes = actionTypes
self.allIns = allIns
self.actionAmounts = actionAmounts
self.actionNos = actionNos
self.hudImportData = hudImportData
self.maxSeats = maxSeats
self.tableName = tableName
self.seatNos = seatNos
except:
print "htw.set_all error: " + str(sys.exc_info)
raise
# end def set_hand
def set_ring_holdem_omaha( self, config, settings, base, category, siteHandNo
, gametypeID, handStartTime, names, playerIDs
, startCashes, positions, cardValues, cardSuits
, boardValues, boardSuits, winnings, rakes
, actionTypes, allIns, actionAmounts, actionNos
, hudImportData, maxSeats, tableName, seatNos ):
self.config = config
self.settings = settings
self.base = base
self.category = category
self.siteHandNo = siteHandNo
self.gametypeID = gametypeID
self.handStartTime = handStartTime
self.names = names
self.playerIDs = playerIDs
self.startCashes = startCashes
self.positions = positions
self.cardValues = cardValues
self.cardSuits = cardSuits
self.boardValues = boardValues
self.boardSuits = boardSuits
self.winnings = winnings
self.rakes = rakes
self.actionTypes = actionTypes
self.allIns = allIns
self.actionAmounts = actionAmounts
self.actionNos = actionNos
self.hudImportData = hudImportData
self.maxSeats = maxSeats
self.tableName = tableName
self.seatNos = seatNos
# end def set_ring_holdem_omaha
def send_ring_holdem_omaha(self, db):
result = db.ring_holdem_omaha(
self.config, self.settings, self.base, self.category, self.siteHandNo
, self.gametypeID, self.handStartTime, self.names, self.playerIDs
, self.startCashes, self.positions, self.cardValues, self.cardSuits
, self.boardValues, self.boardSuits, self.winnings, self.rakes
, self.actionTypes, self.allIns, self.actionAmounts, self.actionNos
, self.hudImportData, self.maxSeats, self.tableName, self.seatNos)
# end def send_ring_holdem_omaha
def get_finished(self):
return( self.finished )
# end def get_finished
def get_siteHandNo(self):
return( self.siteHandNo )
# end def get_siteHandNo
if __name__=="__main__":
c = Configuration.Config()

View File

@ -165,9 +165,10 @@ class Sql:
################################
# List tables
################################
print "db_server =", db_server
if db_server == 'mysql':
self.query['list_tables'] = """SHOW TABLES"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['list_tables'] = """SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"""
elif db_server == 'sqlite': # what is the correct value here?
self.query['list_tables'] = """SELECT name FROM sqlite_master
@ -188,7 +189,7 @@ class Sql:
self.query['createSettingsTable'] = """CREATE TABLE Settings (
version SMALLINT NOT NULL)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createSettingsTable'] = """CREATE TABLE Settings (version SMALLINT)"""
elif db_server == 'sqlite': # what is the correct value here?
@ -206,7 +207,7 @@ class Sql:
name varchar(32) NOT NULL,
currency char(3) NOT NULL)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createSitesTable'] = """CREATE TABLE Sites (
id SERIAL, PRIMARY KEY (id),
name varchar(32),
@ -236,7 +237,7 @@ class Sql:
smallBet int NOT NULL,
bigBet int NOT NULL)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createGametypesTable'] = """CREATE TABLE Gametypes (
id SERIAL, PRIMARY KEY (id),
siteId INTEGER, FOREIGN KEY (siteId) REFERENCES Sites(id),
@ -277,7 +278,7 @@ class Sql:
comment text,
commentTs DATETIME)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createPlayersTable'] = """CREATE TABLE Players (
id SERIAL, PRIMARY KEY (id),
name VARCHAR(32),
@ -308,7 +309,7 @@ class Sql:
ratingTime DATETIME NOT NULL,
handCount int NOT NULL)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createAutoratesTable'] = """CREATE TABLE Autorates (
id BIGSERIAL, PRIMARY KEY (id),
playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id),
@ -360,7 +361,7 @@ class Sql:
comment TEXT,
commentTs DATETIME)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createHandsTable'] = """CREATE TABLE Hands (
id BIGSERIAL, PRIMARY KEY (id),
tableName VARCHAR(20) NOT NULL,
@ -422,7 +423,7 @@ class Sql:
knockout INT NOT NULL,
rebuyOrAddon BOOLEAN NOT NULL)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createTourneyTypesTable'] = """CREATE TABLE TourneyTypes (
id SERIAL, PRIMARY KEY (id),
siteId INT, FOREIGN KEY (siteId) REFERENCES Sites(id),
@ -449,7 +450,7 @@ class Sql:
comment TEXT,
commentTs DATETIME)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createTourneysTable'] = """CREATE TABLE Tourneys (
id SERIAL, PRIMARY KEY (id),
tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
@ -591,7 +592,7 @@ class Sql:
FOREIGN KEY (tourneysPlayersId) REFERENCES TourneysPlayers(id))
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createHandsPlayersTable'] = """CREATE TABLE HandsPlayers (
id BIGSERIAL, PRIMARY KEY (id),
handId BIGINT NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id),
@ -727,7 +728,7 @@ class Sql:
comment TEXT,
commentTs DATETIME)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createTourneysPlayersTable'] = """CREATE TABLE TourneysPlayers (
id BIGSERIAL, PRIMARY KEY (id),
tourneyId INT, FOREIGN KEY (tourneyId) REFERENCES Tourneys(id),
@ -757,7 +758,7 @@ class Sql:
comment TEXT,
commentTs DATETIME)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions (
id BIGSERIAL, PRIMARY KEY (id),
handsPlayerId BIGINT, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id),
@ -877,7 +878,7 @@ class Sql:
street4Raises INT)
ENGINE=INNODB"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['createHudCacheTable'] = """CREATE TABLE HudCache (
id BIGSERIAL, PRIMARY KEY (id),
gametypeId INT, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id),
@ -981,21 +982,21 @@ class Sql:
if db_server == 'mysql':
self.query['addTourneyIndex'] = """ALTER TABLE Tourneys ADD INDEX siteTourneyNo(siteTourneyNo)"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['addTourneyIndex'] = """CREATE INDEX siteTourneyNo ON Tourneys (siteTourneyNo)"""
elif db_server == 'sqlite': # what is the correct value here?
self.query['addHandsIndex'] = """ """
if db_server == 'mysql':
self.query['addHandsIndex'] = """ALTER TABLE Hands ADD INDEX siteHandNo(siteHandNo)"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['addHandsIndex'] = """CREATE INDEX siteHandNo ON Hands (siteHandNo)"""
elif db_server == 'sqlite': # what is the correct value here?
self.query['addHandsIndex'] = """ """
if db_server == 'mysql':
self.query['addPlayersIndex'] = """ALTER TABLE Players ADD INDEX name(name)"""
elif db_server == 'postgres': # what is the correct value here?
elif db_server == 'postgresql': # what is the correct value here?
self.query['addPlayersIndex'] = """CREATE INDEX name ON Players (name)"""
elif db_server == 'sqlite': # what is the correct value here?
self.query['addPlayersIndex'] = """ """

View File

@ -15,15 +15,20 @@
#In the "official" distribution you can find the license in
#agpl-3.0.txt in the docs folder of the package.
#methods that are specific to holdem but not trivial
#parses an in-memory fpdb hand history and calls db routine to store it
import sys
import fpdb_simple
import Database
from time import time, strftime
#parses a holdem hand
def mainParser(settings, siteID, category, hand, config, db = None):
#print "mainparser"
# fdb is not used now - to be removed ...
t0 = time()
#print "mainparser"
backend = settings['db-backend']
@ -51,7 +56,6 @@ def mainParser(settings, siteID, category, hand, config, db = None):
if line[-2:] == "$0": continue
smallBlindLine = i
break
#print "small blind line:",smallBlindLine
gametypeID = fpdb_simple.recogniseGametypeID(backend, db, db.get_cursor(), hand[0], hand[smallBlindLine], siteID, category, isTourney)
if isTourney:
@ -65,6 +69,17 @@ def mainParser(settings, siteID, category, hand, config, db = None):
rebuyOrAddon = fpdb_simple.isRebuyOrAddon(hand[0])
tourneyTypeId = fpdb_simple.recogniseTourneyTypeId(db.get_cursor(), siteID, buyin, fee, knockout, rebuyOrAddon)
else:
siteTourneyNo = -1
buyin = -1
fee = -1
entries = -1
prizepool = -1
knockout = 0
tourneyStartTime= None
rebuyOrAddon = -1
tourneyTypeId = 1
fpdb_simple.isAlreadyInDB(db.get_cursor(), gametypeID, siteHandNo)
@ -145,63 +160,25 @@ def mainParser(settings, siteID, category, hand, config, db = None):
, allIns, actionTypeByNo, winnings, totalWinnings, None
, actionTypes, actionAmounts, antes)
#print "parse: hand data prepared" # only reads up to here apart from inserting new players
try:
db.commit() # need to commit new players as different db connection used
# for other writes. maybe this will change maybe not ...
except:
print "parse: error during rollback: " + str(sys.exc_value)
print "parse: error during commit: " + str(sys.exc_value)
# Following code writes hands to database and commits (or rolls back if there is an error)
try:
if isTourney:
ranks = map(lambda x: 0, names) # create an array of 0's equal to the length of names
payin_amounts = fpdb_simple.calcPayin(len(names), buyin, fee)
if base == "hold":
result = db.tourney_holdem_omaha(
config, settings, base, category, siteTourneyNo, buyin
, fee, knockout, entries, prizepool, tourneyStartTime
, payin_amounts, ranks, tourneyTypeId, siteID, siteHandNo
, gametypeID, handStartTime, names, playerIDs, startCashes
, positions, cardValues, cardSuits, boardValues, boardSuits
, winnings, rakes, actionTypes, allIns, actionAmounts
, actionNos, hudImportData, maxSeats, tableName, seatNos)
elif base == "stud":
result = db.tourney_stud(
config, settings, base, category, siteTourneyNo
, buyin, fee, knockout, entries, prizepool, tourneyStartTime
, payin_amounts, ranks, tourneyTypeId, siteID, siteHandNo
, gametypeID, handStartTime, names, playerIDs, startCashes
, antes, cardValues, cardSuits, winnings, rakes, actionTypes
, allIns, actionAmounts, actionNos, hudImportData, maxSeats
, tableName, seatNos)
else:
raise fpdb_simple.FpdbError("unrecognised category")
else:
if base == "hold":
result = db.ring_holdem_omaha(
config, settings, base, category, siteHandNo
, gametypeID, handStartTime, names, playerIDs
, startCashes, positions, cardValues, cardSuits
, boardValues, boardSuits, winnings, rakes
, actionTypes, allIns, actionAmounts, actionNos
, hudImportData, maxSeats, tableName, seatNos)
elif base == "stud":
result = db.ring_stud(
config, settings, base, category, siteHandNo, gametypeID
, handStartTime, names, playerIDs, startCashes, antes
, cardValues, cardSuits, winnings, rakes, actionTypes, allIns
, actionAmounts, actionNos, hudImportData, maxSeats, tableName
, seatNos)
else:
raise fpdb_simple.FpdbError ("unrecognised category")
db.commit()
except:
print "Error storing hand: " + str(sys.exc_value)
db.rollback()
# save data structures in a HandToWrite instance and then insert into database:
htw = Database.HandToWrite()
htw.set_all( config, settings, base, category, siteTourneyNo, buyin
, fee, knockout, entries, prizepool, tourneyStartTime
, isTourney, tourneyTypeId, siteID, siteHandNo
, gametypeID, handStartTime, names, playerIDs, startCashes
, positions, antes, cardValues, cardSuits, boardValues, boardSuits
, winnings, rakes, actionTypes, allIns, actionAmounts
, actionNos, hudImportData, maxSeats, tableName, seatNos)
result = db.store_the_hand(htw)
t9 = time()
#print "parse and save=(%4.3f)" % (t9-t0)
return result