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