mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
fixed get_table_json
This would throw a subsequent error
['[Database] - get_table_as_json ERROR:', TypeError("'NoneType' object is not iterable")]
This commit is contained in:
@@ -8,6 +8,7 @@ sys.path.extend([f"{INSTALL_PATH}/server"])
|
|||||||
from helper import if_byte_then_to_str
|
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.
|
# 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')
|
return conditions.get(device_status, 'WHERE 1=0')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# Creates a JSON-like dictionary from a database row
|
# Creates a JSON-like dictionary from a database row
|
||||||
def row_to_json(names, row):
|
def row_to_json(names, row):
|
||||||
@@ -71,6 +71,7 @@ def row_to_json(names, row):
|
|||||||
|
|
||||||
return rowEntry
|
return rowEntry
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def sanitize_SQL_input(val):
|
def sanitize_SQL_input(val):
|
||||||
"""
|
"""
|
||||||
@@ -116,7 +117,6 @@ def get_date_from_period(period):
|
|||||||
return period_sql
|
return period_sql
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def print_table_schema(db, table):
|
def print_table_schema(db, table):
|
||||||
"""
|
"""
|
||||||
@@ -195,16 +195,11 @@ def get_table_json(sql, sql_query):
|
|||||||
sql.execute(sql_query)
|
sql.execute(sql_query)
|
||||||
column_names = [col[0] for col in sql.description]
|
column_names = [col[0] for col in sql.description]
|
||||||
rows = sql.fetchall()
|
rows = sql.fetchall()
|
||||||
|
data = [row_to_json(column_names, row) for row in rows] if rows else []
|
||||||
|
return json_obj({"data": data}, column_names if rows else [])
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
mylog('verbose', ['[Database] - SQL ERROR: ', e])
|
mylog('verbose', ['[Database] - SQL ERROR: ', e])
|
||||||
return json_obj({}, []) # return empty object
|
return json_obj({"data": []}, []) # return empty object
|
||||||
|
|
||||||
if (rows):
|
|
||||||
result = {"data": [row_to_json(column_names, row) for row in rows]}
|
|
||||||
return json_obj(result, column_names)
|
|
||||||
else:
|
|
||||||
result = {"data": []}
|
|
||||||
return json_obj(result, column_names)
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user