diff --git a/astrodatagui/db/StarsDB.py b/astrodatagui/db/StarsDB.py index 4241dce..c684f8d 100644 --- a/astrodatagui/db/StarsDB.py +++ b/astrodatagui/db/StarsDB.py @@ -4,35 +4,37 @@ import sqlite3 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() \ No newline at end of file + self.connection.commit() \ No newline at end of file