3
3
*
4
4
* Functions returning results from a remote database
5
5
*
6
- * Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001;
6
+ * Copyright (c) Joseph Conway <mail@joeconway.com>, 2001, 2002,
7
+ * ALL RIGHTS RESERVED;
7
8
*
8
9
* Permission to use, copy, modify, and distribute this software and its
9
10
* documentation for any purpose, without fee, and without a written agreement
25
26
*/
26
27
27
28
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
31
33
32
34
Release Notes:
33
35
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
+
34
42
Version 0.3
35
43
- fixed dblink invalid pointer causing corrupt elog message
36
44
- fixed dblink_tok improper handling of null results
@@ -51,14 +59,36 @@ Installation:
51
59
52
60
installs following functions into database template1:
53
61
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
56
86
57
87
Documentation
58
88
==================================================================
59
89
Name
60
90
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
62
92
63
93
Synopsis
64
94
@@ -78,7 +108,7 @@ Inputs
78
108
79
109
Outputs
80
110
81
- Returns setof int (pointer )
111
+ Returns setof int (res_id )
82
112
83
113
Example usage
84
114
@@ -94,13 +124,13 @@ dblink_tok -- Returns individual select field results from a dblink remote query
94
124
95
125
Synopsis
96
126
97
- dblink_tok(intpointer , int fnumber)
127
+ dblink_tok(intres_id , int fnumber)
98
128
99
129
Inputs
100
130
101
- pointer
131
+ res_id
102
132
103
- apointer returned by a call to dblink()
133
+ aresource id returned by a call to dblink()
104
134
105
135
fnumber
106
136
@@ -131,6 +161,255 @@ Then you can simply write:
131
161
select f1, f2 from myremotetable where f1 like 'bytea%';
132
162
133
163
==================================================================
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
+
134
413
135
414
-- Joe Conway
136
415