-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.py
158 lines (140 loc) · 5.25 KB
/
db.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
"""This module perform operations about database."""
import mysql.connector
def mysql_switch(onoff):
"""
Turn on/off MySQL server.
:param onoff: An integer 0 for Off, 1 for On
"""
import commands
if onoff == 0:
print commands.getstatusoutput("mysql.server stop")
elif onoff == 1:
print commands.getstatusoutput("mysql.server start")
def connect_db():
"""Connect database and return connection, cursor."""
config = {
'user': 'root',
'password': 'root',
'host': 'localhost',
'database': 'time',
'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
return cnx, cursor
def empty_db(table='both', op='truncate'):
"""
Empty time database for reconstruction.
:param table: A string for table name in database. If 'both', empty all tables.
:param op: A string to describe action type for emptying database, 'truncate' (default) or 'drop'.
"""
cnx, cursor = connect_db()
echo, query1, query2 = ("",)*3
if op == 'truncate':
query1 = "truncate table intervals"
query2 = "truncate table types"
echo = 'Database is truncated.'
elif op == 'drop':
query1 = 'drop table intervals'
query2 = 'drop table types'
echo = 'Database is dropped.'
if table == 'both':
cursor.execute(query1)
cursor.execute(query2)
elif table == 'intervals':
cursor.execute(query1)
elif table == 'types':
cursor.execute(query2)
cnx.commit()
cnx.close()
print echo
def create_types_table():
"""Create `types` table in database."""
cnx, cursor = connect_db()
create_types = ("create table types(\n"
" guid char(36) not null,\n"
" `group` tinyint(1) not null,\n"
" `name` varchar(255) not null,\n"
" parent char(36),\n"
" `order` tinyint not null,\n"
" color int not null,\n"
" deleted tinyint(1) not null,\n"
" revision int not null,\n"
" imageId varchar(10) not null,\n"
" primary key (guid)\n"
" )")
cursor.execute(create_types)
cnx.commit()
cnx.close()
def create_intervals_table():
"""Create `intervals` table in database."""
cnx, cursor = connect_db()
create_intervals = ("create table intervals(\n"
" guid char(36) not null,\n"
" type char(36) not null,\n"
" `from` int(10) not null,\n"
" `to` int(10) not null,\n"
" delta MEDIUMINT not null,\n"
" comment varchar(255),\n"
" activityGuid char(36) not null,\n"
" primary key (guid)\n"
" )")
cursor.execute(create_intervals)
cnx.commit()
cnx.close()
def create_all_tables():
"""Create all necessary tables in database"""
create_types_table()
create_intervals_table()
def insert_types(json):
"""
Insert types data into database. Insertion will update existing data.
:param json: A list of dict contain types data from aTimeLogger.
"""
cnx, cursor = connect_db()
for item in json:
guid = '"%s"' % item['guid']
group = 1 if item['group'] else 0
name = '"%s"' % item['name']
parent = '"%s"' % item['parent']['guid'] if item['parent'] else "NULL"
order = item['order']
color = item['color']
deleted = 1 if item['deleted'] else 0
revision = item['revision']
image_id = '"%s"' % item['imageId']
insert = "replace into types \
(guid, `group`, `name`, parent, `order`, color, deleted, revision, imageId) \
values ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})" \
.format(guid, group, name, parent, order, color, deleted, revision, image_id)
cursor.execute(insert)
cnx.commit()
cnx.close()
def insert_intervals(json):
"""
Insert types data into database. Insertion will update existing data.
:param json: A list of dict contain intervals data from aTimeLogger.
"""
cnx, cursor = connect_db()
for item in json:
guid = '"%s"' % item['guid']
type1 = '"%s"' % item['type']['guid']
from1 = item['from']
to = item['to']
delta = item['to'] - item['from']
comment = '"%s"' % item['comment'] if item['comment'] else "NULL"
activity_guid = '"%s"' % item['activityGuid']
insert = "replace into intervals \
(guid, `type`, `from`, `to`, delta, `comment`, activityGuid) \
values ({0}, {1}, {2}, {3}, {4}, {5}, {6})" \
.format(guid, type1, from1, to, delta, comment, activity_guid)
cursor.execute(insert)
cnx.commit()
cnx.close()
def insert_all(types, intervals):
"""
Insert all data into database.
:param types: A list of dict contain types data from aTimeLogger.
:param intervals: A list of dict contain intervals data from aTimeLogger.
"""
insert_types(types)
insert_intervals(intervals)