Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 93a8c6f

Browse files
committed
Move and rename fmtReloptionsArray().
Move fmtReloptionsArray() from pg_dump.c to string_utils.c so that it is available to other frontend code. In particular psql's \ev and \sv commands need it to handle view reloptions. Also rename the function to appendReloptionsArray(), which is a more accurate description of what it does. Author: Dean Rasheed Reviewed-by: Peter Eisentraut Discussion: http://www.postgresql.org/message-id/CAEZATCWZjCgKRyM-agE0p8ax15j9uyQoF=qew7D2xB6cF76T8A@mail.gmail.com
1 parent 306ff0a commit 93a8c6f

File tree

3 files changed

+89
-63
lines changed

3 files changed

+89
-63
lines changed

src/bin/pg_dump/pg_dump.c

+14-63
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
261261
static const char *getAttrName(int attrnum, TableInfo *tblInfo);
262262
static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
263263
static bool nonemptyReloptions(const char *reloptions);
264-
static void fmtReloptionsArray(Archive *fout, PQExpBuffer buffer,
265-
const char *reloptions, const char *prefix);
264+
static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
265+
const char *prefix, Archive *fout);
266266
static char *get_synchronized_snapshot(Archive *fout);
267267
static PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, char *query);
268268
static void setupDumpWorker(Archive *AHX);
@@ -15046,7 +15046,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1504615046
if (nonemptyReloptions(tbinfo->reloptions))
1504715047
{
1504815048
appendPQExpBufferStr(q, " WITH (");
15049-
fmtReloptionsArray(fout, q, tbinfo->reloptions, "");
15049+
appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
1505015050
appendPQExpBufferChar(q, ')');
1505115051
}
1505215052
result = createViewAsClause(fout, tbinfo);
@@ -15301,13 +15301,14 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1530115301
if (nonemptyReloptions(tbinfo->reloptions))
1530215302
{
1530315303
addcomma = true;
15304-
fmtReloptionsArray(fout, q, tbinfo->reloptions, "");
15304+
appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
1530515305
}
1530615306
if (nonemptyReloptions(tbinfo->toast_reloptions))
1530715307
{
1530815308
if (addcomma)
1530915309
appendPQExpBufferStr(q, ", ");
15310-
fmtReloptionsArray(fout, q, tbinfo->toast_reloptions, "toast.");
15310+
appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
15311+
fout);
1531115312
}
1531215313
appendPQExpBufferChar(q, ')');
1531315314
}
@@ -15908,7 +15909,7 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1590815909
if (nonemptyReloptions(indxinfo->indreloptions))
1590915910
{
1591015911
appendPQExpBufferStr(q, " WITH (");
15911-
fmtReloptionsArray(fout, q, indxinfo->indreloptions, "");
15912+
appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
1591215913
appendPQExpBufferChar(q, ')');
1591315914
}
1591415915

@@ -16809,7 +16810,7 @@ dumpRule(Archive *fout, RuleInfo *rinfo)
1680916810
{
1681016811
appendPQExpBuffer(cmd, "ALTER VIEW %s SET (",
1681116812
fmtId(tbinfo->dobj.name));
16812-
fmtReloptionsArray(fout, cmd, rinfo->reloptions, "");
16813+
appendReloptionsArrayAH(cmd, rinfo->reloptions, "", fout);
1681316814
appendPQExpBufferStr(cmd, ");\n");
1681416815
}
1681516816

