Skip to content

Commit

Permalink
Type R in a row-list buffer to prompt to rename current column
Browse files Browse the repository at this point in the history
  • Loading branch information
emarsden committed Aug 2, 2024
1 parent 32eabd0 commit 1685057
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ C-u ! rev
C-u ! tr '[:upper:]' '[:lower:']
```

- Typing `R` in a row-list table will rename the current table column. The new name will be read
from the minibuffer.

- New customizable variable `pgmacs-enable-query-logging` specifies whether SQL queries should be
logged to a dedicated buffer. The default value is false.

Expand Down
1 change: 1 addition & 0 deletions doc/src/editing.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The following keys are bound when the point is located in the row-list table:
| `k` | Copy the current row. |
| `y` | Paste (yank) the copied row. |
| `j` | Copy the current row to the kill ring in JSON format. |
| `R` | Rename the current column. |
| `<` | Move point to the first row of data. |
| `>` | Move point to the last row of data. |
| number | Move point to the nth column (numbering is zero-based). |
Expand Down
25 changes: 22 additions & 3 deletions pgmacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,8 @@ Table names are schema-qualified if the schema is non-default."
(shw "k" "Copy the row at point")
(shw "y" "Yank the previously copied row and insert into the table")
(shw "j" "Copy the current row to the kill-ring in JSON format")
(shw "!" "Run a shell command on the value of the current shell")
(shw "R" "Rename the current column")
(shw "!" "Run a shell command on the value of the current cell")
(shw "n" "Next page of output (if table contents are paginated)")
(shw "p" "Previous page of output (if table contents are paginated)")
(shw "e" "New buffer with output from SQL query")
Expand All @@ -1320,6 +1321,20 @@ Table names are schema-qualified if the schema is non-default."
(shrink-window-if-larger-than-buffer)
(goto-char (point-min)))))

(defun pgmacs--row-list-rename-column (&rest _ignore)
"Rename the current PostgreSQL column."
(let* ((col-id (pgmacstbl-current-column))
(cols (pgmacstbl-columns (pgmacstbl-current-table)))
(col (nth col-id cols))
(col-name (pgmacstbl-column-name col))
(new (read-string (format "Rename column %s to: " col-name)))
(sql (format "ALTER TABLE %s RENAME %s TO %s"
(pg-escape-identifier pgmacs--table)
(pg-escape-identifier col-name)
(pg-escape-identifier new)))
(res (pg-exec pgmacs--con sql)))
(pgmacs--notify "%s" (pg-result res :status))
(pgmacs--display-table pgmacs--table)))

;; Select row-count values from table "around" (ordered by pk) the row where pk=value.
;; center-on is a list of the form (pk pk-value pk-type)
Expand Down Expand Up @@ -1445,6 +1460,7 @@ value, in the limit of pgmacs-row-limit."
"DEL" (lambda (row) (pgmacs--delete-row row ',primary-keys))
"TAB" (lambda (_row) (pgmacstbl-next-column))
"<backtab>" (lambda (_row) (pgmacstbl-previous-column))
"R" pgmacs--row-list-rename-column
"h" pgmacs--row-list-help
"?" pgmacs--row-list-help
"o" pgmacs-open-table
Expand Down Expand Up @@ -1962,8 +1978,9 @@ Uses PostgreSQL connection CON."
"o" pgmacs-open-table
"e" pgmacs-run-sql
"E" pgmacs-run-buffer-sql
;; the functions pgmacstbl-beginning-of-table and pgmacstbl-end-of-table don't work when
;; we have inserted text before the pgmacstbl.
;; the functions pgmacstbl-beginning-of-table and
;; pgmacstbl-end-of-table don't work when we have inserted text before
;; the pgmacstbl.
"<" (lambda (&rest _ignored)
(text-property-search-backward 'pgmacstbl)
(next-line))
Expand Down Expand Up @@ -2048,6 +2065,8 @@ CONNECTION-URI is a PostgreSQL connection URI of the form
;; and the more sophisticated Bitnami PostgreSQL image
;;
;; https://registry.hub.docker.com/r/bitnami/postgresql#!
;;
;; and from libpq/psql.
;;;###autoload
(defun pgmacs ()
"Open a widget-based login buffer for PostgreSQL.
Expand Down

0 comments on commit 1685057

Please sign in to comment.