Skip to content

Commit e18d900

Browse files
author
Thomas G. Lockhart
committed
Adjust handling of data type names to suppress double quotes
unless necessary. Label internal bpchar types as "character" and varchar types as "character varying" to be less Postgres-specific. These types map to the SQL92 definitions anyway. Redefine g_force_quotes to be the local variable force_quotes. Pass this as an argument to fmtId(). These should help with handling the single-byte internal "char" type.
1 parent 239564e commit e18d900

File tree

3 files changed

+64
-52
lines changed

3 files changed

+64
-52
lines changed

src/bin/pg_dump/common.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.27 1998/10/06 22:14:17 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.28 1998/12/13 23:41:32 thomas Exp $
1111
*
1212
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1313
*
@@ -496,20 +496,20 @@ findFuncByName(FuncInfo *finfo, int numFuncs, const char *name)
496496
* returns pointer to input string or string surrounded by double quotes
497497
*
498498
* Note that the returned string should be used immediately since it
499-
* uses a static buffer to hold the string. Non-reentrant but fast.
499+
* uses a static buffer to hold the string. Non-reentrant but faster?
500500
*/
501501
const char *
502-
fmtId(const char *rawid)
502+
fmtId(const char *rawid, bool force_quotes)
503503
{
504504
const char *cp;
505505
static char id[MAXQUERYLEN];
506506

507-
if (! g_force_quotes)
507+
if (! force_quotes)
508508
for (cp = rawid; *cp != '\0'; cp++)
509509
if (!(islower(*cp) || isdigit(*cp) || (*cp == '_')))
510510
break;
511511

512-
if (g_force_quotes || (*cp != '\0'))
512+
if (force_quotes || (*cp != '\0'))
513513
{
514514
strcpy(id, "\"");
515515
strcat(id, rawid);

src/bin/pg_dump/pg_dump.c

+57-45
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*
2323
* IDENTIFICATION
24-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.96 1998/12/05 22:09:57 tgl Exp $
24+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.97 1998/12/13 23:41:32 thomas Exp $
2525
*
2626
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727
*
@@ -101,12 +101,13 @@ extern int optind,
101101
opterr;
102102

103103
/* global decls */
104-
bool g_force_quotes; /* User wants to suppress double-quotes */
105104
bool g_verbose; /* User wants verbose narration of our
106105
* activities. */
107106
int g_last_builtin_oid; /* value of the last builtin oid */
108107
FILE *g_fout; /* the script file */
109108
PGconn *g_conn; /* the database connection */
109+
110+
bool force_quotes; /* User wants to suppress double-quotes */
110111
int dumpData; /* dump data using proper insert strings */
111112
int attrNames; /* put attr names into insert strings */
112113
int schemaOnly;
@@ -228,14 +229,14 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids)
228229
if (oids)
229230
{
230231
fprintf(fout, "COPY %s WITH OIDS FROM stdin;\n",
231-
fmtId(classname));
232+
fmtId(classname, force_quotes));
232233
sprintf(query, "COPY %s WITH OIDS TO stdout;\n",
233-
fmtId(classname));
234+
fmtId(classname, force_quotes));
234235
}
235236
else
236237
{
237-
fprintf(fout, "COPY %s FROM stdin;\n", fmtId(classname));
238-
sprintf(query, "COPY %s TO stdout;\n", fmtId(classname));
238+
fprintf(fout, "COPY %s FROM stdin;\n", fmtId(classname, force_quotes));
239+
sprintf(query, "COPY %s TO stdout;\n", fmtId(classname, force_quotes));
239240
}
240241
res = PQexec(g_conn, query);
241242
if (!res ||
@@ -322,7 +323,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
322323
int tuple;
323324
int field;
324325

325-
sprintf(query, "SELECT * FROM %s", fmtId(classname));
326+
sprintf(query, "SELECT * FROM %s", fmtId(classname, force_quotes));
326327
res = PQexec(g_conn, query);
327328
if (!res ||
328329
PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -333,7 +334,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
333334
tuple = 0;
334335
while (tuple < PQntuples(res))
335336
{
336-
fprintf(fout, "INSERT INTO %s ", fmtId(classname));
337+
fprintf(fout, "INSERT INTO %s ", fmtId(classname, force_quotes));
337338
if (attrNames)
338339
{
339340
int j;
@@ -347,7 +348,7 @@ dumpClasses_dumpData(FILE *fout, const char *classname,
347348
sprintf(q, "%s%s%s",
348349
q,
349350
(actual_atts > 0) ? "," : "",
350-
fmtId(tblinfo.attnames[j]));
351+
fmtId(tblinfo.attnames[j], force_quotes));
351352
actual_atts++;
352353
}
353354
}
@@ -551,7 +552,7 @@ main(int argc, char **argv)
551552
int use_password = 0;
552553

553554
g_verbose = false;
554-
g_force_quotes = true;
555+
force_quotes = true;
555556

556557
strcpy(g_comment_start, "-- ");
557558
g_comment_end[0] = '\0';
@@ -583,10 +584,10 @@ main(int argc, char **argv)
583584
pghost = optarg;
584585
break;
585586
case 'n': /* Do not force double-quotes on identifiers */
586-
g_force_quotes = false;
587+
force_quotes = false;
587588
break;
588589
case 'N': /* Force double-quotes on identifiers */
589-
g_force_quotes = true;
590+
force_quotes = true;
590591
break;
591592
case 'o': /* Dump oids */
592593
oids = 1;
@@ -1555,7 +1556,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
15551556

15561557
query[0] = '\0';
15571558
if (name[0] != '$')
1558-
sprintf(query, "CONSTRAINT %s ", fmtId(name));
1559+
sprintf(query, "CONSTRAINT %s ", fmtId(name, force_quotes));
15591560
sprintf(query + strlen(query), "CHECK (%s)", expr);
15601561
tblinfo[i].check_expr[i2] = strdup(query);
15611562
}
@@ -1630,7 +1631,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16301631
exit_nicely(g_conn);
16311632
}
16321633
tgfunc = finfo[findx].proname;
1633-
sprintf(query, "CREATE TRIGGER %s ", fmtId(PQgetvalue(res2, i2, i_tgname)));
1634+
sprintf(query, "CREATE TRIGGER %s ", fmtId(PQgetvalue(res2, i2, i_tgname), force_quotes));
16341635
/* Trigger type */
16351636
findx = 0;
16361637
if (TRIGGER_FOR_BEFORE(tgtype))
@@ -1658,7 +1659,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16581659
strcat(query, " UPDATE");
16591660
}
16601661
sprintf(query, "%s ON %s FOR EACH ROW EXECUTE PROCEDURE %s (",
1661-
query, fmtId(tblinfo[i].relname), tgfunc);
1662+
query, fmtId(tblinfo[i].relname, force_quotes), tgfunc);
16621663
for (findx = 0; findx < tgnargs; findx++)
16631664
{
16641665
char *s,
@@ -2030,7 +2031,7 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
20302031
"CREATE TYPE %s "
20312032
"( internallength = %s, externallength = %s, input = %s, "
20322033
"output = %s, send = %s, receive = %s, default = '%s'",
2033-
fmtId(tinfo[i].typname),
2034+
fmtId(tinfo[i].typname, force_quotes),
20342035
tinfo[i].typlen,
20352036
tinfo[i].typprtlen,
20362037
tinfo[i].typinput,
@@ -2126,7 +2127,7 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
21262127
"HANDLER %s LANCOMPILER '%s';\n",
21272128
(PQgetvalue(res, i, i_lanpltrusted)[0] == 't') ? "TRUSTED " : "",
21282129
lanname,
2129-
fmtId(finfo[fidx].proname),
2130+
fmtId(finfo[fidx].proname, force_quotes),
21302131
lancompiler);
21312132

