From 2681b517a78451b7be15232d54cd888648e2e237 Mon Sep 17 00:00:00 2001 From: eblade Date: Fri, 27 Mar 2009 01:19:04 -0400 Subject: [PATCH] super hyper-speed version of recognisePlayerIDs() only makes 1-3 database queries per hand instead of between 2 and 30, worst case. --- pyfpdb/fpdb_simple.py | 52 +++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/pyfpdb/fpdb_simple.py b/pyfpdb/fpdb_simple.py index eaada6a9..2c432c75 100644 --- a/pyfpdb/fpdb_simple.py +++ b/pyfpdb/fpdb_simple.py @@ -1388,20 +1388,48 @@ def recogniseTourneyTypeId(cursor, siteId, buyin, fee, knockout, rebuyOrAddon): #end def recogniseTourneyTypeId #returns the SQL ids of the names given in an array +# TODO: if someone gets industrious, they should make the parts that use the output of this function deal with a dict +# { playername: id } instead of depending on it's relation to the positions list +# then this can be reduced in complexity a bit + def recognisePlayerIDs(cursor, names, site_id): - result = [] - for n in names: - cursor.execute("SELECT id FROM Players WHERE name=%s", (n,)) - tmp = cursor.fetchall() - if len(tmp) == 0: - cursor.execute("INSERT INTO Players (name, siteId) VALUES (%s, %s)", (n, site_id)) - cursor.execute("SELECT id FROM Players WHERE name=%s", (n,)) - tmp = cursor.fetchall() - - result.append(tmp[0][0]) - - return result + cursor.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(names)) # get all playerids by the names passed in + ids = dict(cursor.fetchall()) # convert to dict + 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.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(notfound)) # get their new ids + tmp = dict(cursor.fetchall()) + for n in tmp: # put them all into the same dict + ids[n] = tmp[n] + + # return them in the SAME ORDER that they came in in the names argument, rather than the order they came out of the DB + return [ids[n] for n in names] #end def recognisePlayerIDs + + +# Here's a version that would work if it wasn't for the fact that it needs to have the output in the same order as input +# this version could also be improved upon using list comprehensions, etc + +#def recognisePlayerIDs(cursor, names, site_id): +# result = [] +# notfound = [] +# cursor.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(names)) +# tmp = dict(cursor.fetchall()) +# for n in names: +# if n not in tmp: +# notfound.append(n) +# else: +# result.append(tmp[n]) +# if notfound: +# cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", (notfound)) +# cursor.execute("SELECT id FROM Players WHERE name='%s'" % "' OR name='".join(notfound)) +# tmp = cursor.fetchall() +# for n in tmp: +# result.append(n[0]) +# +# return result #recognises the name in the given line and returns its array position in the given array def recognisePlayerNo(line, names, atype):