StarsDB: add possibility for multiple instances of different databases
also fix some typos in the SQL scripts
This commit is contained in:
+18
-16
@@ -5,34 +5,36 @@ class StarDBError(Enum):
|
||||
NO_ERROR = 0
|
||||
DB_CONNECTION_FAILED = 1
|
||||
|
||||
DB_NAME = "stars.db"
|
||||
|
||||
class StarDB():
|
||||
__instanceVar = None
|
||||
__instances = list()
|
||||
__dbNames = list()
|
||||
|
||||
@staticmethod
|
||||
def getInstance():
|
||||
if(StarDB.__instanceVar == None):
|
||||
return StarDB()
|
||||
def getInstance(dbName: str):
|
||||
if(dbName in StarDB.__dbNames):
|
||||
return StarDB.__instances[StarDB.__dbNames.index(dbName)]
|
||||
else:
|
||||
return StarDB.__instanceVar
|
||||
instance = StarDB(dbName)
|
||||
StarDB.__instances.append(instance)
|
||||
StarDB.__dbNames.append(dbName)
|
||||
|
||||
def __init__(self):
|
||||
if(StarDB.__instanceVar != None):
|
||||
raise Exception("StarDB cannot be instantiated more than once!")
|
||||
def __init__(self, dbName: str):
|
||||
if(dbName in StarDB.__dbNames):
|
||||
raise Exception(f"StarDB {dbName} cannot be instantiated more than once!")
|
||||
else:
|
||||
StarDB.__instanceVar = self
|
||||
StarDB.__dbNames.append(dbName)
|
||||
StarDB.__instances.append(self)
|
||||
|
||||
self.error = StarDBError.NO_ERROR
|
||||
|
||||
try:
|
||||
con = sqlite3.connect(DB_NAME)
|
||||
self.dbCursor = con.cursor()
|
||||
self.connection = sqlite3.connect(dbName)
|
||||
self.dbCursor = self.connection.cursor()
|
||||
except:
|
||||
self.error = StarDBError.DB_CONNECTION_FAILED
|
||||
|
||||
if(self.error is not StarDBError.NO_ERROR):
|
||||
raise Exception("StarDB cannot connect to the database!")
|
||||
raise Exception(f"StarDB cannot connect to the database: {self.error}")
|
||||
else:
|
||||
self.initializeDB()
|
||||
|
||||
@@ -65,10 +67,10 @@ class StarDB():
|
||||
sequenceSourceNameID INTEGER NOT NULL,
|
||||
sequencesID INTEGER NOT NULL,
|
||||
filename TEXT,
|
||||
PRIMARY KEY (starID, seqienceSourceNameID, sequenceStart),
|
||||
PRIMARY KEY (starID, sequenceSourceNameID, sequencesID),
|
||||
FOREIGN KEY (starID) REFERENCES starnames(starID),
|
||||
FOREIGN KEY (sequenceSourceNameID) REFERENCES sequenceSourceNames(sequenceSourceNameID)
|
||||
FOREIGN KEY (sequencesID) REFERENCES sequences(sequencesID)
|
||||
);""")
|
||||
|
||||
self.dbCursor.commit()
|
||||
self.connection.commit()
|
||||
Reference in New Issue
Block a user