-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
56 lines (49 loc) · 1.48 KB
/
database.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
from sqlalchemy import create_engine, text
import os
# Obtém a string de conexão com o banco de dados do ambiente
db_connection_string = os.environ['DB_CONN']
# Cria uma conexão com o banco de dados
engine = create_engine(
db_connection_string,
connect_args={
"ssl": {
"ssl_ca": "/etc/ssl/cert.pem"
}
})
# Carrega todos os jobs do banco de dados
def load_jobs_from_db():
with engine.connect() as conn:
result = conn.execute(text("select * from card"))
results_as_dict = result.mappings().all()
jobs = []
for row in results_as_dict:
jobs.append(row)
return jobs
# Carrega um job específico do banco de dados
def load_job_from_db(id):
with engine.connect() as conn:
result = conn.execute(
text(f"SELECT * FROM card WHERE id = :val"),
{"val": id}
)
rows = result.mappings().all()
if len(rows) == 0:
return None
else:
return dict(rows[0])
# Adiciona um job ao banco de dados
def add_job_to_db(data):
with engine.connect() as conn:
query = text("INSERT INTO card (card_title, card_description) VALUES (:card_title, :card_description);")
conn.execute(query,
{
"card_title":data['card_title'],
"card_description":data['card_description']}
)
# Exclui um job do banco de dados
def delete_job_from_db(id):
with engine.connect() as conn:
conn.execute(
text(f"delete from card WHERE id = :id"),
{"id": id}
)