Attached is an update to contrib/dblink. Please apply if there are no
objections. Major changes: - removed cursor wrap around input sql to allow for remote execution of INSERT/UPDATE/DELETE - dblink now returns a resource id instead of a real pointer - added several utility functions I'm still hoping to add explicit cursor open/fetch/close support before 7.3 is released, but I need a bit more time on that. On a somewhat unrelated topic, I never got any feedback on the unknownin/out patch and the mb_substring patch. Is there anything else I need to do to get those applied? Joe Conway
This commit is contained in:
parent
30571b5496
commit
3bf6b8f06a
@ -3,7 +3,8 @@
|
||||
*
|
||||
* Functions returning results from a remote database
|
||||
*
|
||||
* Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001;
|
||||
* Copyright (c) Joseph Conway <mail@joeconway.com>, 2001, 2002,
|
||||
* ALL RIGHTS RESERVED;
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software and its
|
||||
* documentation for any purpose, without fee, and without a written agreement
|
||||
@ -25,12 +26,19 @@
|
||||
*/
|
||||
|
||||
|
||||
Version 0.3 (14 June, 2001):
|
||||
Function to test returning data set from remote database
|
||||
Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.1 and 7.2devel
|
||||
Version 0.4 (7 April, 2002):
|
||||
Functions allowing remote database INSERT/UPDATE/DELETE/SELECT, and
|
||||
various utility functions.
|
||||
Tested under Linux (Red Hat 7.2) and PostgreSQL 7.2 and 7.3devel
|
||||
|
||||
Release Notes:
|
||||
|
||||
Version 0.4
|
||||
- removed cursor wrap around input sql to allow for remote
|
||||
execution of INSERT/UPDATE/DELETE
|
||||
- dblink now returns a resource id instead of a real pointer
|
||||
- added several utility functions -- see below
|
||||
|
||||
Version 0.3
|
||||
- fixed dblink invalid pointer causing corrupt elog message
|
||||
- fixed dblink_tok improper handling of null results
|
||||
@ -51,14 +59,36 @@ Installation:
|
||||
|
||||
installs following functions into database template1:
|
||||
|
||||
dblink() - returns a pointer to results from remote query
|
||||
dblink_tok() - extracts and returns individual field results
|
||||
dblink(text,text) RETURNS setof int
|
||||
- returns a resource id for results from remote query
|
||||
dblink_tok(int,int) RETURNS text
|
||||
- extracts and returns individual field results
|
||||
dblink_strtok(text,text,int) RETURNS text
|
||||
- extracts and returns individual token from delimited text
|
||||
dblink_get_pkey(name) RETURNS setof text
|
||||
- returns the field names of a relation's primary key fields
|
||||
dblink_last_oid(int) RETURNS oid
|
||||
- returns the last inserted oid
|
||||
dblink_build_sql_insert(name,int2vector,int2,_text,_text) RETURNS text
|
||||
- builds an insert statement using a local tuple, replacing the
|
||||
selection key field values with alternate supplied values
|
||||
dblink_build_sql_delete(name,int2vector,int2,_text) RETURNS text
|
||||
- builds a delete statement using supplied values for selection
|
||||
key field values
|
||||
dblink_build_sql_update(name,int2vector,int2,_text,_text) RETURNS text
|
||||
- builds an update statement using a local tuple, replacing the
|
||||
selection key field values with alternate supplied values
|
||||
dblink_current_query() RETURNS text
|
||||
- returns the current query string
|
||||
dblink_replace(text,text,text) RETURNS text
|
||||
- replace all occurences of substring-a in the input-string
|
||||
with substring-b
|
||||
|
||||
Documentation
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink -- Returns a pointer to a data set from a remote database
|
||||
dblink -- Returns a resource id for a data set from a remote database
|
||||
|
||||
Synopsis
|
||||
|
||||
@ -78,7 +108,7 @@ Inputs
|
||||
|
||||
Outputs
|
||||
|
||||
Returns setof int (pointer)
|
||||
Returns setof int (res_id)
|
||||
|
||||
Example usage
|
||||
|
||||
@ -94,13 +124,13 @@ dblink_tok -- Returns individual select field results from a dblink remote query
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_tok(int pointer, int fnumber)
|
||||
dblink_tok(int res_id, int fnumber)
|
||||
|
||||
Inputs
|
||||
|
||||
pointer
|
||||
res_id
|
||||
|
||||
a pointer returned by a call to dblink()
|
||||
a resource id returned by a call to dblink()
|
||||
|
||||
fnumber
|
||||
|
||||
@ -131,6 +161,255 @@ Then you can simply write:
|
||||
select f1, f2 from myremotetable where f1 like 'bytea%';
|
||||
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink_strtok -- Extracts and returns individual token from delimited text
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_strtok(text inputstring, text delimiter, int posn) RETURNS text
|
||||
|
||||
Inputs
|
||||
|
||||
inputstring
|
||||
|
||||
any string you want to parse a token out of;
|
||||
e.g. 'f=1&g=3&h=4'
|
||||
|
||||
delimiter
|
||||
|
||||
a single character to use as the delimiter;
|
||||
e.g. '&' or '='
|
||||
|
||||
posn
|
||||
|
||||
the position of the token of interest, 0 based;
|
||||
e.g. 1
|
||||
|
||||
Outputs
|
||||
|
||||
Returns text
|
||||
|
||||
Example usage
|
||||
|
||||
test=# select dblink_strtok(dblink_strtok('f=1&g=3&h=4','&',1),'=',1);
|
||||
dblink_strtok
|
||||
---------------
|
||||
3
|
||||
(1 row)
|
||||
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink_get_pkey -- returns the field names of a relation's primary
|
||||
key fields
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_get_pkey(name relname) RETURNS setof text
|
||||
|
||||
Inputs
|
||||
|
||||
relname
|
||||
|
||||
any relation name;
|
||||
e.g. 'foobar'
|
||||
|
||||
Outputs
|
||||
|
||||
Returns setof text -- one row for each primary key field, in order of
|
||||
precedence
|
||||
|
||||
Example usage
|
||||
|
||||
test=# select dblink_get_pkey('foobar');
|
||||
dblink_get_pkey
|
||||
-----------------
|
||||
f1
|
||||
f2
|
||||
f3
|
||||
f4
|
||||
f5
|
||||
(5 rows)
|
||||
|
||||
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink_last_oid -- Returns last inserted oid
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_last_oid(int res_id) RETURNS oid
|
||||
|
||||
Inputs
|
||||
|
||||
res_id
|
||||
|
||||
any resource id returned by dblink function;
|
||||
|
||||
Outputs
|
||||
|
||||
Returns oid of last inserted tuple
|
||||
|
||||
Example usage
|
||||
|
||||
test=# select dblink_last_oid(dblink('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
|
||||
,'insert into mytable (f1, f2) values (1,2)'));
|
||||
|
||||
dblink_last_oid
|
||||
----------------
|
||||
16553
|
||||
(1 row)
|
||||
|
||||
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink_build_sql_insert -- builds an insert statement using a local
|
||||
tuple, replacing the selection key field
|
||||
values with alternate supplied values
|
||||
dblink_build_sql_delete -- builds a delete statement using supplied
|
||||
values for selection key field values
|
||||
dblink_build_sql_update -- builds an update statement using a local
|
||||
tuple, replacing the selection key field
|
||||
values with alternate supplied values
|
||||
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_build_sql_insert(name relname
|
||||
,int2vector primary_key_attnums
|
||||
,int2 num_primary_key_atts
|
||||
,_text src_pk_att_vals_array
|
||||
,_text tgt_pk_att_vals_array) RETURNS text
|
||||
dblink_build_sql_delete(name relname
|
||||
,int2vector primary_key_attnums
|
||||
,int2 num_primary_key_atts
|
||||
,_text tgt_pk_att_vals_array) RETURNS text
|
||||
dblink_build_sql_update(name relname
|
||||
,int2vector primary_key_attnums
|
||||
,int2 num_primary_key_atts
|
||||
,_text src_pk_att_vals_array
|
||||
,_text tgt_pk_att_vals_array) RETURNS text
|
||||
|
||||
Inputs
|
||||
|
||||
relname
|
||||
|
||||
any relation name;
|
||||
e.g. 'foobar'
|
||||
|
||||
primary_key_attnums
|
||||
|
||||
vector of primary key attnums (1 based, see pg_index.indkey);
|
||||
e.g. '1 2'
|
||||
|
||||
num_primary_key_atts
|
||||
|
||||
number of primary key attnums in the vector; e.g. 2
|
||||
|
||||
src_pk_att_vals_array
|
||||
|
||||
array of primary key values, used to look up the local matching
|
||||
tuple, the values of which are then used to construct the SQL
|
||||
statement
|
||||
|
||||
tgt_pk_att_vals_array
|
||||
|
||||
array of primary key values, used to replace the local tuple
|
||||
values in the SQL statement
|
||||
|
||||
Outputs
|
||||
|
||||
Returns text -- requested SQL statement
|
||||
|
||||
Example usage
|
||||
|
||||
test=# select dblink_build_sql_insert('foo','1 2',2,'{"1", "a"}','{"1", "b''a"}');
|
||||
dblink_build_sql_insert
|
||||
--------------------------------------------------
|
||||
INSERT INTO foo(f1,f2,f3) VALUES('1','b''a','1')
|
||||
(1 row)
|
||||
|
||||
test=# select dblink_build_sql_delete('MyFoo','1 2',2,'{"1", "b"}');
|
||||
dblink_build_sql_delete
|
||||
---------------------------------------------
|
||||
DELETE FROM "MyFoo" WHERE f1='1' AND f2='b'
|
||||
(1 row)
|
||||
|
||||
test=# select dblink_build_sql_update('foo','1 2',2,'{"1", "a"}','{"1", "b"}');
|
||||
dblink_build_sql_update
|
||||
-------------------------------------------------------------
|
||||
UPDATE foo SET f1='1',f2='b',f3='1' WHERE f1='1' AND f2='b'
|
||||
(1 row)
|
||||
|
||||
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink_current_query -- returns the current query string
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_current_query () RETURNS text
|
||||
|
||||
Inputs
|
||||
|
||||
None
|
||||
|
||||
Outputs
|
||||
|
||||
Returns text -- a copy of the currently executing query
|
||||
|
||||
Example usage
|
||||
|
||||
test=# select dblink_current_query() from (select dblink('dbname=template1','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
|
||||
dblink_current_query
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
select dblink_current_query() from (select dblink('dbname=template1','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
|
||||
(1 row)
|
||||
|
||||
|
||||
==================================================================
|
||||
Name
|
||||
|
||||
dblink_replace -- replace all occurences of substring-a in the
|
||||
input-string with substring-b
|
||||
|
||||
Synopsis
|
||||
|
||||
dblink_replace(text input-string, text substring-a, text substring-b) RETURNS text
|
||||
|
||||
Inputs
|
||||
|
||||
input-string
|
||||
|
||||
the starting string, before replacement of substring-a
|
||||
|
||||
substring-a
|
||||
|
||||
the substring to find and replace
|
||||
|
||||
substring-b
|
||||
|
||||
the substring to be substituted in place of substring-a
|
||||
|
||||
Outputs
|
||||
|
||||
Returns text -- a copy of the starting string, but with all occurences of
|
||||
substring-a replaced with substring-b
|
||||
|
||||
Example usage
|
||||
|
||||
test=# select dblink_replace('12345678901234567890','56','hello');
|
||||
dblink_replace
|
||||
----------------------------
|
||||
1234hello78901234hello7890
|
||||
(1 row)
|
||||
|
||||
==================================================================
|
||||
|
||||
|
||||
-- Joe Conway
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3,7 +3,8 @@
|
||||
*
|
||||
* Functions returning results from a remote database
|
||||
*
|
||||
* Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001;
|
||||
* Copyright (c) Joseph Conway <mail@joeconway.com>, 2001, 2002,
|
||||
* ALL RIGHTS RESERVED;
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software and its
|
||||
* documentation for any purpose, without fee, and without a written agreement
|
||||
@ -33,10 +34,31 @@
|
||||
#include "libpq-int.h"
|
||||
#include "fmgr.h"
|
||||
#include "access/tupdesc.h"
|
||||
#include "access/heapam.h"
|
||||
#include "catalog/catname.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/syscache.h"
|
||||
|
||||
#ifdef NamespaceRelationName
|
||||
#include "catalog/namespace.h"
|
||||
#endif /* NamespaceRelationName */
|
||||
|
||||
/*
|
||||
* Max SQL statement size
|
||||
*/
|
||||
#define DBLINK_MAX_SQLSTATE_SIZE 16384
|
||||
|
||||
/*
|
||||
* This struct holds the results of the remote query.
|
||||
@ -49,22 +71,75 @@ typedef struct
|
||||
*/
|
||||
int tup_num;
|
||||
|
||||
/*
|
||||
* resource index number for this context
|
||||
*/
|
||||
int res_id_index;
|
||||
|
||||
/*
|
||||
* the actual query results
|
||||
*/
|
||||
PGresult *res;
|
||||
|
||||
} dblink_results;
|
||||
|
||||
|
||||
/*
|
||||
* This struct holds results in the form of an array.
|
||||
* Use fn_extra to hold a pointer to it across calls
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
/*
|
||||
* elem being accessed
|
||||
*/
|
||||
int elem_num;
|
||||
|
||||
/*
|
||||
* number of elems
|
||||
*/
|
||||
int num_elems;
|
||||
|
||||
/*
|
||||
* the actual array
|
||||
*/
|
||||
void *res;
|
||||
|
||||
} dblink_array_results;
|
||||
|
||||
/*
|
||||
* External declarations
|
||||
*/
|
||||
extern Datum dblink(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_tok(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_strtok(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_get_pkey(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_last_oid(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_build_sql_insert(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_build_sql_delete(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_build_sql_update(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_current_query(PG_FUNCTION_ARGS);
|
||||
extern Datum dblink_replace_text(PG_FUNCTION_ARGS);
|
||||
|
||||
/*
|
||||
* Internal declarations
|
||||
*/
|
||||
dblink_results *init_dblink_results(MemoryContext fn_mcxt);
|
||||
dblink_array_results *init_dblink_array_results(MemoryContext fn_mcxt);
|
||||
char **get_pkey_attnames(Oid relid, int16 *numatts);
|
||||
char *get_strtok(char *fldtext, char *fldsep, int fldnum);
|
||||
char *getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber);
|
||||
char *get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals);
|
||||
char *get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattvals);
|
||||
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);
|
||||
int16 get_attnum_pk_pos(int16 *pkattnums, int16 pknumatts, int16 key);
|
||||
HeapTuple get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattvals);
|
||||
Oid get_relid_from_relname(char *relname);
|
||||
dblink_results *get_res_ptr(int32 res_id_index);
|
||||
void append_res_ptr(dblink_results *results);
|
||||
void remove_res_ptr(dblink_results *results);
|
||||
|
||||
extern char *debug_query_string;
|
||||
|
||||
#endif /* DBLINK_H */
|
||||
|
@ -1,5 +1,38 @@
|
||||
CREATE FUNCTION dblink (text,text) RETURNS setof int
|
||||
AS 'MODULE_PATHNAME','dblink' LANGUAGE 'c';
|
||||
CREATE OR REPLACE FUNCTION dblink (text,text) RETURNS setof int
|
||||
AS 'MODULE_PATHNAME','dblink' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE FUNCTION dblink_tok (int,int) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_tok' LANGUAGE 'c';
|
||||
CREATE OR REPLACE FUNCTION dblink_tok (int,int) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_tok' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_strtok (text,text,int) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_strtok' LANGUAGE 'c'
|
||||
WITH (iscachable, isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_get_pkey (name) RETURNS setof text
|
||||
AS 'MODULE_PATHNAME','dblink_get_pkey' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_last_oid (int) RETURNS oid
|
||||
AS 'MODULE_PATHNAME','dblink_last_oid' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_build_sql_insert (name, int2vector, int2, _text, _text) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_build_sql_insert' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_build_sql_delete (name, int2vector, int2, _text) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_build_sql_delete' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_build_sql_update (name, int2vector, int2, _text, _text) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_build_sql_update' LANGUAGE 'c'
|
||||
WITH (isstrict);
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_current_query () RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_current_query' LANGUAGE 'c';
|
||||
|
||||
CREATE OR REPLACE FUNCTION dblink_replace (text,text,text) RETURNS text
|
||||
AS 'MODULE_PATHNAME','dblink_replace_text' LANGUAGE 'c'
|
||||
WITH (iscachable, isstrict);
|
||||
|
Loading…
Reference in New Issue
Block a user