#!/usr/bin/python import psycopg2 import csv import os def connect_db(db_settings): conn = None print("Connecting to the PostgreSQL database...") try: conn = psycopg2.connect(**db_settings) print("Connection success") except (Exception, psycopg2.DatabaseError) as error: print(error) return conn def add_item(db_settings, itemid, skuid, choice, attributes, image): connection = connect_db(db_settings) cursor = connection.cursor() cursor.execute(""" INSERT INTO item (uuid, itemid, skuid, choice, attributes, image) VALUES (nextval('uuid_sequence'), %s, %s, %s, %s, %s) """, (itemid, skuid, choice, attributes, image)) connection.commit() connection.close() def add_history_entry(db_settings, itemid, skuid, choice, attributes, image, price, currency, quantity, discount_percentage): connection = connect_db(db_settings) cursor = connection.cursor() cursor.execute(""" SELECT uuid FROM item WHERE itemid = %s AND skuid = %s """, (itemid, skuid)) if cursor.rowcount == 0: add_item(db_settings, itemid, skuid, choice, attributes, image) cursor.execute(""" SELECT uuid FROM item WHERE itemid = %s AND skuid = %s """, (itemid, skuid)) uuid = cursor.fetchall()[0] cursor.execute(""" INSERT INTO history (uuid, price, currency, quantity, discount_percentage, h_timestamp) VALUES (%s, %s, %s, %s, %s, (SELECT LOCALTIMESTAMP)) """, (uuid, price, currency, quantity, discount_percentage)) connection.commit() connection.close() def get_history(db_settings): connection = connect_db(db_settings) cursor = connection.cursor() cursor.execute(""" SELECT uuid, quantity, discount_percentage, price, currency, h_timestamp FROM history """) results = cursor.fetchall() cursor.close() connection.close() return results def get_item(db_settings): connection = connect_db(db_settings) cursor = connection.cursor() cursor.execute(""" SELECT uuid, itemid, skuid, choice, attributes, image FROM item """) results = cursor.fetchall() cursor.close() connection.close() return results def export_csv(db_settings): connection = connect_db(db_settings) cursor = connection.cursor() cursor.execute(""" SELECT i.uuid, i.itemid, i.skuid, i.choice, i.attributes, i.image, h.quantity, h.discount_percentage, h.price, h.currency, h.h_timestamp FROM item i, history h WHERE i.uuid = h.uuid """) results = cursor.fetchall() with open(os.path.dirname(os.path.realpath(__file__))+"/output.csv", 'w') as csv_file: # Create a CSV writer writer = csv.writer(csv_file) # write the column names writer.writerow([col[0] for col in cursor.description]) # write the query results writer.writerows(results) cursor.close() connection.close() def initialize(db_settings): connection = connect_db(db_settings) cursor = connection.cursor() cursor.execute(""" DROP TABLE IF EXISTS history """) cursor.execute(""" DROP TABLE IF EXISTS item """) cursor.execute(""" DROP SEQUENCE IF EXISTS uuid_sequence """) cursor.execute(""" CREATE SEQUENCE uuid_sequence INCREMENT BY 1 START WITH 1 """) cursor.execute(""" CREATE TABLE item ( uuid int, itemid bigint, skuid bigint, choice boolean, attributes text[], image text, primary key (uuid) ) """) cursor.execute(""" CREATE TABLE history ( uuid int, quantity integer, discount_percentage numeric(2), price money, currency varchar(4), h_timestamp timestamp, foreign key (uuid) references item(uuid), primary key (uuid, h_timestamp) ) """) connection.commit() connection.close() print("Database initialized")