diff --git a/.gitignore b/.gitignore index 0854728..f9136bf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /results/ *.o *.so +.idea +*.bc diff --git a/deparse.c b/deparse.c index 4284037..0fd74a9 100644 --- a/deparse.c +++ b/deparse.c @@ -446,12 +446,47 @@ mysql_deparse_from_expr(List *quals, deparse_expr_cxt *context) } } +/** + * convertReturningList() + * + * Generate RETURNING clause of a INSERT/UPDATE/DELETE ... RETURNING + * statement. + */ +static void +convertReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, + Relation rel, bool doNothing, List *returningList) +{ + bool first; + ListCell *lc; + + elog(ERROR, "entering function %s", __func__); + + /* Insert column names into the local query's RETURNING list */ + if (returningList) { + elog(ERROR, "entering function %s with returningList", __func__); + appendStringInfoString(buf, " RETURNING "); + first = true; + elog(ERROR, "query: %s", (char*)buf); + foreach(lc, returningList) + { + int attnum = lfirst_int(lc); + + if (!first) + appendStringInfoString(buf, ", "); + first = false; + + elog(ERROR, "query: %s", (char*)buf); + mysql_deparse_column_ref(buf, rtindex, attnum, root, true); + } + } +} + /* * Deparse remote INSERT statement */ void mysql_deparse_insert(StringInfo buf, PlannerInfo *root, Index rtindex, - Relation rel, List *targetAttrs, bool doNothing) + Relation rel, List *targetAttrs, bool doNothing, List *returningList) { ListCell *lc; #if PG_VERSION_NUM >= 140000 @@ -505,6 +540,10 @@ mysql_deparse_insert(StringInfo buf, PlannerInfo *root, Index rtindex, } else appendStringInfoString(buf, " DEFAULT VALUES"); + convertReturningList(buf, root, rtindex, rel, + doNothing, returningList); + elog(ERROR, "query: %s", (char*)buf); + } void diff --git a/mysql_fdw.c b/mysql_fdw.c index cc79855..8a02fb6 100644 --- a/mysql_fdw.c +++ b/mysql_fdw.c @@ -1593,6 +1593,7 @@ mysqlPlanForeignModify(PlannerInfo *root, RangeTblEntry *rte = planner_rt_fetch(resultRelation, root); Relation rel; List *targetAttrs = NIL; + List *returningList = NIL; StringInfoData sql; char *attname; Oid foreignTableId; @@ -1677,6 +1678,10 @@ mysqlPlanForeignModify(PlannerInfo *root, #else attname = get_relid_attribute_name(foreignTableId, 1); #endif + + /* Extract the relevant RETURNING list, if any */ + if (plan->returningLists) + returningList = (List *) list_nth(plan->returningLists, subplan_index); /* * Construct the SQL command string. @@ -1685,7 +1690,7 @@ mysqlPlanForeignModify(PlannerInfo *root, { case CMD_INSERT: mysql_deparse_insert(&sql, root, resultRelation, rel, targetAttrs, - doNothing); + doNothing, returningList); break; case CMD_UPDATE: mysql_deparse_update(&sql, root, resultRelation, rel, targetAttrs, @@ -1699,11 +1704,6 @@ mysqlPlanForeignModify(PlannerInfo *root, break; } - if (plan->returningLists) - ereport(ERROR, - (errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION), - errmsg("RETURNING is not supported by this FDW"))); - #if PG_VERSION_NUM < 130000 heap_close(rel, NoLock); #else diff --git a/mysql_fdw.h b/mysql_fdw.h index b66b756..a88d6f9 100644 --- a/mysql_fdw.h +++ b/mysql_fdw.h @@ -306,7 +306,7 @@ extern mysql_opt *mysql_get_options(Oid foreigntableid, bool is_foreigntable); /* depare.c headers */ extern void mysql_deparse_insert(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, - List *targetAttrs, bool doNothing); + List *targetAttrs, bool doNothing, List *returningList); extern void mysql_deparse_update(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, List *targetAttrs, char *attname); @@ -324,6 +324,8 @@ extern void mysql_deparse_select_stmt_for_rel(StringInfo buf, bool has_limit, List **retrieved_attrs, List **params_list); +//extern static void convertReturningList(StringInfo buf, PlannerInfo *root, Index rtindex, +// Relation rel, bool doNothing, List *returningList); extern const char *mysql_get_jointype_name(JoinType jointype); extern bool mysql_is_foreign_param(PlannerInfo *root, RelOptInfo *baserel, Expr *expr);