Retab/whitespace demangle fpdb_db.py
This commit is contained in:
parent
c9700fc1a7
commit
7ecfac5437
|
@ -21,159 +21,160 @@ import fpdb_simple
|
||||||
import FpdbSQLQueries
|
import FpdbSQLQueries
|
||||||
|
|
||||||
class fpdb_db:
|
class fpdb_db:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Simple constructor, doesnt really do anything"""
|
"""Simple constructor, doesnt really do anything"""
|
||||||
self.db=None
|
self.db=None
|
||||||
self.cursor=None
|
self.cursor=None
|
||||||
self.sql = {}
|
self.sql = {}
|
||||||
self.MYSQL_INNODB=2
|
self.MYSQL_INNODB=2
|
||||||
self.PGSQL=3
|
self.PGSQL=3
|
||||||
self.SQLITE=4
|
self.SQLITE=4
|
||||||
#end def __init__
|
#end def __init__
|
||||||
|
|
||||||
def connect(self, backend, host, database, user, password):
|
def connect(self, backend, host, database, user, password):
|
||||||
"""Connects a database with the given parameters"""
|
"""Connects a database with the given parameters"""
|
||||||
self.backend=backend
|
print backend, host, database, user, password
|
||||||
self.host=host
|
self.backend=backend
|
||||||
self.database=database
|
self.host=host
|
||||||
self.user=user
|
self.database=database
|
||||||
self.password=password
|
self.user=user
|
||||||
if backend==self.MYSQL_INNODB:
|
self.password=password
|
||||||
import MySQLdb
|
if backend==self.MYSQL_INNODB:
|
||||||
self.db=MySQLdb.connect(host = host, user = user, passwd = password, db = database)
|
import MySQLdb
|
||||||
elif backend==self.PGSQL:
|
self.db=MySQLdb.connect(host = host, user = user, passwd = password, db = database)
|
||||||
import psycopg2
|
elif backend==self.PGSQL:
|
||||||
self.db = psycopg2.connect(host = host,
|
import psycopg2
|
||||||
|
self.db = psycopg2.connect(host = host,
|
||||||
user = user,
|
user = user,
|
||||||
password = password,
|
password = password,
|
||||||
database = database)
|
database = database)
|
||||||
else:
|
else:
|
||||||
raise fpdb_simple.FpdbError("unrecognised database backend:"+backend)
|
raise fpdb_simple.FpdbError("unrecognised database backend:"+backend)
|
||||||
self.cursor=self.db.cursor()
|
self.cursor=self.db.cursor()
|
||||||
self.cursor.execute('SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED')
|
self.cursor.execute('SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED')
|
||||||
# Set up query dictionary as early in the connection process as we can.
|
# Set up query dictionary as early in the connection process as we can.
|
||||||
self.sql = FpdbSQLQueries.FpdbSQLQueries(self.get_backend_name())
|
self.sql = FpdbSQLQueries.FpdbSQLQueries(self.get_backend_name())
|
||||||
self.wrongDbVersion=False
|
self.wrongDbVersion=False
|
||||||
try:
|
try:
|
||||||
self.cursor.execute("SELECT * FROM Settings")
|
self.cursor.execute("SELECT * FROM Settings")
|
||||||
settings=self.cursor.fetchone()
|
settings=self.cursor.fetchone()
|
||||||
if settings[0]!=118:
|
if settings[0]!=118:
|
||||||
print "outdated or too new database version - please recreate tables"
|
print "outdated or too new database version - please recreate tables"
|
||||||
self.wrongDbVersion=True
|
self.wrongDbVersion=True
|
||||||
except:# _mysql_exceptions.ProgrammingError:
|
except:# _mysql_exceptions.ProgrammingError:
|
||||||
print "failed to read settings table - please recreate tables"
|
print "failed to read settings table - please recreate tables"
|
||||||
self.wrongDbVersion=True
|
self.wrongDbVersion=True
|
||||||
#end def connect
|
#end def connect
|
||||||
|
|
||||||
def disconnect(self, due_to_error=False):
|
def disconnect(self, due_to_error=False):
|
||||||
"""Disconnects the DB"""
|
"""Disconnects the DB"""
|
||||||
if due_to_error:
|
if due_to_error:
|
||||||
self.db.rollback()
|
self.db.rollback()
|
||||||
else:
|
else:
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
self.cursor.close()
|
self.cursor.close()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
#end def disconnect
|
#end def disconnect
|
||||||
|
|
||||||
def reconnect(self, due_to_error=False):
|
def reconnect(self, due_to_error=False):
|
||||||
"""Reconnects the DB"""
|
"""Reconnects the DB"""
|
||||||
#print "started fpdb_db.reconnect"
|
#print "started fpdb_db.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 create_tables(self):
|
def create_tables(self):
|
||||||
#todo: should detect and fail gracefully if tables already exist.
|
#todo: should detect and fail gracefully if tables already exist.
|
||||||
self.cursor.execute(self.sql.query['createSettingsTable'])
|
self.cursor.execute(self.sql.query['createSettingsTable'])
|
||||||
self.cursor.execute(self.sql.query['createSitesTable'])
|
self.cursor.execute(self.sql.query['createSitesTable'])
|
||||||
self.cursor.execute(self.sql.query['createGametypesTable'])
|
self.cursor.execute(self.sql.query['createGametypesTable'])
|
||||||
self.cursor.execute(self.sql.query['createPlayersTable'])
|
self.cursor.execute(self.sql.query['createPlayersTable'])
|
||||||
self.cursor.execute(self.sql.query['createAutoratesTable'])
|
self.cursor.execute(self.sql.query['createAutoratesTable'])
|
||||||
self.cursor.execute(self.sql.query['createHandsTable'])
|
self.cursor.execute(self.sql.query['createHandsTable'])
|
||||||
self.cursor.execute(self.sql.query['createBoardCardsTable'])
|
self.cursor.execute(self.sql.query['createBoardCardsTable'])
|
||||||
self.cursor.execute(self.sql.query['createTourneyTypesTable'])
|
self.cursor.execute(self.sql.query['createTourneyTypesTable'])
|
||||||
self.cursor.execute(self.sql.query['createTourneysTable'])
|
self.cursor.execute(self.sql.query['createTourneysTable'])
|
||||||
self.cursor.execute(self.sql.query['createTourneysPlayersTable'])
|
self.cursor.execute(self.sql.query['createTourneysPlayersTable'])
|
||||||
self.cursor.execute(self.sql.query['createHandsPlayersTable'])
|
self.cursor.execute(self.sql.query['createHandsPlayersTable'])
|
||||||
self.cursor.execute(self.sql.query['createHandsActionsTable'])
|
self.cursor.execute(self.sql.query['createHandsActionsTable'])
|
||||||
self.cursor.execute(self.sql.query['createHudCacheTable'])
|
self.cursor.execute(self.sql.query['createHudCacheTable'])
|
||||||
self.cursor.execute(self.sql.query['addTourneyIndex'])
|
self.cursor.execute(self.sql.query['addTourneyIndex'])
|
||||||
self.cursor.execute(self.sql.query['addHandsIndex'])
|
self.cursor.execute(self.sql.query['addHandsIndex'])
|
||||||
self.cursor.execute(self.sql.query['addPlayersIndex'])
|
self.cursor.execute(self.sql.query['addPlayersIndex'])
|
||||||
self.fillDefaultData()
|
self.fillDefaultData()
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
#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"""
|
||||||
|
|
||||||
if(self.get_backend_name() == 'MySQL InnoDB'):
|
if(self.get_backend_name() == 'MySQL InnoDB'):
|
||||||
#Databases with FOREIGN KEY support need this switched of before you can drop tables
|
#Databases with FOREIGN KEY support need this switched of before you can drop tables
|
||||||
self.drop_referencial_integrity()
|
self.drop_referencial_integrity()
|
||||||
|
|
||||||
# Query the DB to see what tables exist
|
# Query the DB to see what tables exist
|
||||||
self.cursor.execute(self.sql.query['list_tables'])
|
self.cursor.execute(self.sql.query['list_tables'])
|
||||||
for table in self.cursor:
|
for table in self.cursor:
|
||||||
self.cursor.execute(self.sql.query['drop_table'] + table[0])
|
self.cursor.execute(self.sql.query['drop_table'] + table[0])
|
||||||
elif(self.get_backend_name() == 'PostgreSQL'):
|
elif(self.get_backend_name() == 'PostgreSQL'):
|
||||||
self.db.commit()# I have no idea why this makes the query work--REB 07OCT2008
|
self.db.commit()# I have no idea why this makes the query work--REB 07OCT2008
|
||||||
self.cursor.execute(self.sql.query['list_tables'])
|
self.cursor.execute(self.sql.query['list_tables'])
|
||||||
tables = self.cursor.fetchall()
|
tables = self.cursor.fetchall()
|
||||||
for table in tables:
|
for table in tables:
|
||||||
self.cursor.execute(self.sql.query['drop_table'] + table[0] + ' cascade')
|
self.cursor.execute(self.sql.query['drop_table'] + table[0] + ' cascade')
|
||||||
elif(self.get_backend_name() == 'SQLite'):
|
elif(self.get_backend_name() == 'SQLite'):
|
||||||
#todo: sqlite version here
|
#todo: sqlite version here
|
||||||
print "Empty function here"
|
print "Empty function here"
|
||||||
|
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
#end def drop_tables
|
#end def drop_tables
|
||||||
|
|
||||||
def drop_referencial_integrity(self):
|
def drop_referencial_integrity(self):
|
||||||
"""Update all tables to remove foreign keys"""
|
"""Update all tables to remove foreign keys"""
|
||||||
|
|
||||||
self.cursor.execute(self.sql.query['list_tables'])
|
self.cursor.execute(self.sql.query['list_tables'])
|
||||||
result = self.cursor.fetchall()
|
result = self.cursor.fetchall()
|
||||||
|
|
||||||
for i in range(len(result)):
|
for i in range(len(result)):
|
||||||
self.cursor.execute("SHOW CREATE TABLE " + result[i][0])
|
self.cursor.execute("SHOW CREATE TABLE " + result[i][0])
|
||||||
inner = self.cursor.fetchall()
|
inner = self.cursor.fetchall()
|
||||||
|
|
||||||
for j in range(len(inner)):
|
for j in range(len(inner)):
|
||||||
# result[i][0] - Table name
|
# result[i][0] - Table name
|
||||||
# result[i][1] - CREATE TABLE parameters
|
# result[i][1] - CREATE TABLE parameters
|
||||||
#Searching for CONSTRAINT `tablename_ibfk_1`
|
#Searching for CONSTRAINT `tablename_ibfk_1`
|
||||||
for m in re.finditer('(ibfk_[0-9]+)', inner[j][1]):
|
for m in re.finditer('(ibfk_[0-9]+)', inner[j][1]):
|
||||||
key = "`" + inner[j][0] + "_" + m.group() + "`"
|
key = "`" + inner[j][0] + "_" + m.group() + "`"
|
||||||
self.cursor.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key)
|
self.cursor.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
#end drop_referencial_inegrity
|
#end drop_referencial_inegrity
|
||||||
|
|
||||||
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:
|
||||||
return "MySQL InnoDB"
|
return "MySQL InnoDB"
|
||||||
elif self.backend==3:
|
elif self.backend==3:
|
||||||
return "PostgreSQL"
|
return "PostgreSQL"
|
||||||
else:
|
else:
|
||||||
raise fpdb_simple.FpdbError("invalid backend")
|
raise fpdb_simple.FpdbError("invalid backend")
|
||||||
#end def get_backend_name
|
#end def get_backend_name
|
||||||
|
|
||||||
def get_db_info(self):
|
def get_db_info(self):
|
||||||
return (self.host, self.database, self.user, self.password)
|
return (self.host, self.database, self.user, self.password)
|
||||||
#end def get_db_info
|
#end def get_db_info
|
||||||
|
|
||||||
def fillDefaultData(self):
|
def fillDefaultData(self):
|
||||||
self.cursor.execute("INSERT INTO Settings VALUES (118);")
|
self.cursor.execute("INSERT INTO Settings VALUES (118);")
|
||||||
self.cursor.execute("INSERT INTO Sites VALUES (DEFAULT, 'Full Tilt Poker', 'USD');")
|
self.cursor.execute("INSERT INTO Sites VALUES (DEFAULT, 'Full Tilt Poker', 'USD');")
|
||||||
self.cursor.execute("INSERT INTO Sites VALUES (DEFAULT, 'PokerStars', 'USD');")
|
self.cursor.execute("INSERT INTO Sites VALUES (DEFAULT, 'PokerStars', 'USD');")
|
||||||
self.cursor.execute("INSERT INTO TourneyTypes VALUES (DEFAULT, 1, 0, 0, 0, False);")
|
self.cursor.execute("INSERT INTO TourneyTypes VALUES (DEFAULT, 1, 0, 0, 0, False);")
|
||||||
#end def fillDefaultData
|
#end def fillDefaultData
|
||||||
|
|
||||||
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.create_tables()
|
self.create_tables()
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
print "Finished recreating tables"
|
print "Finished recreating tables"
|
||||||
#end def recreate_tables
|
#end def recreate_tables
|
||||||
#end class fpdb_db
|
#end class fpdb_db
|
||||||
|
|
Loading…
Reference in New Issue
Block a user