21322133
free(lanname);
@@ -2237,7 +2238,7 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
22372238
PQclear(res);
22382239
}
22392240

2240-
sprintf(q, "CREATE FUNCTION %s (", fmtId(finfo[i].proname));
2241+
sprintf(q, "CREATE FUNCTION %s (", fmtId(finfo[i].proname, force_quotes));
22412242
for (j = 0; j < finfo[i].nargs; j++)
22422243
{
22432244
char *typname;
@@ -2246,12 +2247,12 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
22462247
sprintf(q, "%s%s%s",
22472248
q,
22482249
(j > 0) ? "," : "",
2249-
fmtId(typname));
2250+
fmtId(typname, false));
22502251
}
22512252
sprintf(q, "%s ) RETURNS %s%s AS '%s' LANGUAGE '%s';\n",
22522253
q,
22532254
(finfo[i].retset) ? " SETOF " : "",
2254-
fmtId(findTypeByOid(tinfo, numTypes, finfo[i].prorettype)),
2255+
fmtId(findTypeByOid(tinfo, numTypes, finfo[i].prorettype), false),
22552256
func_def, func_lang);
22562257

22572258
fputs(q, fout);
@@ -2302,13 +2303,13 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
23022303
strcmp(oprinfo[i].oprkind, "b") == 0)
23032304
{
23042305
sprintf(leftarg, ", LEFTARG = %s ",
2305-
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft)));
2306+
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft), false));
23062307
}
23072308
if (strcmp(oprinfo[i].oprkind, "l") == 0 ||
23082309
strcmp(oprinfo[i].oprkind, "b") == 0)
23092310
{
23102311
sprintf(rightarg, ", RIGHTARG = %s ",
2311-
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright)));
2312+
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright), false));
23122313
}
23132314
if (strcmp(oprinfo[i].oprcom, "0") == 0)
23142315
commutator[0] = '\0';
@@ -2391,7 +2392,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
23912392

