Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pgcli/pgbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from prompt_toolkit.filters import Condition
from prompt_toolkit.application import get_app
from .packages.parseutils.utils import is_open_quote
import sqlparse

_logger = logging.getLogger(__name__)

Expand All @@ -12,7 +13,8 @@ def _is_complete(sql):
# A complete command is an sql statement that ends with a semicolon, unless
# there's an open quote surrounding it, as is common when writing a
# CREATE FUNCTION command
return sql.endswith(";") and not is_open_quote(sql)
sql_for_check = sqlparse.format(sql, strip_comments=True)
return sql_for_check.endswith(";") and not is_open_quote(sql)
Comment on lines +16 to +17
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlparse.format(sql, strip_comments=True) is likely to parse/rewrite the SQL and may leave trailing whitespace/newlines; using it without .strip()/.rstrip() can make endswith(';') unreliable. Also, this adds an additional sqlparse parse/format pass on top of is_open_quote(sql) (which already calls sqlparse.parse), and this Condition can be evaluated frequently in the UI; consider a cheaper approach (e.g., stripping only trailing comments/whitespace before checking for ;) to avoid performance regressions in multiline mode.

Copilot uses AI. Check for mistakes.


"""
Expand Down
2 changes: 1 addition & 1 deletion pgcli/pgexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def run(
# run each sql query
for sql in sqlarr:
# Remove spaces, eol and semi-colons.
sql = sql.rstrip(";")
sql = sqlparse.format(sql, strip_comments=True).rstrip(";")
sql = sqlparse.format(sql, strip_comments=False).strip()
Comment on lines 363 to 365
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change introduces new behavior around semicolon stripping with trailing comments, but there isn’t a test covering the specific case ...; -- trailing comment (or ...; /* trailing */) on the same line. Adding a regression test in tests/test_pgexecute.py would help ensure the query is executed successfully and that only the terminator semicolon is removed (not the comment content).

Copilot uses AI. Check for mistakes.
Comment on lines 361 to 365
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlparse.format(sql, strip_comments=True) removes all SQL comments, not just the trailing comment after a semicolon. In this function you intentionally re-add leading comments (lines 355-360) so they show up in logs, but this change strips them back out and also drops any in-query comments/hints (which some users rely on for observability or extensions like pg_hint_plan). Consider using a comment-stripped copy only to locate/remove a trailing semicolon, while keeping the original SQL (including comments) for execution/logging.

Copilot uses AI. Check for mistakes.
if not sql:
continue
Expand Down