Support SQL92 delimited identifiers by checking some attribute names

for mixed-case and surrounding with double quotes.
This commit is contained in:
Thomas G. Lockhart 1997-10-30 16:48:03 +00:00
parent cc1b420cfd
commit 0dd738148c
3 changed files with 59 additions and 29 deletions

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.17 1997/10/02 13:57:03 vadim Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.18 1997/10/30 16:47:57 thomas 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
* *
@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <sys/param.h> /* for MAXHOSTNAMELEN on most */ #include <sys/param.h> /* for MAXHOSTNAMELEN on most */
#ifdef sparc_solaris #ifdef sparc_solaris
#include <netdb.h> /* for MAXHOSTNAMELEN on some */ #include <netdb.h> /* for MAXHOSTNAMELEN on some */
@ -478,3 +479,29 @@ isArchiveName(const char *relname)
{ {
return (strlen(relname) > 1 && relname[1] == ','); return (strlen(relname) > 1 && relname[1] == ',');
} }
/*
* fmtId
*
* checks input string for non-lowercase characters
* returns pointer to input string or string surrounded by double quotes
*/
const char *
fmtId(const char *rawid)
{
const char *cp;
static char id[MAXQUERYLEN];
for (cp = rawid; *cp != '\0'; cp++)
if (! (islower(*cp) || isdigit(*cp) || (*cp == '_'))) break;
if (*cp != '\0') {
strcpy(id, "\"");
strcat(id, rawid);
strcat(id, "\"");
cp = id;
} else {
cp = rawid;
}
return(cp);
} /* fmtId() */

View File

