+

Manage DB disconnection

Benjamin Renard [2014-08-17 17:44:49]
Manage DB disconnection
Filename
mycoserver/db/__init__.py
diff --git a/mycoserver/db/__init__.py b/mycoserver/db/__init__.py
index 171fd68..a920235 100644
--- a/mycoserver/db/__init__.py
+++ b/mycoserver/db/__init__.py
@@ -25,84 +25,101 @@ class DB(object):
 			except Exception, e:
 				log.fatal('Error connecting to database : %s' % e)
 				return
+		else:
+			try:
+				self.con.ping(True)
+			except Exception, e:
+				self.con  = 0
+				return self.connect()
+			return True
+

 	def do_sql(self,sql):
-		try:
-			c=self.con.cursor()
-			c.execute(sql)
-			self.con.commit()
-			return c
-		except Exception,e:
-			log.error('Error executing request %s : %s' % (sql,e))
-			return False
+		if self.connect():
+			try:
+				c=self.con.cursor()
+				c.execute(sql)
+				self.con.commit()
+				return c
+			except Exception,e:
+				log.error('Error executing request %s : %s' % (sql,e))
+				return False

 	def select(self,sql):
-		ret=self.do_sql(sql)
-		if ret!=False:
-			return ret.fetchall()
-		return ret
+		if self.connect():
+			ret=self.do_sql(sql)
+			if ret!=False:
+				return ret.fetchall()
+			return ret

 	def login(self,email,password):
-		ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password))
-		log.debug(ret)
-		if ret:
-			if len(ret)==1:
-				return {
-					'email': ret[0][0],
-					'name': ret[0][1]
-				}
-			elif len(ret)>=1:
-				log.warning('Duplicate user %s in database' % email)
-		elif ret==():
-			return { 'loginerror': 'Utilisateur inconnu' }
-		return { 'loginerror': 'Erreur inconnu' }
+		if self.connect():
+			ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password))
+			log.debug(ret)
+			if ret:
+				if len(ret)==1:
+					return {
+						'email': ret[0][0],
+						'name': ret[0][1]
+					}
+				elif len(ret)>=1:
+					log.warning('Duplicate user %s in database' % email)
+			elif ret==():
+				return { 'loginerror': 'Utilisateur inconnu' }
+			return { 'loginerror': 'Erreur inconnu' }

 	def subscribe(self,email,name,password):
-		ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
-		log.debug(ret)
-		if ret[0][0]!=0:
-			return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'}
-		else:
-			if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)):
-				return {
-					'email': email,
-					'name': name,
-					'password': password
-				}
-		return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}
+		if self.connect():
+			ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
+			log.debug(ret)
+			if ret[0][0]!=0:
+				return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'}
+			else:
+				if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)):
+					return {
+						'email': email,
+						'name': name,
+						'password': password
+					}
+			return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}

 	def sync_group(self,email,groups):
-		db_groups=self.get_group(email)
-		if db_groups!=False:
-			db_grouplist=group.GroupList()
-			db_grouplist.load(db_groups)
-			grouplist=group.GroupList()
-			grouplist.load(groups)
-			synced_grouplist=db_grouplist.sync(grouplist)
-			if self.set_group(email,synced_grouplist.export()):
-				log.debug('Groups successfuly synchronized, return result')
-				return {'groups': synced_grouplist.export()}
-			else:
-				return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'}
-		return {'syncerror': 'Erreur inconnu'}
+		if self.connect():
+			db_groups=self.get_group(email)
+			if db_groups!=False:
+				db_grouplist=group.GroupList()
+				db_grouplist.load(db_groups)
+				grouplist=group.GroupList()
+				grouplist.load(groups)
+				synced_grouplist=db_grouplist.sync(grouplist)
+				if self.set_group(email,synced_grouplist.export()):
+					log.debug('Groups successfuly synchronized, return result')
+					return {'groups': synced_grouplist.export()}
+				else:
+					return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'}
+			return {'syncerror': 'Erreur inconnu'}
+		else:
+			return {'syncerror': u"Erreur de connexion à la base de données"}

 	def get_group(self,email):
-		ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
-		if ret!=False:
-			if len(ret)==1:
-				return json.loads(ret[0][0])
+		if self.connect():
+			ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
+			if ret!=False:
+				if len(ret)==1:
+					return json.loads(ret[0][0])
+				else:
+					return {'groups': {}}
 			else:
-				return {'groups': {}}
-		else:
-			return False
+				return False

 	def set_group(self,email,groups):
-		ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
-		if ret!=False:
-			json_groups=json.dumps(groups)
-			if len(ret)==1:
-				return self.do_sql("UPDATE groups SET groups='%s' WHERE email='%s'" % (json_groups,email))
+		if self.connect():
+			ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
+			if ret!=False:
+				json_groups=json.dumps(groups)
+				if len(ret)==1:
+					return self.do_sql("UPDATE groups SET groups='%s' WHERE email='%s'" % (json_groups,email))
+				else:
+					return self.do_sql("INSERT INTO groups (email,groups) VALUES ('%s','%s')" % (email,json_groups))
 			else:
-				return self.do_sql("INSERT INTO groups (email,groups) VALUES ('%s','%s')" % (email,json_groups))
-		else:
-			return False
+				return False
ViewGit