23922393
sprintf(basetype,
23932394
"BASETYPE = %s, ",
2394-
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype)));
2395+
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype), false));
23952396

23962397
if (strcmp(agginfo[i].aggtransfn1, "-") == 0)
23972398
sfunc1[0] = '\0';
@@ -2400,7 +2401,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
24002401
sprintf(sfunc1,
24012402
"SFUNC1 = %s, STYPE1 = %s",
24022403
agginfo[i].aggtransfn1,
2403-
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype1)));
2404+
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype1), false));
24042405
if (agginfo[i].agginitval1)
24052406
sprintf(sfunc1, "%s, INITCOND1 = '%s'",
24062407
sfunc1, agginfo[i].agginitval1);
@@ -2414,7 +2415,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
24142415
sprintf(sfunc2,
24152416
"SFUNC2 = %s, STYPE2 = %s",
24162417
agginfo[i].aggtransfn2,
2417-
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype2)));
2418+
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype2), false));
24182419
if (agginfo[i].agginitval2)
24192420
sprintf(sfunc2, "%s, INITCOND2 = '%s'",
24202421
sfunc2, agginfo[i].agginitval2);
@@ -2525,7 +2526,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
25252526
*/
25262527
fprintf(fout,
25272528
"REVOKE ALL on %s from PUBLIC;\n",
2528-
fmtId(tbinfo.relname));
2529+
fmtId(tbinfo.relname, force_quotes));
25292530

25302531
/* Make a working copy of acls so we can use strtok */
25312532
aclbuf = strdup(acls);
@@ -2556,7 +2557,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
25562557
{
25572558
fprintf(fout,
25582559
"GRANT %s on %s to ",
2559-
priv, fmtId(tbinfo.relname));
2560+
priv, fmtId(tbinfo.relname, force_quotes));
25602561
/* Note: fmtId() can only be called once per printf, so don't
25612562
* try to merge printing of username into the above printf.
25622563
*/
@@ -2568,7 +2569,7 @@ dumpACL(FILE *fout, TableInfo tbinfo)
25682569
else
25692570
{
25702571
*eqpos = '\0'; /* it's ok to clobber aclbuf */
2571-
fprintf(fout, "%s;\n", fmtId(tok));
2572+
fprintf(fout, "%s;\n", fmtId(tok, force_quotes));
25722573
}
25732574
}
25742575
free(priv);
@@ -2630,7 +2631,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26302631

26312632
becomeUser(fout, tblinfo[i].usename);
26322633

2633-
sprintf(q, "CREATE TABLE %s (\n\t", fmtId(tblinfo[i].relname));
2634+
sprintf(q, "CREATE TABLE %s (\n\t", fmtId(tblinfo[i].relname, force_quotes));
26342635
actual_atts = 0;
26352636
for (j = 0; j < tblinfo[i].numatts; j++)
26362637
{
@@ -2639,28 +2640,39 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26392640
if (actual_atts > 0)
26402641
strcat(q, ",\n\t");
26412642
sprintf(q + strlen(q), "%s ",
2642-
fmtId(tblinfo[i].attnames[j]));
2643+
fmtId(tblinfo[i].attnames[j], force_quotes));
26432644

