Skip to content

Commit c21018f

Browse files
committed
feat: add \ne to edit named queries in the external editor
\ne <name> loads a named query's SQL into $EDITOR and persists the edited result back to the [named queries] section on save (creating it if new). Editing a query from namedqueries.d saves an overriding copy to the main config. Addresses the editor part of upstream issue dbcli#1430.
1 parent e24317e commit c21018f

4 files changed

Lines changed: 121 additions & 1 deletion

File tree

‎changelog.rst‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
4.5.2 (unreleased) - upstream: 4.5.0
2+
=====================================
3+
4+
Features:
5+
---------
6+
* Add ``\ne <name>`` to edit a named query in the external editor. Loads the
7+
named query's SQL into ``$EDITOR``; on save it is persisted back to the main
8+
config's ``[named queries]`` section. If the name does not exist the editor
9+
opens empty and saving creates it. Editing a query that lives in
10+
``namedqueries.d`` saves an overriding copy to the main config. Addresses the
11+
editor part of upstream issue #1430.
12+
113
4.5.1 (2026-06-25) - upstream: 4.5.0
214
=====================================
315

‎pgcli/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "4.5.1"
1+
__version__ = "4.5.2"

‎pgcli/main.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ def refresh_callback():
463463
arg_type=NO_QUERY,
464464
)
465465

466+
self.pgspecial.register(
467+
self.edit_named_query,
468+
"\\ne",
469+
"\\ne name",
470+
"Edit a named query in the external editor.",
471+
)
472+
466473
self.pgspecial.register(
467474
self.enter_restrict_mode,
468475
"\\restrict",
@@ -493,6 +500,42 @@ def toggle_verbose_errors(self, pattern, **_):
493500
def echo(self, pattern, **_):
494501
return [(None, None, None, pattern)]
495502

503+
def edit_named_query(self, pattern, **_):
504+
r"""Edit (or create) a named query in the external editor (\ne name).
505+
506+
Loads the named query's SQL into $EDITOR; on save, persists it back to
507+
the main config's [named queries] section (like \ns). If the name does
508+
not exist, the editor opens empty and saving creates it.
509+
"""
510+
name = pattern.strip()
511+
if not name:
512+
return [(None, None, None, "Usage: \\ne <name>")]
513+
514+
existing = NamedQueries.instance.get(name)
515+
source = None
516+
if hasattr(NamedQueries.instance, "get_source"):
517+
source = NamedQueries.instance.get_source(name)
518+
519+
sql, message = special.open_external_editor(sql=existing or "")
520+
if message:
521+
return [(None, None, None, message)]
522+
523+
sql = (sql or "").strip()
524+
if not sql:
525+
return [(None, None, None, f"{name}: empty query, not saved.")]
526+
if existing is not None and sql == existing.strip():
527+
return [(None, None, None, f"{name}: no changes.")]
528+
529+
NamedQueries.instance.save(name, sql)
530+
531+
if existing is None:
532+
status = f"{name}: Created"
533+
elif source == "include":
534+
status = f"{name}: Saved to main config (overrides the namedqueries.d copy)"
535+
else:
536+
status = f"{name}: Saved"
537+
return [(None, None, None, status)]
538+
496539
def reload_named_queries(self, pattern, **_):
497540
"""Reload named queries from config and namedqueries.d directory."""
498541
# Re-read config file to catch any changes

‎tests/test_main.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,71 @@ def test_reload_named_queries():
483483
assert "3 named queries" in result[0][3]
484484

485485

486+
def test_edit_named_query():
487+
"""Test \\ne edits/creates a named query via the external editor."""
488+
from pgspecial.namedqueries import NamedQueries
489+
490+
with tempfile.TemporaryDirectory() as tmpdir:
491+
config_file = os.path.join(tmpdir, "config")
492+
with open(config_file, "w") as f:
493+
f.write("[main]\n")
494+
f.write(f"log_file = {os.path.join(tmpdir, 'pgcli.log')}\n")
495+
496+
cli = PGCli(pgclirc_file=config_file)
497+
498+
# Create a new named query (editor returns SQL, no error).
499+
with mock.patch("pgcli.main.special.open_external_editor", return_value=("select 1", None)):
500+
out = cli.edit_named_query("foo")
501+
assert "Created" in out[0][3]
502+
assert NamedQueries.instance.get("foo") == "select 1"
503+
504+
# Update the existing one.
505+
with mock.patch("pgcli.main.special.open_external_editor", return_value=("select 2", None)):
506+
out = cli.edit_named_query("foo")
507+
assert "Saved" in out[0][3]
508+
assert NamedQueries.instance.get("foo") == "select 2"
509+
510+
# No changes -> not re-saved.
511+
with mock.patch("pgcli.main.special.open_external_editor", return_value=("select 2", None)):
512+
out = cli.edit_named_query("foo")
513+
assert "no changes" in out[0][3]
514+
515+
# Empty editor result -> not saved, previous value kept.
516+
with mock.patch("pgcli.main.special.open_external_editor", return_value=("", None)):
517+
out = cli.edit_named_query("foo")
518+
assert "not saved" in out[0][3]
519+
assert NamedQueries.instance.get("foo") == "select 2"
520+
521+
# Editor reported an error -> surfaced, nothing saved.
522+
with mock.patch("pgcli.main.special.open_external_editor", return_value=(None, "boom")):
523+
out = cli.edit_named_query("foo")
524+
assert out[0][3] == "boom"
525+
526+
# Missing name -> usage message.
527+
out = cli.edit_named_query("")
528+
assert "Usage" in out[0][3]
529+
530+
531+
def test_edit_named_query_overrides_include(tmpdir):
532+
"""\\ne on a query that lives in namedqueries.d saves a copy to main config."""
533+
from pgspecial.namedqueries import NamedQueries
534+
535+
config_file = str(tmpdir.join("config"))
536+
with open(config_file, "w") as f:
537+
f.write("[main]\n")
538+
f.write(f"log_file = {tmpdir.join('pgcli.log')}\n")
539+
nq_dir = tmpdir.mkdir("namedqueries.d")
540+
nq_dir.join("inc.conf").write('included = "select 100"\n')
541+
542+
cli = PGCli(pgclirc_file=config_file)
543+
assert NamedQueries.instance.get("included") == "select 100"
544+
545+
with mock.patch("pgcli.main.special.open_external_editor", return_value=("select 200", None)):
546+
out = cli.edit_named_query("included")
547+
assert "overrides" in out[0][3]
548+
assert NamedQueries.instance.get("included") == "select 200"
549+
550+
486551
def test_restrict_mode_enter():
487552
"""Test \\restrict command enters restricted mode."""
488553
with tempfile.TemporaryDirectory() as tmpdir:

0 commit comments

Comments
 (0)