@ -21,7 +21,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.51 1997/10/30 03:59:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.52 1997/10/30 16:47:59 thomas Exp $
* *
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
@ -205,14 +205,14 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids)
if (oids) if (oids)
{ {
fprintf(fout, "COPY %s WITH OIDS FROM stdin;\n", fprintf(fout, "COPY %s WITH OIDS FROM stdin;\n",
classname); fmtId(classname));
sprintf(query, "COPY %s WITH OIDS TO stdout;\n", sprintf(query, "COPY %s WITH OIDS TO stdout;\n",
classname); fmtId(classname));
} }
else else
{ {
fprintf(fout, "COPY %s FROM stdin;\n", classname); fprintf(fout, "COPY %s FROM stdin;\n", fmtId(classname));
sprintf(query, "COPY %s TO stdout;\n", classname); sprintf(query, "COPY %s TO stdout;\n", fmtId(classname));
} }
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res) if (!res)
@ -309,7 +309,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
tuple = 0; tuple = 0;
while (tuple < PQntuples(res)) while (tuple < PQntuples(res))
{ {
fprintf(fout, "insert into %s ", classname); fprintf(fout, "insert into %s ", fmtId(classname));
if (attrNames) if (attrNames)
{ {
int j; int j;
@ -323,7 +323,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
sprintf(q, "%s%s%s", sprintf(q, "%s%s%s",
q, q,
(actual_atts > 0) ? "," : "", (actual_atts > 0) ? "," : "",
tblinfo.attnames[j]); fmtId(tblinfo.attnames[j]));
actual_atts++; actual_atts++;
} }
} }
@ -1944,12 +1944,12 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
sprintf(q, "%s%s%s", sprintf(q, "%s%s%s",
q, q,
(j > 0) ? "," : "", (j > 0) ? "," : "",
typname); fmtId(typname));
} }
sprintf(q, "%s ) RETURNS %s%s AS '%s' LANGUAGE '%s';\n", sprintf(q, "%s ) RETURNS %s%s AS '%s' LANGUAGE '%s';\n",
q, q,
(finfo[i].retset) ? " SETOF " : "", (finfo[i].retset) ? " SETOF " : "",
findTypeByOid(tinfo, numTypes, finfo[i].prorettype), fmtId(findTypeByOid(tinfo, numTypes, finfo[i].prorettype)),
(finfo[i].lang == INTERNALlanguageId) ? finfo[i].prosrc : (finfo[i].lang == INTERNALlanguageId) ? finfo[i].prosrc :
(finfo[i].lang == ClanguageId) ? finfo[i].probin : (finfo[i].lang == ClanguageId) ? finfo[i].probin :
(finfo[i].lang == SQLlanguageId) ? finfo[i].prosrc : "unknown", (finfo[i].lang == SQLlanguageId) ? finfo[i].prosrc : "unknown",
@ -2005,13 +2005,13 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
strcmp(oprinfo[i].oprkind, "b") == 0) strcmp(oprinfo[i].oprkind, "b") == 0)
{ {
sprintf(leftarg, ", LEFTARG = %s ", sprintf(leftarg, ", LEFTARG = %s ",
findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft)); fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft)));
} }
if (strcmp(oprinfo[i].oprkind, "l") == 0 || if (strcmp(oprinfo[i].oprkind, "l") == 0 ||
strcmp(oprinfo[i].oprkind, "b") == 0) strcmp(oprinfo[i].oprkind, "b") == 0)
{ {
sprintf(rightarg, ", RIGHTARG = %s ", sprintf(rightarg, ", RIGHTARG = %s ",
findTypeByOid(tinfo, numTypes, oprinfo[i].oprright)); fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright)));
} }
if (strcmp(oprinfo[i].oprcom, "0") == 0) if (strcmp(oprinfo[i].oprcom, "0") == 0)
commutator[0] = '\0'; commutator[0] = '\0';
@ -2094,7 +2094,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
sprintf(basetype, sprintf(basetype,
"BASETYPE = %s, ", "BASETYPE = %s, ",
findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype)); fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype)));
if (strcmp(agginfo[i].aggtransfn1, "-") == 0) if (strcmp(agginfo[i].aggtransfn1, "-") == 0)
sfunc1[0] = '\0'; sfunc1[0] = '\0';
@ -2103,7 +2103,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
sprintf(sfunc1, sprintf(sfunc1,
"SFUNC1 = %s, STYPE1 = %s", "SFUNC1 = %s, STYPE1 = %s",
agginfo[i].aggtransfn1, agginfo[i].aggtransfn1,
findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype1)); fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype1)));
if (agginfo[i].agginitval1) if (agginfo[i].agginitval1)
sprintf(sfunc1, "%s, INITCOND1 = '%s'", sprintf(sfunc1, "%s, INITCOND1 = '%s'",
sfunc1, agginfo[i].agginitval1); sfunc1, agginfo[i].agginitval1);
@ -2117,7 +2117,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
sprintf(sfunc2, sprintf(sfunc2,
"SFUNC2 = %s, STYPE2 = %s", "SFUNC2 = %s, STYPE2 = %s",
agginfo[i].aggtransfn2, agginfo[i].aggtransfn2,
findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype2)); fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype2)));
if (agginfo[i].agginitval2) if (agginfo[i].agginitval2)
sprintf(sfunc2, "%s, INITCOND2 = '%s'", sprintf(sfunc2, "%s, INITCOND2 = '%s'",
sfunc2, agginfo[i].agginitval2); sfunc2, agginfo[i].agginitval2);
@ -2213,7 +2213,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
fprintf(fout, "\\connect - %s\n", tblinfo[i].usename); fprintf(fout, "\\connect - %s\n", tblinfo[i].usename);
sprintf(q, "CREATE TABLE %s (", tblinfo[i].relname); sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname));
actual_atts = 0; actual_atts = 0;
for (j = 0; j < tblinfo[i].numatts; j++) for (j = 0; j < tblinfo[i].numatts; j++)
{ {
@ -2226,7 +2226,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
sprintf(q, "%s%s%s char", sprintf(q, "%s%s%s char",
q, q,
(actual_atts > 0) ? ", " : "", (actual_atts > 0) ? ", " : "",
tblinfo[i].attnames[j]); fmtId(tblinfo[i].attnames[j]));
/* stored length can be -1 (variable) */ /* stored length can be -1 (variable) */
if (tblinfo[i].attlen[j] > 0) if (tblinfo[i].attlen[j] > 0)
@ -2240,7 +2240,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
sprintf(q, "%s%s%s %s", sprintf(q, "%s%s%s %s",
q, q,
(actual_atts > 0) ? ", " : "", (actual_atts > 0) ? ", " : "",
tblinfo[i].attnames[j], fmtId(tblinfo[i].attnames[j]),
tblinfo[i].typnames[j]); tblinfo[i].typnames[j]);
/* stored length can be -1 (variable) */ /* stored length can be -1 (variable) */
@ -2255,8 +2255,8 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
sprintf(q, "%s%s%s %s", sprintf(q, "%s%s%s %s",
q, q,
(actual_atts > 0) ? ", " : "", (actual_atts > 0) ? ", " : "",
tblinfo[i].attnames[j], fmtId(tblinfo[i].attnames[j]),
tblinfo[i].typnames[j]); fmtId(tblinfo[i].typnames[j]));
actual_atts++; actual_atts++;
} }
if (tblinfo[i].adef_expr[j] != NULL) if (tblinfo[i].adef_expr[j] != NULL)
@ -2347,7 +2347,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
for (i = 0; i < numIndices; i++) for (i = 0; i < numIndices; i++)
{ {
tableInd = findTableByName(tblinfo, numTables, tableInd = findTableByName(tblinfo, numTables,
indinfo[i].indrelname); fmtId(indinfo[i].indrelname));
if (strcmp(indinfo[i].indproc, "0") == 0) if (strcmp(indinfo[i].indproc, "0") == 0)
{ {
@ -2420,7 +2420,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
attname = tblinfo[tableInd].attnames[indkey]; attname = tblinfo[tableInd].attnames[indkey];
if (funcname) if (funcname)
sprintf(attlist + strlen(attlist), "%s%s", sprintf(attlist + strlen(attlist), "%s%s",
(k == 0) ? "" : ", ", attname); (k == 0) ? "" : ", ", fmtId(attname));
else else
{ {
if (k >= nclass) if (k >= nclass)
@ -2431,7 +2431,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
exit_nicely(g_conn); exit_nicely(g_conn);
} }
sprintf(attlist + strlen(attlist), "%s%s %s", sprintf(attlist + strlen(attlist), "%s%s %s",
(k == 0) ? "" : ", ", attname, classname[k]); (k == 0) ? "" : ", ", fmtId(attname), fmtId(classname[k]));
free(classname[k]); free(classname[k]);
} }
} }
@ -2441,13 +2441,13 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
sprintf(q, "CREATE %s INDEX %s on %s using %s (", sprintf(q, "CREATE %s INDEX %s on %s using %s (",
(strcmp(indinfo[i].indisunique, "t") == 0) ? "UNIQUE" : "", (strcmp(indinfo[i].indisunique, "t") == 0) ? "UNIQUE" : "",
indinfo[i].indexrelname, fmtId(indinfo[i].indexrelname),
indinfo[i].indrelname, fmtId(indinfo[i].indrelname),
indinfo[i].indamname); indinfo[i].indamname);
if (funcname) if (funcname)
{ {
sprintf(q, "%s %s (%s) %s );\n", sprintf(q, "%s %s (%s) %s );\n",
q, funcname, attlist, classname[0]); q, funcname, attlist, fmtId(classname[0]));
free(funcname); free(funcname);
free(classname[0]); free(classname[0]);
} }
@ -2666,7 +2666,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
sprintf(query, sprintf(query,
"SELECT sequence_name, last_value, increment_by, max_value, " "SELECT sequence_name, last_value, increment_by, max_value, "
"min_value, cache_value, is_cycled, is_called from %s", "min_value, cache_value, is_cycled, is_called from %s",
tbinfo.relname); fmtId(tbinfo.relname));
res = PQexec(g_conn, query); res = PQexec(g_conn, query);
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
@ -2706,7 +2706,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
sprintf(query, sprintf(query,
"CREATE SEQUENCE %s start %d increment %d maxvalue %d " "CREATE SEQUENCE %s start %d increment %d maxvalue %d "
"minvalue %d cache %d %s;\n", "minvalue %d cache %d %s;\n",
tbinfo.relname, last, incby, maxv, minv, cache, fmtId(tbinfo.relname), last, incby, maxv, minv, cache,
(cycled == 't') ? "cycle" : ""); (cycled == 't') ? "cycle" : "");
fputs(query, fout); fputs(query, fout);

View File

@ -5,7 +5,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_dump.h,v 1.25 1997/10/02 13:57:07 vadim Exp $ * $Id: pg_dump.h,v 1.26 1997/10/30 16:48:03 thomas 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
* *
@ -233,5 +233,8 @@ extern void
dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices, dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
TableInfo *tbinfo, int numTables, const char *tablename); TableInfo *tbinfo, int numTables, const char *tablename);
extern const char *
fmtId(const char *identifier);
/* largest query string size */ /* largest query string size */
#define MAXQUERYLEN 5000 #define MAXQUERYLEN 5000