Commit 07c33ba7 authored by Tom Lane's avatar Tom Lane

pg_dump -d or -D didn't quote special characters in INSERT data

correctly.  -D had problems with tables with inherited fields, also.
parent 566c712c
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.100 1999/01/21 22:53:36 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.101 1999/02/08 01:46:28 tgl Exp $
* *
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
...@@ -321,51 +321,41 @@ static void ...@@ -321,51 +321,41 @@ static void
dumpClasses_dumpData(FILE *fout, const char *classname, dumpClasses_dumpData(FILE *fout, const char *classname,
const TableInfo tblinfo, bool oids) const TableInfo tblinfo, bool oids)
{ {
PGresult *res; PGresult *res;
char query[255];
int actual_atts; /* number of attrs in this a table */
char expandbuf[COPYBUFSIZ];
char q[MAXQUERYLEN]; char q[MAXQUERYLEN];
int tuple; int tuple;
int field; int field;
char *expsrc;
char *expdest;
sprintf(query, "SELECT * FROM %s", fmtId(classname, force_quotes)); sprintf(q, "SELECT * FROM %s", fmtId(classname, force_quotes));
res = PQexec(g_conn, query); res = PQexec(g_conn, q);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "dumpClasses(): command failed\n"); fprintf(stderr, "dumpClasses(): command failed\n");
exit_nicely(g_conn); exit_nicely(g_conn);
} }
tuple = 0; for (tuple = 0; tuple < PQntuples(res); tuple++)
while (tuple < PQntuples(res))
{ {
fprintf(fout, "INSERT INTO %s ", fmtId(classname, force_quotes)); fprintf(fout, "INSERT INTO %s ", fmtId(classname, force_quotes));
if (attrNames) if (attrNames)
{ {
int j;
actual_atts = 0;
sprintf(q, "("); sprintf(q, "(");
for (j = 0; j < tblinfo.numatts; j++) for (field = 0; field < PQnfields(res); field++)
{ {
if (tblinfo.inhAttrs[j] == 0) if (field > 0)
{ strcat(q, ",");
sprintf(q, "%s%s%s", strcat(q, fmtId(PQfname(res, field), force_quotes));
q,
(actual_atts > 0) ? "," : "",
fmtId(tblinfo.attnames[j], force_quotes));
actual_atts++;
}
} }
sprintf(q, "%s%s", q, ") "); strcat(q, ") ");
fprintf(fout, q); fprintf(fout, "%s", q);
} }
fprintf(fout, "values ("); fprintf(fout, "values (");
field = 0; for (field = 0; field < PQnfields(res); field++)
do
{ {
if (field > 0)
fprintf(fout, ",");
if (PQgetisnull(res, tuple, field)) if (PQgetisnull(res, tuple, field))
fprintf(fout, "NULL"); fprintf(fout, "NULL");
else else
...@@ -377,41 +367,44 @@ dumpClasses_dumpData(FILE *fout, const char *classname, ...@@ -377,41 +367,44 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
case OIDOID: /* int types */ case OIDOID: /* int types */
case FLOAT4OID: case FLOAT4OID:
case FLOAT8OID: /* float types */ case FLOAT8OID: /* float types */
/* These types are printed without quotes */
fprintf(fout, "%s", fprintf(fout, "%s",
PQgetvalue(res, tuple, field)); PQgetvalue(res, tuple, field));
break; break;
default: default:
/* All other types are printed as string literals,
* with appropriate escaping of special characters.
* Quote mark ' goes to '' per SQL standard,
* other stuff goes to \ sequences.
*/
expsrc = PQgetvalue(res, tuple, field);
expdest = q;
for (; *expsrc; expsrc++)
{ {
char *expsrc, char ch = *expsrc;
*expdest; if (ch == '\\' || ch == '\'')
/*
* Before outputting string value, expand all
* single quotes to twin single quotes - dhb -
* 6/11/96
*/
expsrc = PQgetvalue(res, tuple, field);
expdest = expandbuf;
while (*expsrc)
{ {
*expdest++ = *expsrc; *expdest++ = ch; /* double it */
if (*expsrc == (char) 0x27) /* single quote */ *expdest++ = ch;
*expdest++ = *expsrc;
expsrc++;
} }
*expdest = *expsrc; /* null term. */ else if (ch < '\040')
{
fprintf(fout, "'%s'", expandbuf); /* generate octal escape for control chars */
*expdest++ = '\\';
*expdest++ = ((ch >> 6) & 3) + '0';
*expdest++ = ((ch >> 3) & 7) + '0';
*expdest++ = (ch & 7) + '0';
}
else
*expdest++ = ch;
} }
*expdest = '\0';
fprintf(fout, "'%s'", q);
break; break;
} }
} }
field++; }
if (field != PQnfields(res))
fprintf(fout, ",");
} while (field < PQnfields(res));
fprintf(fout, ");\n"); fprintf(fout, ");\n");
tuple++;
} }
PQclear(res); PQclear(res);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment