From a6d611889328f15f4c054774289390cefa55b85f Mon Sep 17 00:00:00 2001 From: bobo-xxx <111567133+bobo-xxx@users.noreply.github.com> Date: Sat, 18 Apr 2026 08:46:46 +0000 Subject: [PATCH] fix: handle trailing SQL comments in multiline submit Fixes two related bugs when a SQL comment follows the semicolon: - pgbuffer._is_complete(): sql.endswith(';') returned False when a trailing comment followed the semicolon. In multiline mode, pressing Enter never submitted the query. - pgexecute.run(): sql.rstrip(';') couldn't strip the semicolon because the string ended with comment text. The malformed SQL was sent to PostgreSQL via psycopg's extended query protocol. Both locations now use sqlparse.format(sql, strip_comments=True) before checking for / removing the trailing semicolon. --- pgcli/pgbuffer.py | 4 +++- pgcli/pgexecute.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pgcli/pgbuffer.py b/pgcli/pgbuffer.py index aba180c8f..dffae727c 100644 --- a/pgcli/pgbuffer.py +++ b/pgcli/pgbuffer.py @@ -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__) @@ -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) """ diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index 2864c8645..8f8331ed8 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -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() if not sql: continue