Compare commits

...

6 Commits

Author SHA1 Message Date
Jokob @NetAlertX
cb63dd1765 Merge pull request #1167 from ingoratsdorf/db-work
Some checks failed
docker / docker_dev (push) Has been cancelled
Deploy MkDocs / deploy (push) Has been cancelled
Code checks / check-url-paths (push) Has been cancelled
DB result iteration fix on empty result
2025-09-10 12:15:33 +10:00
Ingo Ratsdorf
ccec89f419 Final fix 2025-09-10 12:38:33 +12:00
Ingo Ratsdorf
7f7b0a328f Another fix to get_table_json
IIteration error is not a SQL error, so gotta catch generic errors, too
2025-09-10 12:32:23 +12:00
Ingo Ratsdorf
24eaf1e143 fixed get_table_json
This would throw a subsequent error
['[Database] - get_table_as_json ERROR:', TypeError("'NoneType' object is not iterable")]
2025-09-10 12:25:30 +12:00
Ingo Ratsdorf
2836996a21 Update server/db/db_helper.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-09-10 10:21:32 +12:00
Ingo Ratsdorf
a94c6a291e DB result iteration fix on empty result
get_table_json would throw exceptions when trying to iterate over a NONE result, ie SQL query returned empty result.
2025-09-10 09:28:45 +12:00

View File

@@ -6,7 +6,8 @@ INSTALL_PATH="/app"
sys.path.extend([f"{INSTALL_PATH}/server"])
from helper import if_byte_then_to_str
from logger import mylog
from logger import mylog
#-------------------------------------------------------------------------------
# Return the SQL WHERE clause for filtering devices based on their status.
@@ -41,7 +42,6 @@ def get_device_condition_by_status(device_status):
return conditions.get(device_status, 'WHERE 1=0')
#-------------------------------------------------------------------------------
# Creates a JSON-like dictionary from a database row
def row_to_json(names, row):
@@ -71,6 +71,7 @@ def row_to_json(names, row):
return rowEntry
#-------------------------------------------------------------------------------
def sanitize_SQL_input(val):
"""
@@ -116,7 +117,6 @@ def get_date_from_period(period):
return period_sql
#-------------------------------------------------------------------------------
def print_table_schema(db, table):
"""
@@ -193,15 +193,21 @@ def get_table_json(sql, sql_query):
"""
try:
sql.execute(sql_query)
column_names = [col[0] for col in sql.description]
rows = sql.fetchall()
if (rows):
# We only return data if we actually got some out of SQLite
column_names = [col[0] for col in sql.description]
data = [row_to_json(column_names, row) for row in rows]
return json_obj({"data": data}, column_names)
except sqlite3.Error as e:
# SQLite error, e.g. malformed query
mylog('verbose', ['[Database] - SQL ERROR: ', e])
return json_obj({}, []) # return empty object
result = {"data": [row_to_json(column_names, row) for row in rows]}
return json_obj(result, column_names)
except Exception as e:
# Catch-all for other exceptions, e.g. iteration error
mylog('verbose', ['[Database] - Unexpected ERROR: ', e])
# In case of any error or no data, return empty object
return json_obj({"data": []}, [])
#-------------------------------------------------------------------------------
class json_obj: