Upload 62 files
Browse files- app/ai_assistant.py +22 -0
- app/query_parser.py +17 -4
app/ai_assistant.py
CHANGED
|
@@ -451,6 +451,17 @@ class AIAssistant:
|
|
| 451 |
|
| 452 |
def _create_sample_database(self, user_store, database: str) -> Dict:
|
| 453 |
"""Create multiple sample tables with data in a database"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
tables_to_create = [
|
| 455 |
('customers', [
|
| 456 |
(1, 'John Doe', 'john@example.com', '555-0101', '123 Main St'),
|
|
@@ -524,6 +535,17 @@ class AIAssistant:
|
|
| 524 |
def _create_sample_table(self, user_store, database: str, table: str) -> Dict:
|
| 525 |
"""Create a sample table with realistic data based on table name"""
|
| 526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
# Detect context from table name
|
| 528 |
table_lower = table.lower()
|
| 529 |
|
|
|
|
| 451 |
|
| 452 |
def _create_sample_database(self, user_store, database: str) -> Dict:
|
| 453 |
"""Create multiple sample tables with data in a database"""
|
| 454 |
+
# Create database if it doesn't exist
|
| 455 |
+
from app.metadata import metadata
|
| 456 |
+
dbs = metadata.list_databases(user_store)
|
| 457 |
+
db_list = dbs if isinstance(dbs, list) else dbs.get('databases', [])
|
| 458 |
+
db_names_list = [d['name'] if isinstance(d, dict) else d for d in db_list]
|
| 459 |
+
|
| 460 |
+
if database not in db_names_list:
|
| 461 |
+
result = metadata.create_database(database, user_store)
|
| 462 |
+
if not result.get('ok'):
|
| 463 |
+
return {'type': 'error', 'message': f'Failed to create database: {result.get("error")}'}
|
| 464 |
+
|
| 465 |
tables_to_create = [
|
| 466 |
('customers', [
|
| 467 |
(1, 'John Doe', 'john@example.com', '555-0101', '123 Main St'),
|
|
|
|
| 535 |
def _create_sample_table(self, user_store, database: str, table: str) -> Dict:
|
| 536 |
"""Create a sample table with realistic data based on table name"""
|
| 537 |
|
| 538 |
+
# Create database if it doesn't exist
|
| 539 |
+
from app.metadata import metadata
|
| 540 |
+
dbs = metadata.list_databases(user_store)
|
| 541 |
+
db_list = dbs if isinstance(dbs, list) else dbs.get('databases', [])
|
| 542 |
+
db_names_list = [d['name'] if isinstance(d, dict) else d for d in db_list]
|
| 543 |
+
|
| 544 |
+
if database not in db_names_list:
|
| 545 |
+
result = metadata.create_database(database, user_store)
|
| 546 |
+
if not result.get('ok'):
|
| 547 |
+
return {'type': 'error', 'message': f'Failed to create database: {result.get("error")}'}
|
| 548 |
+
|
| 549 |
# Detect context from table name
|
| 550 |
table_lower = table.lower()
|
| 551 |
|
app/query_parser.py
CHANGED
|
@@ -126,10 +126,23 @@ class QueryParser:
|
|
| 126 |
|
| 127 |
def extract_tables(self, sql: str) -> List[str]:
|
| 128 |
"""Extract table names from SQL query"""
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
def build_safe_query(self, database: str, table: str, filters: Dict = None,
|
| 135 |
limit: int = None, offset: int = None) -> str:
|
|
|
|
| 126 |
|
| 127 |
def extract_tables(self, sql: str) -> List[str]:
|
| 128 |
"""Extract table names from SQL query"""
|
| 129 |
+
tables = set()
|
| 130 |
+
# FROM and JOIN clauses
|
| 131 |
+
for m in re.findall(r'(?:FROM|JOIN)\s+["\']?(\w+\.\w+)["\']?', sql, re.IGNORECASE):
|
| 132 |
+
tables.add(m)
|
| 133 |
+
# CREATE TABLE
|
| 134 |
+
for m in re.findall(r'CREATE\s+(?:OR\s+REPLACE\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["\']?(\w+\.\w+)["\']?', sql, re.IGNORECASE):
|
| 135 |
+
tables.add(m)
|
| 136 |
+
# INSERT INTO
|
| 137 |
+
for m in re.findall(r'INSERT\s+INTO\s+["\']?(\w+\.\w+)["\']?', sql, re.IGNORECASE):
|
| 138 |
+
tables.add(m)
|
| 139 |
+
# UPDATE
|
| 140 |
+
for m in re.findall(r'UPDATE\s+["\']?(\w+\.\w+)["\']?', sql, re.IGNORECASE):
|
| 141 |
+
tables.add(m)
|
| 142 |
+
# DELETE FROM
|
| 143 |
+
for m in re.findall(r'DELETE\s+FROM\s+["\']?(\w+\.\w+)["\']?', sql, re.IGNORECASE):
|
| 144 |
+
tables.add(m)
|
| 145 |
+
return [self.sanitize_identifier(m) for m in tables]
|
| 146 |
|
| 147 |
def build_safe_query(self, database: str, table: str, filters: Dict = None,
|
| 148 |
limit: int = None, offset: int = None) -> str:
|