]> granicus.if.org Git - postgresql/commitdiff
psql: Let \l accept a pattern
authorPeter Eisentraut <peter_e@gmx.net>
Mon, 4 Mar 2013 03:17:08 +0000 (22:17 -0500)
committerMagnus Hagander <magnus@hagander.net>
Mon, 4 Mar 2013 15:17:40 +0000 (15:17 +0000)
reviewed by Satoshi Nagayasu

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/command.c
src/bin/psql/describe.c
src/bin/psql/describe.h
src/bin/psql/help.c
src/bin/psql/startup.c

index fb63845a2606b9cffb76df13f677a79f880b05db..c6347cd6f84075adb190deb62ac0437f611de6ce 100644 (file)
@@ -1745,12 +1745,13 @@ hello 10
 
 
       <varlistentry>
-        <term><literal>\l</literal> (or <literal>\list</literal>)</term>
-        <term><literal>\l+</literal> (or <literal>\list+</literal>)</term>
+        <term><literal>\l[+]</literal> or <literal>\list[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
         <listitem>
         <para>
-        List the names, owners, character set encodings, and access privileges
-        of all the databases in the server.
+        List the databases in the server and show their names, owners,
+        character set encodings, and access privileges.
+        If <replaceable class="parameter">pattern</replaceable> is specified,
+        only databases whose names match the pattern are listed.
         If <literal>+</literal> is appended to the command name, database
         sizes, default tablespaces, and descriptions are also displayed.
         (Size information is only available for databases that the current
index 3be7c442a42f05bb08f86e83c6451b225c9fcdd5..c33f9446b206ea1f6eeb68c63e7be2a2a5414432 100644 (file)
@@ -821,10 +821,22 @@ exec_command(const char *cmd,
        }
 
        /* \l is list databases */
-       else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0)
-               success = listAllDbs(false);
-       else if (strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
-               success = listAllDbs(true);
+       else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
+                        strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
+       {
+               char       *pattern;
+               bool            show_verbose;
+
+               pattern = psql_scan_slash_option(scan_state,
+                                                                                OT_NORMAL, NULL, true);
+
+               show_verbose = strchr(cmd, '+') ? true : false;
+
+               success = listAllDbs(pattern, show_verbose);
+
+               if (pattern)
+                       free(pattern);
+       }
 
        /*
         * large object things
index 217c3f5a3a9eee99e9507a21b6e862642248a209..4ce831a433523711a81f95588c839e3f4018e6ec 100644 (file)
@@ -641,7 +641,7 @@ describeOperators(const char *pattern, bool showSystem)
  * for \l, \list, and -l switch
  */
 bool
-listAllDbs(bool verbose)
+listAllDbs(const char *pattern, bool verbose)
 {
        PGresult   *res;
        PQExpBufferData buf;
@@ -684,6 +684,11 @@ listAllDbs(bool verbose)
        if (verbose && pset.sversion >= 80000)
                appendPQExpBuffer(&buf,
                   "  JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
+
+       if (pattern)
+               processSQLNamePattern(pset.db, &buf, pattern, false, false,
+                                                         NULL, "d.datname", NULL, NULL);
+
        appendPQExpBuffer(&buf, "ORDER BY 1;");
        res = PSQLexec(buf.data, false);
        termPQExpBuffer(&buf);
index 9e71a887c1d12c0d22ec294d5cc0d5a7cf1aebba..09b623751d6f7a36d9acc95522ff92d2da459b1b 100644 (file)
@@ -55,7 +55,7 @@ extern bool listTSDictionaries(const char *pattern, bool verbose);
 extern bool listTSTemplates(const char *pattern, bool verbose);
 
 /* \l */
-extern bool listAllDbs(bool verbose);
+extern bool listAllDbs(const char *pattern, bool verbose);
 
 /* \dt, \di, \ds, \dS, etc. */
 extern bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem);
index 819a20f18d431c7f08c31e857bcc6e478234b695..ccb307b791f53a09283b13bd0a9612b1a83e1a15 100644 (file)
@@ -235,7 +235,7 @@ slashUsage(unsigned short int pager)
        fprintf(output, _("  \\dE[S+] [PATTERN]      list foreign tables\n"));
        fprintf(output, _("  \\dx[+]  [PATTERN]      list extensions\n"));
        fprintf(output, _("  \\dy     [PATTERN]      list event triggers\n"));
-       fprintf(output, _("  \\l[+]                  list all databases\n"));
+       fprintf(output, _("  \\l[+]   [PATTERN]      list databases\n"));
        fprintf(output, _("  \\sf[+] FUNCNAME        show a function's definition\n"));
        fprintf(output, _("  \\z      [PATTERN]      same as \\dp\n"));
        fprintf(output, "\n");
index a59f45b8a7604851766133005acc16b25130ae88..5cb6b5f364866f08e8178f12cb578b1805b525f0 100644 (file)
@@ -260,7 +260,7 @@ main(int argc, char *argv[])
                if (!options.no_psqlrc)
                        process_psqlrc(argv[0]);
 
-               success = listAllDbs(false);
+               success = listAllDbs(NULL, false);
                PQfinish(pset.db);
                exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
        }