prevent crash if not connected to db

This commit is contained in:
Michael Becker 2024-03-22 13:55:29 -04:00
parent 072150f0e6
commit 3b26d9b801
3 changed files with 12 additions and 3 deletions

View File

@ -191,8 +191,6 @@ class MochaShell (REPLApplication):
return prompt
def process_input(self, value):
print(value)
parms = value.split(' ')
if len(parms) > 0:
@ -367,9 +365,13 @@ class MochaShell (REPLApplication):
ik_User = self.oms.get_instance_by_global_identifier(KnownClassGuids.User)
ik_UserName = self.oms.get_instance_by_global_identifier(KnownAttributeGuids.UserName)
users = self.oms.get_instances(ik_User)
if users is None:
self.print_error_not_open()
return
print ("user name ")
print ("---------------------")
users = self.oms.get_instances(ik_User)
for user in users:
user_name = self.oms.get_attribute_value(user, ik_UserName)
print(user_name)

View File

@ -8,6 +8,7 @@ class Oms:
pass
def get_user_by_username(self, username : str):
ik_User = self.get_instance_by_global_identifier(KnownClassGuids.User)
if ik_User is None:
print ("error: class `User` is not defined")

View File

@ -214,6 +214,9 @@ class SQLiteDatabaseOms (DatabaseOms):
def get_instances(self, of_class : InstanceReference = None):
if self.conn is None:
return None
if of_class is not None:
@ -244,6 +247,9 @@ class SQLiteDatabaseOms (DatabaseOms):
return InstanceReference(int(result[0]), InstanceKey(int(result[1]), int(result[2])), Guid.parse(result[3]))
def get_instance_by_global_identifier(self, global_identifier : Guid):
if self.conn is None:
return None
cursor = self.conn.execute("SELECT * FROM instances WHERE global_identifier = ?", [ global_identifier.strip() ])
results = cursor.fetchall()
if len(results) > 0: