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
@ -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) appendPQExpBufferChar(id_return, '\"');
for (cp = rawid; *cp != '\0'; cp++) for (cp = rawid; *cp; cp++)
if (!(islower(*cp) || isdigit(*cp) || (*cp == '_')))
break;
if (force_quotes || (*cp != '\0'))
{ {
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);
@ -1650,7 +1648,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
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);
@ -1669,6 +1667,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
exit_nicely(g_conn); exit_nicely(g_conn);
} }
str[0] = '\0';
for (j = 0; j < PQntuples(res2); j++) for (j = 0; j < PQntuples(res2); j++)
{ {
if (strlen(str)>0) if (strlen(str)>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,8 +1761,10 @@ 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),
force_quotes));
appendPQExpBuffer(query, "ON %s;\n",
fmtId(tblinfo[i].relname, force_quotes)); fmtId(tblinfo[i].relname, force_quotes));
fputs(query->data, fout); fputs(query->data, fout);
} }
@ -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);
} }