-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
111 lines (95 loc) · 2.8 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
#from google.cloud.sql.connector import Connector, IPTypes
import ssl
import os
from sqlalchemy import create_engine, MetaData, Column, Table, ForeignKey, Integer, Sequence, String, Date, Float, BIGINT, select, engine
db_user=os.environ['db_user']
db_pass=os.environ['db_pass']
db_host=os.environ['db_host']
db_port=os.environ['db_port']
db_name=os.environ['db_name']
connect_args = {}
db_root_cert = "keys/server-ca.pem" # e.g. 'keys/server-ca.pem'
db_cert = "keys/client-cert.pem" # e.g. 'keys/client-cert.pem'
db_key = "keys/client-key.pem" # e.g. 'keys/client-key.pem'
ssl_context = ssl.SSLContext()
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.load_verify_locations(db_root_cert)
ssl_context.load_cert_chain(db_cert, db_key)
connect_args["ssl_context"] = ssl_context
pool = create_engine(
engine.url.URL.create(
drivername="postgresql+pg8000",
username=db_user,
password=db_pass,
host=db_host,
port=db_port,
database=db_name,
),
connect_args=connect_args,
)
meta = MetaData()
meals = Table('meals', meta,
Column('ID', Integer, primary_key=True, autoincrement=True),
Column('date', String(20)),
Column('mealName', String(60)),
Column('menuMealName', String(600)),
Column('weight', Float),
Column('calories', Float),
Column('fat', Float),
Column('protein', Float),
Column('carbohydrate', Float),
Column('dietaryFiber', Float),
Column('sugar', Float),
Column('salt', Float),
Column('saturatedFattyAcids', Float),
Column('ingredients', String(6000)),
Column('rate', Integer)
)
# create table
#meals.create(pool)
def insert(
date,
mealName,
menuMealName,
weight,
calories,
fat,
protein,
carbohydrate,
dietaryFiber,
sugar,
salt,
saturatedFattyAcids,
ingredients
):
ins = meals.insert().values(
date=date,
mealName=mealName,
menuMealName=menuMealName,
weight = weight,
calories=calories,
fat=fat,
protein=protein,
carbohydrate=carbohydrate,
dietaryFiber=dietaryFiber,
sugar=sugar,
salt=salt,
saturatedFattyAcids=saturatedFattyAcids,
ingredients=ingredients
)
with pool.connect() as db_conn:
db_conn.execute(ins)
db_conn.commit()
# for row in result:
# print(row)
# close Cloud SQL Connector
#connector.close()
def isEntry(date, mealName):
with pool.connect() as db_conn:
stmt = select(meals).where(meals.c.date==date).where(meals.c.mealName==mealName)
result = db_conn.execute(stmt)
if result.rowcount==0:
return False
else:
return True
#print(isEntry("2024-01-11","Śniadanie"))