Skip to content

Commit 0ea1f6e

Browse files
peteremhagander
authored andcommitted
psql: Let \l accept a pattern
reviewed by Satoshi Nagayasu
1 parent 54d6706 commit 0ea1f6e

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,12 +1745,13 @@ hello 10
17451745

17461746

17471747
<varlistentry>
1748-
<term><literal>\l</literal> (or <literal>\list</literal>)</term>
1749-
<term><literal>\l+</literal> (or <literal>\list+</literal>)</term>
1748+
<term><literal>\l[+]</literal> or <literal>\list[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
17501749
<listitem>
17511750
<para>
1752-
List the names, owners, character set encodings, and access privileges
1753-
of all the databases in the server.
1751+
List the databases in the server and show their names, owners,
1752+
character set encodings, and access privileges.
1753+
If <replaceable class="parameter">pattern</replaceable> is specified,
1754+
only databases whose names match the pattern are listed.
17541755
If <literal>+</literal> is appended to the command name, database
17551756
sizes, default tablespaces, and descriptions are also displayed.
17561757
(Size information is only available for databases that the current

src/bin/psql/command.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,22 @@ exec_command(const char *cmd,
821821
}
822822

823823
/* \l is list databases */
824-
else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0)
825-
success = listAllDbs(false);
826-
else if (strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
827-
success = listAllDbs(true);
824+
else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
825+
strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
826+
{
827+
char *pattern;
828+
bool show_verbose;
829+
830+
pattern = psql_scan_slash_option(scan_state,
831+
OT_NORMAL, NULL, true);
832+
833+
show_verbose = strchr(cmd, '+') ? true : false;
834+
835+
success = listAllDbs(pattern, show_verbose);
836+
837+
if (pattern)
838+
free(pattern);
839+
}
828840

829841
/*
830842
* large object things

src/bin/psql/describe.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ describeOperators(const char *pattern, bool showSystem)
641641
* for \l, \list, and -l switch
642642
*/
643643
bool
644-
listAllDbs(bool verbose)
644+
listAllDbs(const char *pattern, bool verbose)
645645
{
646646
PGresult *res;
647647
PQExpBufferData buf;
@@ -684,6 +684,11 @@ listAllDbs(bool verbose)
684684
if (verbose && pset.sversion >= 80000)
685685
appendPQExpBuffer(&buf,
686686
" JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n");
687+
688+
if (pattern)
689+
processSQLNamePattern(pset.db, &buf, pattern, false, false,
690+
NULL, "d.datname", NULL, NULL);
691+
687692
appendPQExpBuffer(&buf, "ORDER BY 1;");
688693
res = PSQLexec(buf.data, false);
689694
termPQExpBuffer(&buf);

src/bin/psql/describe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern bool listTSDictionaries(const char *pattern, bool verbose);
5555
extern bool listTSTemplates(const char *pattern, bool verbose);
5656

5757
/* \l */
58-
extern bool listAllDbs(bool verbose);
58+
extern bool listAllDbs(const char *pattern, bool verbose);
5959

6060
/* \dt, \di, \ds, \dS, etc. */
6161
extern bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem);

src/bin/psql/help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ slashUsage(unsigned short int pager)
235235
fprintf(output, _(" \\dE[S+] [PATTERN] list foreign tables\n"));
236236
fprintf(output, _(" \\dx[+] [PATTERN] list extensions\n"));
237237
fprintf(output, _(" \\dy [PATTERN] list event triggers\n"));
238-
fprintf(output, _(" \\l[+] list all databases\n"));
238+
fprintf(output, _(" \\l[+] [PATTERN] list databases\n"));
239239
fprintf(output, _(" \\sf[+] FUNCNAME show a function's definition\n"));
240240
fprintf(output, _(" \\z [PATTERN] same as \\dp\n"));
241241
fprintf(output, "\n");

src/bin/psql/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ main(int argc, char *argv[])
260260
if (!options.no_psqlrc)
261261
process_psqlrc(argv[0]);
262262

263-
success = listAllDbs(false);
263+
success = listAllDbs(NULL, false);
264264
PQfinish(pset.db);
265265
exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
266266
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy