-# $Header: /cvsroot/pgsql/contrib/dblink/Makefile,v 1.6 2002/09/03 04:01:05 tgl Exp $
+# $Header: /cvsroot/pgsql/contrib/dblink/Makefile,v 1.7 2002/09/14 20:28:54 tgl Exp $
subdir = contrib/dblink
top_builddir = ../..
DATA_built = dblink.sql
DOCS = README.dblink
-
-EXTRA_CLEAN = dblink.test.out dblink.test.diff
+REGRESS = dblink
include $(top_srcdir)/contrib/contrib-global.mk
AS 'MODULE_PATHNAME','dblink_close' LANGUAGE 'c'
WITH (isstrict);
--- Note: if this is a first time install of dblink, the following DROP
--- FUNCTION line is expected to fail.
--- Comment out the following 4 lines if the DEPRECATED functions are used.
-DROP FUNCTION dblink (text,text);
+-- Note: if this is not a first time install of dblink, uncomment the
+-- following DROP which prepares the database for the new, non-deprecated
+-- version.
+--DROP FUNCTION dblink (text,text);
+
+-- Comment out the following 3 lines if the DEPRECATED functions are used.
CREATE OR REPLACE FUNCTION dblink (text,text) RETURNS setof record
AS 'MODULE_PATHNAME','dblink_record' LANGUAGE 'c'
WITH (isstrict);
CREATE OR REPLACE FUNCTION dblink_current_query () RETURNS text
AS 'MODULE_PATHNAME','dblink_current_query' LANGUAGE 'c';
+
+GRANT EXECUTE ON FUNCTION dblink_connect (text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_disconnect () TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_open (text,text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_fetch (text,int) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_close (text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink (text,text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink (text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_exec (text,text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_exec (text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_get_pkey (text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_build_sql_insert (text, int2vector, int2, _text, _text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_build_sql_delete (text, int2vector, int2, _text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_build_sql_update (text, int2vector, int2, _text, _text) TO PUBLIC;
+GRANT EXECUTE ON FUNCTION dblink_current_query () TO PUBLIC;
+
+++ /dev/null
-#!/bin/sh
-
-dropdb -U postgres dblink_test_master
-createdb -U postgres dblink_test_master
-psql -U postgres dblink_test_master < `pwd`/dblink.sql
-
-dropdb -U postgres dblink_test_slave
-createdb -U postgres dblink_test_slave
-psql -U postgres dblink_test_slave < `pwd`/dblink.sql
-
-psql -eaq -U postgres template1 < `pwd`/dblink.test.sql > dblink.test.out 2>&1
-diff -c ./dblink.test.expected.out `pwd`/dblink.test.out > dblink.test.diff
-ls -l dblink.test.diff
-
-
-\connect dblink_test_slave
+--
+-- First, create a slave database and define the functions.
+-- Turn off echoing so that expected file does not depend on
+-- contents of dblink.sql.
+--
+CREATE DATABASE regression_slave;
+\connect regression_slave
+\set ECHO none
create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'foo_pkey' for table 'foo'
insert into foo values(0,'a','{"a0","b0","c0"}');
insert into foo values(7,'h','{"a7","b7","c7"}');
insert into foo values(8,'i','{"a8","b8","c8"}');
insert into foo values(9,'j','{"a9","b9","c9"}');
-\connect dblink_test_master
+-- misc utilities
+-- show the currently executing query
+select 'hello' as hello, dblink_current_query() as query;
+ hello | query
+-------+-----------------------------------------------------------
+ hello | select 'hello' as hello, dblink_current_query() as query;
+(1 row)
+
+-- list the primary key fields
+select * from dblink_get_pkey('foo');
+ position | colname
+----------+---------
+ 1 | f1
+ 2 | f2
+(2 rows)
+
+-- build an insert statement based on a local tuple,
+-- replacing the primary key values with new ones
+select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
+ dblink_build_sql_insert
+-----------------------------------------------------------
+ INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
+(1 row)
+
+-- build an update statement based on a local tuple,
+-- replacing the primary key values with new ones
+select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
+ dblink_build_sql_update
+----------------------------------------------------------------------------------------
+ UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
+(1 row)
+
+-- build a delete statement based on a local tuple,
+select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
+ dblink_build_sql_delete
+---------------------------------------------
+ DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
+(1 row)
+
+--
+-- Connect back to the regression database and define the functions.
+-- Turn off echoing so that expected file does not depend on
+-- contents of dblink.sql.
+--
+\connect regression
+\set ECHO none
-- regular old dblink
-select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
+select * from dblink('dbname=regression_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
a | b | c
---+---+------------
8 | i | {a8,b8,c8}
select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
ERROR: dblink: no connection available
-- create a persistent connection
-select dblink_connect('dbname=dblink_test_slave');
+select dblink_connect('dbname=regression_slave');
dblink_connect
----------------
OK
ERROR: dblink: no connection available
-- put more data into our slave table, first using arbitrary connection syntax
-- but truncate the actual return value so we can use diff to check for success
-select substr(dblink_exec('dbname=dblink_test_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
+select substr(dblink_exec('dbname=regression_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
substr
--------
INSERT
(1 row)
-- create a persistent connection
-select dblink_connect('dbname=dblink_test_slave');
+select dblink_connect('dbname=regression_slave');
dblink_connect
----------------
OK
---+---+---
(0 rows)
--- misc utilities
-\connect dblink_test_slave
--- show the currently executing query
-select 'hello' as hello, dblink_current_query() as query;
- hello | query
--------+-----------------------------------------------------------
- hello | select 'hello' as hello, dblink_current_query() as query;
+-- close the persistent connection
+select dblink_disconnect();
+ dblink_disconnect
+-------------------
+ OK
(1 row)
--- list the primary key fields
-select * from dblink_get_pkey('foo');
- position | colname
-----------+---------
- 1 | f1
- 2 | f2
-(2 rows)
+-- now wait for the connection to the slave to be cleared before
+-- we try to drop the database
+CREATE FUNCTION wait() RETURNS TEXT AS '
+DECLARE
+ rec record;
+ cntr int;
+BEGIN
+ cntr = 0;
--- build an insert statement based on a local tuple,
--- replacing the primary key values with new ones
-select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
- dblink_build_sql_insert
------------------------------------------------------------
- INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
-(1 row)
+ select into rec d.datname
+ from pg_database d,
+ (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
+ where d.oid = b.dbid and d.datname = ''regression_slave'';
--- build an update statement based on a local tuple,
--- replacing the primary key values with new ones
-select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
- dblink_build_sql_update
-----------------------------------------------------------------------------------------
- UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
-(1 row)
+ WHILE FOUND LOOP
+ cntr = cntr + 1;
--- build a delete statement based on a local tuple,
-select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
- dblink_build_sql_delete
----------------------------------------------
- DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
+ select into rec d.datname
+ from pg_database d,
+ (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
+ where d.oid = b.dbid and d.datname = ''regression_slave'';
+
+ -- safety valve
+ if cntr > 1000 THEN
+ EXIT;
+ end if;
+ END LOOP;
+ RETURN ''OK'';
+END;
+' LANGUAGE 'plpgsql';
+SELECT wait();
+ wait
+------
+ OK
(1 row)
+-- OK, safe to drop the slave
+DROP DATABASE regression_slave;
-\connect dblink_test_slave
+--
+-- First, create a slave database and define the functions.
+-- Turn off echoing so that expected file does not depend on
+-- contents of dblink.sql.
+--
+CREATE DATABASE regression_slave;
+\connect regression_slave
+\set ECHO none
+\i dblink.sql
+\set ECHO all
create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
insert into foo values(0,'a','{"a0","b0","c0"}');
insert into foo values(8,'i','{"a8","b8","c8"}');
insert into foo values(9,'j','{"a9","b9","c9"}');
-\connect dblink_test_master
+-- misc utilities
+
+-- show the currently executing query
+select 'hello' as hello, dblink_current_query() as query;
+
+-- list the primary key fields
+select * from dblink_get_pkey('foo');
+
+-- build an insert statement based on a local tuple,
+-- replacing the primary key values with new ones
+select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
+
+-- build an update statement based on a local tuple,
+-- replacing the primary key values with new ones
+select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
+
+-- build a delete statement based on a local tuple,
+select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
+
+--
+-- Connect back to the regression database and define the functions.
+-- Turn off echoing so that expected file does not depend on
+-- contents of dblink.sql.
+--
+\connect regression
+\set ECHO none
+\i dblink.sql
+\set ECHO all
-- regular old dblink
-select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
+select * from dblink('dbname=regression_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
-- should generate "no connection available" error
select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
-- create a persistent connection
-select dblink_connect('dbname=dblink_test_slave');
+select dblink_connect('dbname=regression_slave');
-- use the persistent connection
select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
-- put more data into our slave table, first using arbitrary connection syntax
-- but truncate the actual return value so we can use diff to check for success
-select substr(dblink_exec('dbname=dblink_test_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
+select substr(dblink_exec('dbname=regression_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
-- create a persistent connection
-select dblink_connect('dbname=dblink_test_slave');
+select dblink_connect('dbname=regression_slave');
-- put more data into our slave table, using persistent connection syntax
-- but truncate the actual return value so we can use diff to check for success
-- let's see it
select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11;
--- misc utilities
-\connect dblink_test_slave
-
--- show the currently executing query
-select 'hello' as hello, dblink_current_query() as query;
-
--- list the primary key fields
-select * from dblink_get_pkey('foo');
-
--- build an insert statement based on a local tuple,
--- replacing the primary key values with new ones
-select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
-
--- build an update statement based on a local tuple,
--- replacing the primary key values with new ones
-select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
+-- close the persistent connection
+select dblink_disconnect();
--- build a delete statement based on a local tuple,
-select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
+-- now wait for the connection to the slave to be cleared before
+-- we try to drop the database
+CREATE FUNCTION wait() RETURNS TEXT AS '
+DECLARE
+ rec record;
+ cntr int;
+BEGIN
+ cntr = 0;
+
+ select into rec d.datname
+ from pg_database d,
+ (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
+ where d.oid = b.dbid and d.datname = ''regression_slave'';
+
+ WHILE FOUND LOOP
+ cntr = cntr + 1;
+
+ select into rec d.datname
+ from pg_database d,
+ (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
+ where d.oid = b.dbid and d.datname = ''regression_slave'';
+
+ -- safety valve
+ if cntr > 1000 THEN
+ EXIT;
+ end if;
+ END LOOP;
+ RETURN ''OK'';
+END;
+' LANGUAGE 'plpgsql';
+SELECT wait();
+
+-- OK, safe to drop the slave
+DROP DATABASE regression_slave;