-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdao.py
57 lines (50 loc) · 2.03 KB
/
dao.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
from mysql.connector import connect, Error
import logging
class DataAccess:
def __init__(self, host, port, user, password, database):
try:
self.connection = connect(host=host, user=user, password=password, port=port, database=database)
logging.debug("connected")
except Error as e:
logging.exception(f"Failed to connect to {host}", e)
raise
def shutdown(self):
if self.connection:
logging.debug("closing")
self.connection.close()
def get_users_by_lastname_range(self, start, end):
"""query the dox users by range of last names"""
users = []
sql = """
select user.id, user.practice_id, user.firstname, user.lastname, user.specialty, user_practice.location
from user, user_practice where
user_practice.id = user.practice_id
and user.lastname >='%s' and user.lastname <= '%s'
order by user.lastname, user.firstname
""" % (
start,
end,
)
with self.connection.cursor() as cursor:
cursor.execute(sql)
for user in cursor.fetchall():
users.append({
'id': user[0],
'practice_id': user[1],
'firstname': user[2],
'lastname': user[3],
'specialty': user[4],
'location': user[5],
})
return users
def save_vendor_users_last_active_date(self, users):
"""upsert the last active date of users at vendor site"""
logging.debug("saving %s" % len(users))
sql = """
insert into user_vendor(user_id, last_active_date_vendor) values(%(user_id)s, %(last_active_date_vendor)s)
ON DUPLICATE KEY UPDATE last_active_date_vendor=VALUES(last_active_date_vendor);
"""
with self.connection.cursor() as cursor:
cursor.executemany(sql, users)
self.connection.commit()
cursor.close()