Fix quoting bugs and incorrect trigger argument printout.

This commit is contained in:
Tom Lane 2000-01-18 07:29:58 +00:00
parent e1cce4d5ea
commit 4cd086ce43
2 changed files with 77 additions and 62 deletions

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.37 2000/01/16 03:54:58 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.38 2000/01/18 07:29:58 tgl Exp $
* *
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2 * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
* *
@ -39,8 +39,6 @@ static void flagInhAttrs(TableInfo *tbinfo, int numTables,
InhInfo *inhinfo, int numInherits); InhInfo *inhinfo, int numInherits);
static int strInArray(const char *pattern, char **arr, int arr_size); static int strInArray(const char *pattern, char **arr, int arr_size);
PQExpBuffer id_return;
/* /*
* findTypeByOid * findTypeByOid
* given an oid of a type, return its typename * given an oid of a type, return its typename
@ -65,7 +63,7 @@ findTypeByOid(TypeInfo *tinfo, int numTypes, const char *oid)
} }
/* should never get here */ /* should never get here */
fprintf(stderr, "failed sanity check, type with oid %s was not found\n", fprintf(stderr, "failed sanity check, type with oid %s was not found\n",
oid); oid);
exit(2); exit(2);
} }
@ -90,7 +88,7 @@ findOprByOid(OprInfo *oprinfo, int numOprs, const char *oid)
} }
/* should never get here */ /* should never get here */
fprintf(stderr, "failed sanity check, opr with oid %s was not found\n", fprintf(stderr, "failed sanity check, opr with oid %s was not found\n",
oid); oid);
exit(2); exit(2);
} }
@ -505,27 +503,40 @@ findFuncByName(FuncInfo *finfo, int numFuncs, const char *name)
const char * const char *
fmtId(const char *rawid, bool force_quotes) fmtId(const char *rawid, bool force_quotes)
{ {
static PQExpBuffer id_return = NULL;
const char *cp; const char *cp;
if (!force_quotes)
{
if (! islower(*rawid))
force_quotes = true;
else
for (cp = rawid; *cp; cp++)
{
if (! (islower(*cp) || isdigit(*cp) || (*cp == '_')))
{
force_quotes = true;
break;
}
}
}
if (!force_quotes)
return rawid; /* no quoting needed */
if (id_return) if (id_return)
resetPQExpBuffer(id_return); resetPQExpBuffer(id_return);
else else
id_return = createPQExpBuffer(); id_return = createPQExpBuffer();
if (!force_quotes)
for (cp = rawid; *cp != '\0'; cp++)
if (!(islower(*cp) || isdigit(*cp) || (*cp == '_')))
break;
if (force_quotes || (*cp != '\0')) appendPQExpBufferChar(id_return, '\"');
for (cp = rawid; *cp; cp++)
{ {
appendPQExpBuffer(id_return, "\""); if (*cp == '\"')
appendPQExpBuffer(id_return, rawid); appendPQExpBufferChar(id_return, '\\');
appendPQExpBuffer(id_return, "\""); appendPQExpBufferChar(id_return, *cp);
} }
else appendPQExpBufferChar(id_return, '\"');
appendPQExpBuffer(id_return, rawid);
cp = id_return->data; return id_return->data;
return cp;
} /* fmtId() */ } /* fmtId() */

View File

