-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.py
167 lines (151 loc) · 5.1 KB
/
database.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
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
from mysql.connector import errorcode, connect, Error
from logger import info, critical, error
class Database:
# database connection config
__config = {
'user': 'xmltvparser',
'password': 'Ndgfhcth2017',
'host': 'localhost',
'database': 'tvservice',
'raise_on_warnings': True,
'charset': 'utf8',
'get_warnings': True
}
# initial tables
__tables = {
'channels': (
"CREATE TABLE `channels` ("
" `id` INT NOT NULL,"
" `title` VARCHAR(255) NOT NULL,"
" `lang` VARCHAR(10) NOT NULL,"
" `icon` VARCHAR(255) DEFAULT NULL,"
"PRIMARY KEY (id),"
"UNIQUE(id),"
"INDEX (id)"
") ENGINE=InnoDb DEFAULT CHARSET utf8"
),
'categories': (
"CREATE TABLE `categories` ("
" `id` INT NOT NULL AUTO_INCREMENT,"
" `title` VARCHAR(255) NOT NULL,"
" `lang` VARCHAR(10) NOT NULL,"
"PRIMARY KEY (id),"
"UNIQUE(id),"
"INDEX (id)"
") ENGINE=InnoDb DEFAULT CHARSET utf8"
),
'programme': (
"CREATE TABLE `programme` ("
" `id` INT NOT NULL AUTO_INCREMENT,"
" `title` VARCHAR(255) NOT NULL,"
" `title_lang` VARCHAR(10) NOT NULL,"
" `start` DATETIME NOT NULL,"
" `end` DATETIME NOT NULL,"
" `duration` TIME NOT NULL,"
" `description` TEXT,"
" `description_lang` VARCHAR(10),"
" `channel_id` INT NOT NULL,"
"PRIMARY KEY (id),"
"INDEX (id),"
"UNIQUE (channel_id, start, end),"
"FOREIGN KEY (channel_id)"
" REFERENCES channels(id)"
" ON DELETE CASCADE"
") ENGINE=InnoDb DEFAULT CHARSET utf8"
),
'programme_category': (
"CREATE TABLE `programme_category` ("
" `programme_id` INT NOT NULL,"
" `category_id` INT NOT NULL,"
"UNIQUE(programme_id, category_id),"
"FOREIGN KEY (programme_id)"
" REFERENCES programme(id)"
" ON DELETE CASCADE,"
"FOREIGN KEY (category_id)"
" REFERENCES categories(id)"
" ON DELETE CASCADE"
") ENGINE=InnoDB DEFAULT CHARSET utf8"
)
}
# creates required variables
def __init__(self):
self.__err = None
self.__connection = None
self.__cursor = None
self.__query = None
# establish database connection
def connect(self):
try:
connection = connect(**self.__config)
connection.cursor()
except Error as err:
self.__err = err
self.__error()
else:
self.__tables = self.__tables
self.__connection = connection
self.__cursor = connection.cursor()
self.__create_tables()
# disconnects from database
def disconnect(self):
self.__cursor.close()
self.__connection.close()
# runs prepared query with data
def exec(self, data=None):
try:
self.__cursor.execute(self.__query, data)
except Error as err:
self.__err = err
return False
else:
if self.__query.startswith('INSERT') or self.__query.startswith('UPDATE') or self.__query.startswith(
'DELETE'):
self.__connection.commit()
return True
else:
result = self.__cursor.fetchall()
return True, result
# prepares query
def prepare(self, query):
self.__query = query
# run query
def query(self, query):
self.prepare(query)
run = self.exec()
return run
# id of last inserted row
def last_insert_id(self):
return self.__cursor.lastrowid
# querying error message
def error(self):
return self.__err
# logs error
def __error(self):
if self.__err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
critical('Something is wrong with your user name or password')
exit(1)
elif self.__err.errno == errorcode.ER_BAD_DB_ERROR:
critical('Database does not exist')
exit(1)
else:
error(self.__err)
# creates initial tables
def __create_tables(self, tables=__tables):
failed_tables = {}
for name, ddl in tables.items():
try:
self.__cursor.execute(ddl)
except Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
info("Creating table {}: table already exists".format(name))
else:
self.__err = err
self.__error()
failed_tables[name] = ddl
else:
info("Creating table {}: OK".format(name))
if failed_tables:
self.__create_tables(failed_tables)
if __name__ == "__main__":
d = Database()
d.connect()