From b7419a6e581c6b3e2491490a9e16baba8a7faa14 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Wed, 31 May 2023 21:42:07 +0200 Subject: [PATCH] database improvements --- jarvis/db/create_empty_database.sql | 6 ++--- jarvis/db/db_utils.py | 36 +++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/jarvis/db/create_empty_database.sql b/jarvis/db/create_empty_database.sql index 28a161c..c9b29de 100644 --- a/jarvis/db/create_empty_database.sql +++ b/jarvis/db/create_empty_database.sql @@ -1,13 +1,13 @@ --- Create a commands table that contains all the already executed commandes and the simplified version by ChatGPT -DROP TABLE IF EXISTS queries; -CREATE TABLE queries ( +DROP TABLE IF EXISTS commands; +CREATE TABLE commands ( id INTEGER PRIMARY KEY, original_command VARCHAR NOT NULL, simplified_command VARCHAR NOT NULL, response INT NOT NULL ); -DROP TABLE IF EXISTS responses: +DROP TABLE IF EXISTS responses; CREATE TABLE responses ( id INTEGER PRIMARY KEY, response VARCHAR NOT NULL diff --git a/jarvis/db/db_utils.py b/jarvis/db/db_utils.py index 9ef3283..26e46e0 100644 --- a/jarvis/db/db_utils.py +++ b/jarvis/db/db_utils.py @@ -1,3 +1,5 @@ +import logging +import os.path import sqlite3 from pathlib import Path @@ -5,6 +7,11 @@ from pathlib import Path def create_database(): """Creates the database.""" + # Check if the database already exists + if os.path.exists(Path(__file__).parent / "jarvis-commands-memory.sqlite"): + logging.debug("Database already exists, skipping creation") + return + with open(Path(__file__).parent / "create_empty_database.sql", "r") as create_database_script: create_database_script = create_database_script.read() @@ -14,6 +21,8 @@ def create_database(): db.commit() db.close() + logging.info("Database created") + def do_request(request, args=None): """Execute SQL request and return result""" @@ -32,8 +41,27 @@ def do_request(request, args=None): return result -def add_query(original_query, simplified_query, response): - """Add a query to the database""" +def add_response(response): + """Add a response to the database""" - do_request("INSERT INTO queries (original_query, simplified_query, response) VALUES (?, ?, ?)", - (original_query, simplified_query, response)) + # Check if the response is already in the database + request = do_request("SELECT * FROM responses WHERE response=?", (response,)) + if request: + response_id = request[0][0] + logging.debug("Response already in database (id: " + str(response_id) + ")") + + # If not, add it + else: + do_request("INSERT INTO responses (response) VALUES (?)", (response,)) + response_id = do_request("SELECT * FROM responses WHERE response=?", (response,))[0][0] + logging.debug("Response added to database with id " + str(response_id) + ": " + str(response)) + + return response_id + + +def add_command(original_command, simplified_command, response): + """Add a command to the database""" + + do_request("INSERT INTO commands (original_command, simplified_command, response) VALUES (?, ?, ?)", + (original_command, simplified_command, add_response(response))) + logging.debug("Query added to database: " + original_command + " -> " + response)