2001-06-14 20:49:03 +04:00
|
|
|
/*
|
|
|
|
* dblink.c
|
|
|
|
*
|
|
|
|
* Functions returning results from a remote database
|
|
|
|
*
|
2002-09-02 10:13:31 +04:00
|
|
|
* Joe Conway <mail@joeconway.com>
|
2003-06-25 05:10:15 +04:00
|
|
|
* And contributors:
|
|
|
|
* Darko Prenosil <Darko.Prenosil@finteh.hr>
|
|
|
|
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
|
2002-09-02 10:13:31 +04:00
|
|
|
*
|
2003-06-25 05:10:15 +04:00
|
|
|
* Copyright (c) 2001, 2002, 2003 by PostgreSQL Global Development Group
|
2002-04-24 06:28:28 +04:00
|
|
|
* ALL RIGHTS RESERVED;
|
2001-10-25 09:50:21 +04:00
|
|
|
*
|
2001-06-14 20:49:03 +04:00
|
|
|
* Permission to use, copy, modify, and distribute this software and its
|
|
|
|
* documentation for any purpose, without fee, and without a written agreement
|
|
|
|
* is hereby granted, provided that the above copyright notice and this
|
|
|
|
* paragraph and the following two paragraphs appear in all copies.
|
2001-10-25 09:50:21 +04:00
|
|
|
*
|
2001-06-14 20:49:03 +04:00
|
|
|
* IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
|
|
|
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
|
|
|
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2001-10-25 09:50:21 +04:00
|
|
|
*
|
2001-06-14 20:49:03 +04:00
|
|
|
* THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
*/
|
2002-09-02 10:13:31 +04:00
|
|
|
#include "postgres.h"
|
2003-07-24 21:52:50 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "funcapi.h"
|
|
|
|
#include "access/tupdesc.h"
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catname.h"
|
|
|
|
#include "catalog/namespace.h"
|
|
|
|
#include "catalog/pg_index.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "lib/stringinfo.h"
|
|
|
|
#include "nodes/nodes.h"
|
|
|
|
#include "nodes/execnodes.h"
|
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "parser/parse_type.h"
|
|
|
|
#include "tcop/tcopprot.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/fmgroids.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/syscache.h"
|
2003-06-25 05:10:15 +04:00
|
|
|
#include "utils/dynahash.h"
|
|
|
|
#include "utils/hsearch.h"
|
|
|
|
#include "utils/memutils.h"
|
2001-06-14 20:49:03 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
#include "dblink.h"
|
2002-05-28 01:59:12 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
typedef struct remoteConn
|
|
|
|
{
|
|
|
|
PGconn *con; /* Hold the remote connection */
|
|
|
|
bool remoteTrFlag; /* Indicates whether or not a transaction
|
|
|
|
* on remote database is in progress*/
|
|
|
|
} remoteConn;
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
/*
|
|
|
|
* Internal declarations
|
|
|
|
*/
|
2003-06-25 05:10:15 +04:00
|
|
|
static remoteConn *getConnectionByName(const char *name);
|
|
|
|
static HTAB *createConnHash(void);
|
2003-07-24 21:52:50 +04:00
|
|
|
static void createNewConnection(const char *name,remoteConn *con);
|
2003-06-25 05:10:15 +04:00
|
|
|
static void deleteConnection(const char *name);
|
2002-05-28 01:59:12 +04:00
|
|
|
static char **get_pkey_attnames(Oid relid, int16 *numatts);
|
|
|
|
static char *get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals);
|
|
|
|
static char *get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattvals);
|
|
|
|
static char *get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals);
|
|
|
|
static char *quote_literal_cstr(char *rawstr);
|
|
|
|
static char *quote_ident_cstr(char *rawstr);
|
|
|
|
static int16 get_attnum_pk_pos(int16 *pkattnums, int16 pknumatts, int16 key);
|
|
|
|
static HeapTuple get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals);
|
2002-09-05 00:31:48 +04:00
|
|
|
static Oid get_relid_from_relname(text *relname_text);
|
2002-09-02 10:13:31 +04:00
|
|
|
static TupleDesc pgresultGetTupleDesc(PGresult *res);
|
2002-11-23 21:59:25 +03:00
|
|
|
static char *generate_relation_name(Oid relid);
|
2002-05-28 01:59:12 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/* Global */
|
2003-06-25 05:10:15 +04:00
|
|
|
List *res_id = NIL;
|
|
|
|
int res_id_index = 0;
|
|
|
|
PGconn *persistent_conn = NULL;
|
|
|
|
static HTAB *remoteConnHash=NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Following is list that holds multiple remote connections.
|
|
|
|
Calling convention of each dblink function changes to accept
|
|
|
|
connection name as the first parameter. The connection list is
|
|
|
|
much like ecpg e.g. a mapping between a name and a PGconn object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct remoteConnHashEnt
|
|
|
|
{
|
|
|
|
char name[NAMEDATALEN];
|
|
|
|
remoteConn *rcon;
|
|
|
|
} remoteConnHashEnt;
|
|
|
|
|
|
|
|
/* initial number of connection hashes */
|
|
|
|
#define NUMCONN 16
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
/* general utility */
|
2002-09-02 10:13:31 +04:00
|
|
|
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
|
|
|
|
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
|
|
|
|
#define xpfree(var_) \
|
|
|
|
do { \
|
|
|
|
if (var_ != NULL) \
|
|
|
|
{ \
|
|
|
|
pfree(var_); \
|
|
|
|
var_ = NULL; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2003-07-24 21:52:50 +04:00
|
|
|
#define DBLINK_RES_INTERNALERROR(p2) \
|
|
|
|
do { \
|
|
|
|
msg = pstrdup(PQerrorMessage(conn)); \
|
|
|
|
if (res) \
|
|
|
|
PQclear(res); \
|
|
|
|
elog(ERROR, "%s: %s", p2, msg); \
|
|
|
|
} while (0)
|
|
|
|
#define DBLINK_RES_ERROR(p2) \
|
2003-06-25 05:10:15 +04:00
|
|
|
do { \
|
|
|
|
msg = pstrdup(PQerrorMessage(conn)); \
|
|
|
|
if (res) \
|
|
|
|
PQclear(res); \
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR), \
|
|
|
|
errmsg("%s", p2), \
|
|
|
|
errdetail("%s", msg))); \
|
2003-06-25 05:10:15 +04:00
|
|
|
} while (0)
|
2003-07-24 21:52:50 +04:00
|
|
|
#define DBLINK_CONN_NOT_AVAIL \
|
2003-06-25 05:10:15 +04:00
|
|
|
do { \
|
|
|
|
if(conname) \
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST), \
|
|
|
|
errmsg("connection \"%s\" not available", conname))); \
|
2003-06-25 05:10:15 +04:00
|
|
|
else \
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST), \
|
|
|
|
errmsg("connection not available"))); \
|
2003-06-25 05:10:15 +04:00
|
|
|
} while (0)
|
2003-07-24 21:52:50 +04:00
|
|
|
#define DBLINK_GET_CONN \
|
2003-06-25 05:10:15 +04:00
|
|
|
do { \
|
|
|
|
char *conname_or_str = GET_STR(PG_GETARG_TEXT_P(0)); \
|
|
|
|
rcon = getConnectionByName(conname_or_str); \
|
|
|
|
if(rcon) \
|
|
|
|
{ \
|
|
|
|
conn = rcon->con; \
|
|
|
|
freeconn = false; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
connstr = conname_or_str; \
|
|
|
|
conn = PQconnectdb(connstr); \
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD) \
|
|
|
|
{ \
|
|
|
|
msg = pstrdup(PQerrorMessage(conn)); \
|
|
|
|
PQfinish(conn); \
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), \
|
|
|
|
errmsg("could not establish connection"), \
|
|
|
|
errdetail("%s", msg))); \
|
2003-06-25 05:10:15 +04:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a persistent connection to another database
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_connect);
|
|
|
|
Datum
|
|
|
|
dblink_connect(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
char *connstr = NULL;
|
|
|
|
char *connname = NULL;
|
2002-09-05 00:31:48 +04:00
|
|
|
char *msg;
|
|
|
|
MemoryContext oldcontext;
|
2003-06-25 05:10:15 +04:00
|
|
|
PGconn *conn = NULL;
|
|
|
|
remoteConn *rcon = NULL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if(PG_NARGS()==2)
|
|
|
|
{
|
|
|
|
connstr = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
connname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
}
|
|
|
|
else if(PG_NARGS()==1)
|
|
|
|
connstr = GET_STR(PG_GETARG_TEXT_P(0));
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
if(connname)
|
|
|
|
rcon=(remoteConn *) palloc(sizeof(remoteConn));
|
|
|
|
conn = PQconnectdb(connstr);
|
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if (PQstatus(conn) == CONNECTION_BAD)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
msg = pstrdup(PQerrorMessage(conn));
|
|
|
|
PQfinish(conn);
|
|
|
|
if(rcon)
|
|
|
|
pfree(rcon);
|
2003-07-24 21:52:50 +04:00
|
|
|
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
|
|
|
|
errmsg("could not establish connection"),
|
|
|
|
errdetail("%s", msg)));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if(connname)
|
|
|
|
{
|
|
|
|
rcon->con = conn;
|
2003-07-24 21:52:50 +04:00
|
|
|
createNewConnection(connname, rcon);
|
2003-06-25 05:10:15 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
persistent_conn = conn;
|
|
|
|
|
|
|
|
PG_RETURN_TEXT_P(GET_TEXT("OK"));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear a persistent connection to another database
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_disconnect);
|
|
|
|
Datum
|
|
|
|
dblink_disconnect(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-07-24 21:52:50 +04:00
|
|
|
char *conname = NULL;
|
2003-06-25 05:10:15 +04:00
|
|
|
remoteConn *rcon = NULL;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
|
|
|
|
if (PG_NARGS() ==1 )
|
|
|
|
{
|
2003-07-24 21:52:50 +04:00
|
|
|
conname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
rcon = getConnectionByName(conname);
|
2003-06-25 05:10:15 +04:00
|
|
|
if (rcon)
|
|
|
|
conn = rcon->con;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
conn = persistent_conn;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if (!conn)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
PQfinish(conn);
|
|
|
|
if (rcon)
|
|
|
|
{
|
2003-07-24 21:52:50 +04:00
|
|
|
deleteConnection(conname);
|
2003-06-25 05:10:15 +04:00
|
|
|
pfree(rcon);
|
|
|
|
}
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT("OK"));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* opens a cursor using a persistent connection
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_open);
|
|
|
|
Datum
|
|
|
|
dblink_open(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
|
|
|
PGconn *conn = NULL;
|
2003-06-25 05:10:15 +04:00
|
|
|
char *curname = NULL;
|
|
|
|
char *sql = NULL;
|
|
|
|
char *conname = NULL;
|
2002-09-05 00:31:48 +04:00
|
|
|
StringInfo str = makeStringInfo();
|
2003-06-25 05:10:15 +04:00
|
|
|
remoteConn *rcon = NULL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if(PG_NARGS() == 2)
|
|
|
|
{
|
|
|
|
curname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
sql = GET_STR(PG_GETARG_TEXT_P(1));
|
2002-09-02 10:13:31 +04:00
|
|
|
conn = persistent_conn;
|
2003-06-25 05:10:15 +04:00
|
|
|
}
|
|
|
|
else if(PG_NARGS() == 3)
|
|
|
|
{
|
|
|
|
conname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
curname = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
sql = GET_STR(PG_GETARG_TEXT_P(2));
|
|
|
|
rcon = getConnectionByName(conname);
|
|
|
|
if (rcon)
|
|
|
|
conn = rcon->con;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
res = PQexec(conn, "BEGIN");
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_INTERNALERROR("begin error");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "DECLARE %s CURSOR FOR %s", curname, sql);
|
2002-09-02 10:13:31 +04:00
|
|
|
res = PQexec(conn, str->data);
|
|
|
|
if (!res ||
|
|
|
|
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
|
|
|
PQresultStatus(res) != PGRES_TUPLES_OK))
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_ERROR("sql error");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
PQclear(res);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT("OK"));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/*
|
|
|
|
* closes a cursor
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_close);
|
|
|
|
Datum
|
|
|
|
dblink_close(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
PGconn *conn = NULL;
|
|
|
|
PGresult *res = NULL;
|
2003-06-25 05:10:15 +04:00
|
|
|
char *curname = NULL;
|
|
|
|
char *conname = NULL;
|
2002-09-05 00:31:48 +04:00
|
|
|
StringInfo str = makeStringInfo();
|
|
|
|
char *msg;
|
2003-06-25 05:10:15 +04:00
|
|
|
remoteConn *rcon = NULL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if (PG_NARGS() == 1)
|
|
|
|
{
|
|
|
|
curname = GET_STR(PG_GETARG_TEXT_P(0));
|
2002-09-02 10:13:31 +04:00
|
|
|
conn = persistent_conn;
|
2003-06-25 05:10:15 +04:00
|
|
|
}
|
|
|
|
else if (PG_NARGS()==2)
|
|
|
|
{
|
|
|
|
conname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
curname = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
rcon = getConnectionByName(conname);
|
|
|
|
if(rcon)
|
|
|
|
conn = rcon->con;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "CLOSE %s", curname);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* close the cursor */
|
|
|
|
res = PQexec(conn, str->data);
|
2002-09-05 00:31:48 +04:00
|
|
|
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_ERROR("sql error");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/* commit the transaction */
|
|
|
|
res = PQexec(conn, "COMMIT");
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_INTERNALERROR("commit error");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT("OK"));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch results from an open cursor
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_fetch);
|
|
|
|
Datum
|
|
|
|
dblink_fetch(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
TupleDesc tupdesc = NULL;
|
|
|
|
int call_cntr;
|
|
|
|
int max_calls;
|
|
|
|
TupleTableSlot *slot;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
|
|
|
MemoryContext oldcontext;
|
2003-06-25 05:10:15 +04:00
|
|
|
char *conname = NULL;
|
|
|
|
remoteConn *rcon=NULL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* stuff done only on the first call of the function */
|
2002-09-05 00:31:48 +04:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
Oid functypeid;
|
|
|
|
char functyptype;
|
|
|
|
Oid funcid = fcinfo->flinfo->fn_oid;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
StringInfo str = makeStringInfo();
|
2003-06-25 05:10:15 +04:00
|
|
|
char *curname = NULL;
|
|
|
|
int howmany = 0;
|
|
|
|
|
|
|
|
if (PG_NARGS() == 3)
|
|
|
|
{
|
|
|
|
conname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
curname = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
howmany = PG_GETARG_INT32(2);
|
|
|
|
|
|
|
|
rcon = getConnectionByName(conname);
|
|
|
|
if(rcon)
|
|
|
|
conn = rcon->con;
|
|
|
|
}
|
|
|
|
else if (PG_NARGS() == 2)
|
|
|
|
{
|
|
|
|
curname = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
howmany = PG_GETARG_INT32(1);
|
|
|
|
conn = persistent_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!conn)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* create a function context for cross-call persistence */
|
2002-09-05 00:31:48 +04:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
/*
|
|
|
|
* switch to memory context appropriate for multiple function
|
|
|
|
* calls
|
|
|
|
*/
|
2002-09-02 10:13:31 +04:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "FETCH %d FROM %s", howmany, curname);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
res = PQexec(conn, str->data);
|
|
|
|
if (!res ||
|
|
|
|
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
|
|
|
PQresultStatus(res) != PGRES_TUPLES_OK))
|
|
|
|
{
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_ERROR("sql error");
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
|
|
|
|
{
|
|
|
|
/* cursor does not exist - closed already or bad name */
|
|
|
|
PQclear(res);
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_CURSOR_NAME),
|
|
|
|
errmsg("cursor \"%s\" does not exist", curname)));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx->max_calls = PQntuples(res);
|
|
|
|
|
|
|
|
/* got results, keep track of them */
|
|
|
|
funcctx->user_fctx = res;
|
|
|
|
|
|
|
|
/* fast track when no results */
|
|
|
|
if (funcctx->max_calls < 1)
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* check typtype to see if we have a predetermined return type */
|
|
|
|
functypeid = get_func_rettype(funcid);
|
|
|
|
functyptype = get_typtype(functypeid);
|
|
|
|
|
|
|
|
if (functyptype == 'c')
|
|
|
|
tupdesc = TypeGetTupleDesc(functypeid, NIL);
|
|
|
|
else if (functyptype == 'p' && functypeid == RECORDOID)
|
|
|
|
tupdesc = pgresultGetTupleDesc(res);
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "return type must be a row type");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* store needed metadata for subsequent calls */
|
|
|
|
slot = TupleDescGetSlot(tupdesc);
|
|
|
|
funcctx->slot = slot;
|
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-05 00:31:48 +04:00
|
|
|
}
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* stuff done on every call of the function */
|
2002-09-05 00:31:48 +04:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize per-call variables
|
|
|
|
*/
|
|
|
|
call_cntr = funcctx->call_cntr;
|
|
|
|
max_calls = funcctx->max_calls;
|
|
|
|
|
|
|
|
slot = funcctx->slot;
|
|
|
|
|
|
|
|
res = (PGresult *) funcctx->user_fctx;
|
|
|
|
attinmeta = funcctx->attinmeta;
|
|
|
|
tupdesc = attinmeta->tupdesc;
|
|
|
|
|
|
|
|
if (call_cntr < max_calls) /* do when there is more left to send */
|
2002-09-05 00:31:48 +04:00
|
|
|
{
|
2002-09-02 10:13:31 +04:00
|
|
|
char **values;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
2002-09-05 00:31:48 +04:00
|
|
|
int i;
|
|
|
|
int nfields = PQnfields(res);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
values = (char **) palloc(nfields * sizeof(char *));
|
|
|
|
for (i = 0; i < nfields; i++)
|
|
|
|
{
|
|
|
|
if (PQgetisnull(res, call_cntr, i) == 0)
|
|
|
|
values[i] = PQgetvalue(res, call_cntr, i);
|
|
|
|
else
|
|
|
|
values[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
|
|
|
|
/* make the tuple into a datum */
|
|
|
|
result = TupleGetDatum(slot, tuple);
|
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
2002-09-05 00:31:48 +04:00
|
|
|
else
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
/* do when there is no more left */
|
2002-09-02 10:13:31 +04:00
|
|
|
PQclear(res);
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: this is the new preferred version of dblink
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_record);
|
|
|
|
Datum
|
|
|
|
dblink_record(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
TupleDesc tupdesc = NULL;
|
|
|
|
int call_cntr;
|
|
|
|
int max_calls;
|
|
|
|
TupleTableSlot *slot;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
|
|
|
bool is_sql_cmd = false;
|
|
|
|
char *sql_cmd_status = NULL;
|
|
|
|
MemoryContext oldcontext;
|
2003-06-25 05:10:15 +04:00
|
|
|
bool freeconn = true;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* stuff done only on the first call of the function */
|
2002-09-05 00:31:48 +04:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
Oid functypeid;
|
|
|
|
char functyptype;
|
|
|
|
Oid funcid = fcinfo->flinfo->fn_oid;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
char *connstr = NULL;
|
|
|
|
char *sql = NULL;
|
2003-06-25 05:10:15 +04:00
|
|
|
char *conname = NULL;
|
|
|
|
remoteConn *rcon=NULL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* create a function context for cross-call persistence */
|
2002-09-05 00:31:48 +04:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
/*
|
|
|
|
* switch to memory context appropriate for multiple function
|
|
|
|
* calls
|
|
|
|
*/
|
2002-09-02 10:13:31 +04:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if (PG_NARGS() == 2)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_GET_CONN;
|
2002-09-02 10:13:31 +04:00
|
|
|
sql = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
}
|
2003-06-25 05:10:15 +04:00
|
|
|
else if (PG_NARGS() == 1)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
conn = persistent_conn;
|
2002-09-02 10:13:31 +04:00
|
|
|
sql = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
}
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "wrong number of arguments");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if(!conn)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2003-06-25 05:10:15 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
res = PQexec(conn, sql);
|
|
|
|
if (!res || (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK))
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_ERROR("sql error");
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
if (PQresultStatus(res) == PGRES_COMMAND_OK)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
is_sql_cmd = true;
|
|
|
|
|
|
|
|
/* need a tuple descriptor representing one TEXT column */
|
|
|
|
tupdesc = CreateTemplateTupleDesc(1, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "status",
|
|
|
|
TEXTOID, -1, 0, false);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
/*
|
|
|
|
* and save a copy of the command status string to return
|
|
|
|
* as our result tuple
|
|
|
|
*/
|
|
|
|
sql_cmd_status = PQcmdStatus(res);
|
|
|
|
funcctx->max_calls = 1;
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
else
|
2003-06-25 05:10:15 +04:00
|
|
|
funcctx->max_calls = PQntuples(res);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
/* got results, keep track of them */
|
|
|
|
funcctx->user_fctx = res;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
/* if needed, close the connection to the database and cleanup */
|
|
|
|
if (freeconn && PG_NARGS() == 2)
|
|
|
|
PQfinish(conn);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* fast track when no results */
|
|
|
|
if (funcctx->max_calls < 1)
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* check typtype to see if we have a predetermined return type */
|
|
|
|
functypeid = get_func_rettype(funcid);
|
|
|
|
functyptype = get_typtype(functypeid);
|
|
|
|
|
|
|
|
if (!is_sql_cmd)
|
|
|
|
{
|
|
|
|
if (functyptype == 'c')
|
|
|
|
tupdesc = TypeGetTupleDesc(functypeid, NIL);
|
|
|
|
else if (functyptype == 'p' && functypeid == RECORDOID)
|
|
|
|
tupdesc = pgresultGetTupleDesc(res);
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "return type must be a row type");
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* store needed metadata for subsequent calls */
|
|
|
|
slot = TupleDescGetSlot(tupdesc);
|
|
|
|
funcctx->slot = slot;
|
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-05 00:31:48 +04:00
|
|
|
}
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* stuff done on every call of the function */
|
2002-09-05 00:31:48 +04:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize per-call variables
|
|
|
|
*/
|
|
|
|
call_cntr = funcctx->call_cntr;
|
|
|
|
max_calls = funcctx->max_calls;
|
|
|
|
|
|
|
|
slot = funcctx->slot;
|
|
|
|
|
|
|
|
res = (PGresult *) funcctx->user_fctx;
|
|
|
|
attinmeta = funcctx->attinmeta;
|
|
|
|
tupdesc = attinmeta->tupdesc;
|
|
|
|
|
|
|
|
if (call_cntr < max_calls) /* do when there is more left to send */
|
2002-09-05 00:31:48 +04:00
|
|
|
{
|
2002-09-02 10:13:31 +04:00
|
|
|
char **values;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
|
|
|
|
|
|
|
if (!is_sql_cmd)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int i;
|
|
|
|
int nfields = PQnfields(res);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
values = (char **) palloc(nfields * sizeof(char *));
|
|
|
|
for (i = 0; i < nfields; i++)
|
|
|
|
{
|
|
|
|
if (PQgetisnull(res, call_cntr, i) == 0)
|
|
|
|
values[i] = PQgetvalue(res, call_cntr, i);
|
|
|
|
else
|
|
|
|
values[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values = (char **) palloc(1 * sizeof(char *));
|
|
|
|
values[0] = sql_cmd_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
|
|
|
|
/* make the tuple into a datum */
|
|
|
|
result = TupleGetDatum(slot, tuple);
|
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
2002-09-05 00:31:48 +04:00
|
|
|
else
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
/* do when there is no more left */
|
2002-09-02 10:13:31 +04:00
|
|
|
PQclear(res);
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute an SQL non-SELECT command
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_exec);
|
|
|
|
Datum
|
|
|
|
dblink_exec(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
2003-06-25 05:10:15 +04:00
|
|
|
text *sql_cmd_status = NULL;
|
2002-09-05 00:31:48 +04:00
|
|
|
TupleDesc tupdesc = NULL;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
char *connstr = NULL;
|
|
|
|
char *sql = NULL;
|
2003-06-25 05:10:15 +04:00
|
|
|
char *conname = NULL;
|
|
|
|
remoteConn *rcon=NULL;
|
|
|
|
bool freeconn = true;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if (PG_NARGS() == 2)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_GET_CONN;
|
2002-09-02 10:13:31 +04:00
|
|
|
sql = GET_STR(PG_GETARG_TEXT_P(1));
|
|
|
|
}
|
2003-06-25 05:10:15 +04:00
|
|
|
else if (PG_NARGS() == 1)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
conn = persistent_conn;
|
2002-09-02 10:13:31 +04:00
|
|
|
sql = GET_STR(PG_GETARG_TEXT_P(0));
|
|
|
|
}
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "wrong number of arguments");
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
if(!conn)
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
res = PQexec(conn, sql);
|
2003-06-25 05:10:15 +04:00
|
|
|
if (!res ||
|
|
|
|
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
|
|
|
PQresultStatus(res) != PGRES_TUPLES_OK))
|
2003-07-24 21:52:50 +04:00
|
|
|
DBLINK_RES_ERROR("sql error");
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
if (PQresultStatus(res) == PGRES_COMMAND_OK)
|
2002-09-02 10:13:31 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
/* need a tuple descriptor representing one TEXT column */
|
|
|
|
tupdesc = CreateTemplateTupleDesc(1, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "status",
|
|
|
|
TEXTOID, -1, 0, false);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
/*
|
|
|
|
* and save a copy of the command status string to return as
|
|
|
|
* our result tuple
|
|
|
|
*/
|
|
|
|
sql_cmd_status = GET_TEXT(PQcmdStatus(res));
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
|
|
|
|
errmsg("statement returning results not allowed")));
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/* if needed, close the connection to the database and cleanup */
|
2003-06-25 05:10:15 +04:00
|
|
|
if (freeconn && fcinfo->nargs == 2)
|
2002-09-02 10:13:31 +04:00
|
|
|
PQfinish(conn);
|
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(sql_cmd_status);
|
2002-09-02 10:13:31 +04:00
|
|
|
}
|
|
|
|
|
2001-06-18 23:09:50 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* dblink_get_pkey
|
2002-09-05 00:31:48 +04:00
|
|
|
*
|
2002-09-02 10:13:31 +04:00
|
|
|
* Return list of primary key fields for the supplied relation,
|
2002-04-24 06:28:28 +04:00
|
|
|
* or NULL if none exists.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_get_pkey);
|
|
|
|
Datum
|
|
|
|
dblink_get_pkey(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int16 numatts;
|
|
|
|
Oid relid;
|
|
|
|
char **results;
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
int32 call_cntr;
|
|
|
|
int32 max_calls;
|
|
|
|
TupleTableSlot *slot;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
MemoryContext oldcontext;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* stuff done only on the first call of the function */
|
2002-09-05 00:31:48 +04:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
TupleDesc tupdesc = NULL;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/* create a function context for cross-call persistence */
|
2002-09-05 00:31:48 +04:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
/*
|
|
|
|
* switch to memory context appropriate for multiple function
|
|
|
|
* calls
|
|
|
|
*/
|
2002-09-02 10:13:31 +04:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
/* convert relname to rel Oid */
|
|
|
|
relid = get_relid_from_relname(PG_GETARG_TEXT_P(0));
|
|
|
|
if (!OidIsValid(relid))
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
|
|
|
GET_STR(PG_GETARG_TEXT_P(0)))));
|
2002-09-05 00:31:48 +04:00
|
|
|
/*
|
|
|
|
* need a tuple descriptor representing one INT and one TEXT
|
|
|
|
* column
|
|
|
|
*/
|
2002-09-03 08:00:37 +04:00
|
|
|
tupdesc = CreateTemplateTupleDesc(2, false);
|
2002-09-02 10:13:31 +04:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "position",
|
|
|
|
INT4OID, -1, 0, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "colname",
|
|
|
|
TEXTOID, -1, 0, false);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* allocate a slot for a tuple with this tupdesc */
|
|
|
|
slot = TupleDescGetSlot(tupdesc);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* assign slot to function context */
|
|
|
|
funcctx->slot = slot;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Generate attribute metadata needed later to produce tuples from
|
|
|
|
* raw C strings
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
2002-09-02 10:13:31 +04:00
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
|
|
|
|
|
|
|
/* get an array of attnums */
|
|
|
|
results = get_pkey_attnames(relid, &numatts);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
if ((results != NULL) && (numatts > 0))
|
2002-04-24 06:28:28 +04:00
|
|
|
{
|
2002-09-02 10:13:31 +04:00
|
|
|
funcctx->max_calls = numatts;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* got results, keep track of them */
|
|
|
|
funcctx->user_fctx = results;
|
|
|
|
}
|
2002-09-05 00:31:48 +04:00
|
|
|
else
|
2003-06-25 05:10:15 +04:00
|
|
|
/* fast track when no results */
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-05 00:31:48 +04:00
|
|
|
}
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* stuff done on every call of the function */
|
2002-09-05 00:31:48 +04:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/*
|
|
|
|
* initialize per-call variables
|
|
|
|
*/
|
|
|
|
call_cntr = funcctx->call_cntr;
|
|
|
|
max_calls = funcctx->max_calls;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
slot = funcctx->slot;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
results = (char **) funcctx->user_fctx;
|
|
|
|
attinmeta = funcctx->attinmeta;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
if (call_cntr < max_calls) /* do when there is more left to send */
|
2002-09-05 00:31:48 +04:00
|
|
|
{
|
2002-09-02 10:13:31 +04:00
|
|
|
char **values;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
values = (char **) palloc(2 * sizeof(char *));
|
2002-09-05 00:31:48 +04:00
|
|
|
values[0] = (char *) palloc(12); /* sign, 10 digits, '\0' */
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
sprintf(values[0], "%d", call_cntr + 1);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
values[1] = results[call_cntr];
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* build the tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* make the tuple into a datum */
|
|
|
|
result = TupleGetDatum(slot, tuple);
|
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
2002-09-05 00:31:48 +04:00
|
|
|
else
|
2002-04-24 06:28:28 +04:00
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
/* do when there is no more left */
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-23 22:03:32 +04:00
|
|
|
#ifndef SHRT_MAX
|
|
|
|
#define SHRT_MAX (0x7FFF)
|
|
|
|
#endif
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* dblink_build_sql_insert
|
2002-09-05 00:31:48 +04:00
|
|
|
*
|
2002-04-24 06:28:28 +04:00
|
|
|
* Used to generate an SQL insert statement
|
|
|
|
* based on an existing tuple in a local relation.
|
|
|
|
* This is useful for selectively replicating data
|
|
|
|
* to another server via dblink.
|
|
|
|
*
|
|
|
|
* API:
|
|
|
|
* <relname> - name of local table of interest
|
|
|
|
* <pkattnums> - an int2vector of attnums which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <pknumatts> - number of attnums in pkattnums
|
|
|
|
* <src_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <tgt_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to build the string for execution remotely. These are substituted
|
|
|
|
* for their counterparts in src_pkattvals_arry
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_build_sql_insert);
|
|
|
|
Datum
|
|
|
|
dblink_build_sql_insert(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid;
|
2002-09-02 10:13:31 +04:00
|
|
|
text *relname_text;
|
|
|
|
int16 *pkattnums;
|
2002-09-23 22:03:32 +04:00
|
|
|
int pknumatts_tmp;
|
|
|
|
int16 pknumatts = 0;
|
2002-09-02 10:13:31 +04:00
|
|
|
char **src_pkattvals;
|
|
|
|
char **tgt_pkattvals;
|
|
|
|
ArrayType *src_pkattvals_arry;
|
|
|
|
ArrayType *tgt_pkattvals_arry;
|
2002-04-24 06:28:28 +04:00
|
|
|
int src_ndim;
|
2002-09-02 10:13:31 +04:00
|
|
|
int *src_dim;
|
2002-04-24 06:28:28 +04:00
|
|
|
int src_nitems;
|
|
|
|
int tgt_ndim;
|
2002-09-05 00:31:48 +04:00
|
|
|
int *tgt_dim;
|
2002-04-24 06:28:28 +04:00
|
|
|
int tgt_nitems;
|
|
|
|
int i;
|
2002-09-02 10:13:31 +04:00
|
|
|
char *ptr;
|
|
|
|
char *sql;
|
|
|
|
int16 typlen;
|
|
|
|
bool typbyval;
|
|
|
|
char typalign;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
relname_text = PG_GETARG_TEXT_P(0);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert relname to rel OID.
|
|
|
|
*/
|
2002-05-28 01:59:12 +04:00
|
|
|
relid = get_relid_from_relname(relname_text);
|
2002-04-24 06:28:28 +04:00
|
|
|
if (!OidIsValid(relid))
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
|
|
|
GET_STR(relname_text))));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
pkattnums = (int16 *) PG_GETARG_POINTER(1);
|
2002-09-23 22:03:32 +04:00
|
|
|
pknumatts_tmp = PG_GETARG_INT32(2);
|
|
|
|
if (pknumatts_tmp <= SHRT_MAX)
|
|
|
|
pknumatts = pknumatts_tmp;
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input for number of primary key " \
|
|
|
|
"attributes too large")));
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* There should be at least one key attribute
|
|
|
|
*/
|
|
|
|
if (pknumatts == 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("number of key attributes must be > 0")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
|
|
|
|
tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
|
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Source array is made up of key values that will be used to locate
|
|
|
|
* the tuple of interest from the local system.
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
|
|
|
src_ndim = ARR_NDIM(src_pkattvals_arry);
|
|
|
|
src_dim = ARR_DIMS(src_pkattvals_arry);
|
|
|
|
src_nitems = ArrayGetNItems(src_ndim, src_dim);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one source array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (src_nitems != pknumatts)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("source key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get array of pointers to c-strings from the input source array
|
|
|
|
*/
|
2002-08-26 21:54:02 +04:00
|
|
|
Assert(ARR_ELEMTYPE(src_pkattvals_arry) == TEXTOID);
|
2002-09-02 10:13:31 +04:00
|
|
|
get_typlenbyvalalign(ARR_ELEMTYPE(src_pkattvals_arry),
|
2002-09-05 00:31:48 +04:00
|
|
|
&typlen, &typbyval, &typalign);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
src_pkattvals = (char **) palloc(src_nitems * sizeof(char *));
|
|
|
|
ptr = ARR_DATA_PTR(src_pkattvals_arry);
|
|
|
|
for (i = 0; i < src_nitems; i++)
|
|
|
|
{
|
|
|
|
src_pkattvals[i] = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(ptr)));
|
2002-09-02 10:13:31 +04:00
|
|
|
ptr = att_addlength(ptr, typlen, PointerGetDatum(ptr));
|
|
|
|
ptr = (char *) att_align(ptr, typalign);
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Target array is made up of key values that will be used to build
|
|
|
|
* the SQL string for use on the remote system.
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
|
|
|
tgt_ndim = ARR_NDIM(tgt_pkattvals_arry);
|
|
|
|
tgt_dim = ARR_DIMS(tgt_pkattvals_arry);
|
|
|
|
tgt_nitems = ArrayGetNItems(tgt_ndim, tgt_dim);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one target array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (tgt_nitems != pknumatts)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("target key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get array of pointers to c-strings from the input target array
|
|
|
|
*/
|
2002-08-26 21:54:02 +04:00
|
|
|
Assert(ARR_ELEMTYPE(tgt_pkattvals_arry) == TEXTOID);
|
2002-09-02 10:13:31 +04:00
|
|
|
get_typlenbyvalalign(ARR_ELEMTYPE(tgt_pkattvals_arry),
|
2002-09-05 00:31:48 +04:00
|
|
|
&typlen, &typbyval, &typalign);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
tgt_pkattvals = (char **) palloc(tgt_nitems * sizeof(char *));
|
|
|
|
ptr = ARR_DATA_PTR(tgt_pkattvals_arry);
|
|
|
|
for (i = 0; i < tgt_nitems; i++)
|
|
|
|
{
|
|
|
|
tgt_pkattvals[i] = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(ptr)));
|
2002-09-02 10:13:31 +04:00
|
|
|
ptr = att_addlength(ptr, typlen, PointerGetDatum(ptr));
|
|
|
|
ptr = (char *) att_align(ptr, typalign);
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep work is finally done. Go get the SQL string.
|
|
|
|
*/
|
|
|
|
sql = get_sql_insert(relid, pkattnums, pknumatts, src_pkattvals, tgt_pkattvals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And send it
|
|
|
|
*/
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT(sql));
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dblink_build_sql_delete
|
2002-09-05 00:31:48 +04:00
|
|
|
*
|
2002-04-24 06:28:28 +04:00
|
|
|
* Used to generate an SQL delete statement.
|
|
|
|
* This is useful for selectively replicating a
|
|
|
|
* delete to another server via dblink.
|
|
|
|
*
|
|
|
|
* API:
|
|
|
|
* <relname> - name of remote table of interest
|
|
|
|
* <pkattnums> - an int2vector of attnums which will be used
|
|
|
|
* to identify the remote tuple of interest
|
|
|
|
* <pknumatts> - number of attnums in pkattnums
|
|
|
|
* <tgt_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to build the string for execution remotely.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_build_sql_delete);
|
|
|
|
Datum
|
|
|
|
dblink_build_sql_delete(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid;
|
2002-09-05 00:31:48 +04:00
|
|
|
text *relname_text;
|
|
|
|
int16 *pkattnums;
|
2002-09-23 22:03:32 +04:00
|
|
|
int pknumatts_tmp;
|
|
|
|
int16 pknumatts = 0;
|
2002-09-05 00:31:48 +04:00
|
|
|
char **tgt_pkattvals;
|
|
|
|
ArrayType *tgt_pkattvals_arry;
|
2002-04-24 06:28:28 +04:00
|
|
|
int tgt_ndim;
|
2002-09-05 00:31:48 +04:00
|
|
|
int *tgt_dim;
|
2002-04-24 06:28:28 +04:00
|
|
|
int tgt_nitems;
|
|
|
|
int i;
|
2002-09-05 00:31:48 +04:00
|
|
|
char *ptr;
|
|
|
|
char *sql;
|
2002-09-02 10:13:31 +04:00
|
|
|
int16 typlen;
|
|
|
|
bool typbyval;
|
|
|
|
char typalign;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
relname_text = PG_GETARG_TEXT_P(0);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert relname to rel OID.
|
|
|
|
*/
|
2002-05-28 01:59:12 +04:00
|
|
|
relid = get_relid_from_relname(relname_text);
|
2002-04-24 06:28:28 +04:00
|
|
|
if (!OidIsValid(relid))
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
|
|
|
GET_STR(relname_text))));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
pkattnums = (int16 *) PG_GETARG_POINTER(1);
|
2002-09-23 22:03:32 +04:00
|
|
|
pknumatts_tmp = PG_GETARG_INT32(2);
|
|
|
|
if (pknumatts_tmp <= SHRT_MAX)
|
|
|
|
pknumatts = pknumatts_tmp;
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input for number of primary key " \
|
|
|
|
"attributes too large")));
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* There should be at least one key attribute
|
|
|
|
*/
|
|
|
|
if (pknumatts == 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("number of key attributes must be > 0")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
|
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Target array is made up of key values that will be used to build
|
|
|
|
* the SQL string for use on the remote system.
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
|
|
|
tgt_ndim = ARR_NDIM(tgt_pkattvals_arry);
|
|
|
|
tgt_dim = ARR_DIMS(tgt_pkattvals_arry);
|
|
|
|
tgt_nitems = ArrayGetNItems(tgt_ndim, tgt_dim);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one target array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (tgt_nitems != pknumatts)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("target key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get array of pointers to c-strings from the input target array
|
|
|
|
*/
|
2002-08-26 21:54:02 +04:00
|
|
|
Assert(ARR_ELEMTYPE(tgt_pkattvals_arry) == TEXTOID);
|
2002-09-02 10:13:31 +04:00
|
|
|
get_typlenbyvalalign(ARR_ELEMTYPE(tgt_pkattvals_arry),
|
2002-09-05 00:31:48 +04:00
|
|
|
&typlen, &typbyval, &typalign);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
tgt_pkattvals = (char **) palloc(tgt_nitems * sizeof(char *));
|
|
|
|
ptr = ARR_DATA_PTR(tgt_pkattvals_arry);
|
|
|
|
for (i = 0; i < tgt_nitems; i++)
|
|
|
|
{
|
|
|
|
tgt_pkattvals[i] = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(ptr)));
|
2002-09-02 10:13:31 +04:00
|
|
|
ptr = att_addlength(ptr, typlen, PointerGetDatum(ptr));
|
|
|
|
ptr = (char *) att_align(ptr, typalign);
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep work is finally done. Go get the SQL string.
|
|
|
|
*/
|
|
|
|
sql = get_sql_delete(relid, pkattnums, pknumatts, tgt_pkattvals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And send it
|
|
|
|
*/
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT(sql));
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dblink_build_sql_update
|
2002-09-05 00:31:48 +04:00
|
|
|
*
|
2002-04-24 06:28:28 +04:00
|
|
|
* Used to generate an SQL update statement
|
|
|
|
* based on an existing tuple in a local relation.
|
|
|
|
* This is useful for selectively replicating data
|
|
|
|
* to another server via dblink.
|
|
|
|
*
|
|
|
|
* API:
|
|
|
|
* <relname> - name of local table of interest
|
|
|
|
* <pkattnums> - an int2vector of attnums which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <pknumatts> - number of attnums in pkattnums
|
|
|
|
* <src_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <tgt_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to build the string for execution remotely. These are substituted
|
|
|
|
* for their counterparts in src_pkattvals_arry
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_build_sql_update);
|
|
|
|
Datum
|
|
|
|
dblink_build_sql_update(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid;
|
2002-09-05 00:31:48 +04:00
|
|
|
text *relname_text;
|
|
|
|
int16 *pkattnums;
|
2002-09-23 22:03:32 +04:00
|
|
|
int pknumatts_tmp;
|
|
|
|
int16 pknumatts = 0;
|
2002-09-05 00:31:48 +04:00
|
|
|
char **src_pkattvals;
|
|
|
|
char **tgt_pkattvals;
|
|
|
|
ArrayType *src_pkattvals_arry;
|
|
|
|
ArrayType *tgt_pkattvals_arry;
|
2002-04-24 06:28:28 +04:00
|
|
|
int src_ndim;
|
2002-09-05 00:31:48 +04:00
|
|
|
int *src_dim;
|
2002-04-24 06:28:28 +04:00
|
|
|
int src_nitems;
|
|
|
|
int tgt_ndim;
|
2002-09-05 00:31:48 +04:00
|
|
|
int *tgt_dim;
|
2002-04-24 06:28:28 +04:00
|
|
|
int tgt_nitems;
|
|
|
|
int i;
|
2002-09-05 00:31:48 +04:00
|
|
|
char *ptr;
|
|
|
|
char *sql;
|
2002-09-02 10:13:31 +04:00
|
|
|
int16 typlen;
|
|
|
|
bool typbyval;
|
|
|
|
char typalign;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
relname_text = PG_GETARG_TEXT_P(0);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert relname to rel OID.
|
|
|
|
*/
|
2002-05-28 01:59:12 +04:00
|
|
|
relid = get_relid_from_relname(relname_text);
|
2002-04-24 06:28:28 +04:00
|
|
|
if (!OidIsValid(relid))
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
|
|
|
GET_STR(relname_text))));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
pkattnums = (int16 *) PG_GETARG_POINTER(1);
|
2002-09-23 22:03:32 +04:00
|
|
|
pknumatts_tmp = PG_GETARG_INT32(2);
|
|
|
|
if (pknumatts_tmp <= SHRT_MAX)
|
|
|
|
pknumatts = pknumatts_tmp;
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input for number of primary key " \
|
|
|
|
"attributes too large")));
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* There should be one source array key values for each key attnum
|
|
|
|
*/
|
|
|
|
if (pknumatts == 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("number of key attributes must be > 0")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
|
|
|
|
tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
|
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Source array is made up of key values that will be used to locate
|
|
|
|
* the tuple of interest from the local system.
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
|
|
|
src_ndim = ARR_NDIM(src_pkattvals_arry);
|
|
|
|
src_dim = ARR_DIMS(src_pkattvals_arry);
|
|
|
|
src_nitems = ArrayGetNItems(src_ndim, src_dim);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one source array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (src_nitems != pknumatts)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("source key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get array of pointers to c-strings from the input source array
|
|
|
|
*/
|
2002-08-26 21:54:02 +04:00
|
|
|
Assert(ARR_ELEMTYPE(src_pkattvals_arry) == TEXTOID);
|
2002-09-02 10:13:31 +04:00
|
|
|
get_typlenbyvalalign(ARR_ELEMTYPE(src_pkattvals_arry),
|
2002-09-05 00:31:48 +04:00
|
|
|
&typlen, &typbyval, &typalign);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
src_pkattvals = (char **) palloc(src_nitems * sizeof(char *));
|
|
|
|
ptr = ARR_DATA_PTR(src_pkattvals_arry);
|
|
|
|
for (i = 0; i < src_nitems; i++)
|
|
|
|
{
|
|
|
|
src_pkattvals[i] = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(ptr)));
|
2002-09-02 10:13:31 +04:00
|
|
|
ptr = att_addlength(ptr, typlen, PointerGetDatum(ptr));
|
|
|
|
ptr = (char *) att_align(ptr, typalign);
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Target array is made up of key values that will be used to build
|
|
|
|
* the SQL string for use on the remote system.
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
|
|
|
tgt_ndim = ARR_NDIM(tgt_pkattvals_arry);
|
|
|
|
tgt_dim = ARR_DIMS(tgt_pkattvals_arry);
|
|
|
|
tgt_nitems = ArrayGetNItems(tgt_ndim, tgt_dim);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one target array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (tgt_nitems != pknumatts)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("target key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get array of pointers to c-strings from the input target array
|
|
|
|
*/
|
2002-08-26 21:54:02 +04:00
|
|
|
Assert(ARR_ELEMTYPE(tgt_pkattvals_arry) == TEXTOID);
|
2002-09-02 10:13:31 +04:00
|
|
|
get_typlenbyvalalign(ARR_ELEMTYPE(tgt_pkattvals_arry),
|
2002-09-05 00:31:48 +04:00
|
|
|
&typlen, &typbyval, &typalign);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
tgt_pkattvals = (char **) palloc(tgt_nitems * sizeof(char *));
|
|
|
|
ptr = ARR_DATA_PTR(tgt_pkattvals_arry);
|
|
|
|
for (i = 0; i < tgt_nitems; i++)
|
|
|
|
{
|
|
|
|
tgt_pkattvals[i] = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(ptr)));
|
2002-09-02 10:13:31 +04:00
|
|
|
ptr = att_addlength(ptr, typlen, PointerGetDatum(ptr));
|
|
|
|
ptr = (char *) att_align(ptr, typalign);
|
2001-06-18 23:09:50 +04:00
|
|
|
}
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep work is finally done. Go get the SQL string.
|
|
|
|
*/
|
|
|
|
sql = get_sql_update(relid, pkattnums, pknumatts, src_pkattvals, tgt_pkattvals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And send it
|
|
|
|
*/
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT(sql));
|
2001-06-14 20:49:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-04-24 06:28:28 +04:00
|
|
|
* dblink_current_query
|
|
|
|
* return the current query string
|
|
|
|
* to allow its use in (among other things)
|
|
|
|
* rewrite rules
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_current_query);
|
|
|
|
Datum
|
|
|
|
dblink_current_query(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
PG_RETURN_TEXT_P(GET_TEXT(debug_query_string));
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************
|
2001-06-14 20:49:03 +04:00
|
|
|
* internal functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* get_pkey_attnames
|
2002-09-05 00:31:48 +04:00
|
|
|
*
|
2002-04-24 06:28:28 +04:00
|
|
|
* Get the primary key attnames for the given relation.
|
|
|
|
* Return NULL, and set numatts = 0, if no primary key exists.
|
|
|
|
*/
|
2002-05-28 01:59:12 +04:00
|
|
|
static char **
|
2002-04-24 06:28:28 +04:00
|
|
|
get_pkey_attnames(Oid relid, int16 *numatts)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
Relation indexRelation;
|
|
|
|
ScanKeyData entry;
|
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple indexTuple;
|
|
|
|
int i;
|
|
|
|
char **result = NULL;
|
|
|
|
Relation rel;
|
|
|
|
TupleDesc tupdesc;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* open relation using relid, get tupdesc */
|
2002-04-24 06:28:28 +04:00
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* initialize numatts to 0 in case no primary key exists */
|
2002-04-24 06:28:28 +04:00
|
|
|
*numatts = 0;
|
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* use relid to get all related indexes */
|
2002-04-24 06:28:28 +04:00
|
|
|
indexRelation = heap_openr(IndexRelationName, AccessShareLock);
|
|
|
|
ScanKeyEntryInitialize(&entry, 0, Anum_pg_index_indrelid,
|
|
|
|
F_OIDEQ, ObjectIdGetDatum(relid));
|
2002-05-21 03:51:44 +04:00
|
|
|
scan = heap_beginscan(indexRelation, SnapshotNow, 1, &entry);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-05-21 03:51:44 +04:00
|
|
|
while ((indexTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
2002-04-24 06:28:28 +04:00
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
Form_pg_index index = (Form_pg_index) GETSTRUCT(indexTuple);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
/* we're only interested if it is the primary key */
|
2002-04-24 06:28:28 +04:00
|
|
|
if (index->indisprimary == TRUE)
|
|
|
|
{
|
2003-06-25 05:10:15 +04:00
|
|
|
i = 0;
|
|
|
|
while (index->indkey[i++] != 0)
|
|
|
|
(*numatts)++;
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
if (*numatts > 0)
|
|
|
|
{
|
|
|
|
result = (char **) palloc(*numatts * sizeof(char *));
|
2002-09-02 10:13:31 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
for (i = 0; i < *numatts; i++)
|
|
|
|
result[i] = SPI_fname(tupdesc, index->indkey[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(indexRelation, AccessShareLock);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
static char *
|
2002-04-24 06:28:28 +04:00
|
|
|
get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int natts;
|
|
|
|
StringInfo str = makeStringInfo();
|
|
|
|
char *sql;
|
|
|
|
char *val;
|
|
|
|
int16 key;
|
|
|
|
int i;
|
|
|
|
bool needComma;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
|
|
|
|
|
|
|
tuple = get_tuple_of_interest(relid, pkattnums, pknumatts, src_pkattvals);
|
2002-09-02 10:13:31 +04:00
|
|
|
if (!tuple)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CARDINALITY_VIOLATION),
|
|
|
|
errmsg("source row not found")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "INSERT INTO %s(", relname);
|
2002-08-02 22:15:10 +04:00
|
|
|
|
|
|
|
needComma = false;
|
2002-04-24 06:28:28 +04:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needComma)
|
2002-04-24 06:28:28 +04:00
|
|
|
appendStringInfo(str, ",");
|
|
|
|
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s",
|
2002-09-05 00:31:48 +04:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
|
2002-08-02 22:15:10 +04:00
|
|
|
needComma = true;
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfo(str, ") VALUES(");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remember attvals are 1 based
|
|
|
|
*/
|
2002-08-02 22:15:10 +04:00
|
|
|
needComma = false;
|
2002-04-24 06:28:28 +04:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needComma)
|
2002-04-24 06:28:28 +04:00
|
|
|
appendStringInfo(str, ",");
|
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
|
|
|
key = get_attnum_pk_pos(pkattnums, pknumatts, i + 1);
|
|
|
|
else
|
|
|
|
key = -1;
|
|
|
|
|
|
|
|
if (key > -1)
|
|
|
|
val = pstrdup(tgt_pkattvals[key]);
|
|
|
|
else
|
|
|
|
val = SPI_getvalue(tuple, tupdesc, i + 1);
|
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s", quote_literal_cstr(val));
|
2002-04-24 06:28:28 +04:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendStringInfo(str, "NULL");
|
2002-08-02 22:15:10 +04:00
|
|
|
needComma = true;
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
appendStringInfo(str, ")");
|
|
|
|
|
|
|
|
sql = pstrdup(str->data);
|
|
|
|
pfree(str->data);
|
|
|
|
pfree(str);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
return (sql);
|
|
|
|
}
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
static char *
|
2002-04-24 06:28:28 +04:00
|
|
|
get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattvals)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int natts;
|
|
|
|
StringInfo str = makeStringInfo();
|
|
|
|
char *sql;
|
2003-07-24 21:52:50 +04:00
|
|
|
char *val = NULL;
|
2002-09-05 00:31:48 +04:00
|
|
|
int i;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "DELETE FROM %s WHERE ", relname);
|
2002-04-24 06:28:28 +04:00
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int16 pkattnum = pkattnums[i];
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
appendStringInfo(str, " AND ");
|
|
|
|
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s",
|
2002-09-05 00:31:48 +04:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
|
|
|
val = pstrdup(tgt_pkattvals[i]);
|
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "target key array must not be NULL");
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, " = %s", quote_literal_cstr(val));
|
2002-04-24 06:28:28 +04:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, " IS NULL");
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sql = pstrdup(str->data);
|
|
|
|
pfree(str->data);
|
|
|
|
pfree(str);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
return (sql);
|
|
|
|
}
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
static char *
|
2002-04-24 06:28:28 +04:00
|
|
|
get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int natts;
|
|
|
|
StringInfo str = makeStringInfo();
|
|
|
|
char *sql;
|
|
|
|
char *val;
|
|
|
|
int16 key;
|
|
|
|
int i;
|
|
|
|
bool needComma;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
|
|
|
|
|
|
|
tuple = get_tuple_of_interest(relid, pkattnums, pknumatts, src_pkattvals);
|
2002-09-02 10:13:31 +04:00
|
|
|
if (!tuple)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CARDINALITY_VIOLATION),
|
|
|
|
errmsg("source row not found")));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "UPDATE %s SET ", relname);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-08-02 22:15:10 +04:00
|
|
|
needComma = false;
|
2002-04-24 06:28:28 +04:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needComma)
|
|
|
|
appendStringInfo(str, ", ");
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s = ",
|
2002-09-05 00:31:48 +04:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
|
|
|
key = get_attnum_pk_pos(pkattnums, pknumatts, i + 1);
|
|
|
|
else
|
|
|
|
key = -1;
|
|
|
|
|
|
|
|
if (key > -1)
|
|
|
|
val = pstrdup(tgt_pkattvals[key]);
|
|
|
|
else
|
|
|
|
val = SPI_getvalue(tuple, tupdesc, i + 1);
|
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s", quote_literal_cstr(val));
|
2002-04-24 06:28:28 +04:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendStringInfo(str, "NULL");
|
2002-08-02 22:15:10 +04:00
|
|
|
needComma = true;
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfo(str, " WHERE ");
|
|
|
|
|
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int16 pkattnum = pkattnums[i];
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
appendStringInfo(str, " AND ");
|
|
|
|
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s",
|
2002-09-05 00:31:48 +04:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
|
|
|
val = pstrdup(tgt_pkattvals[i]);
|
|
|
|
else
|
|
|
|
val = SPI_getvalue(tuple, tupdesc, pkattnum);
|
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, " = %s", quote_literal_cstr(val));
|
2002-04-24 06:28:28 +04:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, " IS NULL");
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sql = pstrdup(str->data);
|
|
|
|
pfree(str->data);
|
|
|
|
pfree(str);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
return (sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a properly quoted literal value.
|
|
|
|
* Uses quote_literal in quote.c
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
quote_literal_cstr(char *rawstr)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
text *rawstr_text;
|
|
|
|
text *result_text;
|
|
|
|
char *result;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr)));
|
|
|
|
result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(rawstr_text)));
|
|
|
|
result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text)));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a properly quoted identifier.
|
|
|
|
* Uses quote_ident in quote.c
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
quote_ident_cstr(char *rawstr)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
text *rawstr_text;
|
|
|
|
text *result_text;
|
|
|
|
char *result;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr)));
|
|
|
|
result_text = DatumGetTextP(DirectFunctionCall1(quote_ident, PointerGetDatum(rawstr_text)));
|
|
|
|
result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text)));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
static int16
|
2002-04-24 06:28:28 +04:00
|
|
|
get_attnum_pk_pos(int16 *pkattnums, int16 pknumatts, int16 key)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int i;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Not likely a long list anyway, so just scan for the value
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
if (key == pkattnums[i])
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
static HeapTuple
|
2002-04-24 06:28:28 +04:00
|
|
|
get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
StringInfo str = makeStringInfo();
|
|
|
|
char *sql = NULL;
|
|
|
|
int ret;
|
|
|
|
HeapTuple tuple;
|
|
|
|
int i;
|
|
|
|
char *val = NULL;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
2002-09-02 10:13:31 +04:00
|
|
|
tupdesc = CreateTupleDescCopy(rel->rd_att);
|
|
|
|
relation_close(rel, AccessShareLock);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to SPI manager
|
|
|
|
*/
|
|
|
|
if ((ret = SPI_connect()) < 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI connect failure - returned %d", ret);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Build sql statement to look up tuple of interest Use src_pkattvals
|
|
|
|
* as the criteria.
|
2002-04-24 06:28:28 +04:00
|
|
|
*/
|
2002-11-23 21:59:25 +03:00
|
|
|
appendStringInfo(str, "SELECT * FROM %s WHERE ", relname);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int16 pkattnum = pkattnums[i];
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
appendStringInfo(str, " AND ");
|
|
|
|
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, "%s",
|
2002-09-05 00:31:48 +04:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
val = pstrdup(src_pkattvals[i]);
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, " = %s", quote_literal_cstr(val));
|
2002-04-24 06:28:28 +04:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
2002-08-02 22:15:10 +04:00
|
|
|
appendStringInfo(str, " IS NULL");
|
2002-04-24 06:28:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sql = pstrdup(str->data);
|
|
|
|
pfree(str->data);
|
|
|
|
pfree(str);
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
/*
|
|
|
|
* Retrieve the desired tuple
|
|
|
|
*/
|
|
|
|
ret = SPI_exec(sql, 0);
|
|
|
|
pfree(sql);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only allow one qualifying tuple
|
|
|
|
*/
|
|
|
|
if ((ret == SPI_OK_SELECT) && (SPI_processed > 1))
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CARDINALITY_VIOLATION),
|
|
|
|
errmsg("source criteria matched more than one record")));
|
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
else if (ret == SPI_OK_SELECT && SPI_processed == 1)
|
|
|
|
{
|
|
|
|
SPITupleTable *tuptable = SPI_tuptable;
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-04-24 06:28:28 +04:00
|
|
|
tuple = SPI_copytuple(tuptable->vals[0]);
|
|
|
|
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* no qualifying tuples
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* never reached, but keep compiler quiet
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
static Oid
|
|
|
|
get_relid_from_relname(text *relname_text)
|
2002-04-24 06:28:28 +04:00
|
|
|
{
|
2002-05-28 01:59:12 +04:00
|
|
|
RangeVar *relvar;
|
|
|
|
Relation rel;
|
|
|
|
Oid relid;
|
2002-04-24 06:28:28 +04:00
|
|
|
|
2002-05-28 01:59:12 +04:00
|
|
|
relvar = makeRangeVarFromNameList(textToQualifiedNameList(relname_text, "get_relid_from_relname"));
|
|
|
|
rel = heap_openrv(relvar, AccessShareLock);
|
|
|
|
relid = RelationGetRelid(rel);
|
|
|
|
relation_close(rel, AccessShareLock);
|
2002-04-24 06:28:28 +04:00
|
|
|
|
|
|
|
return relid;
|
|
|
|
}
|
|
|
|
|
2002-09-02 10:13:31 +04:00
|
|
|
static TupleDesc
|
|
|
|
pgresultGetTupleDesc(PGresult *res)
|
|
|
|
{
|
2002-09-05 00:31:48 +04:00
|
|
|
int natts;
|
|
|
|
AttrNumber attnum;
|
|
|
|
TupleDesc desc;
|
|
|
|
char *attname;
|
|
|
|
int32 atttypmod;
|
|
|
|
int attdim;
|
|
|
|
bool attisset;
|
|
|
|
Oid atttypid;
|
|
|
|
int i;
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a new tuple descriptor
|
|
|
|
*/
|
|
|
|
natts = PQnfields(res);
|
|
|
|
if (natts < 1)
|
2003-07-24 21:52:50 +04:00
|
|
|
/* shouldn't happen */
|
2002-09-02 10:13:31 +04:00
|
|
|
elog(ERROR, "cannot create a description for empty results");
|
|
|
|
|
2002-09-03 08:00:37 +04:00
|
|
|
desc = CreateTemplateTupleDesc(natts, false);
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
attnum = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
|
|
|
/*
|
2002-09-05 00:31:48 +04:00
|
|
|
* for each field, get the name and type information from the
|
|
|
|
* query result and have TupleDescInitEntry fill in the attribute
|
2002-09-02 10:13:31 +04:00
|
|
|
* information we need.
|
|
|
|
*/
|
|
|
|
attnum++;
|
|
|
|
|
|
|
|
attname = PQfname(res, i);
|
|
|
|
atttypid = PQftype(res, i);
|
|
|
|
atttypmod = PQfmod(res, i);
|
|
|
|
|
|
|
|
if (PQfsize(res, i) != get_typlen(atttypid))
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_MOST_SPECIFIC_TYPE_MISMATCH),
|
|
|
|
errmsg("field size mismatch"),
|
|
|
|
errdetail("Size of remote field \"%s\" does not match " \
|
|
|
|
"size of local type \"%s\".", attname,
|
|
|
|
format_type_with_typemod(atttypid,
|
|
|
|
atttypmod))));
|
2002-09-02 10:13:31 +04:00
|
|
|
|
|
|
|
attdim = 0;
|
|
|
|
attisset = false;
|
|
|
|
|
|
|
|
TupleDescInitEntry(desc, attnum, attname, atttypid,
|
|
|
|
atttypmod, attdim, attisset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
2002-11-23 21:59:25 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* generate_relation_name - copied from ruleutils.c
|
|
|
|
* Compute the name to display for a relation specified by OID
|
|
|
|
*
|
|
|
|
* The result includes all necessary quoting and schema-prefixing.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
generate_relation_name(Oid relid)
|
|
|
|
{
|
|
|
|
HeapTuple tp;
|
|
|
|
Form_pg_class reltup;
|
|
|
|
char *nspname;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
tp = SearchSysCache(RELOID,
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-24 21:52:50 +04:00
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relid);
|
|
|
|
|
2002-11-23 21:59:25 +03:00
|
|
|
reltup = (Form_pg_class) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
/* Qualify the name if not visible in search path */
|
|
|
|
if (RelationIsVisible(relid))
|
|
|
|
nspname = NULL;
|
|
|
|
else
|
|
|
|
nspname = get_namespace_name(reltup->relnamespace);
|
|
|
|
|
|
|
|
result = quote_qualified_identifier(nspname, NameStr(reltup->relname));
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
static remoteConn *
|
|
|
|
getConnectionByName(const char *name)
|
|
|
|
{
|
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
char key[NAMEDATALEN];
|
|
|
|
|
|
|
|
if(!remoteConnHash)
|
|
|
|
remoteConnHash=createConnHash();
|
|
|
|
|
|
|
|
MemSet(key, 0, NAMEDATALEN);
|
|
|
|
snprintf(key, NAMEDATALEN - 1, "%s", name);
|
|
|
|
hentry = (remoteConnHashEnt*) hash_search(remoteConnHash,
|
|
|
|
key, HASH_FIND, NULL);
|
|
|
|
|
|
|
|
if(hentry)
|
|
|
|
return(hentry->rcon);
|
|
|
|
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HTAB *
|
|
|
|
createConnHash(void)
|
|
|
|
{
|
|
|
|
HASHCTL ctl;
|
|
|
|
HTAB *ptr;
|
|
|
|
|
|
|
|
ctl.keysize = NAMEDATALEN;
|
|
|
|
ctl.entrysize = sizeof(remoteConnHashEnt);
|
|
|
|
|
|
|
|
ptr=hash_create("Remote Con hash", NUMCONN, &ctl, HASH_ELEM);
|
|
|
|
|
|
|
|
if(!ptr)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
return(ptr);
|
|
|
|
}
|
|
|
|
|
2003-07-24 21:52:50 +04:00
|
|
|
static void
|
2003-06-25 05:10:15 +04:00
|
|
|
createNewConnection(const char *name, remoteConn *con)
|
|
|
|
{
|
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
bool found;
|
|
|
|
char key[NAMEDATALEN];
|
|
|
|
|
|
|
|
if(!remoteConnHash)
|
2003-07-24 21:52:50 +04:00
|
|
|
remoteConnHash = createConnHash();
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
MemSet(key, 0, NAMEDATALEN);
|
|
|
|
snprintf(key, NAMEDATALEN - 1, "%s", name);
|
|
|
|
hentry = (remoteConnHashEnt *) hash_search(remoteConnHash, key,
|
|
|
|
HASH_ENTER, &found);
|
|
|
|
|
|
|
|
if(!hentry)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
if(found)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("duplicate connection name")));
|
2003-06-25 05:10:15 +04:00
|
|
|
|
|
|
|
hentry->rcon = con;
|
|
|
|
strncpy(hentry->name, name, NAMEDATALEN - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deleteConnection(const char *name)
|
|
|
|
{
|
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
bool found;
|
|
|
|
char key[NAMEDATALEN];
|
|
|
|
|
|
|
|
if(!remoteConnHash)
|
|
|
|
remoteConnHash=createConnHash();
|
|
|
|
|
|
|
|
MemSet(key, 0, NAMEDATALEN);
|
|
|
|
snprintf(key, NAMEDATALEN - 1, "%s", name);
|
|
|
|
|
|
|
|
hentry = (remoteConnHashEnt *) hash_search(remoteConnHash,
|
|
|
|
key, HASH_REMOVE, &found);
|
|
|
|
|
|
|
|
if(!hentry)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("undefined connection name")));
|
|
|
|
|
2003-06-25 05:10:15 +04:00
|
|
|
}
|