Skip to content

Commit

Permalink
Cleanup the code structure a little to move the remote query out of
Browse files Browse the repository at this point in the history
BeginForeignScan and into IterateForeignScan, per recent discussion
on pgsql-hackers on Albe's Oracle FDW.
  • Loading branch information
dpage committed Aug 1, 2011
1 parent 106bd29 commit c8c3fc4
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions mysql_fdw.c
Expand Up @@ -84,7 +84,6 @@ typedef struct MySQLFdwExecutionState
{
MYSQL *conn;
MYSQL_RES *result;
AttInMetadata *attinmeta;
char *query;
} MySQLFdwExecutionState;

Expand Down Expand Up @@ -530,28 +529,8 @@ mysqlBeginForeignScan(ForeignScanState *node, int eflags)
festate = (MySQLFdwExecutionState *) palloc(sizeof(MySQLFdwExecutionState));
node->fdw_state = (void *) festate;
festate->conn = conn;
festate->result = NULL;
festate->query = query;

/* OK, we connected. If this is an EXPLAIN, bail out now */
if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
return;

/* Execute the query */
if (mysql_query(conn, query) != 0)
{
char *err = pstrdup(mysql_error(conn));
mysql_close(conn);
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to execute the MySQL query: %s", err)
));
}

/* Guess the query succeeded then */
festate->result = mysql_store_result(conn);

/* Store the additional state info */
festate->attinmeta = TupleDescGetAttInMetadata(node->ss.ss_currentRelation->rd_att);
}

/*
Expand All @@ -570,6 +549,23 @@ mysqlIterateForeignScan(ForeignScanState *node)
MySQLFdwExecutionState *festate = (MySQLFdwExecutionState *) node->fdw_state;
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;

/* Execute the query, if required */
if (!festate->result)
{
if (mysql_query(festate->conn, festate->query) != 0)
{
char *err = pstrdup(mysql_error(festate->conn));
mysql_close(festate->conn);
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to execute the MySQL query: %s", err)
));
}

/* Guess the query succeeded then */
festate->result = mysql_store_result(festate->conn);
}

/* Cleanup */
ExecClearTuple(slot);

Expand All @@ -582,7 +578,7 @@ mysqlIterateForeignScan(ForeignScanState *node)
for (x = 0; x < mysql_num_fields(festate->result); x++)
values[x] = row[x];

tuple = BuildTupleFromCStrings(festate->attinmeta, values);
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(node->ss.ss_currentRelation->rd_att), values);
ExecStoreTuple(tuple, slot, InvalidBuffer, false);
}

Expand All @@ -609,6 +605,12 @@ mysqlEndForeignScan(ForeignScanState *node)
mysql_close(festate->conn);
festate->conn = NULL;
}

if (festate->query)
{
pfree(festate->query);
festate->query = 0;
}
}

/*
Expand Down

0 comments on commit c8c3fc4

Please sign in to comment.