check if a table exists before making it instead of catching the sqlite
exception
This commit is contained in:
parent
33b48f5ea3
commit
8386e45343
1 changed files with 23 additions and 29 deletions
42
Database.py
42
Database.py
|
@ -265,65 +265,59 @@ class Database(object):
|
|||
def execute(self, query, params=[]):
|
||||
return self._execute_fetch(query, lambda cursor: None, params)
|
||||
|
||||
def has_table(self, table_name):
|
||||
result = self.execute_fetchone("""SELECT COUNT(*) FROM
|
||||
sqlite_master WHERE type='table' AND name=?""",
|
||||
[table_name])
|
||||
return result[0] == 1
|
||||
|
||||
def make_servers_table(self):
|
||||
try:
|
||||
if not self.has_table("servers"):
|
||||
self.execute("""CREATE TABLE servers
|
||||
(server_id INTEGER PRIMARY KEY,
|
||||
hostname TEXT, port INTEGER, password TEXT,
|
||||
ipv4 BOOLEAN, tls BOOLEAN, nickname TEXT,
|
||||
username TEXT, realname TEXT)""")
|
||||
except sqlite3.Error as e:
|
||||
pass
|
||||
def make_bot_settings_table(self):
|
||||
try:
|
||||
if not self.has_table("bot_settings"):
|
||||
self.execute("""CREATE TABLE bot_settings
|
||||
(setting TEXT PRIMARY KEY, value TEXT)""")
|
||||
except sqlite3.Error as e:
|
||||
return
|
||||
self.execute("""CREATE INDEX bot_settings_index ON
|
||||
bot_settings (setting)""")
|
||||
self.execute("""CREATE INDEX bot_settings_index
|
||||
ON bot_settings (setting)""")
|
||||
def make_server_settings_table(self):
|
||||
try:
|
||||
if not self.has_table("server_settings"):
|
||||
self.execute("""CREATE TABLE server_settings
|
||||
(server_id INTEGER, setting TEXT, value TEXT,
|
||||
FOREIGN KEY(server_id) REFERENCES
|
||||
servers(server_id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (server_id, setting))""")
|
||||
except sqlite3.Error as e:
|
||||
return
|
||||
self.execute("""CREATE INDEX server_settings_index ON
|
||||
server_settings (server_id, setting)""")
|
||||
self.execute("""CREATE INDEX server_settings_index
|
||||
ON server_settings (server_id, setting)""")
|
||||
def make_channel_settings_table(self):
|
||||
try:
|
||||
if not self.has_table("channel_settings"):
|
||||
self.execute("""CREATE TABLE channel_settings
|
||||
(server_id INTEGER, channel TEXT, setting TEXT,
|
||||
value TEXT, FOREIGN KEY (server_id) REFERENCES
|
||||
servers(server_id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (server_id, channel, setting))""")
|
||||
except sqlite3.Error as e:
|
||||
return
|
||||
self.execute("""CREATE INDEX channel_settings_index ON
|
||||
channel_settings (server_id, channel, setting)""")
|
||||
self.execute("""CREATE INDEX channel_settings_index
|
||||
ON channel_settings (server_id, channel, setting)""")
|
||||
def make_user_settings_table(self):
|
||||
try:
|
||||
if not self.has_table("user_settings"):
|
||||
self.execute("""CREATE TABLE user_settings
|
||||
(server_id INTEGER, nickname TEXT, setting TEXT,
|
||||
value TEXT, FOREIGN KEY (server_id) REFERENCES
|
||||
servers(server_id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (server_id, nickname, setting))""")
|
||||
except sqlite3.Error as e:
|
||||
return
|
||||
self.execute("""CREATE INDEX user_settings_index ON
|
||||
user_settings (server_id, nickname, setting)""")
|
||||
def make_user_channel_settings_table(self):
|
||||
try:
|
||||
if not self.has_table("user_channel_settings"):
|
||||
self.execute("""CREATE TABLE user_channel_settings
|
||||
(server_id INTEGER, channel TEXT, nickname TEXT,
|
||||
setting TEXT, value TEXT, FOREIGN KEY (server_id)
|
||||
REFERENCES servers(server_id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (server_id, nickname, channel, setting))""")
|
||||
except sqlite3.Error as e:
|
||||
return
|
||||
self.execute("""CREATE INDEX user_channel_settings_index
|
||||
ON user_channel_settings (server_id, nickname, channel,
|
||||
setting)""")
|
||||
|
|
Loading…
Reference in a new issue