MySQL fixed session issues
parent
52849d7a67
commit
b715d9e0b9
|
@ -220,8 +220,6 @@ class DiscordDatabaseInterface(DatabaseInterface):
|
||||||
self.database.add_object(player)
|
self.database.add_object(player)
|
||||||
finally:
|
finally:
|
||||||
player.name = player_name
|
player.name = player_name
|
||||||
|
|
||||||
self.database.session.commit()
|
|
||||||
return player
|
return player
|
||||||
|
|
||||||
def find_location_by_owner_uuid(self, owner_uuid):
|
def find_location_by_owner_uuid(self, owner_uuid):
|
||||||
|
@ -248,58 +246,58 @@ class DiscordDatabaseInterface(DatabaseInterface):
|
||||||
class GeoffreyDatabase:
|
class GeoffreyDatabase:
|
||||||
|
|
||||||
def __init__(self, engine_arg):
|
def __init__(self, engine_arg):
|
||||||
self.engine = create_engine(engine_arg, echo=True, pool_recycle=3600)
|
self.engine = create_engine(engine_arg, echo=True, pool_recycle=3600, pool_pre_ping=True)
|
||||||
self.Session = scoped_session(sessionmaker(bind=self.engine))
|
Session = sessionmaker(bind=self.engine)
|
||||||
self.Session()
|
self.session = Session()
|
||||||
|
self.meta = MetaData()
|
||||||
SQL_Base.metadata.create_all(self.engine)
|
SQL_Base.metadata.create_all(self.engine)
|
||||||
|
|
||||||
|
def clear_all(self):
|
||||||
|
self.session.query(Tunnel).delete()
|
||||||
|
self.session.query(ItemListing).delete()
|
||||||
|
self.session.query(Shop).delete()
|
||||||
|
self.session.query(Location).delete()
|
||||||
|
self.session.query(Player).delete()
|
||||||
|
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
def add_object(self, obj):
|
def add_object(self, obj):
|
||||||
try:
|
try:
|
||||||
session = self.Session()
|
ret = not self.session.query(exists().where(type(obj).id == obj.id))
|
||||||
ret = not session.query(exists().where(type(obj).id == obj.id))
|
|
||||||
if not ret:
|
if not ret:
|
||||||
session.add(obj)
|
self.session.add(obj)
|
||||||
session.commit()
|
self.session.commit()
|
||||||
session.close()
|
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
session.rollback()
|
self.session.rollback()
|
||||||
raise EntryNameNotUniqueError
|
raise EntryNameNotUniqueError
|
||||||
except DataError:
|
except DataError:
|
||||||
session.rollback()
|
self.session.rollback()
|
||||||
raise StringTooLong
|
raise StringTooLong
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def query_by_filter(self, obj_type, * args):
|
def query_by_filter(self, obj_type, * args):
|
||||||
session = self.Session()
|
|
||||||
filter_value = self.combine_filter(args)
|
filter_value = self.combine_filter(args)
|
||||||
return session.query(obj_type).filter(filter_value).all()
|
return self.session.query(obj_type).filter(filter_value).all()
|
||||||
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
def delete_entry(self, obj_type, * args):
|
def delete_entry(self, obj_type, * args):
|
||||||
session = self.Session()
|
|
||||||
filter_value = self.combine_filter(args)
|
filter_value = self.combine_filter(args)
|
||||||
entry = session.query(obj_type).filter(filter_value)
|
entry = self.session.query(obj_type).filter(filter_value)
|
||||||
|
|
||||||
if entry.first() is not None:
|
if entry.first() is not None:
|
||||||
entry.delete()
|
entry.delete()
|
||||||
session.commit()
|
self.session.commit()
|
||||||
else:
|
else:
|
||||||
raise DeleteEntryError
|
raise DeleteEntryError
|
||||||
|
|
||||||
session.close()
|
self.session.close()
|
||||||
|
|
||||||
def print_database(self, obj_type):
|
def print_database(self, obj_type):
|
||||||
session = self.Session()
|
obj_list = self.session.query(obj_type).all()
|
||||||
obj_list = session.query(obj_type).all()
|
|
||||||
|
|
||||||
s = ''
|
s = ''
|
||||||
|
|
||||||
for obj in obj_list:
|
for obj in obj_list:
|
||||||
s = s + '\n' + obj.id
|
s = s + '\n' + obj.id
|
||||||
|
|
||||||
session.close()
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def combine_filter(self, filter_value):
|
def combine_filter(self, filter_value):
|
||||||
|
|
|
@ -16,13 +16,7 @@ class TestGeoffreyDatabase(TestCase):
|
||||||
self.tunnel = Tunnel(self.owner, 'Green', 105, self.loc)
|
self.tunnel = Tunnel(self.owner, 'Green', 105, self.loc)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.interface.database.session.query(Tunnel).delete()
|
self.interface.database.clear_all()
|
||||||
self.interface.database.session.query(ItemListing).delete()
|
|
||||||
self.interface.database.session.query(Shop).delete()
|
|
||||||
self.interface.database.session.query(Location).delete()
|
|
||||||
self.interface.database.session.query(Player).delete()
|
|
||||||
|
|
||||||
self.interface.database.session.commit()
|
|
||||||
|
|
||||||
def test_add_object(self):
|
def test_add_object(self):
|
||||||
self.interface.database.add_object(self.loc)
|
self.interface.database.add_object(self.loc)
|
||||||
|
|
Loading…
Reference in New Issue