Strengthens SQL requests #5

- Use 'id' to select
- Adds NOT NULL for product to avoid absurd substitut request
This commit is contained in:
Fred Z 2018-08-07 23:11:15 +02:00
parent c112134f0b
commit 18107a065f
2 changed files with 10 additions and 10 deletions

12
cli.py
View File

@ -91,13 +91,13 @@ def get_data_list(db_obj, sql):
db_obj.execute(sql)
max_id = int(db_obj.cursor.rowcount - 1)
results_list = [(idx, val['name'], val['option'])
results_list = [(idx, val['name'], val['option'], val['id'])
for idx, val in enumerate(db_obj.result)]
# Hacky results-split for rendering in 2 columns
res_even = [(idx, val['name'], val['option'])
res_even = [(idx, val['name'], val['option'], val['id'])
for idx, val in enumerate(db_obj.result) if idx % 2 == 0]
res_uneven = [(idx, val['name'], val['option'])
res_uneven = [(idx, val['name'], val['option'], val['id'])
for idx, val in enumerate(db_obj.result) if idx % 2 != 0]
# category list
results_txt = ""
@ -148,7 +148,7 @@ category_asked = ask_user(
##################
if category_asked['valid_item']:
product_list = get_data_list(
LOCAL_DB, DB_REQUEST['list_prod'].format(category_asked['item'][1])
LOCAL_DB, DB_REQUEST['list_prod'].format(category_asked['item'][3])
)
head_msg = CLI_MSG_DISCLAIMER
@ -168,7 +168,7 @@ if product_asked['valid_item']:
substitute_list = get_data_list(
LOCAL_DB,
DB_REQUEST['list_substitute'].format(
category_asked['item'][1],
category_asked['item'][3],
product_asked['item'][2]
)
)
@ -197,7 +197,7 @@ if product_asked['valid_item']:
##########################
if substit_asked['valid_item']:
LOCAL_DB.execute(DB_REQUEST['select_substitute'].format(
substit_asked['item'][1]
substit_asked['item'][3]
))
head_msg = CLI_MSG_DISCLAIMER

View File

@ -27,10 +27,10 @@ FIELD_KEPT = {
# CLI
DB_REQUEST = {
'list_cat': "SELECT c.name, COUNT(*) AS 'option' FROM category AS c JOIN product AS p ON p.category_id = c.id GROUP BY c.name ORDER BY COUNT(*) DESC;",
'list_prod': "SELECT p.name, p.nutrition_grades AS 'option' FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.name = '{}';",
'list_substitute': "SELECT p.name, p.nutrition_grades AS 'option' FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.name = '{}' AND p.nutrition_grades < '{}'",
'select_substitute': "SELECT p.*, c.name FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE p.name = '{}'",
'list_cat': "SELECT c.name, COUNT(*) AS 'option', c.id AS 'id' FROM category AS c JOIN product AS p ON p.category_id = c.id GROUP BY c.name ORDER BY COUNT(*) DESC;",
'list_prod': "SELECT p.name, p.nutrition_grades AS 'option', p.id AS 'id' FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.id = '{}' AND p.nutrition_grades IS NOT NULL;",
'list_substitute': "SELECT p.name, p.nutrition_grades AS 'option', p.id AS 'id' FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.id = '{}' AND p.nutrition_grades < '{}'",
'select_substitute': "SELECT p.*, c.name FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE p.id = '{}'",
'save_substitute': "UPDATE product SET substitute_id={} WHERE id={}",
}