File size: 2,123 Bytes
0cac9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Rich table rendering helpers used by SQL preview and execution flows."""

from __future__ import annotations

from typing import Any

import pandas as pd
from rich.console import Console
from rich import box
from rich.table import Table


def _format_cell(value: Any) -> str:
	"""Normalize values for compact terminal table rendering."""
	if value is None:
		return ""
	# Keep table output clean for missing numeric values.
	if pd.isna(value):
		return ""
	return str(value)


def _is_delete_statement(statement: str) -> bool:
	"""Return True when SQL statement starts with DELETE."""
	return statement.lstrip().lower().startswith("delete")


def print_sql_statements_table(sql_statements: list[str], console: Console) -> None:
	"""Render the SQL statement list as a numbered Rich table."""
	table = Table(
		title="SQL statements to execute",
		show_lines=True,
		box=box.ROUNDED,
		border_style="bright_magenta",
		title_style="bold magenta",
	)
	table.add_column("#", style="cyan", no_wrap=True)
	table.add_column("Statement", style="white", header_style="bold blue")

	has_delete_statement = False

	for index, statement in enumerate(sql_statements, start=1):
		if _is_delete_statement(statement):
			has_delete_statement = True
			table.add_row(str(index), f"[bold red]{statement}[/bold red]")
		else:
			table.add_row(str(index), statement)

	console.print(table)
	if has_delete_statement:
		console.print("[bold red]Warning:[/bold red] DELETE statements will remove rows from the selected table PERMANENTLY.")


def print_dataframe_as_table(df: pd.DataFrame, console: Console, title: str = "Query result") -> None:
	"""Render a DataFrame as a Rich table with folded columns."""
	table = Table(
		title=title,
		show_lines=True,
		box=box.ROUNDED,
		border_style="bright_blue",
		title_style="bold cyan",
	)

	for column_name in df.columns:
		table.add_column(str(column_name), overflow="fold", header_style="bold blue")

	for row in df.itertuples(index=False, name=None):
		table.add_row(*[_format_cell(value) for value in row])

	if df.empty:
		console.print("No rows returned.")
		return

	console.print(table)