-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_controller.py
More file actions
173 lines (144 loc) · 5.93 KB
/
Copy pathdb_controller.py
File metadata and controls
173 lines (144 loc) · 5.93 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sqlite3
from string import Template
# Это должен был быть статический класс, но здесь нельзя создавать статические поля, только методы:(
# здесь представлены методы для работы с локальной бд
class DBController:
con = None
cur = None
path_db = './Chats_base.db'
table_chat = '''CREATE TABLE IF NOT EXISTS chat( id INTEGER PRIMARY KEY,
chat_name TEXT NOT NULL,
id_from_server INTEGER NOT NULL)'''
table_message = '''CREATE TABLE IF NOT EXISTS message( id INTEGER PRIMARY KEY,
text_message TEXT NOT NULL,
time TEXT NOT NULL,
author TEXT NOT NULL,
chat_id INTEGER NOT NULL,
FOREIGN KEY (chat_id) REFERENCES chat(id))'''
table_username = '''CREATE TABLE IF NOT EXISTS options( key TEXT PRIMARY KEY,
value TEXT);'''
query_select_chats = '''SELECT *
FROM chat'''
query_insert_chats = '''INSERT INTO chat(chat_name, id_from_server)
VALUES(?, ?)'''
query_select_messages = '''SELECT text_message,time,author
FROM message
WHERE chat_id = '''
query_insert_message = '''INSERT INTO message(text_message, time, author, chat_id)
VALUES(?, ?, ?, ?)'''
query_delete_messages = '''DELETE FROM message
WHERE chat_id = '''
query_insert_username = '''INSERT INTO options
VALUES(?, ?)'''
query_insert_default_username = '''INSERT INTO options
VALUES('username','Guest')'''
query_select_username = '''SELECT value
FROM options
WHERE key='username' '''
query_update_username = Template('''UPDATE options
SET value='$new_value'
WHERE key='username' ''')
query_select_chat_name = '''SELECT chat_name
FROM chat
WHERE id_from_server= '''
def __init__(self):
try:
self.create_db()
except ConnectionError:
return
def open_con(self):
self.con = sqlite3.connect(self.path_db)
self.cur = self.con.cursor()
def create_db(self):
self.open_con()
self.cur.execute(self.table_chat)
self.cur.execute(self.table_message)
self.cur.execute(self.table_username)
self.init_username()
self.con.commit()
self.close_con()
def init_username(self):
if not self.cur.execute(self.query_select_username).fetchall():
self.cur.execute(self.query_insert_default_username)
def set_username(self, username):
self.open_con()
self.cur.execute(self.query_insert_username, ('username', username))
self.con.commit()
self.close_con()
def get_chats(self):
try:
self.open_con()
self.cur.execute(self.query_select_chats)
chats = self.cur.fetchall()
self.close_con()
return chats
except BaseException:
return list()
def set_chats(self, chat_name, id_from_server):
try:
self.open_con()
self.cur.execute(self.query_insert_chats, (chat_name, id_from_server))
self.con.commit()
self.close_con()
return
except BaseException:
return
def get_messages(self, chat_id):
try:
self.open_con()
self.cur.execute(self.query_select_messages + str(chat_id))
messages = self.cur.fetchall()
self.close_con()
return messages
except BaseException:
return list()
def set_message(self, text_message, time, chat_id, author):
try:
self.open_con()
self.cur.execute(self.query_insert_message, (text_message, time, author, chat_id))
self.con.commit()
self.close_con()
return
except BaseException:
return
def delete_messages(self, chat_id):
try:
self.open_con()
self.cur.execute(self.query_delete_messages + str(chat_id))
self.con.commit()
self.close_con()
return
except BaseException:
return
def get_username(self):
try:
self.open_con()
self.cur.execute(self.query_select_username)
username = self.cur.fetchone()[0]
self.close_con()
return username
except BaseException:
return str()
def set_username(self, new_username):
try:
self.open_con()
self.cur.execute(self.query_update_username.substitute(new_value=new_username))
self.con.commit()
self.close_con()
return
except BaseException:
return
def get_chat_name(self, chat_id):
try:
self.open_con()
self.cur.execute(self.query_select_chat_name + str(chat_id))
chat_name = self.cur.fetchone()[0]
self.close_con()
return chat_name
except BaseException:
return str()
def get_path_db(self):
return self.path_db
def close_con(self):
self.cur.close()
self.con.close()