created and added all create sql

created and added drop sql for items &
item_info
This commit is contained in:
Jadowyne Ulve 2024-09-25 21:21:22 -05:00
parent ffed29d187
commit 7ae046092b
12 changed files with 160 additions and 54 deletions

99
main.py
View File

@ -21,14 +21,15 @@ def insert_row(table_name, name):
finally: finally:
return id return id
def create_table(table_name): def create_table(sql_file: str):
conn = None conn = None
try: try:
params = config() params = config()
conn = psycopg2.connect(**params) conn = psycopg2.connect(**params)
cur = conn.cursor() cur = conn.cursor()
cur.execute(f"CREATE TABLE {table_name}(id INTEGER PRIMARY KEY, name TEXT);") with open(sql_file, 'r') as file:
cur.execute(file.read())
cur.close() cur.close()
conn.commit() conn.commit()
@ -38,38 +39,68 @@ def create_table(table_name):
if conn is not None: if conn is not None:
conn.close() conn.close()
def connect(): def add_item(barcode: str, name: str):
""" Connect to the PostgreSQL database server """ sql = f"INSERT INTO item_info(barcode) VALUES ('{barcode}') RETURNING id;"
conn = None database_config = config()
try: item_info_id = None
# read connection parameters with psycopg2.connect(**database_config) as conn:
params = config() try:
with conn.cursor() as cur:
cur.execute(sql)
rows = cur.fetchone()
if rows:
item_info_id = rows[0]
except (Exception, psycopg2.DatabaseError) as error:
print(error)
conn.rollback()
return False
# connect to the PostgreSQL server sqltwo = f"INSERT INTO items(barcode, item_name, item_info_id, row_type, item_type, search_string) VALUES('{barcode}', '{name}', {item_info_id}, 'item', 'other', '{barcode}%{name}') RETURNING *;"
print('Connecting to the PostgreSQL database...') row = None
conn = psycopg2.connect(**params) try:
with conn.cursor() as cur:
cur.execute(sqltwo)
rows = cur.fetchone()
if rows:
row = rows[:]
except (Exception, psycopg2.DatabaseError) as error:
print(error)
conn.rollback()
return False
conn.commit()
return row
def drop_table(sql_file: str):
database_config = config()
with open(sql_file, 'r') as sql_file:
sql = sql_file.read()
with psycopg2.connect(**database_config) as conn:
try:
with conn.cursor() as cur:
cur.execute(sql)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
conn.rollback()
return False
# create a cursor conn.commit()
cur = conn.cursor() return True
# execute a statement if __name__ == '__main__':
print('PostgreSQL database version:') drop_table('sql/drop/item_info.sql')
cur.execute('SELECT version()') drop_table('sql/drop/items.sql')
create_table('sql/create/logins.sql')
create_table('sql/create/groups.sql')
create_table('sql/create/linked_items.sql')
create_table('sql/create/transactions.sql')
create_table('sql/create/brands.sql')
create_table('sql/create/food_info.sql')
create_table('sql/create/item_info.sql')
create_table('sql/create/item.sql')
# display the PostgreSQL database server version row = add_item(barcode='1237', name='test237')
db_version = cur.fetchone() print(row)
print(db_version)
# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
if __name__ == '__main__':
# create_table("mytable")
print(insert_row('mytable', 'ted'))

4
sql/create/brands.sql Normal file
View File

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS brands (
id SERIAL PRIMARY KEY,
name VARCHAR(255)
);

7
sql/create/food_info.sql Normal file
View File

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS food_info (
id SERIAL PRIMARY KEY,
food_groups TEXT [],
ingrediants TEXT [],
nutrients JSONB,
exires BOOLEAN
);

8
sql/create/groups.sql Normal file
View File

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS groups(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
included_items INTEGER [],
group_type VARCHAR(255),
UNIQUE (name)
);

24
sql/create/item.sql Normal file
View File

@ -0,0 +1,24 @@
CREATE TABLE IF NOT EXISTS items(
id SERIAL PRIMARY KEY,
barcode VARCHAR(255) NOT NULL,
item_name VARCHAR(255) NOT NULL,
brand INTEGER,
tags TEXT [],
links TEXT [],
item_info_id INTEGER NOT NULL,
food_info_id INTEGER,
row_type VARCHAR(255) NOT NULL,
item_type VARCHAR(255) NOT NULL,
search_string TEXT NOT NULL,
quantity_on_hand FLOAT8,
UNIQUE(barcode, item_info_id),
CONSTRAINT fk_item_info
FOREIGN KEY(item_info_id)
REFERENCES item_info(id),
CONSTRAINT fk_food_info
FOREIGN KEY(food_info_id)
REFERENCES food_info(id),
CONSTRAINT fk_brand
FOREIGN KEY(brand)
REFERENCES brands(id)
);

15
sql/create/item_info.sql Normal file
View File

@ -0,0 +1,15 @@
CREATE TABLE IF NOt EXISTS item_info (
id SERIAL PRIMARY KEY,
barcode VARCHAR(255) NOT NULL,
linked_items INTEGER [],
shopping_lists INTEGER [],
recipes INTEGER [],
groups INTEGER [],
packaging VARCHAR(255),
uom VARCHAR(255),
cost FLOAT8,
safety_stock FLOAT8,
lead_time_days FLOAT8,
ai_pick BOOLEAN,
UNIQUE(barcode)
);

View File

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS itemlinks (
id SERIAL PRIMARY KEY,
barcode VARCHAR(255) NOt NULL,
link INTEGER NOT NULL,
data JSONB NOT NULL,
conv_factor FLOAT8 NOt NULL,
UNIQUE(barcode)
);

16
sql/create/logins.sql Normal file
View File

@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS logins(
id SERIAL PRIMARY KEY,
username VARCHAR(255),
password VARCHAR(255),
favorites JSONB,
unseen_pantry_items INTEGER [],
unseen_groups INTEGER [],
unseen_shopping_lists INTEGER [],
unseen_recipes INTEGER [],
seen_pantry_items INTEGER [],
seen_groups INTEGER[],
seen_shopping_lists INTEGER [],
seen_recipes INTEGER [],
flags JSONB
);

View File

@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS Transactions (
id SERIAL PRIMARY KEY,
timestamp TIMESTAMP,
barcode VARCHAR(255) NOT NULL,
name VARCHAR(255),
transaction_type VARCHAR(255) NOT NULL,
quantity FLOAT8 NOT NULL,
description TEXT,
user_id INTEGER NOT NULL,
data JSONB
);

View File

@ -1,20 +0,0 @@
CREATE TABLE brand(
id SERIAL PRIMARY KEY,
brand_name VARCHAR(255),
brand_website TEXT
;)
CREATE TABLE item_info(
id SERIAL PRIMARY KEY,
item_description TEXT,
item_cost FLOAT,
item_packaging VARCHAR(255),
item_safety_stock FLOAT,
;)
CREATE TABLE item(
id SERIAL PRIMARY KEY,
item_name VARCHAR(255),
brand_id INTEGER REFRENCES brand (id),
item_info INTEGER REFRENCES item_info (id),
)

1
sql/drop/item_info.sql Normal file
View File

@ -0,0 +1 @@
DROP TABLE item_info CASCADE;

1
sql/drop/items.sql Normal file
View File

@ -0,0 +1 @@
DROP TABLE items CASCADE;