kamau1 commited on
Commit
a538f32
·
verified ·
1 Parent(s): 57a7a21

Upload 12 files

Browse files
Files changed (1) hide show
  1. app/utils/db_http.py +66 -14
app/utils/db_http.py CHANGED
@@ -361,14 +361,19 @@ def select_records(table: str, columns: List[str] = None, condition: str = None,
361
  value = row[i]["value"]
362
 
363
  # Convert value based on type
364
- if row[i]["type"] == "integer":
365
- value = int(value)
366
- elif row[i]["type"] == "float":
367
- value = float(value)
368
- elif row[i]["type"] == "null":
369
- value = None
370
-
371
- record[col_name] = value
 
 
 
 
 
372
 
373
  records.append(record)
374
 
@@ -397,15 +402,62 @@ def get_record_by_id(table: str, id: int, columns: List[str] = None, operation_i
397
  if operation_id is None:
398
  operation_id = f"get_by_id_{int(time.time())}"
399
 
400
- condition = "id = ?"
401
- condition_params = [{"type": "integer", "value": str(id)}]
 
 
 
402
 
403
- records = select_records(table, columns, condition, condition_params, limit=1, operation_id=operation_id)
 
 
404
 
405
- if records:
406
- return records[0]
 
 
407
 
408
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
 
410
  def count_records(table: str, condition: str = None, condition_params: List[Dict[str, Any]] = None, operation_id: str = None) -> int:
411
  """
 
361
  value = row[i]["value"]
362
 
363
  # Convert value based on type
364
+ try:
365
+ if row[i]["type"] == "integer":
366
+ value = int(value)
367
+ elif row[i]["type"] == "float":
368
+ value = float(value)
369
+ elif row[i]["type"] == "null":
370
+ value = None
371
+
372
+ record[col_name] = value
373
+ except (KeyError, TypeError, ValueError) as e:
374
+ # Handle missing or invalid values
375
+ logger.warning(f"Error converting value for column {col_name}: {str(e)}")
376
+ record[col_name] = None
377
 
378
  records.append(record)
379
 
 
402
  if operation_id is None:
403
  operation_id = f"get_by_id_{int(time.time())}"
404
 
405
+ try:
406
+ condition = "id = ?"
407
+ condition_params = [{"type": "integer", "value": str(id)}]
408
+
409
+ records = select_records(table, columns, condition, condition_params, limit=1, operation_id=operation_id)
410
 
411
+ if records and len(records) > 0:
412
+ logger.debug(f"[{operation_id}] Found record by ID {id}: {records[0]}")
413
+ return records[0]
414
 
415
+ logger.warning(f"[{operation_id}] No record found with ID {id} in table {table}")
416
+ return None
417
+ except Exception as e:
418
+ logger.error(f"[{operation_id}] Error getting record by ID {id}: {str(e)}")
419
 
420
+ # Try a direct query as a fallback
421
+ try:
422
+ logger.info(f"[{operation_id}] Trying direct query as fallback")
423
+ cols = "*" if not columns else ", ".join(columns)
424
+ sql = f"SELECT {cols} FROM {table} WHERE id = ? LIMIT 1"
425
+
426
+ result = execute_query(sql, [{"type": "integer", "value": str(id)}], operation_id=f"{operation_id}_fallback")
427
+
428
+ if "results" in result and len(result["results"]) > 0 and result["results"][0]["type"] == "ok":
429
+ response = result["results"][0]["response"]
430
+ if "result" in response and "rows" in response["result"] and len(response["result"]["rows"]) > 0:
431
+ row = response["result"]["rows"][0]
432
+ cols = response["result"]["cols"]
433
+
434
+ record = {}
435
+ for i, col in enumerate(cols):
436
+ try:
437
+ col_name = col["name"]
438
+ value = row[i]["value"]
439
+
440
+ # Convert value based on type
441
+ if row[i]["type"] == "integer":
442
+ value = int(value)
443
+ elif row[i]["type"] == "float":
444
+ value = float(value)
445
+ elif row[i]["type"] == "null":
446
+ value = None
447
+
448
+ record[col_name] = value
449
+ except (KeyError, TypeError, ValueError) as e:
450
+ logger.warning(f"[{operation_id}] Error converting value for column {col['name'] if 'name' in col else i}: {str(e)}")
451
+ continue
452
+
453
+ logger.info(f"[{operation_id}] Found record by ID {id} using fallback: {record}")
454
+ return record
455
+
456
+ logger.warning(f"[{operation_id}] No record found with ID {id} using fallback")
457
+ return None
458
+ except Exception as e2:
459
+ logger.error(f"[{operation_id}] Error in fallback query: {str(e2)}")
460
+ return None
461
 
462
  def count_records(table: str, condition: str = None, condition_params: List[Dict[str, Any]] = None, operation_id: str = None) -> int:
463
  """