From c58cd58c63f333c0cc51a9b820c93585a4e603d7 Mon Sep 17 00:00:00 2001 From: Shigeru Hanada Date: Thu, 18 Nov 2010 18:54:43 +0900 Subject: [PATCH] Separate function to generate SQL statement in postgresql_fdw. Conflicts: contrib/postgresql_fdw/postgresql_fdw.c --- contrib/postgresql_fdw/postgresql_fdw.c | 45 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/contrib/postgresql_fdw/postgresql_fdw.c b/contrib/postgresql_fdw/postgresql_fdw.c index 49d2868b33..9aa460e29f 100644 --- a/contrib/postgresql_fdw/postgresql_fdw.c +++ b/contrib/postgresql_fdw/postgresql_fdw.c @@ -42,20 +42,19 @@ extern Datum postgresql_fdw_handler(PG_FUNCTION_ARGS); */ static FSConnection* pgConnectServer(ForeignServer *server, UserMapping *user); static void pgFreeFSConnection(FSConnection *conn); +static void pgEstimateCosts(ForeignPath *path, PlannerInfo *root, RelOptInfo *baserel); static void pgOpen(ForeignScanState *scanstate); static void pgBeginScan(ForeignScanState *scanstate); static void pgIterate(ForeignScanState *scanstate); static void pgClose(ForeignScanState *scanstate); static void pgReOpen(ForeignScanState *scanstate); -static void pgEstimateCosts(ForeignPath *path, PlannerInfo *root, RelOptInfo *baserel); /* helper for deparsing a request into SQL statement */ static bool is_immutable_func(Oid funcid); static bool is_foreign_qual(Expr *expr); static bool foreign_qual_walker(Node *node, void *context); -static void deparseSelectFrom(StringInfo sql, ForeignTable *table, - TupleDesc tupdesc, const char *aliasname, - bool prefix); +static void deparseSelectClause(StringInfo sql, ForeignTable *table, TupleDesc tupdesc, const char *aliasname, bool prefix); +static void deparseFromClause(StringInfo sql, ForeignTable *table, const char *aliasname, bool prefix); static char *deparseSql(ForeignScanState *scanstate); /* helper for handling result tuples */ @@ -240,20 +239,16 @@ foreign_qual_walker(Node *node, void *context) } /* - * Deparse the passed TupleDesc into SELECT & FROM clauses and append to sql. + * Deparse the passed TupleDesc into SELECT clauses and append to the buffer + * 'sql'. */ static void -deparseSelectFrom(StringInfo sql, ForeignTable *table, TupleDesc tupdesc, - const char *aliasname, bool prefix) +deparseSelectClause(StringInfo sql, ForeignTable *table, TupleDesc tupdesc, + const char *aliasname, bool prefix) { bool first; int i; - char *nspname = NULL; - char *relname = NULL; - const char *nspname_q; - const char *relname_q; const char *aliasname_q; - ListCell *lc; /* The alias of relation is used in both SELECT clause and FROM clause. */ aliasname_q = quote_identifier(aliasname); @@ -309,6 +304,27 @@ deparseSelectFrom(StringInfo sql, ForeignTable *table, TupleDesc tupdesc, if (first) appendStringInfo(sql, "NULL"); + if (aliasname_q != aliasname) + pfree((char *) aliasname_q); +} + +/* + * Deparse the passed information into FROM clauses and append to the buffer + * 'sql'. + */ +static void +deparseFromClause(StringInfo sql, ForeignTable *table, const char *aliasname, bool prefix) +{ + char *nspname = NULL; + char *relname = NULL; + const char *nspname_q; + const char *relname_q; + const char *aliasname_q; + ListCell *lc; + + /* The alias of relation is used in both SELECT clause and FROM clause. */ + aliasname_q = quote_identifier(aliasname); + /* deparse FROM clause */ appendStringInfo(sql, " FROM "); @@ -378,8 +394,9 @@ deparseSql(ForeignScanState *scanstate) prefix = list_length(estate->es_range_table) > 1; /* deparse SELECT and FROM clauses */ - deparseSelectFrom(&sql, table, scanstate->ss.ss_currentRelation->rd_att, - rte->eref->aliasname, prefix); + deparseSelectClause(&sql, table, scanstate->ss.ss_currentRelation->rd_att, + rte->eref->aliasname, prefix); + deparseFromClause(&sql, table, rte->eref->aliasname, prefix); /* * deparse WHERE cluase -- 2.39.5