super hyper-speed version of recognisePlayerIDs() only makes 1-3 database queries per hand instead of between 2 and 30, worst case.

This commit is contained in:
eblade 2009-03-27 01:19:04 -04:00
parent 3c14055244
commit 2681b517a7

View File

@ -1388,21 +1388,49 @@ def recogniseTourneyTypeId(cursor, siteId, buyin, fee, knockout, rebuyOrAddon):
#end def recogniseTourneyTypeId #end def recogniseTourneyTypeId
#returns the SQL ids of the names given in an array #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): def recognisePlayerIDs(cursor, names, site_id):
result = [] cursor.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(names)) # get all playerids by the names passed in
for n in names: ids = dict(cursor.fetchall()) # convert to dict
cursor.execute("SELECT id FROM Players WHERE name=%s", (n,)) if len(ids) != len(names):
tmp = cursor.fetchall() notfound = [n for n in names if n not in ids] # make list of names not in database
if len(tmp) == 0: if notfound: # insert them into database
cursor.execute("INSERT INTO Players (name, siteId) VALUES (%s, %s)", (n, site_id)) cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", (notfound))
cursor.execute("SELECT id FROM Players WHERE name=%s", (n,)) cursor.execute("SELECT name,id FROM Players WHERE name='%s'" % "' OR name='".join(notfound)) # get their new ids
tmp = cursor.fetchall() tmp = dict(cursor.fetchall())
for n in tmp: # put them all into the same dict
ids[n] = tmp[n]
result.append(tmp[0][0]) # 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]
return result
#end def recognisePlayerIDs #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 #recognises the name in the given line and returns its array position in the given array
def recognisePlayerNo(line, names, atype): def recognisePlayerNo(line, names, atype):
#print "recogniseplayerno, names:",names #print "recogniseplayerno, names:",names