Skip to content

Commit d226469

Browse files
committed
Add public ruleutils.c entry point to deparse a Query.
This has no in-core callers but will be wanted by extensions. It's just a thin wrapper around get_query_def, so it adds little code. Also, fix get_from_clause_item() to force insertion of an alias for a SUBQUERY RTE item. This is irrelevant to existing uses because RTE_SUBQUERY items made by the parser always have aliases already. However, if one tried to use pg_get_querydef() to inspect a post-rewrite Query, it could be an issue. In any case, get_from_clause_item already contained logic to force alias insertion for VALUES items, so the lack of the same for SUBQUERY is a pretty clear oversight. In passing, replace duplicated code for selection of pretty-print options with a common macro. Julien Rouhaud, reviewed by Pavel Stehule, Gilles Darold, and myself Discussion: https://postgr.es/m/20210627041138.zklczwmu3ms4ufnk@nol
1 parent 386ca0a commit d226469

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
#define PRETTYFLAG_INDENT 0x0002
9191
#define PRETTYFLAG_SCHEMA 0x0004
9292

93+
/* Standard conversion of a "bool pretty" option to detailed flags */
94+
#define GET_PRETTY_FLAGS(pretty) \
95+
((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \
96+
: PRETTYFLAG_INDENT)
97+
9398
/* Default line length for pretty-print wrapping: 0 means wrap always */
9499
#define WRAP_COLUMN_DEFAULT 0
95100

@@ -532,7 +537,7 @@ pg_get_ruledef_ext(PG_FUNCTION_ARGS)
532537
int prettyFlags;
533538
char *res;
534539

535-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
540+
prettyFlags = GET_PRETTY_FLAGS(pretty);
536541

537542
res = pg_get_ruledef_worker(ruleoid, prettyFlags);
538543

@@ -653,7 +658,7 @@ pg_get_viewdef_ext(PG_FUNCTION_ARGS)
653658
int prettyFlags;
654659
char *res;
655660

656-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
661+
prettyFlags = GET_PRETTY_FLAGS(pretty);
657662

658663
res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
659664

@@ -673,7 +678,7 @@ pg_get_viewdef_wrap(PG_FUNCTION_ARGS)
673678
char *res;
674679

675680
/* calling this implies we want pretty printing */
676-
prettyFlags = PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA;
681+
prettyFlags = GET_PRETTY_FLAGS(true);
677682

678683
res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
679684

@@ -719,7 +724,7 @@ pg_get_viewdef_name_ext(PG_FUNCTION_ARGS)
719724
Oid viewoid;
720725
char *res;
721726

722-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
727+
prettyFlags = GET_PRETTY_FLAGS(pretty);
723728

724729
/* Look up view name. Can't lock it - we might not have privileges. */
725730
viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
@@ -1062,7 +1067,7 @@ pg_get_triggerdef_worker(Oid trigid, bool pretty)
10621067
context.windowClause = NIL;
10631068
context.windowTList = NIL;
10641069
context.varprefix = true;
1065-
context.prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
1070+
context.prettyFlags = GET_PRETTY_FLAGS(pretty);
10661071
context.wrapColumn = WRAP_COLUMN_DEFAULT;
10671072
context.indentLevel = PRETTYINDENT_STD;
10681073
context.special_exprkind = EXPR_KIND_NONE;
@@ -1152,7 +1157,7 @@ pg_get_indexdef_ext(PG_FUNCTION_ARGS)
11521157
int prettyFlags;
11531158
char *res;
11541159

1155-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
1160+
prettyFlags = GET_PRETTY_FLAGS(pretty);
11561161

11571162
res = pg_get_indexdef_worker(indexrelid, colno, NULL,
11581163
colno != 0, false,
@@ -1185,7 +1190,7 @@ pg_get_indexdef_columns(Oid indexrelid, bool pretty)
11851190
{
11861191
int prettyFlags;
11871192

1188-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
1193+
prettyFlags = GET_PRETTY_FLAGS(pretty);
11891194

11901195
return pg_get_indexdef_worker(indexrelid, 0, NULL,
11911196
true, true,
@@ -1516,6 +1521,30 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
15161521
return buf.data;
15171522
}
15181523

1524+
/* ----------
1525+
* pg_get_querydef
1526+
*
1527+
* Public entry point to deparse one query parsetree.
1528+
* The pretty flags are determined by GET_PRETTY_FLAGS(pretty).
1529+
*
1530+
* The result is a palloc'd C string.
1531+
* ----------
1532+
*/
1533+
char *
1534+
pg_get_querydef(Query *query, bool pretty)
1535+
{
1536+
StringInfoData buf;
1537+
int prettyFlags;
1538+
1539+
prettyFlags = GET_PRETTY_FLAGS(pretty);
1540+
1541+
initStringInfo(&buf);
1542+
1543+
get_query_def(query, &buf, NIL, NULL, prettyFlags, WRAP_COLUMN_DEFAULT, 0);
1544+
1545+
return buf.data;
1546+
}
1547+
15191548
/*
15201549
* pg_get_statisticsobjdef
15211550
* Get the definition of an extended statistics object
@@ -1848,7 +1877,7 @@ pg_get_partkeydef_columns(Oid relid, bool pretty)
18481877
{
18491878
int prettyFlags;
18501879

1851-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
1880+
prettyFlags = GET_PRETTY_FLAGS(pretty);
18521881

18531882
return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
18541883
}
@@ -2095,7 +2124,7 @@ pg_get_constraintdef_ext(PG_FUNCTION_ARGS)
20952124
int prettyFlags;
20962125
char *res;
20972126

2098-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
2127+
prettyFlags = GET_PRETTY_FLAGS(pretty);
20992128

21002129
res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
21012130

@@ -2625,7 +2654,7 @@ pg_get_expr_ext(PG_FUNCTION_ARGS)
26252654
int prettyFlags;
26262655
char *relname;
26272656

2628-
prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
2657+
prettyFlags = GET_PRETTY_FLAGS(pretty);
26292658

26302659
if (OidIsValid(relid))
26312660
{
@@ -11210,9 +11239,10 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1121011239
*/
1121111240
printalias = true;
1121211241
}
11213-
else if (rte->rtekind == RTE_VALUES)
11242+
else if (rte->rtekind == RTE_SUBQUERY ||
11243+
rte->rtekind == RTE_VALUES)
1121411244
{
11215-
/* Alias is syntactically required for VALUES */
11245+
/* Alias is syntactically required for SUBQUERY and VALUES */
1121611246
printalias = true;
1121711247
}
1121811248
else if (rte->rtekind == RTE_CTE)

src/include/utils/ruleutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct PlannedStmt;
2323

2424
extern char *pg_get_indexdef_string(Oid indexrelid);
2525
extern char *pg_get_indexdef_columns(Oid indexrelid, bool pretty);
26+
extern char *pg_get_querydef(Query *query, bool pretty);
2627

2728
extern char *pg_get_partkeydef_columns(Oid relid, bool pretty);
2829
extern char *pg_get_partconstrdef_string(Oid partitionId, char *aliasname);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy