Updated API CRUD and SQL SELECT related functions. They can now handle full text searching!

This commit is contained in:
Scott Idem
2023-11-29 18:24:39 -05:00
parent ce4be9f5e2
commit 6d1cc6c1ff
2 changed files with 68 additions and 3 deletions

View File

@@ -541,6 +541,8 @@ def sql_select(
field_value = None,
enabled: str|None = None, # enabled, disabled, all
hidden: str|None = None, # hidden, not_hidden, all
fulltext_qry_field_li: list|None = None, # ['field_name_1', 'field_name_2']
fulltext_qry_str: str|None = None, # 'search string'
order_by_li: dict|None = None, # {"the_field_name": "DESC"}
limit: int = 9999999,
offset: int = 0,
@@ -581,6 +583,32 @@ def sql_select(
sql_order_by = ''
log.debug(sql_order_by)
# NOTE: Version 1 of the fulltext search
# NOTE: This version works fine, but can only do one MATCH AGAINST at a time. - STI 2023-11-29
# sql_fulltext_match_against = ''
# log.debug(fulltext_qry_field_li)
# if fulltext_qry_field_li and isinstance(fulltext_qry_field_li, list) and fulltext_qry_str: # fulltext_qry_field_li should be a list
# fulltext_qry_field_string = ', '.join(fulltext_qry_field_li)
# sql_fulltext_match_against = f'AND MATCH( {fulltext_qry_field_string} ) AGAINST( :fulltext_qry_str IN BOOLEAN MODE )'
# else:
# sql_fulltext_match_against = ''
# log.debug(sql_fulltext_match_against)
# NOTE: Version 2 of the fulltext search
# NOTE: This version works well and can do multiple MATCH AGAINST at a time. - STI 2023-11-29
sql_fulltext_match_against = ''
log.debug(fulltext_qry_field_li)
if fulltext_qry_field_li and isinstance(fulltext_qry_field_li, list) and fulltext_qry_str: # fulltext_qry_field_li should be a list
log.info('Creating partial SQL string for fulltext search.')
fulltext_qry_field_li_str = []
for value in fulltext_qry_field_li:
log.debug(value)
fulltext_qry_field_li_str.append(f'MATCH( {value} ) AGAINST( :fulltext_qry_str IN BOOLEAN MODE )')
fulltext_qry_field_string = ' OR '.join(fulltext_qry_field_li_str)
sql_fulltext_match_against = f'AND ({fulltext_qry_field_string})'
log.debug(sql_fulltext_match_against)
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -670,6 +698,9 @@ def sql_select(
data = {}
data[field_name] = field_value
if sql_fulltext_match_against:
data['fulltext_qry_str'] = fulltext_qry_str
if enabled:
sql_enabled, data['enabled'] = sql_enable_part(table_name=table_name, enabled=enabled) # Reasonably safe return str
else:
@@ -697,6 +728,7 @@ def sql_select(
SELECT *
FROM `{table_name}`
WHERE `{table_name}`.{field_name} = :{field_name}
{sql_fulltext_match_against}
{sql_enabled}
{sql_hidden}
{sql_order_by}