From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Simen =?UTF-8?Q?Heggest=C3=B8yl?= Newsgroups: gmane.emacs.bugs Subject: bug#24646: [PATCH] Complete the name of PostgreSQL databases Date: Sun, 09 Oct 2016 11:55:14 +0200 Message-ID: <1476006914.21082.0@smtp.gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-hx/Dj+LRBE/6IdPylbmG" X-Trace: blaine.gmane.org 1476007009 31755 195.159.176.226 (9 Oct 2016 09:56:49 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 9 Oct 2016 09:56:49 +0000 (UTC) To: 24646@debbugs.gnu.org, michael@mauger.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Oct 09 11:56:44 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1btAq8-0004Zz-5f for geb-bug-gnu-emacs@m.gmane.org; Sun, 09 Oct 2016 11:56:16 +0200 Original-Received: from localhost ([::1]:43728 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btAq6-0006TV-KW for geb-bug-gnu-emacs@m.gmane.org; Sun, 09 Oct 2016 05:56:14 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btAq0-0006TO-5a for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:56:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1btApu-0005iK-4Q for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:56:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:43140) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btApu-0005i7-1I for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:56:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1btApt-0001rV-Sh for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:56:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Simen =?UTF-8?Q?Heggest=C3=B8yl?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 09 Oct 2016 09:56:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 24646 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org, michael@mauger.com Original-Received: via spool by submit@debbugs.gnu.org id=B.14760069357122 (code B ref -1); Sun, 09 Oct 2016 09:56:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 9 Oct 2016 09:55:35 +0000 Original-Received: from localhost ([127.0.0.1]:49330 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1btApT-0001qn-DN for submit@debbugs.gnu.org; Sun, 09 Oct 2016 05:55:35 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:56315) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1btApR-0001qZ-4E for submit@debbugs.gnu.org; Sun, 09 Oct 2016 05:55:33 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1btApK-0005HP-NE for submit@debbugs.gnu.org; Sun, 09 Oct 2016 05:55:28 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:49062) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btApK-0005GD-Jy for submit@debbugs.gnu.org; Sun, 09 Oct 2016 05:55:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47271) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btApH-0006RX-Qj for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:55:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1btApC-0005BQ-Pu for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:55:23 -0400 Original-Received: from mail-lf0-x230.google.com ([2a00:1450:4010:c07::230]:35744) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btApC-0005Al-CP for bug-gnu-emacs@gnu.org; Sun, 09 Oct 2016 05:55:18 -0400 Original-Received: by mail-lf0-x230.google.com with SMTP id l131so68695678lfl.2 for ; Sun, 09 Oct 2016 02:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:subject:to:message-id:mime-version; bh=wzv4Gyp/fSzqdowZuyx+QWR9RSGjfq6/KT4uZu6dVOs=; b=kn9uxH9SVvfqO2ks+G1vuQrXIdaWL6PfOhESkCejtQ7D5gb0vMaEgT/Gho16T+sAPj IXuF7JiKNkYvpdSGGn0HiBGJVzEZRwG/mdRg4eh+T80Q3JKLspM7xApe16r7yeAPA65w VROciYZFptFtrMnxZl5OjzmarvQxkyJ/pr41XbaXDOPFuEwC78XhUefswx77rC3IhnJt 6sgPRoRpSLsfuwKkVS4SU65M97iLPXwoXvpDFJuCzJ9mFKhKY/uTNGYHi3ThOZZitSo7 oYcPOTKmt80PbycFwdSFHMC0AZy3Ns+7/DUWRfo2pU8UJu0GddCHCoUnRyF+JllF5KQv fA3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:subject:to:message-id:mime-version; bh=wzv4Gyp/fSzqdowZuyx+QWR9RSGjfq6/KT4uZu6dVOs=; b=GSBzw5rQ2r/nj5b2Ut3caxHcW2LQehyTNp1U9wyymDUn+hG1GraLfL/2MRiJDuby+h oRnjxsAtQGXTZtGaIklc7DqOsuZ0daeX7c8wnEEp3svU6v+s9fj1z2Aqm00jQc8rTVoW CobhT5O1juE3aeFYFJQtFFkWwDa+/wbihYI2NQyRYcYDOPnd+J0fCCG0b+fn61HTdriq xEE2WgXzh/h123C/1eitamjCCQUa1Ovaatgrrbml9JLcj2DkCjN9Fuh4SFiySIufJcAS 5pTXXvg1G9QGv4KOb7R675vZgYXDwqJYP6MtK3QnAkTTPman+DDU1AXZifG3o6z4ZW7j uGvg== X-Gm-Message-State: AA6/9Rnybd/qEbwEScRgIoY7WpSDeoYJZ58YraTG+k8LeiG2/0EcK3ZOdSMwtyaPdZJ2MQ== X-Received: by 10.46.33.65 with SMTP id h62mr3402043ljh.51.1476006916802; Sun, 09 Oct 2016 02:55:16 -0700 (PDT) Original-Received: from [10.0.1.43] (228.193.9.46.customer.cdi.no. [46.9.193.228]) by smtp.gmail.com with ESMTPSA id 187sm1119085ljj.4.2016.10.09.02.55.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 09 Oct 2016 02:55:16 -0700 (PDT) X-Mailer: geary/0.11.2 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:124261 Archived-At: --=-hx/Dj+LRBE/6IdPylbmG Content-Type: text/plain; charset=utf-8; format=flowed Hi. I recently started using sql.el with PostgreSQL, and thought it would be nice if `sql-postgres' would provide completion of available PostgreSQL databases. Is something like the attached patch sufficient? -- Simen --=-hx/Dj+LRBE/6IdPylbmG Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Complete-the-name-of-PostgreSQL-databases.patch Content-Transfer-Encoding: quoted-printable >From 564096195e41ef00e09fbfbaadfe8c7bf769773f Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?Simen=3D20Heggest=3DC3=3DB8yl?=3D Date: Thu, 6 Oct 2016 20:42:53 +0200 Subject: [PATCH] Complete the name of PostgreSQL databases * lisp/progmodes/sql.el (sql-postgres-login-params): Complete database name. (sql-postgres-list-databases): New function returning a list of available PostgreSQL databases. (sql-get-login-ext): Don't require an exact match from completion. * test/lisp/progmodes/sql-tests.el: New file with tests for sql.el. --- lisp/progmodes/sql.el | 20 ++++++++++++---- test/lisp/progmodes/sql-tests.el | 50 ++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/lisp/progmodes/sql-tests.el diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el index a11d456..a1710e5 100644 --- a/lisp/progmodes/sql.el +++ b/lisp/progmodes/sql.el @@ -1072,14 +1072,26 @@ sql-postgres-options :version "20.8" :group 'SQL) =20 -(defcustom sql-postgres-login-params `((user :default ,(user-login-name)) - (database :default ,(user-login-nam= e)) - server) +(defcustom sql-postgres-login-params + `((user :default ,(user-login-name)) + (database :default ,(user-login-name) + :completion ,(completion-table-dynamic + (lambda (_) (sql-postgres-list-databases)))) + server) "List of login parameters needed to connect to Postgres." :type 'sql-login-params :version "24.1" :group 'SQL) =20 +(defun sql-postgres-list-databases () + "Return a list of available PostgreSQL databases." + (when (executable-find "psql") + (let ((res '())) + (dolist (row (process-lines "psql" "-l")) + (when (string-match "^ \\([[:alnum:]-_]+\\) +|.*" row) + (push (substring row (match-beginning 1) (match-end 1)) res))) + res))) + ;; Customization for Interbase =20 (defcustom sql-interbase-program "isql" @@ -2951,7 +2963,7 @@ sql-get-login-ext (file-name-nondirectory f))))))) =20 ((plist-member plist :completion) - (completing-read prompt-def (plist-get plist :completion) nil t + (completing-read prompt-def (plist-get plist :completion) nil nil last-value history-var default)) =20 ((plist-get plist :number) diff --git a/test/lisp/progmodes/sql-tests.el b/test/lisp/progmodes/sql-tes= ts.el new file mode 100644 index 0000000..9dc92c2 --- /dev/null +++ b/test/lisp/progmodes/sql-tests.el @@ -0,0 +1,50 @@ +;;; sql-tests.el --- Tests for sql.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2016 Simen Heggest=C3=B8yl + +;; Author: Simen Heggest=C3=B8yl +;; Keywords: + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;; + +;;; Code: + +(require 'cl-lib) +(require 'ert) +(require 'sql) + +(ert-deftest sql-tests-postgres-list-databases () + "Test that output from `psql -l' is parsed correctly." + (cl-letf + (((symbol-function 'executable-find) + (lambda (_command) t)) + ((symbol-function 'process-lines) + (lambda (_program &rest _args) + '(" List of databases = " + " Name | Owner | Encoding | Collate | Ctype = | Access privileges " + "-----------+----------+----------+-------------+-------------= +-----------------------" + " db-name-1 | foo-user | UTF8 | en_US.UTF-8 | en_US.UTF-8 = | " + " db_name_2 | foo-user | UTF8 | en_US.UTF-8 | en_US.UTF-8 = | " + "(2 rows) = ")))) + (should (equal (sort (sql-postgres-list-databases) #'string<) + '("db-name-1" "db_name_2"))))) + +(provide 'sql-tests) +;;; sql-tests.el ends here --=20 2.9.3 = --=-hx/Dj+LRBE/6IdPylbmG--