-
Notifications
You must be signed in to change notification settings - Fork 2
/
MysqlDatabase.py
137 lines (121 loc) · 4.16 KB
/
MysqlDatabase.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
import pymysql.cursors
from Cafeteria.MealContainer import MealContainer
from Alacarte.AlacarteMealContainer import AlacarteMealContainer
from CredentialsConfig import CredentialsConfig
class MysqlDatabase:
def __init__(self):
self.credentials = CredentialsConfig.mysqlDbCredentials
try:
self._connect()
except:
print ("MySql connection failed.")
def _connect(self):
self.connection = pymysql.connect(
user=self.credentials.user,
password=self.credentials.password,
host=self.credentials.ip,
db=self.credentials.dbName,
charset='utf8',
cursorclass=pymysql.cursors.DictCursor,
autocommit=True)
self.cursor = self.connection.cursor()
def setCafeteriaMenu(self, mealArray):
for eachMeal in mealArray:
assert isinstance(eachMeal, MealContainer)
self._upsertMeal(eachMeal)
def setAlacarteMenu(self, mealArray):
for eachMeal in mealArray:
assert isinstance(eachMeal, AlacarteMealContainer)
self._uploadAlacarteMeal(eachMeal)
def _uploadAlacarteMeal(self, meal):
assert isinstance(meal, AlacarteMealContainer)
#sqlVariables = meal.serialize() + meal.serialize()
#placeholders = ', '.join(['%s'] * len(sqlVariables)) # "%s, %s, %s, ... %s"
sql = """INSERT INTO cafeteriaMenu (
activeUntilDate,
en_name,
tr_name,
startDate,
endDate,
en_mainDish,
en_sideDish,
en_soup,
en_extra1,
en_extra2,
en_extra3,
tr_mainDish,
tr_sideDish,
tr_soup,
tr_extra1,
tr_extra2,
tr_extra3
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
activeUntilDate=?,
en_name=?,
tr_name=?,
startDate=?,
endDate=?,
en_mainDish=?,
en_sideDish=?,
en_soup=?,
en_extra1=?,
en_extra2=?,
en_extra3=?,
tr_mainDish=?,
tr_sideDish=?,
tr_soup=?,
tr_extra1=?,
tr_extra2=?,
tr_extra3=?
"""
#self.cursor.execute(sql, sqlVariables)
#self.connection.commit()
return
def _upsertMeal(self, meal):
assert isinstance(meal, MealContainer)
sqlVariables = meal.serialize() + meal.serialize()
#placeholders = ', '.join(['%s'] * len(sqlVariables)) # "%s, %s, %s, ... %s"
sql = """INSERT INTO cafeteriaMenu (
activeUntilDate,
en_name,
tr_name,
startDate,
endDate,
en_mainDish,
en_sideDish,
en_soup,
en_extra1,
en_extra2,
en_extra3,
tr_mainDish,
tr_sideDish,
tr_soup,
tr_extra1,
tr_extra2,
tr_extra3
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
activeUntilDate=?,
en_name=?,
tr_name=?,
startDate=?,
endDate=?,
en_mainDish=?,
en_sideDish=?,
en_soup=?,
en_extra1=?,
en_extra2=?,
en_extra3=?,
tr_mainDish=?,
tr_sideDish=?,
tr_soup=?,
tr_extra1=?,
tr_extra2=?,
tr_extra3=?
"""
self.cursor.execute(sql, sqlVariables)
self.connection.commit()
return