Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -389,32 +389,49 @@ def import_zip_of_csvs_to_sqlite(db_path: str, zip_bytes: bytes) -> None:
|
|
| 389 |
# ======================================================
|
| 390 |
|
| 391 |
def introspect_sqlite_schema(db_path: str) -> Dict[str, Any]:
|
| 392 |
-
"""
|
| 393 |
-
Devuelve:
|
| 394 |
-
- tables: {table_name: {"columns": [col1, col2, ...]}}
|
| 395 |
-
- schema_str: "table(col1, col2) ; table2(...)"
|
| 396 |
-
"""
|
| 397 |
if not os.path.exists(db_path):
|
| 398 |
raise FileNotFoundError(f"SQLite no encontrado: {db_path}")
|
| 399 |
|
| 400 |
conn = sqlite3.connect(db_path)
|
| 401 |
cur = conn.cursor()
|
|
|
|
|
|
|
| 402 |
cur.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
| 403 |
tables = [row[0] for row in cur.fetchall()]
|
| 404 |
|
| 405 |
-
tables_info
|
|
|
|
|
|
|
| 406 |
parts = []
|
| 407 |
|
| 408 |
for t in tables:
|
|
|
|
| 409 |
cur.execute(f"PRAGMA table_info('{t}');")
|
| 410 |
-
rows = cur.fetchall()
|
| 411 |
cols = [r[1] for r in rows]
|
| 412 |
tables_info[t] = {"columns": cols}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
parts.append(f"{t}(" + ", ".join(cols) + ")")
|
| 414 |
|
| 415 |
conn.close()
|
| 416 |
schema_str = " ; ".join(parts) if parts else "(empty_schema)"
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
|
| 420 |
def execute_sqlite(db_path: str, sql: str) -> Dict[str, Any]:
|
|
|
|
| 389 |
# ======================================================
|
| 390 |
|
| 391 |
def introspect_sqlite_schema(db_path: str) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
if not os.path.exists(db_path):
|
| 393 |
raise FileNotFoundError(f"SQLite no encontrado: {db_path}")
|
| 394 |
|
| 395 |
conn = sqlite3.connect(db_path)
|
| 396 |
cur = conn.cursor()
|
| 397 |
+
|
| 398 |
+
# --- TABLAS
|
| 399 |
cur.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
| 400 |
tables = [row[0] for row in cur.fetchall()]
|
| 401 |
|
| 402 |
+
tables_info = {}
|
| 403 |
+
foreign_keys = [] # <--- nuevo
|
| 404 |
+
|
| 405 |
parts = []
|
| 406 |
|
| 407 |
for t in tables:
|
| 408 |
+
# Columnas
|
| 409 |
cur.execute(f"PRAGMA table_info('{t}');")
|
| 410 |
+
rows = cur.fetchall()
|
| 411 |
cols = [r[1] for r in rows]
|
| 412 |
tables_info[t] = {"columns": cols}
|
| 413 |
+
|
| 414 |
+
# Relaciones FK
|
| 415 |
+
cur.execute(f"PRAGMA foreign_key_list('{t}');")
|
| 416 |
+
fks = cur.fetchall()
|
| 417 |
+
for (id, seq, table, from_col, to_col, on_update, on_delete, match) in fks:
|
| 418 |
+
foreign_keys.append({
|
| 419 |
+
"from_table": t,
|
| 420 |
+
"from_column": from_col,
|
| 421 |
+
"to_table": table,
|
| 422 |
+
"to_column": to_col
|
| 423 |
+
})
|
| 424 |
+
|
| 425 |
parts.append(f"{t}(" + ", ".join(cols) + ")")
|
| 426 |
|
| 427 |
conn.close()
|
| 428 |
schema_str = " ; ".join(parts) if parts else "(empty_schema)"
|
| 429 |
+
|
| 430 |
+
return {
|
| 431 |
+
"tables": tables_info,
|
| 432 |
+
"foreign_keys": foreign_keys, # <--- nuevo
|
| 433 |
+
"schema_str": schema_str
|
| 434 |
+
}
|
| 435 |
|
| 436 |
|
| 437 |
def execute_sqlite(db_path: str, sql: str) -> Dict[str, Any]:
|