Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -212,18 +212,15 @@ class PostgresManager:
|
|
| 212 |
- Reescribe public./pagila. -> schema_name.
|
| 213 |
- Respeta funciones con $$...$$ (no corta por ';' internos).
|
| 214 |
- Ignora statements peligrosos via _should_skip_statement().
|
| 215 |
-
- Ignora bloques CREATE DOMAIN ... multi-línea.
|
| 216 |
"""
|
| 217 |
|
| 218 |
in_copy = False
|
| 219 |
copy_sql = ""
|
| 220 |
copy_lines: list[str] = []
|
| 221 |
|
| 222 |
-
buffer = ""
|
| 223 |
-
in_dollar = False
|
| 224 |
-
dollar_tag = ""
|
| 225 |
-
|
| 226 |
-
in_domain_block = False # estamos dentro de un bloque CREATE DOMAIN ?
|
| 227 |
|
| 228 |
def flush_statement():
|
| 229 |
nonlocal buffer
|
|
@@ -248,14 +245,15 @@ class PostgresManager:
|
|
| 248 |
line = raw_line.rstrip("\n")
|
| 249 |
stripped = line.strip()
|
| 250 |
|
| 251 |
-
# Comentarios y líneas vacías (fuera de COPY
|
| 252 |
-
if not in_copy
|
| 253 |
if not stripped or stripped.startswith("--"):
|
| 254 |
continue
|
| 255 |
|
| 256 |
# ====== BLOQUE COPY ... FROM stdin ======
|
| 257 |
if in_copy:
|
| 258 |
if stripped == r"\.":
|
|
|
|
| 259 |
data = "\n".join(copy_lines) + "\n"
|
| 260 |
cur.copy_expert(copy_sql, io.StringIO(data))
|
| 261 |
in_copy = False
|
|
@@ -265,30 +263,18 @@ class PostgresManager:
|
|
| 265 |
copy_lines.append(line)
|
| 266 |
continue
|
| 267 |
|
| 268 |
-
#
|
| 269 |
-
if in_domain_block:
|
| 270 |
-
# Terminamos el bloque cuando encontramos una línea con solo ';'
|
| 271 |
-
if stripped == ";":
|
| 272 |
-
in_domain_block = False
|
| 273 |
-
# No añadimos nada al buffer
|
| 274 |
-
continue
|
| 275 |
-
|
| 276 |
-
# Detectar inicio de bloque DOMAIN
|
| 277 |
-
if stripped.upper().startswith("CREATE DOMAIN"):
|
| 278 |
-
in_domain_block = True
|
| 279 |
-
continue
|
| 280 |
-
|
| 281 |
-
# 👉 Primero reescribimos la línea según el schema de sesión
|
| 282 |
line = self._rewrite_line_for_schema(line, schema_name)
|
| 283 |
-
if not line.strip():
|
| 284 |
-
continue
|
| 285 |
stripped = line.strip()
|
|
|
|
|
|
|
| 286 |
|
| 287 |
-
# Detectar inicio de COPY
|
| 288 |
if stripped.upper().startswith("COPY ") and "FROM stdin" in stripped.upper():
|
|
|
|
| 289 |
flush_statement()
|
| 290 |
in_copy = True
|
| 291 |
-
copy_sql = stripped
|
| 292 |
copy_lines = []
|
| 293 |
continue
|
| 294 |
|
|
@@ -309,11 +295,9 @@ class PostgresManager:
|
|
| 309 |
if j < length and line[j] == "$":
|
| 310 |
tag = line[i : j + 1] # incluye ambos '$'
|
| 311 |
if not in_dollar:
|
| 312 |
-
# comenzamos bloque
|
| 313 |
in_dollar = True
|
| 314 |
dollar_tag = tag
|
| 315 |
else:
|
| 316 |
-
# posible fin de bloque
|
| 317 |
if tag == dollar_tag:
|
| 318 |
in_dollar = False
|
| 319 |
dollar_tag = ""
|
|
@@ -322,7 +306,6 @@ class PostgresManager:
|
|
| 322 |
|
| 323 |
# Fin de statement: ';' fuera de bloque dollar-quoted
|
| 324 |
if ch == ";" and not in_dollar:
|
| 325 |
-
# segmento hasta aquí (incluyendo ';')
|
| 326 |
segment = line[start_seg : i + 1]
|
| 327 |
buffer += segment + "\n"
|
| 328 |
flush_statement()
|
|
|
|
| 212 |
- Reescribe public./pagila. -> schema_name.
|
| 213 |
- Respeta funciones con $$...$$ (no corta por ';' internos).
|
| 214 |
- Ignora statements peligrosos via _should_skip_statement().
|
|
|
|
| 215 |
"""
|
| 216 |
|
| 217 |
in_copy = False
|
| 218 |
copy_sql = ""
|
| 219 |
copy_lines: list[str] = []
|
| 220 |
|
| 221 |
+
buffer = "" # statement acumulado
|
| 222 |
+
in_dollar = False # estamos dentro de $$...$$ ?
|
| 223 |
+
dollar_tag = "" # por ej. "$func$"
|
|
|
|
|
|
|
| 224 |
|
| 225 |
def flush_statement():
|
| 226 |
nonlocal buffer
|
|
|
|
| 245 |
line = raw_line.rstrip("\n")
|
| 246 |
stripped = line.strip()
|
| 247 |
|
| 248 |
+
# Comentarios y líneas vacías (fuera de COPY)
|
| 249 |
+
if not in_copy:
|
| 250 |
if not stripped or stripped.startswith("--"):
|
| 251 |
continue
|
| 252 |
|
| 253 |
# ====== BLOQUE COPY ... FROM stdin ======
|
| 254 |
if in_copy:
|
| 255 |
if stripped == r"\.":
|
| 256 |
+
# fin de COPY
|
| 257 |
data = "\n".join(copy_lines) + "\n"
|
| 258 |
cur.copy_expert(copy_sql, io.StringIO(data))
|
| 259 |
in_copy = False
|
|
|
|
| 263 |
copy_lines.append(line)
|
| 264 |
continue
|
| 265 |
|
| 266 |
+
# Reescribimos la línea según el schema de sesión
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
line = self._rewrite_line_for_schema(line, schema_name)
|
|
|
|
|
|
|
| 268 |
stripped = line.strip()
|
| 269 |
+
if not stripped:
|
| 270 |
+
continue
|
| 271 |
|
| 272 |
+
# Detectar inicio de COPY ahora que la línea ya está reescrita
|
| 273 |
if stripped.upper().startswith("COPY ") and "FROM stdin" in stripped.upper():
|
| 274 |
+
# Ejecutar lo que haya pendiente antes del COPY
|
| 275 |
flush_statement()
|
| 276 |
in_copy = True
|
| 277 |
+
copy_sql = stripped # ya reescrita
|
| 278 |
copy_lines = []
|
| 279 |
continue
|
| 280 |
|
|
|
|
| 295 |
if j < length and line[j] == "$":
|
| 296 |
tag = line[i : j + 1] # incluye ambos '$'
|
| 297 |
if not in_dollar:
|
|
|
|
| 298 |
in_dollar = True
|
| 299 |
dollar_tag = tag
|
| 300 |
else:
|
|
|
|
| 301 |
if tag == dollar_tag:
|
| 302 |
in_dollar = False
|
| 303 |
dollar_tag = ""
|
|
|
|
| 306 |
|
| 307 |
# Fin de statement: ';' fuera de bloque dollar-quoted
|
| 308 |
if ch == ";" and not in_dollar:
|
|
|
|
| 309 |
segment = line[start_seg : i + 1]
|
| 310 |
buffer += segment + "\n"
|
| 311 |
flush_statement()
|