forked from MasterGroosha/telegram-reminder-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLighter.py
108 lines (94 loc) · 3.43 KB
/
SQLighter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
import sqlite3
# Table structure
# cursor.execute('CREATE TABLE Schedules '
# '(Id INTEGER PRIMARY KEY, '
# 'Chat_id INTEGER, '
# 'Scheduled_time INTEGER)')
class SQLiteInsertError(Exception):
def __init__(self, message):
self.message = message
super(SQLiteInsertError, self).__init__('{0}'.format(self.message))
class SQLighter:
"""
Warning! This version works only with one table and fixed columns
"""
def __init__(self, database):
self.connection = sqlite3.connect(database)
self.cursor = self.connection.cursor()
def select_all(self):
"""
Get all data from table
:return:
"""
with self.connection:
return self.cursor.execute('SELECT * FROM Schedules').fetchall()
def select_execution_times(self, starting_from=None):
"""
Selects "at" executions which will happen in future (if starting_from is set)
or selects all "at" execution times
:param starting_from: starting Unix time
:return: list of execution times or None
"""
if starting_from:
result = self.cursor.execute(
'SELECT Scheduled_time FROM Schedules WHERE Scheduled_time > ?', (starting_from,)) \
.fetchall()
if len(result) > 0:
return result
else:
return None
else:
result = self.cursor.execute('SELECT Scheduled_time FROM Schedules').fetchall()
if len(result) > 0:
return result
else:
return None
def count_entries_for_id(self, chat_id):
"""
Counts number of entries for specific ID.
Now non-VIP users can have not more than 5 reminders at once
:param chat_id:
:return:
"""
result = self.cursor.execute('SELECT Id FROM Schedules WHERE Chat_id=?', (int(chat_id),))
return len(result.fetchall())
def insert(self, chat_id, time, job_id):
"""
Creates new entry in table
:param chat_id: User's chat id
:param time: Time of execution
:param job_id: Job ID given by "at" command output
:return: True if successfully inserted
:raise SQLiteInsertError: If fail to insert
"""
with self.connection:
if self.cursor.execute('INSERT INTO Schedules (Chat_id, Scheduled_time, Job_id) values (?, ?, ?)',
(int(chat_id), int(time), int(job_id))).rowcount < 0:
raise SQLiteInsertError('Failed to insert data')
# self.connection.commit()
return True
def delete_old(self, time):
"""
Deletes rows with execution time in the past (related to server's time)
:param time: Time from which to delete
:return: ...
"""
with self.connection:
result = self.cursor.execute('DELETE FROM Schedules WHERE Scheduled_time < ?', (time,))
self.connection.commit()
return result
def execute(self, code):
"""
Executes SQL command (use with caution!)
:param code: SQL command
"""
with self.connection:
try:
self.cursor.execute(code)
except:
pass
def commit(self):
self.connection.commit()
def close(self):
self.connection.close()