forked from DataDog/apm-tutorial-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_connection.py
More file actions
104 lines (96 loc) · 3.45 KB
/
postgres_connection.py
File metadata and controls
104 lines (96 loc) · 3.45 KB
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
# Unless explicitly stated otherwise all files in this repository are dual-licensed
# under the Apache 2.0 or BSD3 Licenses.
#
# This product includes software developed at Datadog (https://www.datadoghq.com/)
# Copyright 2022 Datadog, Inc.
import psycopg2
import logging
import os
class PostgresConnection:
def __init__(self):
host = os.getenv("DB_HOST")
self.connection = psycopg2.connect(
host=host,
port="5432",
database="testdb",
user="postgres",
password="password"
)
cursor = self.connection.cursor()
create_table_query = '''CREATE TABLE notes (id SERIAL PRIMARY KEY, description VARCHAR(255) NOT NULL);'''
# Execute a command: this creates a new table
logging.info("Creating notes table.")
try:
cursor.execute(create_table_query)
self.connection.commit()
logging.info("Table created successfully in PostgreSQL")
cursor.close()
except Exception as e:
print(e)
self.connection.rollback()
cursor.close()
logging.info("Table already exists.")
def create_note(self, note):
try:
cursor = self.connection.cursor()
sql = """INSERT INTO notes(description) VALUES (%s) RETURNING id;"""
cursor.execute(sql, (note.description,))
note_id = cursor.fetchone()[0]
self.connection.commit()
cursor.close()
logging.info(f"Created note with id: {note.id}")
return note_id
except Exception as e:
logging.error(e)
self.connection.rollback()
cursor.close()
def update_note(self, note):
try:
cursor = self.connection.cursor()
sql = """UPDATE notes
SET description = %s
WHERE id = %s"""
cursor.execute(sql, (note.description, note.id))
self.connection.commit()
cursor.close()
logging.info(f"Updated note with id: {note.id}")
return str(note)
except Exception as e:
self.connection.rollback()
cursor.close()
def get_notes(self, id=None):
cursor = self.connection.cursor()
if id:
try:
query = "SELECT id, description FROM notes WHERE id = %s"
cursor.execute(query, [id])
note = cursor.fetchone()
cursor.close()
return note
except:
cursor.close()
logging.error(f"Couldn't get note with id: {id}")
else:
try:
query = "SELECT id, description FROM notes ORDER BY id"
cursor.execute(query)
notes = cursor.fetchall()
cursor.close()
response = {}
for note in notes:
response[note[0]] = note[1]
return response
except:
cursor.close()
logging.error(f"Couldn't get notes")
def delete_note(self, id):
try:
cursor = self.connection.cursor()
sql = """DELETE FROM notes WHERE id = %s"""
cursor.execute(sql, (id,))
self.connection.commit()
cursor.close()
logging.info(f"Deleted note with id: {id}")
except:
self.connection.rollback()
cursor.close()