-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connect.py
210 lines (197 loc) · 5.96 KB
/
db_connect.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import pymysql as db
import password
from datetime import datetime as dt
import calculations as calcs
import data_helpers as helpers
def insertPrice(type, date, price):
connection = connectDB()
cursor = connection.cursor()
try:
cursor.callproc('insert_historical_price',[type, date, price])
except:
print('error in insert_historical_price at: ', dt.now())
connection.rollback()
else:
connection.commit()
disconnectDB(connection)
return
def getLatestCarbonFromDB():
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_latest_carbon')
except:
print('error in getLatestCarbonFromDB at: ', dt.now())
connection.rollback()
disconnectDB(connection)
return
else:
result = cursor.fetchall()
for r in result:
if r is not None:
disconnectDB(connection)
return r
#hack for first row
return [0,0,0]
def getLatestSaltFromDB():
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_latest_salt')
except:
print('error in getLatestSaltFromDB at: ', dt.now())
connection.rollback()
disconnectDB()
return
else:
result = cursor.fetchall()
for r in result:
if r is not None:
disconnectDB(connection)
return r
#hack for empty table
return [0,0,0]
def getLatestNTA():
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_latest_nta')
except:
print('error in getLatestNTA at: ', dt.now())
connection.rollback()
disconnectDB()
return
else:
result = cursor.fetchall()
for r in result:
if r is not None:
disconnectDB(connection)
return r
#hack for empty table
return [0,0,0]
def getAllSaltFromDB():
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_all_salt')
except:
print('error in getAllSaltFromDB at: ', dt.now())
connection.rollback()
disconnectDB()
return
else:
result_raw = cursor.fetchall()[0][0]
refined = helpers.refinePricesForReports(result_raw)
disconnectDB()
return refined
def getAllCarbonFromDB():
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_all_carbon')
except:
print('error in getAllCarbonFromDB at: ', dt.now())
connection.rollback()
disconnectDB()
return
else:
result_raw = cursor.fetchall()[0][0]
refined = helpers.refinePricesForReports(result_raw)
disconnectDB(connection)
return refined
def connectDB():
connection = db.connect(host="localhost", port=3306, user="root", password=password.getPassword(), database="carbon_market_schema")
if connection is not None:
return connection
else:
print('could not connect to db')
return None
def disconnectDB(connection):
if connection is not None:
connection.close()
return
else:
print('database was already disconnected')
return
def getPriceWithDate(date, type):
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_price_with_date', [date, type])
except:
print('error in get_price_with_date at: ', dt.now())
connection.rollback()
disconnectDB(connection)
return
else:
result = cursor.fetchall()
for r in result:
if r is not None:
disconnectDB(connection)
return r
#hack for empty table
return 'none'
def getPricesByDate(start, end, type):
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_prices_by_dates', [start, end, type])
except:
print('error in get_prices_by_date')
connection.rollback()
disconnectDB(connection)
return
else:
result_raw = cursor.fetchall()[0][0]
if result_raw:
refined = helpers.refinePricesForReports(result_raw)
return refined
else:
return None
def insertEvent(type, text, description, date):
connection = connectDB()
cursor = connection.cursor()
try:
cursor.callproc('insert_event',[type, text, description, date])
except connection.Error as error:
print("Failed to execute stored procedure: {}".format(error))
print('error in insert_event at: ', dt.now())
connection.rollback()
disconnectDB(connection)
else:
connection.commit()
disconnectDB(connection)
return
def getEventWithDateType(date, type):
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_event_by_date_type', [date, type])
except:
print('error in get_event_by_date_type at: ', dt.now())
connection.rollback()
disconnectDB(connection)
return
else:
result = cursor.fetchall()
print(result)
for r in result:
if r is not None:
disconnectDB(connection)
return r
#hack for empty table
return 'none'
def getEventsByDateRangeType(start, end, type):
connection = connectDB()
cursor= connection.cursor()
try:
cursor.callproc('get_events_by_date_range_type', [start, end, type])
except connection.Error as error:
print("Failed to execute stored procedure: {}".format(error))
print('error in insert_event at: ', dt.now())
connection.rollback()
disconnectDB(connection)
return
else:
result_raw = cursor.fetchall()
return result_raw