From 7ae046092b9596a1d7838f5b26b4cb03fecb2c50 Mon Sep 17 00:00:00 2001 From: Jadowyne Ulve Date: Wed, 25 Sep 2024 21:21:22 -0500 Subject: [PATCH] created and added all create sql created and added drop sql for items & item_info --- main.py | 99 ++++++++++++++++++++++++------------- sql/create/brands.sql | 4 ++ sql/create/food_info.sql | 7 +++ sql/create/groups.sql | 8 +++ sql/create/item.sql | 24 +++++++++ sql/create/item_info.sql | 15 ++++++ sql/create/linked_items.sql | 8 +++ sql/create/logins.sql | 16 ++++++ sql/create/transactions.sql | 11 +++++ sql/create_table.sql | 20 -------- sql/drop/item_info.sql | 1 + sql/drop/items.sql | 1 + 12 files changed, 160 insertions(+), 54 deletions(-) create mode 100644 sql/create/brands.sql create mode 100644 sql/create/food_info.sql create mode 100644 sql/create/groups.sql create mode 100644 sql/create/item.sql create mode 100644 sql/create/item_info.sql create mode 100644 sql/create/linked_items.sql create mode 100644 sql/create/logins.sql create mode 100644 sql/create/transactions.sql delete mode 100644 sql/create_table.sql create mode 100644 sql/drop/item_info.sql create mode 100644 sql/drop/items.sql diff --git a/main.py b/main.py index c9db9d5..3b2f1d3 100644 --- a/main.py +++ b/main.py @@ -21,14 +21,15 @@ def insert_row(table_name, name): finally: return id -def create_table(table_name): +def create_table(sql_file: str): conn = None try: params = config() conn = psycopg2.connect(**params) 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() conn.commit() @@ -38,38 +39,68 @@ def create_table(table_name): if conn is not None: conn.close() -def connect(): - """ Connect to the PostgreSQL database server """ - conn = None - try: - # read connection parameters - params = config() +def add_item(barcode: str, name: str): + sql = f"INSERT INTO item_info(barcode) VALUES ('{barcode}') RETURNING id;" + database_config = config() + item_info_id = None + with psycopg2.connect(**database_config) as conn: + 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 - print('Connecting to the PostgreSQL database...') - conn = psycopg2.connect(**params) + 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 *;" + row = None + 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 - cur = conn.cursor() - - # execute a statement - print('PostgreSQL database version:') - cur.execute('SELECT version()') + conn.commit() + return True + +if __name__ == '__main__': + drop_table('sql/drop/item_info.sql') + 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 - db_version = cur.fetchone() - 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')) \ No newline at end of file + row = add_item(barcode='1237', name='test237') + print(row) \ No newline at end of file diff --git a/sql/create/brands.sql b/sql/create/brands.sql new file mode 100644 index 0000000..b4b4580 --- /dev/null +++ b/sql/create/brands.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS brands ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) +); \ No newline at end of file diff --git a/sql/create/food_info.sql b/sql/create/food_info.sql new file mode 100644 index 0000000..0c7f9e2 --- /dev/null +++ b/sql/create/food_info.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS food_info ( + id SERIAL PRIMARY KEY, + food_groups TEXT [], + ingrediants TEXT [], + nutrients JSONB, + exires BOOLEAN +); \ No newline at end of file diff --git a/sql/create/groups.sql b/sql/create/groups.sql new file mode 100644 index 0000000..be03cc0 --- /dev/null +++ b/sql/create/groups.sql @@ -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) +); \ No newline at end of file diff --git a/sql/create/item.sql b/sql/create/item.sql new file mode 100644 index 0000000..13e5831 --- /dev/null +++ b/sql/create/item.sql @@ -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) +); diff --git a/sql/create/item_info.sql b/sql/create/item_info.sql new file mode 100644 index 0000000..444104a --- /dev/null +++ b/sql/create/item_info.sql @@ -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) +); \ No newline at end of file diff --git a/sql/create/linked_items.sql b/sql/create/linked_items.sql new file mode 100644 index 0000000..825551b --- /dev/null +++ b/sql/create/linked_items.sql @@ -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) +); \ No newline at end of file diff --git a/sql/create/logins.sql b/sql/create/logins.sql new file mode 100644 index 0000000..f69b01b --- /dev/null +++ b/sql/create/logins.sql @@ -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 +); + diff --git a/sql/create/transactions.sql b/sql/create/transactions.sql new file mode 100644 index 0000000..70c7bd6 --- /dev/null +++ b/sql/create/transactions.sql @@ -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 +); \ No newline at end of file diff --git a/sql/create_table.sql b/sql/create_table.sql deleted file mode 100644 index 5cb5564..0000000 --- a/sql/create_table.sql +++ /dev/null @@ -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), -) \ No newline at end of file diff --git a/sql/drop/item_info.sql b/sql/drop/item_info.sql new file mode 100644 index 0000000..1a6b7e8 --- /dev/null +++ b/sql/drop/item_info.sql @@ -0,0 +1 @@ +DROP TABLE item_info CASCADE; \ No newline at end of file diff --git a/sql/drop/items.sql b/sql/drop/items.sql new file mode 100644 index 0000000..4773a52 --- /dev/null +++ b/sql/drop/items.sql @@ -0,0 +1 @@ +DROP TABLE items CASCADE; \ No newline at end of file