mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Cleaned up makeDb.
This commit is contained in:
parent
30d3747f1d
commit
44972da513
@ -55,36 +55,7 @@ import callbacks
|
|||||||
|
|
||||||
dbFilename = os.path.join(conf.dataDir, 'FunDB.db')
|
dbFilename = os.path.join(conf.dataDir, 'FunDB.db')
|
||||||
|
|
||||||
def makeDb(dbfilename, replace=False):
|
tableCreateStatements = {
|
||||||
if os.path.exists(dbfilename):
|
|
||||||
if replace:
|
|
||||||
os.remove(dbfilename)
|
|
||||||
db = sqlite.connect(dbfilename)
|
|
||||||
createTables(db, [x for x in tableDict.keys() if not tableExists(db, x)])
|
|
||||||
return db
|
|
||||||
|
|
||||||
def addWord(db, word, commit=False):
|
|
||||||
word = word.strip().lower()
|
|
||||||
L = list(word)
|
|
||||||
L.sort()
|
|
||||||
sorted = ''.join(L)
|
|
||||||
cursor = db.cursor()
|
|
||||||
cursor.execute("""INSERT INTO sorted_words VALUES (NULL, %s)""", sorted)
|
|
||||||
cursor.execute("""INSERT INTO words VALUES (NULL, %s,
|
|
||||||
(SELECT id FROM sorted_words
|
|
||||||
WHERE word=%s))""", word, sorted)
|
|
||||||
if commit:
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
def tableExists(db, table):
|
|
||||||
cursor = db.cursor()
|
|
||||||
try:
|
|
||||||
cursor.execute("""SELECT * from %s LIMIT 1""" % table)
|
|
||||||
return True
|
|
||||||
except sqlite.DatabaseError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
tableDict = {
|
|
||||||
'larts': ("""CREATE TABLE larts (
|
'larts': ("""CREATE TABLE larts (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
lart TEXT, added_by TEXT,
|
lart TEXT, added_by TEXT,
|
||||||
@ -114,31 +85,46 @@ tableDict = {
|
|||||||
word TEXT UNIQUE ON CONFLICT IGNORE,
|
word TEXT UNIQUE ON CONFLICT IGNORE,
|
||||||
sorted_word_id INTEGER
|
sorted_word_id INTEGER
|
||||||
)""",
|
)""",
|
||||||
"""CREATE INDEX sorted_word_id ON words (sorted_word_id)"""),
|
"""CREATE INDEX sorted_word_id ON words (sorted_word_id)""",
|
||||||
'sorted_words': ("""CREATE TABLE sorted_words (
|
"""CREATE TABLE sorted_words (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
word TEXT UNIQUE ON CONFLICT IGNORE
|
word TEXT UNIQUE ON CONFLICT IGNORE
|
||||||
)""",
|
)""",
|
||||||
"""CREATE INDEX sorted_words_word
|
"""CREATE INDEX sorted_words_word ON sorted_words (word)"""),
|
||||||
ON sorted_words (word)"""),
|
|
||||||
'uptime': ("""CREATE TABLE uptime (
|
'uptime': ("""CREATE TABLE uptime (
|
||||||
started INTEGER UNIQUE ON CONFLICT IGNORE,
|
started INTEGER UNIQUE ON CONFLICT IGNORE,
|
||||||
ended INTEGER
|
ended INTEGER
|
||||||
)""",)
|
)""",)
|
||||||
}
|
}
|
||||||
|
|
||||||
def createTables(db, tables=tableDict.keys()):
|
def makeDb(dbfilename, replace=False):
|
||||||
if len(tables) < 1:
|
if os.path.exists(dbfilename):
|
||||||
return db
|
if replace:
|
||||||
else:
|
os.remove(dbfilename)
|
||||||
cursor = db.cursor()
|
db = sqlite.connect(dbfilename)
|
||||||
tableList = [table for table in tableDict.keys() if table in
|
cursor = db.cursor()
|
||||||
tables]
|
for table in tableCreateStatements:
|
||||||
for table in tableList:
|
try:
|
||||||
for command in tableDict[table]:
|
cursor.execute("""SELECT * FROM %s LIMIT 1""" % table)
|
||||||
cursor.execute(command)
|
except sqlite.DatabaseError: # The table doesn't exist.
|
||||||
|
for sql in tableCreateStatements[table]:
|
||||||
|
cursor.execute(sql)
|
||||||
|
db.commit()
|
||||||
|
return db
|
||||||
|
|
||||||
|
def addWord(db, word, commit=False):
|
||||||
|
word = word.strip().lower()
|
||||||
|
L = list(word)
|
||||||
|
L.sort()
|
||||||
|
sorted = ''.join(L)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""INSERT INTO sorted_words VALUES (NULL, %s)""", sorted)
|
||||||
|
cursor.execute("""INSERT INTO words VALUES (NULL, %s,
|
||||||
|
(SELECT id FROM sorted_words
|
||||||
|
WHERE word=%s))""", word, sorted)
|
||||||
|
if commit:
|
||||||
db.commit()
|
db.commit()
|
||||||
return db
|
|
||||||
|
|
||||||
class FunDB(callbacks.Privmsg):
|
class FunDB(callbacks.Privmsg):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user