2018-10-30 14:58:48 +00:00
|
|
|
import json, os, sqlite3, threading, time, typing
|
|
|
|
from src import Logging
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-11-27 11:38:13 +00:00
|
|
|
sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))
|
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class Table(object):
|
|
|
|
def __init__(self, database):
|
|
|
|
self.database = database
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class Servers(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def add(self, alias: str, hostname: str, port: int, password: str,
|
|
|
|
ipv4: bool, tls: bool, bindhost: str,
|
|
|
|
nickname: str, username: str=None, realname: str=None):
|
2018-08-05 11:52:29 +00:00
|
|
|
username = username or nickname
|
|
|
|
realname = realname or nickname
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-11-05 18:23:02 +00:00
|
|
|
"""INSERT INTO servers (alias, hostname, port, password, tls,
|
|
|
|
ipv4, bindhost, nickname, username, realname) VALUES (
|
2018-10-03 15:00:25 +00:00
|
|
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
2018-11-05 18:23:02 +00:00
|
|
|
[alias, hostname, port, password, tls, ipv4, bindhost, nickname,
|
2018-10-03 15:00:25 +00:00
|
|
|
username, realname])
|
2018-11-13 11:13:04 +00:00
|
|
|
return self.database.execute_fetchone(
|
|
|
|
"SELECT server_id FROM servers ORDER BY server_id DESC LIMIT 1")[0]
|
2018-08-05 21:41:38 +00:00
|
|
|
def get_all(self):
|
|
|
|
return self.database.execute_fetchall(
|
2018-09-21 10:34:38 +00:00
|
|
|
"SELECT server_id, alias FROM servers")
|
2018-11-05 18:23:02 +00:00
|
|
|
def get(self, id: int) -> typing.Tuple[int, typing.Optional[str], str,
|
|
|
|
int, typing.Optional[str], bool, bool, typing.Optional[str], str,
|
|
|
|
typing.Optional[str], typing.Optional[str]]:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.execute_fetchone(
|
2018-11-05 18:23:02 +00:00
|
|
|
"""SELECT server_id, alias, hostname, port, password, tls,
|
|
|
|
ipv4, bindhost, nickname, username, realname FROM servers WHERE
|
2018-08-05 11:52:29 +00:00
|
|
|
server_id=?""",
|
|
|
|
[id])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-18 23:19:53 +00:00
|
|
|
class Channels(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def add(self, server_id: int, name: str):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.database.execute("""INSERT OR IGNORE INTO channels
|
|
|
|
(server_id, name) VALUES (?, ?)""",
|
|
|
|
[server_id, name.lower()])
|
2018-11-13 11:19:43 +00:00
|
|
|
return self.database.execute_fetchone(
|
2019-02-10 00:04:00 +00:00
|
|
|
"SELECT channel_id FROM channels ORDER BY channel_id DESC LIMIT 1")[0]
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, channel_id: int):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.database.execute("DELETE FROM channels WHERE channel_id=?",
|
|
|
|
[channel_id])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_id(self, server_id: int, name: str):
|
2018-08-18 23:19:53 +00:00
|
|
|
value = self.database.execute_fetchone("""SELECT channel_id FROM
|
|
|
|
channels WHERE server_id=? AND name=?""",
|
|
|
|
[server_id, name.lower()])
|
|
|
|
return value if value == None else value[0]
|
2018-11-05 21:39:00 +00:00
|
|
|
def rename(self, channel_id: int, new_name: str):
|
2018-11-05 16:46:24 +00:00
|
|
|
self.database.execute("UPDATE channels SET name=? where channel_id=?",
|
|
|
|
[new_name.lower(), channel_id])
|
2018-08-18 23:19:53 +00:00
|
|
|
|
|
|
|
class Users(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def add(self, server_id: int, nickname: str):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.database.execute("""INSERT OR IGNORE INTO users
|
|
|
|
(server_id, nickname) VALUES (?, ?)""",
|
2019-02-06 11:25:20 +00:00
|
|
|
[server_id, nickname])
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, user_id: int):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.database.execute("DELETE FROM users WHERE user_id=?",
|
|
|
|
[user_id])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_id(self, server_id: int, nickname: str):
|
2018-08-18 23:19:53 +00:00
|
|
|
value = self.database.execute_fetchone("""SELECT user_id FROM
|
|
|
|
users WHERE server_id=? and nickname=?""",
|
2019-02-06 11:25:20 +00:00
|
|
|
[server_id, nickname])
|
2018-08-18 23:19:53 +00:00
|
|
|
return value if value == None else value[0]
|
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class BotSettings(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def set(self, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 10:30:43 +00:00
|
|
|
"INSERT OR REPLACE INTO bot_settings VALUES (?, ?)",
|
|
|
|
[setting.lower(), json.dumps(value)])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, setting: str, default: typing.Any=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
value = self.database.execute_fetchone(
|
2018-08-05 10:30:43 +00:00
|
|
|
"SELECT value FROM bot_settings WHERE setting=?",
|
|
|
|
[setting.lower()])
|
2016-04-14 15:44:27 +00:00
|
|
|
if value:
|
|
|
|
return json.loads(value[0])
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find(self, pattern: str, default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute_fetchall(
|
2018-08-05 10:30:43 +00:00
|
|
|
"SELECT setting, value FROM bot_settings WHERE setting LIKE ?",
|
|
|
|
[pattern.lower()])
|
2016-04-14 15:44:27 +00:00
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
|
|
|
values[i] = value[0], json.loads(value[1])
|
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_prefix(self, prefix: str, default: typing.Any=[]):
|
2018-09-28 15:52:17 +00:00
|
|
|
return self.find("%s%%" % prefix, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, setting: str):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 10:30:43 +00:00
|
|
|
"DELETE FROM bot_settings WHERE setting=?",
|
|
|
|
[setting.lower()])
|
2016-04-14 15:44:27 +00:00
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class ServerSettings(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def set(self, server_id: int, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 10:30:43 +00:00
|
|
|
"INSERT OR REPLACE INTO server_settings VALUES (?, ?, ?)",
|
|
|
|
[server_id, setting.lower(), json.dumps(value)])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, server_id: int, setting: str, default: typing.Any=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
value = self.database.execute_fetchone(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""SELECT value FROM server_settings WHERE
|
|
|
|
server_id=? AND setting=?""",
|
|
|
|
[server_id,setting.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
if value:
|
|
|
|
return json.loads(value[0])
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find(self, server_id: int, pattern: str, default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute_fetchall(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""SELECT setting, value FROM server_settings WHERE
|
|
|
|
server_id=? AND setting LIKE ?""",
|
|
|
|
[server_id, pattern.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
|
|
|
values[i] = value[0], json.loads(value[1])
|
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_prefix(self, server_id: int, prefix: str, default: typing.Any=[]):
|
2018-11-28 10:12:17 +00:00
|
|
|
return self.find(server_id, "%s%%" % prefix, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, server_id: int, setting: str):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 10:30:43 +00:00
|
|
|
"DELETE FROM server_settings WHERE server_id=? AND setting=?",
|
|
|
|
[server_id, setting.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class ChannelSettings(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def set(self, channel_id: int, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-18 23:19:53 +00:00
|
|
|
"INSERT OR REPLACE INTO channel_settings VALUES (?, ?, ?)",
|
2018-08-31 04:48:53 +00:00
|
|
|
[channel_id, setting.lower(), json.dumps(value)])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, channel_id: int, setting: str, default: typing.Any=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
value = self.database.execute_fetchone(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""SELECT value FROM channel_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
channel_id=? AND setting=?""", [channel_id, setting.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
if value:
|
|
|
|
return json.loads(value[0])
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find(self, channel_id: int, pattern: str, default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute_fetchall(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""SELECT setting, value FROM channel_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
channel_id=? setting LIKE '?'""", [channel_id, pattern.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
2018-08-03 12:43:45 +00:00
|
|
|
values[i] = value[0], json.loads(value[1])
|
2016-03-29 11:56:58 +00:00
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_prefix(self, channel_id: int, prefix: str, default: typing.Any=[]):
|
2018-11-28 10:12:17 +00:00
|
|
|
return self.find(channel_id, "%s%%" % prefix,
|
2018-08-03 12:43:45 +00:00
|
|
|
default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, channel_id: int, setting: str):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-18 23:19:53 +00:00
|
|
|
"""DELETE FROM channel_settings WHERE channel_id=?
|
|
|
|
AND setting=?""", [channel_id, setting.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_by_setting(self, setting: str, default: typing.Any=[]):
|
2018-10-06 08:54:21 +00:00
|
|
|
values = self.database.execute_fetchall(
|
|
|
|
"""SELECT channels.server_id, channels.name,
|
|
|
|
channel_settings.value FROM channel_settings
|
|
|
|
INNER JOIN channels ON
|
|
|
|
channel_settings.channel_id=channels.channel_id
|
|
|
|
WHERE channel_settings.setting=?""", [setting])
|
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
|
|
|
values[i] = value[0], value[1], json.loads(value[2])
|
|
|
|
return values
|
|
|
|
return default
|
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class UserSettings(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def set(self, user_id: int, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-18 23:19:53 +00:00
|
|
|
"INSERT OR REPLACE INTO user_settings VALUES (?, ?, ?)",
|
|
|
|
[user_id, setting.lower(), json.dumps(value)])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, user_id: int, setting: str, default: typing.Any=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
value = self.database.execute_fetchone(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""SELECT value FROM user_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id=? and setting=?""", [user_id, setting.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
if value:
|
|
|
|
return json.loads(value[0])
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_all_by_setting(self, server_id: int, setting: str,
|
|
|
|
default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute_fetchall(
|
2018-08-18 23:19:53 +00:00
|
|
|
"""SELECT users.nickname, user_settings.value FROM
|
|
|
|
user_settings INNER JOIN users ON
|
|
|
|
user_settings.user_id=users.user_id WHERE
|
|
|
|
users.server_id=? AND user_settings.setting=?""",
|
2018-08-03 12:43:45 +00:00
|
|
|
[server_id, setting])
|
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
2018-08-09 12:49:44 +00:00
|
|
|
values[i] = value[0], json.loads(value[1])
|
2018-08-03 12:43:45 +00:00
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find(self, user_id: int, pattern: str, default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""SELECT setting, value FROM user_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id=? AND setting LIKE '?'""", [user_id, pattern.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
2018-08-03 12:43:45 +00:00
|
|
|
values[i] = value[0], json.loads(value[1])
|
2016-03-29 11:56:58 +00:00
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_prefix(self, user_id: int, prefix: str, default: typing.Any=[]):
|
2018-11-28 10:12:17 +00:00
|
|
|
return self.find(user_id, "%s%%" % prefix, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, user_id: int, setting: str):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 10:30:43 +00:00
|
|
|
"""DELETE FROM user_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id=? AND setting=?""", [user_id, setting.lower()])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class UserChannelSettings(Table):
|
2018-10-30 14:58:48 +00:00
|
|
|
def set(self, user_id: int, channel_id: int, setting: str,
|
|
|
|
value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 11:52:29 +00:00
|
|
|
"""INSERT OR REPLACE INTO user_channel_settings VALUES
|
2018-08-18 23:19:53 +00:00
|
|
|
(?, ?, ?, ?)""",
|
|
|
|
[user_id, channel_id, setting.lower(), json.dumps(value)])
|
2018-10-30 14:58:48 +00:00
|
|
|
def get(self, user_id: int, channel_id: int, setting: str,
|
|
|
|
default: typing.Any=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
value = self.database.execute_fetchone(
|
2018-08-05 11:52:29 +00:00
|
|
|
"""SELECT value FROM user_channel_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id=? AND channel_id=? AND setting=?""",
|
|
|
|
[user_id, channel_id, setting.lower()])
|
2018-08-05 11:52:29 +00:00
|
|
|
if value:
|
|
|
|
return json.loads(value[0])
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find(self, user_id: int, channel_id: int, pattern: str,
|
|
|
|
default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute_fetchall(
|
2018-08-05 11:52:29 +00:00
|
|
|
"""SELECT setting, value FROM user_channel_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id=? AND channel_id=? AND setting LIKE '?'""",
|
|
|
|
[user_id, channel_id, pattern.lower()])
|
2018-08-05 11:52:29 +00:00
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
|
|
|
values[i] = value[0], json.loads(value[1])
|
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_prefix(self, user_id: int, channel_id: int, prefix: str,
|
|
|
|
default: typing.Any=[]):
|
2018-11-28 10:12:17 +00:00
|
|
|
return self.find(user_id, channel_id, "%s%%" % prefix,
|
2018-08-05 11:52:29 +00:00
|
|
|
default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_by_setting(self, user_id: int, setting: str,
|
|
|
|
default: typing.Any=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
values = self.database.execute_fetchall(
|
2018-08-18 23:19:53 +00:00
|
|
|
"""SELECT channels.name, user_channel_settings.value FROM
|
|
|
|
user_channel_settings INNER JOIN channels ON
|
|
|
|
user_channel_settings.channel_id=channels.channel_id
|
2018-08-31 14:13:56 +00:00
|
|
|
WHERE user_channel_settings.setting=?
|
|
|
|
AND user_channel_settings.user_id=?""", [setting, user_id])
|
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
|
|
|
values[i] = value[0], json.loads(value[1])
|
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_all_by_setting(self, server_id: int, setting: str,
|
|
|
|
default: typing.Any=[]):
|
2018-08-31 14:34:56 +00:00
|
|
|
values = self.database.execute_fetchall(
|
|
|
|
"""SELECT channels.name, users.nickname,
|
|
|
|
user_channel_settings.value FROM
|
|
|
|
user_channel_settings INNER JOIN channels ON
|
|
|
|
user_channel_settings.channel_id=channels.channel_id
|
2018-08-31 15:02:48 +00:00
|
|
|
INNER JOIN users on user_channel_settings.user_id=users.user_id
|
2018-08-31 14:34:56 +00:00
|
|
|
WHERE user_channel_settings.setting=? AND
|
|
|
|
users.server_id=?""", [setting, server_id])
|
|
|
|
if values:
|
|
|
|
for i, value in enumerate(values):
|
|
|
|
values[i] = value[0], value[1], json.loads(value[2])
|
|
|
|
return values
|
|
|
|
return default
|
2018-10-30 14:58:48 +00:00
|
|
|
def delete(self, user_id: int, channel_id: int, setting: str):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute(
|
2018-08-05 11:52:29 +00:00
|
|
|
"""DELETE FROM user_channel_settings WHERE
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id=? AND channel_id=? AND setting=?""",
|
|
|
|
[user_id, channel_id, setting.lower()])
|
2018-08-05 11:52:29 +00:00
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
class Database(object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, log: "Logging.Log", location: str):
|
2018-09-27 10:07:29 +00:00
|
|
|
self.log = log
|
|
|
|
self.location = location
|
|
|
|
self.database = sqlite3.connect(self.location,
|
2018-11-27 11:38:13 +00:00
|
|
|
check_same_thread=False, isolation_level=None,
|
|
|
|
detect_types=sqlite3.PARSE_DECLTYPES)
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.execute("PRAGMA foreign_keys = ON")
|
|
|
|
self._cursor = None
|
2018-11-11 12:40:24 +00:00
|
|
|
self._lock = threading.Lock()
|
2018-08-05 21:41:38 +00:00
|
|
|
|
|
|
|
self.make_servers_table()
|
2018-08-18 23:19:53 +00:00
|
|
|
self.make_channels_table()
|
|
|
|
self.make_users_table()
|
2018-08-05 21:41:38 +00:00
|
|
|
self.make_bot_settings_table()
|
|
|
|
self.make_server_settings_table()
|
|
|
|
self.make_channel_settings_table()
|
|
|
|
self.make_user_settings_table()
|
|
|
|
self.make_user_channel_settings_table()
|
|
|
|
|
|
|
|
self.servers = Servers(self)
|
2018-08-18 23:19:53 +00:00
|
|
|
self.channels = Channels(self)
|
|
|
|
self.users = Users(self)
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot_settings = BotSettings(self)
|
|
|
|
self.server_settings = ServerSettings(self)
|
|
|
|
self.channel_settings = ChannelSettings(self)
|
|
|
|
self.user_settings = UserSettings(self)
|
|
|
|
self.user_channel_settings = UserChannelSettings(self)
|
|
|
|
|
|
|
|
def cursor(self):
|
|
|
|
if self._cursor == None:
|
|
|
|
self._cursor = self.database.cursor()
|
|
|
|
return self._cursor
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def _execute_fetch(self, query: str,
|
|
|
|
fetch_func: typing.Callable[[sqlite3.Cursor], typing.Any],
|
|
|
|
params: typing.List=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
printable_query = " ".join(query.split())
|
2018-08-09 09:38:13 +00:00
|
|
|
start = time.monotonic()
|
2018-08-05 21:41:38 +00:00
|
|
|
|
|
|
|
cursor = self.cursor()
|
2018-11-11 12:40:24 +00:00
|
|
|
with self._lock:
|
|
|
|
cursor.execute(query, params)
|
2018-11-11 12:45:10 +00:00
|
|
|
value = fetch_func(cursor)
|
2018-08-05 21:41:38 +00:00
|
|
|
|
2018-08-09 09:38:13 +00:00
|
|
|
end = time.monotonic()
|
2018-08-05 21:41:38 +00:00
|
|
|
total_milliseconds = (end - start) * 1000
|
2018-11-14 23:04:54 +00:00
|
|
|
self.log.trace("executed query in %fms: \"%s\" (params: %s)",
|
2018-11-14 23:06:41 +00:00
|
|
|
[total_milliseconds, printable_query, params])
|
2018-08-28 11:23:57 +00:00
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
return value
|
2018-10-30 14:58:48 +00:00
|
|
|
def execute_fetchall(self, query: str, params: typing.List=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self._execute_fetch(query,
|
|
|
|
lambda cursor: cursor.fetchall(), params)
|
2018-10-30 14:58:48 +00:00
|
|
|
def execute_fetchone(self, query: str, params: typing.List=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self._execute_fetch(query,
|
|
|
|
lambda cursor: cursor.fetchone(), params)
|
2018-10-30 14:58:48 +00:00
|
|
|
def execute(self, query: str, params: typing.List=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self._execute_fetch(query, lambda cursor: None, params)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def has_table(self, table_name: str):
|
2018-08-18 20:53:50 +00:00
|
|
|
result = self.execute_fetchone("""SELECT COUNT(*) FROM
|
|
|
|
sqlite_master WHERE type='table' AND name=?""",
|
|
|
|
[table_name])
|
|
|
|
return result[0] == 1
|
|
|
|
|
2018-08-05 21:41:38 +00:00
|
|
|
def make_servers_table(self):
|
2018-08-18 20:53:50 +00:00
|
|
|
if not self.has_table("servers"):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.execute("""CREATE TABLE servers
|
2018-09-19 00:13:54 +00:00
|
|
|
(server_id INTEGER PRIMARY KEY, alias TEXT, hostname TEXT,
|
2018-10-01 13:06:50 +00:00
|
|
|
port INTEGER, password TEXT, ipv4 BOOLEAN, tls BOOLEAN,
|
|
|
|
bindhost TEXT, nickname TEXT, username TEXT, realname TEXT)""")
|
2018-08-18 23:19:53 +00:00
|
|
|
def make_channels_table(self):
|
|
|
|
if not self.has_table("channels"):
|
|
|
|
self.execute("""CREATE TABLE channels
|
|
|
|
(channel_id INTEGER PRIMARY KEY, server_id INTEGER,
|
|
|
|
name TEXT, FOREIGN KEY (server_id) REFERENCES
|
|
|
|
servers (server_id) ON DELETE CASCADE,
|
|
|
|
UNIQUE (server_id, name))""")
|
|
|
|
self.execute("""CREATE INDEX channels_index
|
|
|
|
on channels (server_id, name)""")
|
|
|
|
def make_users_table(self):
|
|
|
|
if not self.has_table("users"):
|
|
|
|
self.execute("""CREATE TABLE users
|
|
|
|
(user_id INTEGER PRIMARY KEY, server_id INTEGER,
|
|
|
|
nickname TEXT, FOREIGN KEY (server_id) REFERENCES
|
|
|
|
servers (server_id) ON DELETE CASCADE,
|
|
|
|
UNIQUE (server_id, nickname))""")
|
|
|
|
self.execute("""CREATE INDEX users_index
|
|
|
|
on users (server_id, nickname)""")
|
2018-08-05 21:41:38 +00:00
|
|
|
def make_bot_settings_table(self):
|
2018-08-18 20:53:50 +00:00
|
|
|
if not self.has_table("bot_settings"):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.execute("""CREATE TABLE bot_settings
|
|
|
|
(setting TEXT PRIMARY KEY, value TEXT)""")
|
2018-08-18 20:53:50 +00:00
|
|
|
self.execute("""CREATE INDEX bot_settings_index
|
|
|
|
ON bot_settings (setting)""")
|
2018-08-05 21:41:38 +00:00
|
|
|
def make_server_settings_table(self):
|
2018-08-18 20:53:50 +00:00
|
|
|
if not self.has_table("server_settings"):
|
2018-08-05 21:41:38 +00:00
|
|
|
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))""")
|
2018-08-18 20:53:50 +00:00
|
|
|
self.execute("""CREATE INDEX server_settings_index
|
|
|
|
ON server_settings (server_id, setting)""")
|
2018-08-05 21:41:38 +00:00
|
|
|
def make_channel_settings_table(self):
|
2018-08-18 20:53:50 +00:00
|
|
|
if not self.has_table("channel_settings"):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.execute("""CREATE TABLE channel_settings
|
2018-08-18 23:19:53 +00:00
|
|
|
(channel_id INTEGER, setting TEXT, value TEXT,
|
|
|
|
FOREIGN KEY (channel_id) REFERENCES channels(channel_id)
|
|
|
|
ON DELETE CASCADE, PRIMARY KEY (channel_id, setting))""")
|
2018-08-18 20:53:50 +00:00
|
|
|
self.execute("""CREATE INDEX channel_settings_index
|
2018-08-18 23:19:53 +00:00
|
|
|
ON channel_settings (channel_id, setting)""")
|
2018-08-05 21:41:38 +00:00
|
|
|
def make_user_settings_table(self):
|
2018-08-18 20:53:50 +00:00
|
|
|
if not self.has_table("user_settings"):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.execute("""CREATE TABLE user_settings
|
2018-08-18 23:19:53 +00:00
|
|
|
(user_id INTEGER, setting TEXT, value TEXT,
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users(user_id)
|
|
|
|
ON DELETE CASCADE, PRIMARY KEY (user_id, setting))""")
|
2018-08-18 20:53:50 +00:00
|
|
|
self.execute("""CREATE INDEX user_settings_index ON
|
2018-08-18 23:19:53 +00:00
|
|
|
user_settings (user_id, setting)""")
|
2018-08-05 21:41:38 +00:00
|
|
|
def make_user_channel_settings_table(self):
|
2018-08-18 20:53:50 +00:00
|
|
|
if not self.has_table("user_channel_settings"):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.execute("""CREATE TABLE user_channel_settings
|
2018-08-18 23:19:53 +00:00
|
|
|
(user_id INTEGER, channel_id INTEGER, setting TEXT,
|
|
|
|
value TEXT, FOREIGN KEY (user_id) REFERENCES
|
|
|
|
users(user_id) ON DELETE CASCADE, FOREIGN KEY
|
|
|
|
(channel_id) REFERENCES channels(channel_id) ON
|
|
|
|
DELETE CASCADE, PRIMARY KEY (user_id, channel_id,
|
|
|
|
setting))""")
|
2018-08-18 20:53:50 +00:00
|
|
|
self.execute("""CREATE INDEX user_channel_settings_index
|
2018-08-31 14:13:56 +00:00
|
|
|
ON user_channel_settings (user_id, channel_id, setting)""")
|