I have been working with postgresql where I am trying to get information from database, update, exists and much more. I have created a context manager for commit and to close whenever it is in finished.
This project is about storing some variables (mostly regarding store and link)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from typing import List, Set, Tuple
import pendulum
import psycopg2
import psycopg2.extras
from config import configuration
DATABASE_CONNECTION = {
"host": configuration.path.database.environment,
"database": configuration.postgresql.database,
"user": configuration.postgresql.user,
"password": configuration.postgresql.password,
}
ps_connection = psycopg2.connect(**DATABASE_CONNECTION)
class QuickConnection:
"""
Function that commits and closes when its done.
Rollbacks if an error happens
"""
def __init__(self):
self.ps_cursor = ps_connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
ps_connection.autocommit = True
def __enter__(self):
return self.ps_cursor
def __exit__(self, err_type, err_value, traceback):
if err_type and err_value:
ps_connection.rollback()
self.ps_cursor.close()
return False
# ------------------------------------------------------------------------------- #
# Exists functions
# ------------------------------------------------------------------------------- #
def link_exists(store: str, link: str) -> bool:
query_params = {
"store": store,
"link": link
}
sql_query = "SELECT EXISTS (SELECT 1 FROM store_items WHERE store=%(store)s AND link=%(link)s);"
with QuickConnection() as ps_cursor:
ps_cursor.execute(sql_query, query_params)
exists, = ps_cursor.fetchone()
return exists
# ------------------------------------------------------------------------------- #
# Insert / Update / Delete / Return value from database
# ------------------------------------------------------------------------------- #
def store_exists(store: str) -> bool:
query_params = {
"store": store
}
sql_query = "INSERT INTO store_config (store) SELECT %(store)s WHERE NOT EXISTS (SELECT 1 FROM store_config WHERE store = %(store)s);"
with QuickConnection() as ps_cursor:
ps_cursor.execute(sql_query, query_params)
return bool(ps_cursor.rowcount)
def register_product(store: str, pageData) -> bool:
query_params = {
"store": store,
"name": pageData.name,
"link": pageData.link,
"image": pageData.image,
"visible": "yes",
"added_date": pendulum.now('Europe/Stockholm').format('YYYY-MM-DD HH:mm:ss.SSSSSS')
}
sql_query = "INSERT INTO store_items (store, name, link, image, visible, added_date) VALUES (%(store)s, %(name)s, %(link)s, %(image)s, %(visible)s, %(added_date)s);"
with QuickConnection() as ps_cursor:
ps_cursor.execute(sql_query, query_params)
return bool(ps_cursor.rowcount)
# ------------------------------------------------------------------------------- #
# Get all functions
# ------------------------------------------------------------------------------- #
def get_all_active_links(store: str) -> List[str]:
query_params = {
"store": store,
"visible": "yes"
}
sql_query = "SELECT link FROM store_items WHERE store = %(store)s AND visible = %(visible)s;"
with QuickConnection() as ps_cursor:
ps_cursor.execute(sql_query, query_params)
return [links["link"] for links in ps_cursor]
def get_feed_urls(store: str) -> List[str]:
query_params = {
"store": store
}
sql_query = "SELECT link FROM feed_urls WHERE store = %(store)s;"
with QuickConnection() as ps_cursor:
ps_cursor.execute(sql_query, query_params)
return [links["link"] for links in ps_cursor]
def get_all_stores() -> List[str]:
sql_query = "SELECT store FROM store_config;"
with QuickConnection() as ps_cursor:
ps_cursor.execute(sql_query)
return [stores["store"] for stores in ps_cursor]
I wonder if there is anything I could improve :)