diff options
author | Peter Eisentraut | 2000-01-29 16:58:54 +0000 |
---|---|---|
committer | Peter Eisentraut | 2000-01-29 16:58:54 +0000 |
commit | 2b84cbb60f6ff6cb58d42dff026aaf0b2e9ca8ab (patch) | |
tree | dcd592dcfdebbc799bd225ac8bf50c0dfe1e61eb /src/bin/psql | |
parent | 7e7416bd4e221fd95701a048eaf2893ab379d9a5 (diff) |
A few minor psql enhancements
Initdb help correction
Changed end/abort to commit/rollback and changed related notices
Commented out way old printing functions in libpq
Fixed a typo in alter table / alter column
Diffstat (limited to 'src/bin/psql')
-rw-r--r-- | src/bin/psql/command.c | 36 | ||||
-rw-r--r-- | src/bin/psql/command.h | 7 | ||||
-rw-r--r-- | src/bin/psql/common.c | 4 | ||||
-rw-r--r-- | src/bin/psql/common.h | 4 | ||||
-rw-r--r-- | src/bin/psql/copy.c | 4 | ||||
-rw-r--r-- | src/bin/psql/copy.h | 4 | ||||
-rw-r--r-- | src/bin/psql/create_help.pl | 36 | ||||
-rw-r--r-- | src/bin/psql/describe.c | 491 | ||||
-rw-r--r-- | src/bin/psql/describe.h | 4 | ||||
-rw-r--r-- | src/bin/psql/help.c | 4 | ||||
-rw-r--r-- | src/bin/psql/help.h | 4 | ||||
-rw-r--r-- | src/bin/psql/input.c | 4 | ||||
-rw-r--r-- | src/bin/psql/input.h | 4 | ||||
-rw-r--r-- | src/bin/psql/large_obj.c | 8 | ||||
-rw-r--r-- | src/bin/psql/large_obj.h | 4 | ||||
-rw-r--r-- | src/bin/psql/mainloop.h | 4 | ||||
-rw-r--r-- | src/bin/psql/print.c | 4 | ||||
-rw-r--r-- | src/bin/psql/print.h | 4 | ||||
-rw-r--r-- | src/bin/psql/prompt.c | 8 | ||||
-rw-r--r-- | src/bin/psql/prompt.h | 4 | ||||
-rw-r--r-- | src/bin/psql/settings.h | 7 | ||||
-rw-r--r-- | src/bin/psql/startup.c | 24 | ||||
-rw-r--r-- | src/bin/psql/stringutils.c | 4 | ||||
-rw-r--r-- | src/bin/psql/stringutils.h | 4 | ||||
-rw-r--r-- | src/bin/psql/tab-complete.c | 4 | ||||
-rw-r--r-- | src/bin/psql/tab-complete.h | 4 | ||||
-rw-r--r-- | src/bin/psql/variables.c | 4 | ||||
-rw-r--r-- | src/bin/psql/variables.h | 4 |
28 files changed, 383 insertions, 314 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index a12d311b2e0..1441fbc0bcc 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.15 2000/01/23 01:27:37 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.16 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "command.h" @@ -361,7 +361,7 @@ exec_command(const char *cmd, switch (cmd[1]) { case '\0': - case '?': + case '+': if (options[0]) success = describeTableDetails(options[0], show_verbose); else @@ -992,12 +992,42 @@ do_connect(const char *new_dbname, const char *new_user) SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); + pset.issuper = test_superuser(PQuser(pset.db)); + return success; } /* + * Test if the given user is a database superuser. + * (Used to set up the prompt right.) + */ +bool +test_superuser(const char * username) +{ + PGresult *res; + char buf[64 + NAMEDATALEN]; + bool answer; + + if (!username) + return false; + + sprintf(buf, "SELECT usesuper FROM pg_user WHERE usename = '%.*s'", NAMEDATALEN, username); + res = PSQLexec(buf); + + answer = + (PQntuples(res)>0 && PQnfields(res)>0 + && !PQgetisnull(res,0,0) + && PQgetvalue(res,0,0) + && strcmp(PQgetvalue(res,0,0), "t")==0); + PQclear(res); + return answer; +} + + + +/* * do_edit -- handler for \e * * If you do not specify a filename, the current query buffer will be copied diff --git a/src/bin/psql/command.h b/src/bin/psql/command.h index f3a7b4d36a4..65431e9c9ec 100644 --- a/src/bin/psql/command.h +++ b/src/bin/psql/command.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/command.h,v 1.6 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/command.h,v 1.7 2000/01/29 16:58:48 petere Exp $ */ #ifndef COMMAND_H #define COMMAND_H @@ -37,6 +37,9 @@ bool process_file(char *filename); bool +test_superuser(const char * username); + +bool do_pset(const char *param, const char *value, printQueryOpt * popt, diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index c5a2e78af7d..700542cd4d1 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.10 2000/01/19 20:08:33 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.11 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "common.h" diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 08519f5a400..b4b7d93abe9 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.4 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.5 2000/01/29 16:58:48 petere Exp $ */ #ifndef COMMON_H #define COMMON_H diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index d3a5f79b5c0..d2d638c0664 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.8 2000/01/21 04:21:12 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.9 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "copy.h" diff --git a/src/bin/psql/copy.h b/src/bin/psql/copy.h index 4710a623eb7..8a57e7a66d3 100644 --- a/src/bin/psql/copy.h +++ b/src/bin/psql/copy.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/copy.h,v 1.5 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/copy.h,v 1.6 2000/01/29 16:58:48 petere Exp $ */ #ifndef COPY_H #define COPY_H diff --git a/src/bin/psql/create_help.pl b/src/bin/psql/create_help.pl index c5b0d868065..f802d5ed791 100644 --- a/src/bin/psql/create_help.pl +++ b/src/bin/psql/create_help.pl @@ -1,18 +1,22 @@ -#!/usr/bin/perl +#! /usr/bin/perl +################################################################# +# create_help.pl -- converts SGML docs to internal psql help # -# This script automatically generates the help on SQL in psql from the -# SGML docs. So far the format of the docs was consistent enough that -# this worked, but this here is my no means an SGML parser. +# Copyright 2000 by PostgreSQL Global Development Group # -# It might be a good idea that this is just done once before distribution -# so people that don't have the docs or have slightly messed up docs or -# don't have perl, etc. won't have to bother. +# $Header: /cvsroot/pgsql/src/bin/psql/create_help.pl,v 1.3 2000/01/29 16:58:48 petere Exp $ +################################################################# + +# +# This script automatically generates the help on SQL in psql from +# the SGML docs. So far the format of the docs was consistent +# enough that this worked, but this here is my no means an SGML +# parser. # # Call: perl create_help.pl sql_help.h -# (Do not rely on this script to be executable.) -# The name of the header file doesn't matter to this script, but it sure -# does matter to the rest of the source. +# The name of the header file doesn't matter to this script, but it +# sure does matter to the rest of the source. # # A rule for this is also in the psql makefile. # @@ -29,9 +33,15 @@ open OUT, ">$outputfile" or die "Couldn't open output file '$outputfile': $!\n"; print OUT "/* - * This file is automatically generated from the SGML documentation. - * Direct changes here will be overwritten. + * *** Do not change this file directly. Changes will be overwritten. *** + * + * This file was generated by + * $^X $0 $outputfile + * from the DocBook documentation in + * $docdir + * */ + #ifndef $define #define $define @@ -76,7 +86,7 @@ foreach $file (sort readdir DIR) { print OUT " { \"$cmdname\",\n \"$cmddesc\",\n \"$cmdsynopsis\" },\n\n"; } else { - print STDERR "Couldn't parse file '$file'. (N='$cmdname' D='$cmddesc')\n"; + print STDERR "$0: parsing file '$file' failed at or near line $. (N='$cmdname' D='$cmddesc')\n"; } } diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index ca5dcaafb30..bb259b31d37 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.14 2000/01/20 15:29:20 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.15 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "describe.h" @@ -504,7 +504,7 @@ xmalloc(size_t size) tmp = malloc(size); if (!tmp) { - perror("malloc"); + psql_error("out of memory"); exit(EXIT_FAILURE); } return tmp; @@ -530,20 +530,20 @@ describeTableDetails(const char *name, bool desc) /* truncate table name */ if (strlen(name) > NAMEDATALEN) { - char *my_name = xmalloc(NAMEDATALEN+1); - strncpy(my_name, name, NAMEDATALEN); - my_name[NAMEDATALEN] = '\0'; - name = my_name; + char *my_name = xmalloc(NAMEDATALEN+1); + strncpy(my_name, name, NAMEDATALEN); + my_name[NAMEDATALEN] = '\0'; + name = my_name; } /* Get general table info */ sprintf(buf, - "SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules\n" - "FROM pg_class WHERE relname='%s'", - name); + "SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules\n" + "FROM pg_class WHERE relname='%s'", + name); res = PSQLexec(buf); if (!res) - return false; + return false; /* Did we get anything? */ if (PQntuples(res) == 0) @@ -567,15 +567,15 @@ describeTableDetails(const char *name, bool desc) headers[1] = "Type"; cols = 2; - if (tableinfo.relkind == 'r' || tableinfo.relkind == 's') + if (tableinfo.relkind == 'r') { - cols++; - headers[cols-1] = "Extra"; + cols++; + headers[cols-1] = "Modifier"; } if (desc) { - cols++; + cols++; headers[cols-1] = "Description"; } @@ -598,19 +598,19 @@ describeTableDetails(const char *name, bool desc) /* Check if table is a view */ if (tableinfo.hasrules) { - PGresult *result; - sprintf(buf, "SELECT definition FROM pg_views WHERE viewname = '%s'", name); - result = PSQLexec(buf); - if (!result) - { - PQclear(res); - PQclear(result); - return false; - } - - if (PQntuples(result) > 0) - view_def = xstrdup(PQgetvalue(result, 0, 0)); - PQclear(result); + PGresult *result; + sprintf(buf, "SELECT definition FROM pg_views WHERE viewname = '%s'", name); + result = PSQLexec(buf); + if (!result) + { + PQclear(res); + PQclear(result); + return false; + } + + if (PQntuples(result) > 0) + view_def = xstrdup(PQgetvalue(result, 0, 0)); + PQclear(result); } @@ -621,115 +621,137 @@ describeTableDetails(const char *name, bool desc) for (i = 0; i < PQntuples(res); i++) { int4 attypmod = atoi(PQgetvalue(res, i, 3)); - const char *attype = PQgetvalue(res, i, 1); + const char *attype = PQgetvalue(res, i, 1); + const char *typename; + bool isarray = false; /* Name */ cells[i * cols + 0] = (char *)PQgetvalue(res, i, 0); /* don't free this afterwards */ /* Type */ - /* (convert some internal type names to "readable") */ + if (attype[0] == '_') + { + isarray = true; + attype++; + } + /* (convert some internal type names to SQL'ish) */ + if (strcmp(attype, "bpchar")==0) + typename = "char"; + else if (strcmp(attype, "int2")==0) + typename = "smallint"; + else if (strcmp(attype, "int4")==0) + typename = "integer"; + else if (strcmp(attype, "int8")==0) + typename = "bigint"; + else if (strcmp(attype, "bool")==0) + typename = "boolean"; + else + typename = attype; + /* more might need to be added when date/time types are sorted out */ + cells[i * cols + 1] = xmalloc(NAMEDATALEN + 16); - if (strcmp(attype, "bpchar") == 0) - sprintf(cells[i * cols + 1], "char(%d)", attypmod != -1 ? attypmod - VARHDRSZ : 0); - else if (strcmp(attype, "varchar") == 0) - sprintf(cells[i * cols + 1], "varchar(%d)", attypmod != -1 ? attypmod - VARHDRSZ : 0); - else if (strcmp(attype, "numeric") == 0) + if (strcmp(typename, "char") == 0) + sprintf(cells[i * cols + 1], "char(%d)", attypmod != -1 ? attypmod - VARHDRSZ : 1); + else if (strcmp(typename, "varchar") == 0) + sprintf(cells[i * cols + 1], "varchar(%d)", attypmod != -1 ? attypmod - VARHDRSZ : 1); + else if (strcmp(typename, "numeric") == 0) sprintf(cells[i * cols + 1], "numeric(%d,%d)", ((attypmod - VARHDRSZ) >> 16) & 0xffff, (attypmod - VARHDRSZ) & 0xffff); - else if (attype[0] == '_') - sprintf(cells[i * cols + 1], "%s[]", attype + 1); else - strcpy(cells[i * cols + 1], attype); + strcpy(cells[i * cols + 1], typename); + + if (isarray) + strcat(cells[i * cols + 1], "[]"); /* Extra: not null and default */ /* (I'm cutting off the 'default' string at 128) */ - if (tableinfo.relkind == 'r' || tableinfo.relkind == 's') - { - cells[i * cols + 2] = xmalloc(128 + 128); - cells[i * cols + 2][0] = '\0'; - if (strcmp(PQgetvalue(res, i, 4), "t") == 0) - strcat(cells[i * cols + 2], "not null"); - - /* handle "default" here */ - if (strcmp(PQgetvalue(res, i, 5), "t") == 0) - { - PGresult *result; - - sprintf(buf, "SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c\n" - "WHERE c.relname = '%s' AND c.oid = d.adrelid AND d.adnum = %s", - name, PQgetvalue(res, i, 6)); - - result = PSQLexec(buf); - if (!result) - error = true; - else - { - if (cells[i * cols + 2][0]) - strcat(cells[i * cols + 2], " "); - strcat(cells[i * cols + 2], "default "); - strcat(cells[i * cols + 2], PQgetvalue(result, 0, 0)); - PQclear(result); - } - } - } - - if (error) - break; - + if (tableinfo.relkind == 'r') + { + cells[i * cols + 2] = xmalloc(128 + 128); + cells[i * cols + 2][0] = '\0'; + if (strcmp(PQgetvalue(res, i, 4), "t") == 0) + strcat(cells[i * cols + 2], "not null"); + + /* handle "default" here */ + if (strcmp(PQgetvalue(res, i, 5), "t") == 0) + { + PGresult *result; + + sprintf(buf, "SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c\n" + "WHERE c.relname = '%s' AND c.oid = d.adrelid AND d.adnum = %s", + name, PQgetvalue(res, i, 6)); + + result = PSQLexec(buf); + if (!result) + error = true; + else + { + if (cells[i * cols + 2][0]) + strcat(cells[i * cols + 2], " "); + strcat(cells[i * cols + 2], "default "); + strcat(cells[i * cols + 2], PQgetvalue(result, 0, 0)); + PQclear(result); + } + } + } + + if (error) + break; + /* Description */ if (desc) cells[i * cols + cols-1] = (char*)PQgetvalue(res, i, 7); } /* Make title */ - title = xmalloc(20 + strlen(name)); + title = xmalloc(22 + strlen(name)); switch (tableinfo.relkind) { - case 'r': - if (view_def) - sprintf(title, "View \"%s\"", name); - else - sprintf(title, "Table \"%s\"", name); - break; - case 'S': - sprintf(title, "Sequence \"%s\"", name); - break; - case 'i': - sprintf(title, "Index \"%s\"", name); - break; - case 's': - sprintf(title, "System table \"%s\"", name); - break; - default: - sprintf(title, "?%c?", tableinfo.relkind); + case 'r': + if (view_def) + sprintf(title, "View \"%s\"", name); + else + sprintf(title, "Table \"%s\"", name); + break; + case 'S': + sprintf(title, "Sequence \"%s\"", name); + break; + case 'i': + sprintf(title, "Index \"%s\"", name); + break; + case 's': + sprintf(title, "Special relation \"%s\"", name); + break; + default: + sprintf(title, "?%c?", tableinfo.relkind); } /* Make footers */ /* Information about the index */ if (tableinfo.relkind == 'i') { - PGresult * result; - - sprintf(buf, "SELECT i.indisunique, i.indisprimary, a.amname\n" - "FROM pg_index i, pg_class c, pg_am a\n" - "WHERE i.indexrelid = c.oid AND c.relname = '%s' AND c.relam = a.oid", - name); - - result = PSQLexec(buf); - if (!result) - error = true; - else - { - footers = xmalloc(2 * sizeof(*footers)); - footers[0] = xmalloc(NAMEDATALEN + 32); - sprintf(footers[0], "%s%s", - strcmp(PQgetvalue(result, 0, 0), "t")==0 ? "unique " : "", - PQgetvalue(result, 0, 2) - ); - if (strcmp(PQgetvalue(result, 0, 1), "t")==0) - strcat(footers[0], " (primary key)"); - footers[1] = NULL; - } + PGresult * result; + + sprintf(buf, "SELECT i.indisunique, i.indisprimary, a.amname\n" + "FROM pg_index i, pg_class c, pg_am a\n" + "WHERE i.indexrelid = c.oid AND c.relname = '%s' AND c.relam = a.oid", + name); + + result = PSQLexec(buf); + if (!result) + error = true; + else + { + footers = xmalloc(2 * sizeof(*footers)); + footers[0] = xmalloc(NAMEDATALEN + 32); + sprintf(footers[0], "%s%s", + strcmp(PQgetvalue(result, 0, 0), "t")==0 ? "unique " : "", + PQgetvalue(result, 0, 2) + ); + if (strcmp(PQgetvalue(result, 0, 1), "t")==0) + strcat(footers[0], " (primary key)"); + footers[1] = NULL; + } } /* Information about the view */ else if (tableinfo.relkind == 'r' && view_def) @@ -743,131 +765,134 @@ describeTableDetails(const char *name, bool desc) /* Information about the table */ else if (tableinfo.relkind == 'r') { - PGresult *result1=NULL, *result2=NULL, *result3=NULL, *result4=NULL; - int index_count=0, constr_count=0, rule_count=0, trigger_count=0; - int count_footers=0; + PGresult *result1=NULL, *result2=NULL, *result3=NULL, *result4=NULL; + int index_count=0, constr_count=0, rule_count=0, trigger_count=0; + int count_footers=0; /* count indices */ - if (!error && tableinfo.hasindex) - { - sprintf(buf, "SELECT c2.relname\n" - "FROM pg_class c, pg_class c2, pg_index i\n" - "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" - "ORDER BY c2.relname", - name); - result1 = PSQLexec(buf); - if (!result1) - error = true; - else - index_count = PQntuples(result1); - } - - /* count table (and column) constraints */ - if (!error && tableinfo.checks) - { - sprintf(buf, "SELECT rcsrc\n" - "FROM pg_relcheck r, pg_class c\n" - "WHERE c.relname='%s' AND c.oid = r.rcrelid", - name); - result2 = PSQLexec(buf); - if (!result2) - error = true; - else - constr_count = PQntuples(result2); - } - - /* count rules */ - if (!error && tableinfo.hasrules) - { - sprintf(buf, - "SELECT r.rulename\n" - "FROM pg_rewrite r, pg_class c\n" - "WHERE c.relname='%s' AND c.oid = r.ev_class", - name); - result3 = PSQLexec(buf); - if (!result3) - error = true; - else - rule_count = PQntuples(result3); - } - - /* count triggers */ - if (!error && tableinfo.hasrules) - { - sprintf(buf, - "SELECT t.tgname\n" - "FROM pg_trigger t, pg_class c\n" - "WHERE c.relname='%s' AND c.oid = t.tgrelid", - name); - result4 = PSQLexec(buf); - if (!result4) - error = true; - else - trigger_count = PQntuples(result4); - } - - footers = xmalloc((index_count + constr_count + rule_count + trigger_count + 1) * sizeof(*footers)); - - /* print indices */ - for (i = 0; i < index_count; i++) - { - sprintf(buf, "%s %s", - index_count==1 ? "Index:" : (i==0 ? "Indices:" : " "), - PQgetvalue(result1, i, 0) - ); - if (i < index_count-1) - strcat(buf, ","); - - footers[count_footers++] = xstrdup(buf); - } - - /* print contraints */ - for (i = 0; i < constr_count; i++) - { - sprintf(buf, "%s %s", - constr_count==1 ? "Constraint:" : (i==0 ? "Constraints:" : " "), - PQgetvalue(result2, i, 0) - ); - footers[count_footers++] = xstrdup(buf); - } - - /* print rules */ - for (i = 0; i < rule_count; i++) - { - sprintf(buf, "%s %s", - rule_count==1 ? "Rule:" : (i==0 ? "Rules:" : " "), - PQgetvalue(result3, i, 0) - ); - if (i < rule_count-1) - strcat(buf, ","); - - footers[count_footers++] = xstrdup(buf); - } - - /* print triggers */ - for (i = 0; i < trigger_count; i++) - { - sprintf(buf, "%s %s", - trigger_count==1 ? "Trigger:" : (i==0 ? "Triggers:" : " "), - PQgetvalue(result4, i, 0) - ); - if (i < trigger_count-1) - strcat(buf, ","); - - footers[count_footers++] = xstrdup(buf); - } - - /* end of list marker */ - footers[count_footers] = NULL; - - PQclear(result1); - PQclear(result2); - PQclear(result3); - PQclear(result4); + if (!error && tableinfo.hasindex) + { + sprintf(buf, "SELECT c2.relname\n" + "FROM pg_class c, pg_class c2, pg_index i\n" + "WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" + "ORDER BY c2.relname", + name); + result1 = PSQLexec(buf); + if (!result1) + error = true; + else + index_count = PQntuples(result1); + } + + /* count table (and column) constraints */ + if (!error && tableinfo.checks) + { + sprintf(buf, "SELECT rcsrc\n" + "FROM pg_relcheck r, pg_class c\n" + "WHERE c.relname='%s' AND c.oid = r.rcrelid", + name); + result2 = PSQLexec(buf); + if (!result2) + error = true; + else + constr_count = PQntuples(result2); + } + + /* count rules */ + if (!error && tableinfo.hasrules) + { + sprintf(buf, + "SELECT r.rulename\n" + "FROM pg_rewrite r, pg_class c\n" + "WHERE c.relname='%s' AND c.oid = r.ev_class", + name); + result3 = PSQLexec(buf); + if (!result3) + error = true; + else + rule_count = PQntuples(result3); + } + + /* count triggers */ + if (!error && tableinfo.hasrules) + { + sprintf(buf, + "SELECT t.tgname\n" + "FROM pg_trigger t, pg_class c\n" + "WHERE c.relname='%s' AND c.oid = t.tgrelid", + name); + result4 = PSQLexec(buf); + if (!result4) + error = true; + else + trigger_count = PQntuples(result4); + } + + footers = xmalloc((index_count + constr_count + rule_count + trigger_count + 1) + * sizeof(*footers)); + + /* print indices */ + for (i = 0; i < index_count; i++) + { + sprintf(buf, "%s %s", + index_count==1 ? "Index:" : (i==0 ? "Indices:" : " "), + PQgetvalue(result1, i, 0) + ); + if (i < index_count-1) + strcat(buf, ","); + + footers[count_footers++] = xstrdup(buf); + } + + /* print contraints */ + for (i = 0; i < constr_count; i++) + { + sprintf(buf, "%s %s", + constr_count==1 ? "Constraint:" : (i==0 ? "Constraints:" : " "), + PQgetvalue(result2, i, 0) + ); + footers[count_footers++] = xstrdup(buf); + } + + /* print rules */ + for (i = 0; i < rule_count; i++) + { + sprintf(buf, "%s %s", + rule_count==1 ? "Rule:" : (i==0 ? "Rules:" : " "), + PQgetvalue(result3, i, 0) + ); + if (i < rule_count-1) + strcat(buf, ","); + + footers[count_footers++] = xstrdup(buf); + } + + /* print triggers */ + for (i = 0; i < trigger_count; i++) + { + sprintf(buf, "%s %s", + trigger_count==1 ? "Trigger:" : (i==0 ? "Triggers:" : " "), + PQgetvalue(result4, i, 0) + ); + if (i < trigger_count-1) + strcat(buf, ","); + + footers[count_footers++] = xstrdup(buf); + } + + /* end of list marker */ + footers[count_footers] = NULL; + + PQclear(result1); + PQclear(result2); + PQclear(result3); + PQclear(result4); } if (!error) - printTable(title, headers, (const char**)cells, (const char**)footers, "llll", &myopt, pset.queryFout); + printTable(title, headers, + (const char**)cells, (const char**)footers, + "llll", &myopt, pset.queryFout); /* clean up */ free(title); @@ -875,8 +900,8 @@ describeTableDetails(const char *name, bool desc) for (i = 0; i < PQntuples(res); i++) { free(cells[i * cols + 1]); - if (tableinfo.relkind == 'r' || tableinfo.relkind == 's') - free(cells[i * cols + 2]); + if (tableinfo.relkind == 'r') + free(cells[i * cols + 2]); } free(cells); diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h index 7c87baa06d3..36f4ceca4ea 100644 --- a/src/bin/psql/describe.h +++ b/src/bin/psql/describe.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.6 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/describe.h,v 1.7 2000/01/29 16:58:48 petere Exp $ */ #ifndef DESCRIBE_H #define DESCRIBE_H diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index ea503fc14fa..70100c7791f 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.14 2000/01/26 16:10:01 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.15 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "help.h" diff --git a/src/bin/psql/help.h b/src/bin/psql/help.h index 45e02cc2a93..cbd9a81488f 100644 --- a/src/bin/psql/help.h +++ b/src/bin/psql/help.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.4 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.5 2000/01/29 16:58:48 petere Exp $ */ #ifndef HELP_H #define HELP_H diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 9aa56284379..2adff9d3422 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.7 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.8 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "input.h" diff --git a/src/bin/psql/input.h b/src/bin/psql/input.h index b8e7082900a..6e078cbfbea 100644 --- a/src/bin/psql/input.h +++ b/src/bin/psql/input.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/input.h,v 1.5 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/input.h,v 1.6 2000/01/29 16:58:48 petere Exp $ */ #ifndef INPUT_H #define INPUT_H diff --git a/src/bin/psql/large_obj.c b/src/bin/psql/large_obj.c index cdc2c24c065..164956fded9 100644 --- a/src/bin/psql/large_obj.c +++ b/src/bin/psql/large_obj.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.6 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.7 2000/01/29 16:58:48 petere Exp $ */ #include <c.h> #include "large_obj.h" @@ -60,8 +60,8 @@ handle_transaction(void) if (notice[0]) { - if ((!commit && strcmp(notice, "NOTICE: UserAbortTransactionBlock and not in in-progress state\n") != 0) || - (commit && strcmp(notice, "NOTICE: EndTransactionBlock and not inprogress/abort state\n") != 0)) + if ((!commit && strcmp(notice, "NOTICE: ROLLBACK: no transaction in progress\n") != 0) || + (commit && strcmp(notice, "NOTICE: COMMIT: no transaction in progress\n") != 0)) fputs(notice, stderr); } else if (!QUIET()) diff --git a/src/bin/psql/large_obj.h b/src/bin/psql/large_obj.h index 26de0e147f0..17e1c81d6df 100644 --- a/src/bin/psql/large_obj.h +++ b/src/bin/psql/large_obj.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.h,v 1.6 2000/01/18 23:30:23 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.h,v 1.7 2000/01/29 16:58:49 petere Exp $ */ #ifndef LARGE_OBJ_H #define LARGE_OBJ_H diff --git a/src/bin/psql/mainloop.h b/src/bin/psql/mainloop.h index 8caa8c6c80c..0664ddfcafa 100644 --- a/src/bin/psql/mainloop.h +++ b/src/bin/psql/mainloop.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.h,v 1.5 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.h,v 1.6 2000/01/29 16:58:49 petere Exp $ */ #ifndef MAINLOOP_H #define MAINLOOP_H diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c index af1172a21b3..3ff0d27c227 100644 --- a/src/bin/psql/print.c +++ b/src/bin/psql/print.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.8 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.9 2000/01/29 16:58:49 petere Exp $ */ #include <c.h> #include "print.h" diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h index 252236bf33e..1098ff8e012 100644 --- a/src/bin/psql/print.h +++ b/src/bin/psql/print.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/print.h,v 1.5 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/print.h,v 1.6 2000/01/29 16:58:49 petere Exp $ */ #ifndef PRINT_H #define PRINT_H diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index f9d6cc098ea..a06a712e401 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.6 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.7 2000/01/29 16:58:49 petere Exp $ */ #include <c.h> #include "prompt.h" @@ -39,7 +39,7 @@ * %n - database user name * %/ - current database * %~ - like %/ but "~" when database name equals user name - * %# - "#" if the username is postgres, ">" otherwise + * %# - "#" if superuser, ">" otherwise * %R - in prompt1 normally =, or ^ if single line mode, * or a ! if session is not connected to a database; * in prompt2 -, *, ', or "; @@ -194,7 +194,7 @@ get_prompt(promptStatus_t status) case '#': { - if (pset.db && strcmp(PQuser(pset.db), "postgres") == 0) + if (pset.issuper) buf[0] = '#'; else buf[0] = '>'; diff --git a/src/bin/psql/prompt.h b/src/bin/psql/prompt.h index 9134f8331c9..f70a334a54d 100644 --- a/src/bin/psql/prompt.h +++ b/src/bin/psql/prompt.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/prompt.h,v 1.4 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/prompt.h,v 1.5 2000/01/29 16:58:49 petere Exp $ */ #ifndef PROMPT_H #define PROMPT_H diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 6cfeca79b29..af8171e7da5 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/settings.h,v 1.7 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/settings.h,v 1.8 2000/01/29 16:58:49 petere Exp $ */ #ifndef SETTINGS_H #define SETTINGS_H @@ -53,6 +53,9 @@ typedef struct _psqlSettings char *progname; /* in case you renamed psql */ char *inputfile; /* for error reporting */ unsigned lineno; /* also for error reporting */ + + bool issuper; /* is the current user a superuser? + (used to form the prompt) */ } PsqlSettings; extern PsqlSettings pset; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index d3169ab84c7..d92adce381e 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.19 2000/01/27 05:33:51 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.20 2000/01/29 16:58:49 petere Exp $ */ #include <c.h> @@ -162,8 +162,8 @@ main(int argc, char **argv) if (PQstatus(pset.db) == CONNECTION_BAD) { - fprintf(stderr, "%s: connection to database \"%s\" failed - %s", - pset.progname, PQdb(pset.db), PQerrorMessage(pset.db)); + fprintf(stderr, "%s: %s", + pset.progname, PQerrorMessage(pset.db)); PQfinish(pset.db); exit(EXIT_BADCONN); } @@ -188,6 +188,8 @@ main(int argc, char **argv) SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); + pset.issuper = test_superuser(PQuser(pset.db)); + if (!QUIET() && !pset.notty && !options.action) { printf("Welcome to %s, the PostgreSQL interactive terminal.\n\n" @@ -436,11 +438,13 @@ parse_options(int argc, char *argv[], struct adhoc_opts * options) pset.getPassword = true; break; case '?': - if (strcmp(argv[optind-1], "-?")==0) + /* Actual help option given */ + if (strcmp(argv[optind-1], "-?")==0 || strcmp(argv[optind-1], "--help")==0) { usage(); exit(EXIT_SUCCESS); } + /* unknown option reported by getopt */ else { fputs("Try -? for help.\n", stderr); @@ -486,7 +490,7 @@ parse_options(int argc, char *argv[], struct adhoc_opts * options) /* - * Load /etc/psqlrc or .psqlrc file, if found. + * Load .psqlrc file, if found. */ static void process_psqlrc(void) @@ -498,12 +502,6 @@ process_psqlrc(void) #define R_OK 0 #endif - /* System-wide startup file */ - if (access("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, R_OK) == 0) - process_file("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION); - else if (access("/etc/psqlrc", R_OK) == 0) - process_file("/etc/psqlrc"); - /* Look for one in the home dir */ home = getenv("HOME"); @@ -573,6 +571,6 @@ showVersion(void) puts("Portions Copyright (c) 1996-2000, PostgreSQL, Inc"); puts("Portions Copyright (C) 1996 Regents of the University of California"); - puts("Read the file COPYING or use the command \\copyright to see the"); + puts("Read the file COPYRIGHT or use the command \\copyright to see the"); puts("usage and distribution terms."); } diff --git a/src/bin/psql/stringutils.c b/src/bin/psql/stringutils.c index c8cc6034224..5ba48dc454c 100644 --- a/src/bin/psql/stringutils.c +++ b/src/bin/psql/stringutils.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.22 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.23 2000/01/29 16:58:49 petere Exp $ */ #include <c.h> #include "stringutils.h" diff --git a/src/bin/psql/stringutils.h b/src/bin/psql/stringutils.h index 0a109b56394..914871d94eb 100644 --- a/src/bin/psql/stringutils.h +++ b/src/bin/psql/stringutils.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.h,v 1.12 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.h,v 1.13 2000/01/29 16:58:49 petere Exp $ */ #ifndef STRINGUTILS_H #define STRINGUTILS_H diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 718c1732f05..92112cead07 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.8 2000/01/21 23:32:36 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.9 2000/01/29 16:58:49 petere Exp $ */ /*----------- diff --git a/src/bin/psql/tab-complete.h b/src/bin/psql/tab-complete.h index 42fa6fe1b06..8c54cffeb64 100644 --- a/src/bin/psql/tab-complete.h +++ b/src/bin/psql/tab-complete.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.h,v 1.2 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.h,v 1.3 2000/01/29 16:58:49 petere Exp $ */ #ifndef TAB_COMPLETE_H #define TAB_COMPLETE_H diff --git a/src/bin/psql/variables.c b/src/bin/psql/variables.c index 0c559af7e67..b0523dcb675 100644 --- a/src/bin/psql/variables.c +++ b/src/bin/psql/variables.c @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.4 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.5 2000/01/29 16:58:49 petere Exp $ */ #include <c.h> #include "variables.h" diff --git a/src/bin/psql/variables.h b/src/bin/psql/variables.h index d6b5fb72358..2b476dd0c31 100644 --- a/src/bin/psql/variables.h +++ b/src/bin/psql/variables.h @@ -1,9 +1,9 @@ /* * psql - the PostgreSQL interactive terminal * - * Copyright 2000 by PostgreSQL Global Development Team + * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/variables.h,v 1.4 2000/01/18 23:30:24 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/variables.h,v 1.5 2000/01/29 16:58:49 petere Exp $ */ /* |