2018-02-04 07:13:09 +03:00
|
|
|
import datetime as dt
|
|
|
|
|
2018-08-03 15:15:35 +03:00
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
class DataBase:
|
|
|
|
def __init__(self, basefile, scheme):
|
|
|
|
import sqlite3
|
2018-02-26 19:13:04 +01:00
|
|
|
self.scheme = ''
|
2018-02-04 07:13:09 +03:00
|
|
|
try:
|
|
|
|
self.conn = sqlite3.connect(basefile, check_same_thread=False)
|
|
|
|
except:
|
|
|
|
print('Could not connect to DataBase.')
|
|
|
|
return None
|
|
|
|
with open(scheme, 'r') as scheme_sql:
|
|
|
|
sql = scheme_sql.read()
|
2018-02-26 19:13:04 +01:00
|
|
|
self.scheme = sql
|
2018-02-04 07:13:09 +03:00
|
|
|
if self.conn is not None:
|
|
|
|
try:
|
|
|
|
cursor = self.conn.cursor()
|
|
|
|
cursor.executescript(sql)
|
|
|
|
except:
|
|
|
|
print('Could not create scheme.')
|
|
|
|
else:
|
|
|
|
print("Error! cannot create the database connection.")
|
|
|
|
print('DB created.')
|
|
|
|
|
|
|
|
def execute(self, sql):
|
|
|
|
cursor = self.conn.cursor()
|
|
|
|
cursor.execute(sql)
|
|
|
|
self.conn.commit()
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
def save_word(self, word):
|
|
|
|
sql = "INSERT OR IGNORE INTO word('word') VALUES ('%s')" % word
|
|
|
|
self.execute(sql)
|
|
|
|
sql = "SELECT id FROM word WHERE word = '%s'" % word
|
|
|
|
return(self.execute(sql)[0][0])
|
|
|
|
|
|
|
|
def add_user(self,
|
2018-08-03 15:15:35 +03:00
|
|
|
username,
|
|
|
|
user_id,
|
|
|
|
first_name,
|
|
|
|
last_name):
|
2018-02-04 07:13:09 +03:00
|
|
|
date = int(dt.datetime.now().strftime("%s"))
|
|
|
|
sql = """INSERT OR IGNORE INTO
|
|
|
|
user('id', 'username', 'first_name', 'last_name', 'date')
|
|
|
|
VALUES ('%s','%s','%s','%s','%s')""" % (
|
|
|
|
user_id,
|
|
|
|
username,
|
|
|
|
first_name,
|
|
|
|
last_name,
|
|
|
|
date
|
|
|
|
)
|
|
|
|
self.execute(sql)
|
|
|
|
|
|
|
|
def add_conf(self, id, title):
|
|
|
|
date = int(dt.datetime.now().strftime("%s"))
|
|
|
|
sql = """INSERT OR IGNORE INTO
|
|
|
|
conf('id', 'title', 'date')
|
|
|
|
VALUES ('%s','%s','%s')""" % (
|
|
|
|
id,
|
|
|
|
title,
|
|
|
|
date
|
|
|
|
)
|
|
|
|
self.execute(sql)
|
|
|
|
|
|
|
|
def add_relation(self, word, user_id, conf_id):
|
|
|
|
word_id = self.save_word(word)
|
|
|
|
date = int(dt.datetime.now().strftime("%s"))
|
|
|
|
sql = """INSERT OR IGNORE INTO
|
|
|
|
relations('word_id', 'user_id', 'conf_id', 'date')
|
|
|
|
VALUES ('%s','%s','%s','%s')""" % (
|
|
|
|
word_id,
|
|
|
|
user_id,
|
|
|
|
conf_id,
|
|
|
|
date
|
|
|
|
)
|
|
|
|
self.execute(sql)
|
2018-08-03 15:15:35 +03:00
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
def get_top(self, user_id, conf_id, limit=10):
|
2018-08-03 15:15:35 +03:00
|
|
|
sql = """
|
2018-02-04 07:13:09 +03:00
|
|
|
SELECT w.word, COUNT(*) as count FROM relations r
|
|
|
|
LEFT JOIN word w ON w.id = r.word_id
|
|
|
|
LEFT JOIN `user` u ON u.id = r.user_id
|
|
|
|
WHERE u.id = '%s' AND
|
2018-02-19 18:24:38 +03:00
|
|
|
r.conf_id = '%s' AND
|
2018-02-20 18:05:49 +03:00
|
|
|
r.id > (
|
|
|
|
SELECT IFNULL(MAX(relation_id), 0) FROM reset WHERE user_id = '%s' AND conf_id = '%s'
|
|
|
|
)
|
2018-02-04 07:13:09 +03:00
|
|
|
GROUP BY w.word
|
|
|
|
ORDER BY count DESC
|
|
|
|
LIMIT %s
|
|
|
|
""" % (
|
|
|
|
user_id,
|
|
|
|
conf_id,
|
2018-02-19 18:24:38 +03:00
|
|
|
user_id,
|
2018-02-20 16:17:00 +01:00
|
|
|
conf_id,
|
2018-02-04 07:13:09 +03:00
|
|
|
limit
|
|
|
|
)
|
|
|
|
result = self.execute(sql)
|
|
|
|
return(result)
|
|
|
|
|
2018-02-19 17:22:55 +03:00
|
|
|
def here(self, user_id, conf_id):
|
2018-08-03 15:15:35 +03:00
|
|
|
sql = """
|
2018-02-19 18:24:38 +03:00
|
|
|
SELECT DISTINCT(u.username) FROM relations r
|
|
|
|
LEFT JOIN user u
|
|
|
|
ON u.id = r.user_id
|
|
|
|
LEFT JOIN conf c
|
|
|
|
ON r.conf_id = c.id
|
|
|
|
WHERE c.id = '%s' and
|
2018-02-19 17:22:55 +03:00
|
|
|
u.id != '%s'
|
|
|
|
""" % (
|
|
|
|
conf_id,
|
|
|
|
user_id
|
|
|
|
)
|
|
|
|
result = self.execute(sql)
|
|
|
|
return(result)
|
|
|
|
|
2018-02-20 18:05:49 +03:00
|
|
|
def reset(self, user_id, conf_id):
|
|
|
|
date = int(dt.datetime.now().strftime("%s"))
|
|
|
|
sql = """
|
|
|
|
INSERT OR IGNORE INTO reset (user_id, conf_id, date, relation_id)
|
|
|
|
VALUES ('%s', '%s', '%s', (SELECT MAX(rowid) FROM relations));
|
|
|
|
""" % (
|
|
|
|
user_id,
|
|
|
|
conf_id,
|
|
|
|
date
|
|
|
|
)
|
|
|
|
result = self.execute(sql)
|
|
|
|
return(result)
|
2018-02-19 18:24:38 +03:00
|
|
|
|
2018-02-26 15:50:32 +01:00
|
|
|
def command(self, sql):
|
|
|
|
if 'DELETE' in sql.upper() \
|
2018-08-03 15:15:35 +03:00
|
|
|
or 'INSERT' in sql.upper() \
|
|
|
|
or 'UPDATE' in sql.upper() \
|
|
|
|
or 'DROP' in sql.upper() \
|
|
|
|
or 'CREATE' in sql.upper() \
|
|
|
|
or 'ALTER' in sql.upper():
|
2018-02-26 15:50:32 +01:00
|
|
|
return('gtfo')
|
|
|
|
try:
|
|
|
|
if 'LIMIT' in sql.upper()[-9:]:
|
2018-08-03 15:15:35 +03:00
|
|
|
result = self.execute(sql)
|
2018-02-26 15:50:32 +01:00
|
|
|
else:
|
2018-08-03 15:15:35 +03:00
|
|
|
result = self.execute(sql + ' limit 20')
|
2018-02-26 15:50:32 +01:00
|
|
|
except Exception as err:
|
|
|
|
result = err
|
|
|
|
return(result)
|
2018-08-03 15:15:35 +03:00
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
def close(self):
|
2018-02-20 16:17:00 +01:00
|
|
|
self.conn.close()
|