From 069aa025e4881c8eec5c7f4c68f193bfee929ff2 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 31 May 2009 21:25:36 -0400 Subject: [PATCH] Fixes for importing and HUD with the postgres db. --- pyfpdb/Database.py | 36 +++++++++++++++++++----------------- pyfpdb/HUD_main.py | 3 +-- pyfpdb/SQL.py | 40 ++++++++++++++++++++++++++-------------- pyfpdb/fpdb_db.py | 4 +++- pyfpdb/fpdb_simple.py | 2 +- 5 files changed, 50 insertions(+), 35 deletions(-) diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 3fba5c5e..26ee6ee3 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -38,12 +38,15 @@ class Database: if c.supported_databases[db_name].db_server == 'postgresql': # psycopg2 database module for posgres via DB-API import psycopg2 + import psycopg2.extensions + psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) try: - self.connection = psycopg2.connect(host = c.supported_databases[db_name].db_ip, - user = c.supported_databases[db_name].db_user, - password = c.supported_databases[db_name].db_pass, - database = c.supported_databases[db_name].db_name) +# self.connection = psycopg2.connect(host = c.supported_databases[db_name].db_ip, +# user = c.supported_databases[db_name].db_user, +# password = c.supported_databases[db_name].db_pass, +# database = c.supported_databases[db_name].db_name) + self.connection = psycopg2.connect(database = c.supported_databases[db_name].db_name) except: print "Error opening database connection %s. See error log file." % (file) traceback.print_exc(file=sys.stderr) @@ -120,7 +123,7 @@ class Database: """Get and return the cards for each player in the hand.""" cards = {} # dict of cards, the key is the seat number example: {1: 'AcQd9hTs5d'} c = self.connection.cursor() - c.execute(self.sql.query['get_cards'], hand) + c.execute(self.sql.query['get_cards'], (hand, )) colnames = [desc[0] for desc in c.description] for row in c.fetchall(): s_dict = {} @@ -133,7 +136,7 @@ class Database: """Get and return the community cards for the specified hand.""" cards = {} c = self.connection.cursor() - c.execute(self.sql.query['get_common_cards'], hand) + c.execute(self.sql.query['get_common_cards'], (hand,)) colnames = [desc[0] for desc in c.description] for row in c.fetchall(): s_dict = {} @@ -154,20 +157,20 @@ class Database: # cards += "xx" # else: # cards += ranks[d['card' + str(i) + 'Value']] + d['card' +str(i) + 'Suit'] - cv = "card%dValue" % i + cv = "card%dvalue" % i if cv not in d or d[cv] == None: break elif d[cv] == 0: cards += "xx" else: - cs = "card%dSuit" % i + cs = "card%dsuit" % i cards = "%s%s%s" % (cards, ranks[d[cv]], d[cs]) return cards def get_action_from_hand(self, hand_no): action = [ [], [], [], [], [] ] c = self.connection.cursor() - c.execute(self.sql.query['get_action_from_hand'], (hand_no)) + c.execute(self.sql.query['get_action_from_hand'], (hand_no, )) for row in c.fetchall(): street = row[0] act = row[1:] @@ -178,7 +181,7 @@ class Database: """Returns a hash of winners:amount won, given a hand number.""" winners = {} c = self.connection.cursor() - c.execute(self.sql.query['get_winners_from_hand'], (hand)) + c.execute(self.sql.query['get_winners_from_hand'], (hand, )) for row in c.fetchall(): winners[row[0]] = row[1] return winners @@ -205,7 +208,6 @@ class Database: return stat_dict def get_player_id(self, config, site, player_name): - print "site = %s, player name = %s" % (site, player_name) c = self.connection.cursor() c.execute(self.sql.query['get_player_id'], {'player': player_name, 'site': site}) row = c.fetchone() @@ -224,19 +226,19 @@ if __name__=="__main__": h = db_connection.get_last_hand() print "last hand = ", h - hero = db_connection.get_player_id(c, 'PokerStars', 'nutOmatic') + hero = db_connection.get_player_id(c, 'Full Tilt Poker', 'PokerAscetic') print "nutOmatic is id_player = %d" % hero stat_dict = db_connection.get_stats_from_hand(h) for p in stat_dict.keys(): print p, " ", stat_dict[p] - print "nutOmatics stats:" - stat_dict = db_connection.get_stats_from_hand(h, hero) - for p in stat_dict.keys(): - print p, " ", stat_dict[p] +# print "nutOmatics stats:" +# stat_dict = db_connection.get_stats_from_hand(h, hero) +# for p in stat_dict.keys(): +# print p, " ", stat_dict[p] - print "cards =", db_connection.get_cards(73525) + print "cards =", db_connection.get_cards(u'1') db_connection.close_connection print "press enter to continue" diff --git a/pyfpdb/HUD_main.py b/pyfpdb/HUD_main.py index 4501821a..bce77cf1 100755 --- a/pyfpdb/HUD_main.py +++ b/pyfpdb/HUD_main.py @@ -147,7 +147,6 @@ class HUD_main(object): if new_hand_id == "": # blank line means quit self.destroy() break # this thread is not always killed immediately with gtk.main_quit() - # get basic info about the new hand from the db # if there is a db error, complain, skip hand, and proceed try: @@ -168,7 +167,7 @@ class HUD_main(object): (tour_number, tab_number) = mat_obj.group(1, 2) temp_key = tour_number else: # tourney, but can't get number and table - print "could not find tournamtne: skipping " + print "could not find tournament: skipping " sys.stderr.write("Could not find tournament %d in hand %d. Skipping.\n" % (int(tour_number), int(new_hand_id))) continue diff --git a/pyfpdb/SQL.py b/pyfpdb/SQL.py index c9314305..dcdac17d 100644 --- a/pyfpdb/SQL.py +++ b/pyfpdb/SQL.py @@ -237,7 +237,7 @@ class Sql: AND HudCache.gametypeId+0 = Hands.gametypeId+0) INNER JOIN Players ON (Players.id = HandsPlayers.PlayerId+0) WHERE Hands.id = %s - GROUP BY HudCache.PlayerId + GROUP BY HudCache.PlayerId, Players.name, seatNo """ # same as above except stats are aggregated for all blind/limit levels @@ -312,7 +312,7 @@ class Sql: AND gt1.limittype = gt2.limittype AND gt2.id = Hands.gametypeId AND Hands.id = %s) - GROUP BY HudCache.PlayerId + GROUP BY HudCache.PlayerId, Players.name, seatNo """ self.query['get_players_from_hand'] = """ @@ -349,13 +349,20 @@ class Sql: select seatNo AS seat_number, name AS screen_name, - card1Value, card1Suit, - card2Value, card2Suit, - card3Value, card3Suit, - card4Value, card4Suit, - card5Value, card5Suit, - card6Value, card6Suit, - card7Value, card7Suit + card1Value AS card1value, + card1Suit AS card1suit, + card2Value AS card2value, + card2Suit AS card2suit, + card3Value AS card3value, + card3Suit AS card3suit, + card4Value AS card4value, + card4Suit AS card4suit, + card5Value AS card5value, + card5Suit AS card5suit, + card6Value AS card6value, + card6Suit AS card6suit, + card7Value AS card7value, + card7Suit AS card7suit from HandsPlayers, Players where handID = %s and HandsPlayers.playerId = Players.id order by seatNo @@ -363,11 +370,16 @@ class Sql: self.query['get_common_cards'] = """ select - card1Value, card1Suit, - card2Value, card2Suit, - card3Value, card3Suit, - card4Value, card4Suit, - card5Value, card5Suit + card1Value AS card1value, + card1Suit AS card1suit, + card2Value AS card2value, + card2Suit AS card2suit, + card3Value AS card3value, + card3Suit AS card3suit, + card4Value AS card4value, + card4Suit AS card4suit, + card5Value AS card5value, + card5Suit AS card5suit from BoardCards where handId = %s """ diff --git a/pyfpdb/fpdb_db.py b/pyfpdb/fpdb_db.py index eba87f52..e8ff8109 100644 --- a/pyfpdb/fpdb_db.py +++ b/pyfpdb/fpdb_db.py @@ -62,9 +62,10 @@ class fpdb_db: self.db=MySQLdb.connect(host = host, user = user, passwd = password, db = database, use_unicode=True) elif backend==self.PGSQL: import psycopg2 + import psycopg2.extensions + psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) # If DB connection is made over TCP, then the variables # host, user and password are required - print "host=%s user=%s pass=%s." % (host, user, password) if self.host and self.user and self.password: self.db = psycopg2.connect(host = host, user = user, @@ -75,6 +76,7 @@ class fpdb_db: # flat out wrong else: self.db = psycopg2.connect(database = database) +# self.db.set_client_encoding('UNICODE') else: raise fpdb_simple.FpdbError("unrecognised database backend:"+backend) self.cursor=self.db.cursor() diff --git a/pyfpdb/fpdb_simple.py b/pyfpdb/fpdb_simple.py index b2ef9d59..5978b806 100644 --- a/pyfpdb/fpdb_simple.py +++ b/pyfpdb/fpdb_simple.py @@ -1410,7 +1410,7 @@ def recognisePlayerIDs(cursor, names, site_id): if len(ids) != len(names): notfound = [n for n in names if n not in ids] # make list of names not in database if notfound: # insert them into database - cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", (notfound)) + cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", [(n,) for n in notfound]) q2 = "SELECT name,id FROM Players WHERE name=%s" % " OR name=".join(["%s" for n in notfound]) cursor.execute(q2, notfound) # get their new ids tmp = dict(cursor.fetchall())