33 *
44 * Functions returning results from a remote database
55 *
6- * Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001;
6+ * Copyright (c) Joseph Conway <mail@joeconway.com>, 2001, 2002,
7+ * ALL RIGHTS RESERVED;
78 *
89 * Permission to use, copy, modify, and distribute this software and its
910 * documentation for any purpose, without fee, and without a written agreement
2526 */
2627
2728
28- Version 0.3 (14 June, 2001):
29- Function to test returning data set from remote database
30- Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.1 and 7.2devel
29+ Version 0.4 (7 April, 2002):
30+ Functions allowing remote database INSERT/UPDATE/DELETE/SELECT, and
31+ various utility functions.
32+ Tested under Linux (Red Hat 7.2) and PostgreSQL 7.2 and 7.3devel
3133
3234Release Notes:
3335
36+ Version 0.4
37+ - removed cursor wrap around input sql to allow for remote
38+ execution of INSERT/UPDATE/DELETE
39+ - dblink now returns a resource id instead of a real pointer
40+ - added several utility functions -- see below
41+
3442 Version 0.3
3543 - fixed dblink invalid pointer causing corrupt elog message
3644 - fixed dblink_tok improper handling of null results
@@ -51,14 +59,36 @@ Installation:
5159
5260 installs following functions into database template1:
5361
54- dblink() - returns a pointer to results from remote query
55- dblink_tok() - extracts and returns individual field results
62+ dblink(text,text) RETURNS setof int
63+ - returns a resource id for results from remote query
64+ dblink_tok(int,int) RETURNS text
65+ - extracts and returns individual field results
66+ dblink_strtok(text,text,int) RETURNS text
67+ - extracts and returns individual token from delimited text
68+ dblink_get_pkey(name) RETURNS setof text
69+ - returns the field names of a relation's primary key fields
70+ dblink_last_oid(int) RETURNS oid
71+ - returns the last inserted oid
72+ dblink_build_sql_insert(name,int2vector,int2,_text,_text) RETURNS text
73+ - builds an insert statement using a local tuple, replacing the
74+ selection key field values with alternate supplied values
75+ dblink_build_sql_delete(name,int2vector,int2,_text) RETURNS text
76+ - builds a delete statement using supplied values for selection
77+ key field values
78+ dblink_build_sql_update(name,int2vector,int2,_text,_text) RETURNS text
79+ - builds an update statement using a local tuple, replacing the
80+ selection key field values with alternate supplied values
81+ dblink_current_query() RETURNS text
82+ - returns the current query string
83+ dblink_replace(text,text,text) RETURNS text
84+ - replace all occurences of substring-a in the input-string
85+ with substring-b
5686
5787Documentation
5888==================================================================
5989Name
6090
61- dblink -- Returns apointer to a data set from a remote database
91+ dblink -- Returns aresource id for a data set from a remote database
6292
6393Synopsis
6494
@@ -78,7 +108,7 @@ Inputs
78108
79109Outputs
80110
81- Returns setof int (pointer )
111+ Returns setof int (res_id )
82112
83113Example usage
84114
@@ -94,13 +124,13 @@ dblink_tok -- Returns individual select field results from a dblink remote query
94124
95125Synopsis
96126
97- dblink_tok(intpointer , int fnumber)
127+ dblink_tok(intres_id , int fnumber)
98128
99129Inputs
100130
101- pointer
131+ res_id
102132
103- apointer returned by a call to dblink()
133+ aresource id returned by a call to dblink()
104134
105135 fnumber
106136
@@ -131,6 +161,255 @@ Then you can simply write:
131161 select f1, f2 from myremotetable where f1 like 'bytea%';
132162
133163==================================================================
164+ Name
165+
166+ dblink_strtok -- Extracts and returns individual token from delimited text
167+
168+ Synopsis
169+
170+ dblink_strtok(text inputstring, text delimiter, int posn) RETURNS text
171+
172+ Inputs
173+
174+ inputstring
175+
176+ any string you want to parse a token out of;
177+ e.g. 'f=1&g=3&h=4'
178+
179+ delimiter
180+
181+ a single character to use as the delimiter;
182+ e.g. '&' or '='
183+
184+ posn
185+
186+ the position of the token of interest, 0 based;
187+ e.g. 1
188+
189+ Outputs
190+
191+ Returns text
192+
193+ Example usage
194+
195+ test=# select dblink_strtok(dblink_strtok('f=1&g=3&h=4','&',1),'=',1);
196+ dblink_strtok
197+ ---------------
198+ 3
199+ (1 row)
200+
201+ ==================================================================
202+ Name
203+
204+ dblink_get_pkey -- returns the field names of a relation's primary
205+ key fields
206+
207+ Synopsis
208+
209+ dblink_get_pkey(name relname) RETURNS setof text
210+
211+ Inputs
212+
213+ relname
214+
215+ any relation name;
216+ e.g. 'foobar'
217+
218+ Outputs
219+
220+ Returns setof text -- one row for each primary key field, in order of
221+ precedence
222+
223+ Example usage
224+
225+ test=# select dblink_get_pkey('foobar');
226+ dblink_get_pkey
227+ -----------------
228+ f1
229+ f2
230+ f3
231+ f4
232+ f5
233+ (5 rows)
234+
235+
236+ ==================================================================
237+ Name
238+
239+ dblink_last_oid -- Returns last inserted oid
240+
241+ Synopsis
242+
243+ dblink_last_oid(int res_id) RETURNS oid
244+
245+ Inputs
246+
247+ res_id
248+
249+ any resource id returned by dblink function;
250+
251+ Outputs
252+
253+ Returns oid of last inserted tuple
254+
255+ Example usage
256+
257+ test=# select dblink_last_oid(dblink('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
258+ ,'insert into mytable (f1, f2) values (1,2)'));
259+
260+ dblink_last_oid
261+ ----------------
262+ 16553
263+ (1 row)
264+
265+
266+ ==================================================================
267+ Name
268+
269+ dblink_build_sql_insert -- builds an insert statement using a local
270+ tuple, replacing the selection key field
271+ values with alternate supplied values
272+ dblink_build_sql_delete -- builds a delete statement using supplied
273+ values for selection key field values
274+ dblink_build_sql_update -- builds an update statement using a local
275+ tuple, replacing the selection key field
276+ values with alternate supplied values
277+
278+
279+ Synopsis
280+
281+ dblink_build_sql_insert(name relname
282+ ,int2vector primary_key_attnums
283+ ,int2 num_primary_key_atts
284+ ,_text src_pk_att_vals_array
285+ ,_text tgt_pk_att_vals_array) RETURNS text
286+ dblink_build_sql_delete(name relname
287+ ,int2vector primary_key_attnums
288+ ,int2 num_primary_key_atts
289+ ,_text tgt_pk_att_vals_array) RETURNS text
290+ dblink_build_sql_update(name relname
291+ ,int2vector primary_key_attnums
292+ ,int2 num_primary_key_atts
293+ ,_text src_pk_att_vals_array
294+ ,_text tgt_pk_att_vals_array) RETURNS text
295+
296+ Inputs
297+
298+ relname
299+
300+ any relation name;
301+ e.g. 'foobar'
302+
303+ primary_key_attnums
304+
305+ vector of primary key attnums (1 based, see pg_index.indkey);
306+ e.g. '1 2'
307+
308+ num_primary_key_atts
309+
310+ number of primary key attnums in the vector; e.g. 2
311+
312+ src_pk_att_vals_array
313+
314+ array of primary key values, used to look up the local matching
315+ tuple, the values of which are then used to construct the SQL
316+ statement
317+
318+ tgt_pk_att_vals_array
319+
320+ array of primary key values, used to replace the local tuple
321+ values in the SQL statement
322+
323+ Outputs
324+
325+ Returns text -- requested SQL statement
326+
327+ Example usage
328+
329+ test=# select dblink_build_sql_insert('foo','1 2',2,'{"1", "a"}','{"1", "b''a"}');
330+ dblink_build_sql_insert
331+ --------------------------------------------------
332+ INSERT INTO foo(f1,f2,f3) VALUES('1','b''a','1')
333+ (1 row)
334+
335+ test=# select dblink_build_sql_delete('MyFoo','1 2',2,'{"1", "b"}');
336+ dblink_build_sql_delete
337+ ---------------------------------------------
338+ DELETE FROM "MyFoo" WHERE f1='1' AND f2='b'
339+ (1 row)
340+
341+ test=# select dblink_build_sql_update('foo','1 2',2,'{"1", "a"}','{"1", "b"}');
342+ dblink_build_sql_update
343+ -------------------------------------------------------------
344+ UPDATE foo SET f1='1',f2='b',f3='1' WHERE f1='1' AND f2='b'
345+ (1 row)
346+
347+
348+ ==================================================================
349+ Name
350+
351+ dblink_current_query -- returns the current query string
352+
353+ Synopsis
354+
355+ dblink_current_query () RETURNS text
356+
357+ Inputs
358+
359+ None
360+
361+ Outputs
362+
363+ Returns text -- a copy of the currently executing query
364+
365+ Example usage
366+
367+ test=# select dblink_current_query() from (select dblink('dbname=template1','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
368+ dblink_current_query
369+ -----------------------------------------------------------------------------------------------------------------------------------------------------
370+ select dblink_current_query() from (select dblink('dbname=template1','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
371+ (1 row)
372+
373+
374+ ==================================================================
375+ Name
376+
377+ dblink_replace -- replace all occurences of substring-a in the
378+ input-string with substring-b
379+
380+ Synopsis
381+
382+ dblink_replace(text input-string, text substring-a, text substring-b) RETURNS text
383+
384+ Inputs
385+
386+ input-string
387+
388+ the starting string, before replacement of substring-a
389+
390+ substring-a
391+
392+ the substring to find and replace
393+
394+ substring-b
395+
396+ the substring to be substituted in place of substring-a
397+
398+ Outputs
399+
400+ Returns text -- a copy of the starting string, but with all occurences of
401+ substring-a replaced with substring-b
402+
403+ Example usage
404+
405+ test=# select dblink_replace('12345678901234567890','56','hello');
406+ dblink_replace
407+ ----------------------------
408+ 1234hello78901234hello7890
409+ (1 row)
410+
411+ ==================================================================
412+
134413
135414-- Joe Conway
136415