*** ./src/bin/psql/describe.c.orig Sat Jun 22 08:50:52 2002 --- ./src/bin/psql/describe.c Sat Jun 22 08:54:33 2002 *************** *** 450,461 **** bool ! describeTableDetails(const char *name, bool desc) { PQExpBufferData buf; PGresult *res = NULL; printTableOpt myopt = pset.popt.topt; int i; const char *view_def = NULL; const char *headers[5]; char **cells = NULL; --- 450,462 ---- bool ! describeTableDetails(const char *relname, bool desc) { PQExpBufferData buf; PGresult *res = NULL; printTableOpt myopt = pset.popt.topt; int i; + int ii; const char *view_def = NULL; const char *headers[5]; char **cells = NULL; *************** *** 472,488 **** bool hasrules; } tableinfo; bool retval; - retval = false; initPQExpBuffer(&buf); initPQExpBuffer(&title); ! /* Get general table info */ printfPQExpBuffer(&buf, ! "SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules\n" ! "FROM pg_class WHERE relname='%s'", ! name); res = PSQLexec(buf.data); if (!res) goto error_return; --- 473,536 ---- bool hasrules; } tableinfo; bool retval; retval = false; + char *aa; + char *rname; + char *name = NULL; + char *schema = NULL; initPQExpBuffer(&buf); initPQExpBuffer(&title); ! rname = xstrdup(relname); ! ! if (strchr(rname,'.')==NULL) ! { ! name = xstrdup(rname); ! schema = xstrdup("current_schema() OR n.nspname ~ '^pg_temp_'"); ! } ! else ! { ! schema = malloc(NAMEDATALEN); ! name = malloc(NAMEDATALEN); ! rname[NAMEDATALEN]='\0'; ! schema[0]='\''; ! for(i=ii=cols=0,aa=rname;*aa;aa++) { ! if (*aa=='.') ! { ! if (++cols>1 || i==0 || !(*(aa+1))) //i==nlen-1) ! { ! if (!QUIET()) ! fprintf(stderr, _("\"%s\" is not a valid relation name.\n" ! "Relation names must be either be \"name\" or \"schema.name\"\n"), rname); ! goto error_return; ! ! } ! schema[++i] = '\'';schema[++i] = '\0'; ! } ! else if (cols) ! { ! name[ii++]=*aa; ! } ! else ! { ! schema[++i]=*aa; ! } ! } ! name[ii] = '\0'; ! } ! ! /* Get general table info (and the schema name) */ printfPQExpBuffer(&buf, ! "SELECT c.relhasindex, c.relkind, c.relchecks, c.reltriggers, c.relhasrules, n.nspname\n" ! "FROM pg_class c, pg_namespace n\n" ! "WHERE relname = '%s'\n" ! " AND c.relnamespace = n.oid\n" ! " AND (n.nspname = %s)\n" ! "ORDER BY (CASE WHEN n.nspname ~ '^pg_temp_' THEN 0 ELSE 1 END)\n" ! "LIMIT 1\n", ! name, schema); ! res = PSQLexec(buf.data); if (!res) goto error_return; *************** *** 503,511 **** tableinfo.checks = atoi(PQgetvalue(res, 0, 2)); tableinfo.triggers = atoi(PQgetvalue(res, 0, 3)); tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0; PQclear(res); - headers[0] = _("Column"); headers[1] = _("Type"); cols = 2; --- 551,559 ---- tableinfo.checks = atoi(PQgetvalue(res, 0, 2)); tableinfo.triggers = atoi(PQgetvalue(res, 0, 3)); tableinfo.hasrules = strcmp(PQgetvalue(res, 0, 4), "t") == 0; + schema = xstrdup(PQgetvalue(res,0,5)); PQclear(res); headers[0] = _("Column"); headers[1] = _("Type"); cols = 2; *************** *** 533,542 **** appendPQExpBuffer(&buf, "format_type(a.atttypid, a.atttypmod), a.attnotnull, a.atthasdef, a.attnum"); if (desc) appendPQExpBuffer(&buf, ", col_description(a.attrelid, a.attnum)"); ! appendPQExpBuffer(&buf, "\nFROM pg_class c, pg_attribute a"); if (tableinfo.relkind == 'i') appendPQExpBuffer(&buf, ", pg_index i"); ! appendPQExpBuffer(&buf, "\nWHERE c.relname = '%s'\n AND a.attnum > 0 AND a.attrelid = c.oid", name); if (tableinfo.relkind == 'i') appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid"); appendPQExpBuffer(&buf, "\nORDER BY a.attnum"); --- 581,595 ---- appendPQExpBuffer(&buf, "format_type(a.atttypid, a.atttypmod), a.attnotnull, a.atthasdef, a.attnum"); if (desc) appendPQExpBuffer(&buf, ", col_description(a.attrelid, a.attnum)"); ! appendPQExpBuffer(&buf, "\nFROM pg_class c, pg_attribute a, pg_namespace n"); if (tableinfo.relkind == 'i') appendPQExpBuffer(&buf, ", pg_index i"); ! appendPQExpBuffer(&buf, ! "\nWHERE a.attnum > 0 AND a.attrelid = c.oid\n" ! " AND c.relname = '%s'\n" ! " AND c.relnamespace = n.oid\n" ! " AND n.nspname = '%s'", ! name, schema); if (tableinfo.relkind == 'i') appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid"); appendPQExpBuffer(&buf, "\nORDER BY a.attnum"); *************** *** 617,641 **** switch (tableinfo.relkind) { case 'r': ! printfPQExpBuffer(&title, _("Table \"%s\""), name); break; case 'v': ! printfPQExpBuffer(&title, _("View \"%s\""), name); break; case 'S': ! printfPQExpBuffer(&title, _("Sequence \"%s\""), name); break; case 'i': ! printfPQExpBuffer(&title, _("Index \"%s\""), name); break; case 's': ! printfPQExpBuffer(&title, _("Special relation \"%s\""), name); break; case 't': ! printfPQExpBuffer(&title, _("TOAST table \"%s\""), name); break; default: ! printfPQExpBuffer(&title, _("?%c? \"%s\""), tableinfo.relkind, name); break; } --- 670,694 ---- switch (tableinfo.relkind) { case 'r': ! printfPQExpBuffer(&title, _("Table \"%s.%s\""), schema,name); break; case 'v': ! printfPQExpBuffer(&title, _("View \"%s.%s\""), schema,name); break; case 'S': ! printfPQExpBuffer(&title, _("Sequence \"%s.%s\""), schema,name); break; case 'i': ! printfPQExpBuffer(&title, _("Index \"%s.%s\""), schema,name); break; case 's': ! printfPQExpBuffer(&title, _("Special relation \"%s.%s\""), schema,name); break; case 't': ! printfPQExpBuffer(&title, _("TOAST table \"%s.%s\""), schema,name); break; default: ! printfPQExpBuffer(&title, _("?%c? \"%s.%s\""), tableinfo.relkind, schema,name); break; } *************** *** 645,657 **** /* Footer information about an index */ PGresult *result; printfPQExpBuffer(&buf, ! "SELECT i.indisunique, i.indisprimary, a.amname, c2.relname,\n" ! "pg_get_expr(i.indpred,i.indrelid)\n" ! "FROM pg_index i, pg_class c, pg_class c2, pg_am a\n" ! "WHERE i.indexrelid = c.oid AND c.relname = '%s' AND c.relam = a.oid\n" ! "AND i.indrelid = c2.oid", ! name); ! result = PSQLexec(buf.data); if (!result) goto error_return; --- 698,715 ---- /* Footer information about an index */ PGresult *result; printfPQExpBuffer(&buf, ! "SELECT i.indisunique, i.indisprimary, a.amname,\n" ! " n2.nspname ||'.'|| c2.relname,\n" ! " pg_get_expr(i.indpred,i.indrelid)\n" ! "FROM pg_index i, pg_class c, pg_class c2, pg_am a,\n" ! " pg_namespace n, pg_namespace n2\n" ! "WHERE i.indexrelid = c.oid\n" ! " AND c.relname = '%s' AND c.relam = a.oid\n" ! " AND c.relnamespace = n.oid\n" ! " AND n.nspname = '%s'\n" ! " AND i.indrelid = c2.oid\n" ! " AND c2.relnamespace = n2.oid\n", ! name, schema); result = PSQLexec(buf.data); if (!result) goto error_return; *************** *** 704,710 **** "SELECT r.rulename\n" "FROM pg_rewrite r, pg_class c\n" "WHERE c.relname = '%s' AND c.oid = r.ev_class\n" ! "AND r.rulename != '_RETURN'", name); result = PSQLexec(buf.data); if (!result) --- 762,768 ---- "SELECT r.rulename\n" "FROM pg_rewrite r, pg_class c\n" "WHERE c.relname = '%s' AND c.oid = r.ev_class\n" ! " AND r.rulename != '_RETURN'", name); result = PSQLexec(buf.data); if (!result) *************** *** 759,768 **** "SELECT c2.relname, i.indisprimary, i.indisunique,\n" "SUBSTR(pg_get_indexdef(i.indexrelid),\n" "POSITION('USING ' IN pg_get_indexdef(i.indexrelid))+5)\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 i.indisprimary DESC, i.indisunique DESC, c2.relname", ! name); result1 = PSQLexec(buf.data); if (!result1) goto error_return; --- 817,829 ---- "SELECT c2.relname, i.indisprimary, i.indisunique,\n" "SUBSTR(pg_get_indexdef(i.indexrelid),\n" "POSITION('USING ' IN pg_get_indexdef(i.indexrelid))+5)\n" ! "FROM pg_class c, pg_class c2, pg_index i, pg_namespace n\n" ! "WHERE c.relname = '%s'\n" ! " AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" ! " AND c.relnamespace = n.oid\n" ! " AND n.nspname = '%s'\n" "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname", ! name,schema); result1 = PSQLexec(buf.data); if (!result1) goto error_return; *************** *** 776,782 **** printfPQExpBuffer(&buf, "SELECT rcsrc, rcname\n" "FROM pg_relcheck r, pg_class c\n" ! "WHERE c.relname='%s' AND c.oid = r.rcrelid", name); result2 = PSQLexec(buf.data); if (!result2) --- 837,843 ---- printfPQExpBuffer(&buf, "SELECT rcsrc, rcname\n" "FROM pg_relcheck r, pg_class c\n" ! "WHERE c.relname = '%s' AND c.oid = r.rcrelid", name); result2 = PSQLexec(buf.data); if (!result2) *************** *** 791,797 **** printfPQExpBuffer(&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.data); if (!result3) --- 852,858 ---- printfPQExpBuffer(&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.data); if (!result3) *************** *** 806,812 **** printfPQExpBuffer(&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.data); if (!result4) --- 867,873 ---- printfPQExpBuffer(&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.data); if (!result4) *************** *** 994,1000 **** * i - indexes * v - views * s - sequences ! * S - systems tables (~ '^pg_') * (any order of the above is fine) * * Note: For some reason it always happens to people that their tables have owners --- 1055,1061 ---- * i - indexes * v - views * s - sequences ! * S - systems tables (~ '^pg_' AND !~ '^pg_temp_') * (any order of the above is fine) * * Note: For some reason it always happens to people that their tables have owners *************** *** 1020,1030 **** initPQExpBuffer(&buf); printfPQExpBuffer(&buf, ! "SELECT c.relname as \"%s\",\n" ! " CASE c.relkind WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s' WHEN 'S' THEN '%s' WHEN 's' THEN '%s' END as \"%s\",\n" " u.usename as \"%s\"", ! _("Name"), _("table"), _("view"), _("index"), _("sequence"), ! _("special"), _("Type"), _("Owner")); if (desc) appendPQExpBuffer(&buf, --- 1081,1094 ---- initPQExpBuffer(&buf); printfPQExpBuffer(&buf, ! "SELECT n.nspname as \"%s\", c.relname as \"%s\",\n" ! " CASE c.relkind\n" ! " WHEN 'r' THEN '%s' WHEN 'v' THEN '%s' WHEN 'i' THEN '%s'\n" ! " WHEN 'S' THEN '%s' WHEN 's' THEN '%s'\n" ! " END as \"%s\",\n" " u.usename as \"%s\"", ! _("Schema"), _("Name"), _("table"), _("view"), _("index"), ! _("sequence"), _("special"), _("Type"), _("Owner")); if (desc) appendPQExpBuffer(&buf, *************** *** 1032,1048 **** _("Description")); if (showIndexes) appendPQExpBuffer(&buf, ! ",\n c2.relname as \"%s\"" ! "\nFROM pg_class c, pg_class c2, pg_index i, pg_user u\n" "WHERE c.relowner = u.usesysid\n" ! "AND i.indrelid = c2.oid AND i.indexrelid = c.oid\n", _("Table")); else appendPQExpBuffer(&buf, ! "\nFROM pg_class c, pg_user u\n" "WHERE c.relowner = u.usesysid\n"); ! appendPQExpBuffer(&buf, "AND c.relkind IN ("); if (showTables) appendPQExpBuffer(&buf, "'r',"); if (showViews) --- 1096,1114 ---- _("Description")); if (showIndexes) appendPQExpBuffer(&buf, ! ",\n n2.nspname||'.'||c2.relname as \"%s\"" ! "\nFROM pg_class c, pg_class c2, pg_index i, pg_user u,\n" ! " pg_namespace n, pg_namespace n2\n" "WHERE c.relowner = u.usesysid\n" ! " AND i.indrelid = c2.oid AND c2.relnamespace = n2.oid\n" ! " AND i.indexrelid = c.oid\n", _("Table")); else appendPQExpBuffer(&buf, ! "\nFROM pg_class c, pg_user u, pg_namespace n\n" "WHERE c.relowner = u.usesysid\n"); ! appendPQExpBuffer(&buf, " AND c.relkind IN ("); if (showTables) appendPQExpBuffer(&buf, "'r',"); if (showViews) *************** *** 1057,1070 **** appendPQExpBuffer(&buf, ")\n"); if (showSystem) ! appendPQExpBuffer(&buf, " AND c.relname ~ '^pg_'\n"); else ! appendPQExpBuffer(&buf, " AND c.relname !~ '^pg_'\n"); if (name) appendPQExpBuffer(&buf, " AND c.relname ~ '^%s'\n", name); ! appendPQExpBuffer(&buf, "ORDER BY 1;"); res = PSQLexec(buf.data); termPQExpBuffer(&buf); --- 1123,1138 ---- appendPQExpBuffer(&buf, ")\n"); if (showSystem) ! appendPQExpBuffer(&buf, " AND (n.nspname !~ '^pg_temp_' AND n.nspname ~ '^pg_')\n"); else ! appendPQExpBuffer(&buf, " AND (n.nspname ~ '^pg_temp_' OR n.nspname !~ '^pg_')\n"); if (name) appendPQExpBuffer(&buf, " AND c.relname ~ '^%s'\n", name); ! appendPQExpBuffer(&buf, ! " AND c.relnamespace = n.oid\n" ! "ORDER BY (CASE WHEN n.nspname ~ '^pg_temp_' THEN 0 ELSE 1 END),1,2,3;"); res = PSQLexec(buf.data); termPQExpBuffer(&buf); *************** *** 1120,1126 **** _("Type"), _("Modifier")); if (name) ! appendPQExpBuffer(&buf, "AND t.typname ~ '^%s'\n", name); appendPQExpBuffer(&buf, "ORDER BY 1;"); res = PSQLexec(buf.data); --- 1188,1194 ---- _("Type"), _("Modifier")); if (name) ! appendPQExpBuffer(&buf, " AND t.typname ~ '^%s'\n", name); appendPQExpBuffer(&buf, "ORDER BY 1;"); res = PSQLexec(buf.data); *** ./src/bin/psql/describe.h.orig Sat Jun 22 08:51:14 2002 --- ./src/bin/psql/describe.h Fri Jun 21 23:06:28 2002 *************** *** 32,38 **** bool objectDescription(const char *object); /* \d foo */ ! bool describeTableDetails(const char *name, bool desc); /* \l */ bool listAllDbs(bool desc); --- 32,38 ---- bool objectDescription(const char *object); /* \d foo */ ! bool describeTableDetails(const char *relname, bool desc); /* \l */ bool listAllDbs(bool desc);