26442645
/* Show lengths on bpchar and varchar */
26452646
if (!strcmp(tblinfo[i].typnames[j], "bpchar"))
26462647
{
2647-
sprintf(q + strlen(q), "char(%d)",
2648-
tblinfo[i].atttypmod[j] - VARHDRSZ);
2648+
int len = (tblinfo[i].atttypmod[j] - VARHDRSZ);
2649+
sprintf(q + strlen(q), "character");
2650+
if (len > 1)
2651+
sprintf(q + strlen(q), "(%d)",
2652+
tblinfo[i].atttypmod[j] - VARHDRSZ);
26492653
}
26502654
else if (!strcmp(tblinfo[i].typnames[j], "varchar"))
26512655
{
2652-
sprintf(q + strlen(q), "%s",
2653-
tblinfo[i].typnames[j]);
2656+
sprintf(q + strlen(q), "character varying");
26542657
if (tblinfo[i].atttypmod[j] != -1)
26552658
{
26562659
sprintf(q + strlen(q), "(%d)",
26572660
tblinfo[i].atttypmod[j] - VARHDRSZ);
26582661
}
26592662
}
2663+
/* char is an internal single-byte data type;
2664+
* Let's make sure we force it through with quotes.
2665+
* - thomas 1998-12-13
2666+
*/
2667+
else if (!strcmp(tblinfo[i].typnames[j], "char"))
2668+
{
2669+
sprintf(q + strlen(q), "%s",
2670+
fmtId(tblinfo[i].typnames[j], true));
2671+
}
26602672
else
26612673
{
26622674
sprintf(q + strlen(q), "%s",
2663-
fmtId(tblinfo[i].typnames[j]));
2675+
fmtId(tblinfo[i].typnames[j], false));
26642676
}
26652677
if (tblinfo[i].adef_expr[j] != NULL)
26662678
sprintf(q + strlen(q), " DEFAULT %s",
@@ -2689,7 +2701,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26892701
{
26902702
sprintf(q + strlen(q), "%s%s",
26912703
(k > 0) ? ", " : "",
2692-
fmtId(parentRels[k]));
2704+
fmtId(parentRels[k], force_quotes));
26932705
}
26942706
strcat(q, ")");
26952707
}
@@ -2807,7 +2819,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28072819
attname = tblinfo[tableInd].attnames[indkey];
28082820
if (funcname)
28092821
sprintf(attlist + strlen(attlist), "%s%s",
2810-
(k == 0) ? "" : ", ", fmtId(attname));
2822+
(k == 0) ? "" : ", ", fmtId(attname, force_quotes));
28112823
else
28122824
{
28132825
if (k >= nclass)
@@ -2817,8 +2829,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28172829
attname, indinfo[i].indexrelname);
28182830
exit_nicely(g_conn);
28192831
}
2820-
strcpy(id1, fmtId(attname));
2821-
strcpy(id2, fmtId(classname[k]));
2832+
strcpy(id1, fmtId(attname, force_quotes));
2833+
strcpy(id2, fmtId(classname[k], force_quotes));
28222834
sprintf(attlist + strlen(attlist), "%s%s %s",
28232835
(k == 0) ? "" : ", ", id1, id2);
28242836
free(classname[k]);
@@ -2833,8 +2845,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28332845
*/
28342846
becomeUser(fout, tblinfo[tableInd].usename);
28352847

2836-
strcpy(id1, fmtId(indinfo[i].indexrelname));
2837-
strcpy(id2, fmtId(indinfo[i].indrelname));
2848+
strcpy(id1, fmtId(indinfo[i].indexrelname, force_quotes));
2849+
strcpy(id2, fmtId(indinfo[i].indrelname, force_quotes));
28382850
fprintf(fout, "CREATE %s INDEX %s on %s using %s (",
28392851
(strcmp(indinfo[i].indisunique, "t") == 0) ? "UNIQUE" : "",
28402852
id1,
@@ -2843,8 +2855,8 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28432855
if (funcname)
28442856
{
28452857
/* need 2 printf's here cuz fmtId has static return area */
2846-
fprintf(fout, " %s", fmtId(funcname));
2847-
fprintf(fout, " (%s) %s );\n", attlist, fmtId(classname[0]));
2858+
fprintf(fout, " %s", fmtId(funcname, false));
2859+
fprintf(fout, " (%s) %s );\n", attlist, fmtId(classname[0], force_quotes));
28482860
free(funcname);
28492861
free(classname[0]);
28502862
}
@@ -3058,7 +3070,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
30583070
sprintf(query,
30593071
"SELECT sequence_name, last_value, increment_by, max_value, "
30603072
"min_value, cache_value, is_cycled, is_called from %s",
3061-
fmtId(tbinfo.relname));
3073+
fmtId(tbinfo.relname, force_quotes));
30623074

30633075
res = PQexec(g_conn, query);
30643076
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -3098,7 +3110,7 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
30983110
sprintf(query,
30993111
"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
31003112
"minvalue %d cache %d %s;\n",
3101-
fmtId(tbinfo.relname), last, incby, maxv, minv, cache,
3113+
fmtId(tbinfo.relname, force_quotes), last, incby, maxv, minv, cache,
31023114
(cycled == 't') ? "cycle" : "");
31033115

31043116
fputs(query, fout);

0 commit comments

Comments
 (0)