@@ -17707,67 +17708,17 @@ nonemptyReloptions(const char *reloptions)
1770717708
* Format a reloptions array and append it to the given buffer.
1770817709
*
1770917710
* "prefix" is prepended to the option names; typically it's "" or "toast.".
17710-
*
17711-
* Note: this logic should generally match the backend's flatten_reloptions()
17712-
* (in adt/ruleutils.c).
1771317711
*/
1771417712
static void
17715-
fmtReloptionsArray(Archive *fout, PQExpBuffer buffer, const char *reloptions,
17716-
const char *prefix)
17713+
appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
17714+
const char *prefix, Archive *fout)
1771717715
{
17718-
char **options;
17719-
int noptions;
17720-
int i;
17716+
bool res;
1772117717

17722-
if (!parsePGArray(reloptions, &options, &noptions))
17723-
{
17718+
res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
17719+
fout->std_strings);
17720+
if (!res)
1772417721
write_msg(NULL, "WARNING: could not parse reloptions array\n");
17725-
if (options)
17726-
free(options);
17727-
return;
17728-
}
17729-
17730-
for (i = 0; i < noptions; i++)
17731-
{
17732-
char *option = options[i];
17733-
char *name;
17734-
char *separator;
17735-
char *value;
17736-
17737-
/*
17738-
* Each array element should have the form name=value. If the "=" is
17739-
* missing for some reason, treat it like an empty value.
17740-
*/
17741-
name = option;
17742-
separator = strchr(option, '=');
17743-
if (separator)
17744-
{
17745-
*separator = '\0';
17746-
value = separator + 1;
17747-
}
17748-
else
17749-
value = "";
17750-
17751-
if (i > 0)
17752-
appendPQExpBufferStr(buffer, ", ");
17753-
appendPQExpBuffer(buffer, "%s%s=", prefix, fmtId(name));
17754-
17755-
/*
17756-
* In general we need to quote the value; but to avoid unnecessary
17757-
* clutter, do not quote if it is an identifier that would not need
17758-
* quoting. (We could also allow numbers, but that is a bit trickier
17759-
* than it looks --- for example, are leading zeroes significant? We
17760-
* don't want to assume very much here about what custom reloptions
17761-
* might mean.)
17762-
*/
17763-
if (strcmp(fmtId(value), value) == 0)
17764-
appendPQExpBufferStr(buffer, value);
17765-
else
17766-
appendStringLiteralAH(buffer, value, fout);
17767-
}
17768-
17769-
if (options)
17770-
free(options);
1777117722
}
1777217723

1777317724
/*

src/fe_utils/string_utils.c

+72
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,78 @@ parsePGArray(const char *atext, char ***itemarray, int *nitems)
461461
}
462462

463463

464+
/*
465+
* Format a reloptions array and append it to the given buffer.
466+
*
467+
* "prefix" is prepended to the option names; typically it's "" or "toast.".
468+
*
469+
* Returns false if the reloptions array could not be parsed (in which case
470+
* nothing will have been appended to the buffer), or true on success.
471+
*
472+
* Note: this logic should generally match the backend's flatten_reloptions()
473+
* (in adt/ruleutils.c).
474+
*/
475+
bool
476+
appendReloptionsArray(PQExpBuffer buffer, const char *reloptions,
477+
const char *prefix, int encoding, bool std_strings)
478+
{
479+
char **options;
480+
int noptions;
481+
int i;
482+
483+
if (!parsePGArray(reloptions, &options, &noptions))
484+
{
485+
if (options)
486+
free(options);
487+
return false;
488+
}
489+
490+
for (i = 0; i < noptions; i++)
491+
{
492+
char *option = options[i];
493+
char *name;
494+
char *separator;
495+
char *value;
496+
497+
/*
498+
* Each array element should have the form name=value. If the "=" is
499+
* missing for some reason, treat it like an empty value.
500+
*/
501+
name = option;
502+
separator = strchr(option, '=');
503+
if (separator)
504+
{
505+
*separator = '\0';
506+
value = separator + 1;
507+
}
508+
else
509+
value = "";
510+
511+
if (i > 0)
512+
appendPQExpBufferStr(buffer, ", ");
513+
appendPQExpBuffer(buffer, "%s%s=", prefix, fmtId(name));
514+
515+
/*
516+
* In general we need to quote the value; but to avoid unnecessary
517+
* clutter, do not quote if it is an identifier that would not need
518+
* quoting. (We could also allow numbers, but that is a bit trickier
519+
* than it looks --- for example, are leading zeroes significant? We
520+
* don't want to assume very much here about what custom reloptions
521+
* might mean.)
522+
*/
523+
if (strcmp(fmtId(value), value) == 0)
524+
appendPQExpBufferStr(buffer, value);
525+
else
526+
appendStringLiteral(buffer, value, encoding, std_strings);
527+
}
528+
529+
if (options)
530+
free(options);
531+
532+
return true;
533+
}
534+
535+
464536
/*
465537
* processSQLNamePattern
466538
*

src/include/fe_utils/string_utils.h

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ extern void appendByteaLiteral(PQExpBuffer buf,
4242

4343
extern bool parsePGArray(const char *atext, char ***itemarray, int *nitems);
4444

45+
extern bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions,
46+
const char *prefix, int encoding, bool std_strings);
47+
4548
extern bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf,
4649
const char *pattern,
4750
bool have_where, bool force_escape,

0 commit comments

Comments
 (0)