Skip to content

Commit 9b5510a

Browse files
Simplify the system services
1 parent 674fd12 commit 9b5510a

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

core/services.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
import json
3-
from typing import Optional, List, Dict, Any
2+
from datetime import datetime, date
3+
from typing import List, Dict, Any
44
import psycopg2
55
from psycopg2.extras import RealDictCursor
66
from django.conf import settings
@@ -11,22 +11,7 @@
1111

1212

1313
class DatabaseService:
14-
@staticmethod
15-
def test_connection(connection_data: Dict[str, Any]) -> Dict[str, Any]:
16-
try:
17-
conn = psycopg2.connect(
18-
host=connection_data['host'],
19-
port=connection_data['port'],
20-
database=connection_data['database'],
21-
user=connection_data['username'],
22-
password=connection_data['password'],
23-
connect_timeout=5
24-
)
25-
conn.close()
26-
return {'success': True, 'message': 'Connection successful'}
27-
except Exception as e:
28-
return {'success': False, 'message': str(e)}
29-
14+
3015
@staticmethod
3116
def get_connection(connection_data: Dict[str, Any]):
3217
return psycopg2.connect(
@@ -68,6 +53,17 @@ def get_schema(connection_data: Dict[str, Any]) -> Dict[str, Any]:
6853

6954
@staticmethod
7055
def execute_query(connection_data: Dict[str, Any], sql: str) -> Dict[str, Any]:
56+
def convert_row(row):
57+
result = {}
58+
for key, value in dict(row).items():
59+
if isinstance(value, (datetime, date)):
60+
result[key] = value.isoformat()
61+
elif isinstance(value, bytes):
62+
result[key] = value.decode('utf-8', errors='replace')
63+
else:
64+
result[key] = value
65+
return result
66+
7167
try:
7268
with DatabaseService.get_connection(connection_data) as conn:
7369
with conn.cursor() as cursor:
@@ -78,7 +74,7 @@ def execute_query(connection_data: Dict[str, Any], sql: str) -> Dict[str, Any]:
7874
return {
7975
'success': True,
8076
'columns': columns,
81-
'rows': [dict(r) for r in results],
77+
'rows': [convert_row(r) for r in results],
8278
'row_count': len(results)
8379
}
8480
else:

0 commit comments

Comments
 (0)