@ -21,7 +21,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.133 2000/01/18 00:03:37 petere Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.134 2000/01/18 07:29:58 tgl Exp $
* *
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
@ -643,7 +643,8 @@ main(int argc, char **argv)
force_quotes = true; force_quotes = true;
break; break;
case 'o': case 'o':
fprintf(stderr, "%s: The -o option for dumping oids is deprecated. Please use -O."); fprintf(stderr, "%s: The -o option for dumping oids is deprecated. Please use -O.\n", progname);
/* FALLTHRU */
case 'O': /* Dump oids */ case 'O': /* Dump oids */
oids = true; oids = true;
break; break;
@ -1632,13 +1633,10 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
resetPQExpBuffer(query); resetPQExpBuffer(query);
if (name[0] != '$') { if (name[0] != '$') {
appendPQExpBuffer(query, "CONSTRAINT "); appendPQExpBuffer(query, "CONSTRAINT %s ",
appendPQExpBuffer(query, fmtId(name, force_quotes)); fmtId(name, force_quotes));
appendPQExpBufferChar(query, ' ');
} }
appendPQExpBuffer(query, "CHECK ("); appendPQExpBuffer(query, "CHECK (%s)", expr);
appendPQExpBuffer(query, expr);
appendPQExpBuffer(query, ")");
tblinfo[i].check_expr[i2] = strdup(query->data); tblinfo[i].check_expr[i2] = strdup(query->data);
} }
PQclear(res2); PQclear(res2);
@ -1647,11 +1645,11 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
tblinfo[i].check_expr = NULL; tblinfo[i].check_expr = NULL;
/* Get primary key */ /* Get primary key */
if (strcmp(PQgetvalue(res, i, i_relhasindex), "t")==0) if (strcmp(PQgetvalue(res, i, i_relhasindex), "t")==0)
{ {
PGresult * res2; PGresult * res2;
char str[INDEX_MAX_KEYS * NAMEDATALEN + 3] = ""; char str[INDEX_MAX_KEYS * (NAMEDATALEN*2 + 4) + 1];
int j; int j;
resetPQExpBuffer(query); resetPQExpBuffer(query);
appendPQExpBuffer(query, appendPQExpBuffer(query,
@ -1661,7 +1659,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
" AND i.indexrelid = c.oid AND a.attnum > 0 AND a.attrelid = c.oid " " AND i.indexrelid = c.oid AND a.attnum > 0 AND a.attrelid = c.oid "
"ORDER BY a.attnum ", "ORDER BY a.attnum ",
tblinfo[i].oid); tblinfo[i].oid);
res2 = PQexec(g_conn, query->data); res2 = PQexec(g_conn, query->data);
if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK) if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "getTables(): SELECT (for PRIMARY KEY) failed. Explanation from backend: %s", fprintf(stderr, "getTables(): SELECT (for PRIMARY KEY) failed. Explanation from backend: %s",
@ -1669,25 +1667,26 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
exit_nicely(g_conn); exit_nicely(g_conn);
} }
for (j = 0; j < PQntuples(res2); j++) str[0] = '\0';
{ for (j = 0; j < PQntuples(res2); j++)
if (strlen(str)>0) {
strcat(str, ", "); if (strlen(str)>0)
strcat(str, fmtId(PQgetvalue(res2, j, 0), force_quotes)); strcat(str, ", ");
} strcat(str, fmtId(PQgetvalue(res2, j, 0), force_quotes));
}
if (strlen(str)>0) { if (strlen(str)>0) {
tblinfo[i].primary_key = strdup(str); tblinfo[i].primary_key = strdup(str);
if (tblinfo[i].primary_key == NULL) { if (tblinfo[i].primary_key == NULL) {
perror("strdup"); perror("strdup");
exit(1); exit(1);
}
}
else
tblinfo[i].primary_key = NULL;
} }
} else
else tblinfo[i].primary_key = NULL;
tblinfo[i].primary_key = NULL;
}
else
tblinfo[i].primary_key = NULL;
/* Get Triggers */ /* Get Triggers */
if (tblinfo[i].ntrig > 0) if (tblinfo[i].ntrig > 0)
@ -1740,7 +1739,6 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
int tgnargs = atoi(PQgetvalue(res2, i2, i_tgnargs)); int tgnargs = atoi(PQgetvalue(res2, i2, i_tgnargs));
const char *tgargs = PQgetvalue(res2, i2, i_tgargs); const char *tgargs = PQgetvalue(res2, i2, i_tgargs);
const char *p; const char *p;
PQExpBuffer farg = createPQExpBuffer();
int findx; int findx;
for (findx = 0; findx < numFuncs; findx++) for (findx = 0; findx < numFuncs; findx++)
@ -1763,9 +1761,11 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
if (dropSchema) if (dropSchema)
{ {
resetPQExpBuffer(query); resetPQExpBuffer(query);
appendPQExpBuffer(query, "DROP TRIGGER %s ON %s;\n", appendPQExpBuffer(query, "DROP TRIGGER %s ",
fmtId(PQgetvalue(res2, i2, i_tgname), force_quotes), fmtId(PQgetvalue(res2, i2, i_tgname),
fmtId(tblinfo[i].relname, force_quotes)); force_quotes));
appendPQExpBuffer(query, "ON %s;\n",
fmtId(tblinfo[i].relname, force_quotes));
fputs(query->data, fout); fputs(query->data, fout);
} }
#endif #endif
@ -1800,8 +1800,10 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
else else
appendPQExpBuffer(query, " UPDATE"); appendPQExpBuffer(query, " UPDATE");
} }
appendPQExpBuffer(query, " ON %s FOR EACH ROW EXECUTE PROCEDURE %s (", appendPQExpBuffer(query, " ON %s FOR EACH ROW",
fmtId(tblinfo[i].relname, force_quotes), tgfunc); fmtId(tblinfo[i].relname, force_quotes));
appendPQExpBuffer(query, " EXECUTE PROCEDURE %s (",
fmtId(tgfunc, force_quotes));
for (findx = 0; findx < tgnargs; findx++) for (findx = 0; findx < tgnargs; findx++)
{ {
const char *s; const char *s;
@ -1827,15 +1829,14 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
break; break;
} }
p--; p--;
appendPQExpBufferChar(query, '\'');
for (s = tgargs; s < p;) for (s = tgargs; s < p;)
{ {
if (*s == '\'') if (*s == '\'')
appendPQExpBufferChar(farg, '\\'); appendPQExpBufferChar(query, '\\');
appendPQExpBufferChar(farg, *s++); appendPQExpBufferChar(query, *s++);
} }
appendPQExpBufferChar(query, '\''); appendPQExpBufferChar(query, '\'');
appendPQExpBuffer(query, farg->data);
appendPQExpBufferChar(query, '\'');
appendPQExpBuffer(query, (findx < tgnargs - 1) ? ", " : ""); appendPQExpBuffer(query, (findx < tgnargs - 1) ? ", " : "");
tgargs = p + 4; tgargs = p + 4;
} }
@ -2476,9 +2477,12 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
if (dropSchema) if (dropSchema)
{ {
resetPQExpBuffer(q); resetPQExpBuffer(q);
appendPQExpBuffer(q, "DROP OPERATOR %s (%s, %s);\n", oprinfo[i].oprname, appendPQExpBuffer(q, "DROP OPERATOR %s (%s", oprinfo[i].oprname,
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft), false), fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft),
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright), false)); false));
appendPQExpBuffer(q, ", %s);\n",
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright),
false));
fputs(q->data, fout); fputs(q->data, fout);
} }