# pgsql lookups
# The pgsql-devel (or equivalent) package will be need for Exim to build, and
# the postgresql-server (or equivalent) package for this test to run.
#
# first, populate a DB to test against
sudo rm -fr DIR/pgsql
echo Starting DB server
perl
system 'DIR/bin.sys/initdb -D DIR/pgsql/data';
****
#
# I don not understand why, but the DB fails to start if this rm is present.
#sudo rm test-stdout
#
# Start the DB server
background
DIR/bin.sys/postgres -D DIR/pgsql/data -p PORT_N -k DIR/pgsql
****
#
sleep 1
echo Waiting for DB server
perl
system 'DIR/bin.sys/pg_ctl -w -D DIR/pgsql/data status';
system 'createdb -h localhost -p PORT_N test';
system 'DIR/bin.sys/psql -h localhost -p PORT_N -d test \
 -c "CREATE TABLE them ( name text, id text ); \
     INSERT INTO them VALUES ( \'Philip Hazel\', \'ph10\' ); \
     INSERT INTO them VALUES ( \'Aristotle\',    \'aaaa\' ); \
     INSERT INTO them VALUES ( \'\', \'nothing\' ); \
     INSERT INTO them VALUES ( \'\"stquot\', \'quote2\' ); \
     INSERT INTO them VALUES ( \'before\' || CHR(13) || CHR(10) || \'after\', \'newline\' ); \
     INSERT INTO them VALUES ( \'x\' || CHR(9) || \'x\', \'tab\' ); \
     INSERT INTO them VALUES ( CHR(39) || \'stquot\', \'quote1\' );" \
 ';
****
#
echo Populated DB server
#
# now, the tests
exim -d-all+lookup -be
${lookup pgsql {select name from them where id='ph10';}}
${lookup pgsql {select name from them where id='ph10';}}
${lookup pgsql {select name from them where id='xxxx';}}
${lookup pgsql {select name from them where id='nothing';}}
${lookup pgsql {select id,name from them where id='nothing';}}
${lookup pgsql {delete from them where id='nonexist';}}
${lookup pgsql {select * from them where id='quote2';}}
${lookup pgsql {select * from them where id='newline';}}
${lookup pgsql {select * from them where id='tab';}}
${lookup pgsql {select * from them where name='${quote_pgsql:'stquot}';}}
${lookup pgsql {servers=x:localhost; select name from them where id='ph10';}}
${lookup pgsql {servers=localhost::PORT_N:x; select name from them where id='ph10';}}
name: ${lookup pgsql {servers=localhost::PORT_N/test/CALLER/:x; select name from them where id='ph10';}}
ipv4: ${lookup pgsql {servers=127.0.0.1::PORT_N/test/CALLER/:x; select name from them where id='ph10';}}
oldsyntax ipv6: ${lookup pgsql {servers=<!::1.PORT_N/test/CALLER/!x; select name from them where id='ph10';}}
newsyntax ipv6: ${lookup pgsql,servers=<!::1.PORT_N/test/CALLER/!x { select name from them where id='ph10';}}
${lookup pgsql {servers=(DIR/pgsql/.s.PGSQL.PORT_N)/test/CALLER/:x; select name from them where id='ph10';}}
x
${lookup pgsql {SELECT name FROM them WHERE id IN ('ph10', 'aaaa');}}
${lookup pgsql {SELECT *    FROM them WHERE id IN ('ph10', 'aaaa');}}
${lookup pgsql {delete from them where id='aaaa'}}
****
exim -d -bh 10.0.0.0
helo test
mail from:<a@b>
rcpt to:<c@d>
rcpt to:<c@d>
quit
****
exim -odi -d CALLER
Test message
.
****
exim -DSERVERS=\(DIR/pgsql/.s.PGSQL.PORT_N\)/test/CALLER/ -d-all+lookup -be
${lookup pgsql {select name from them where id='ph10';}}
****
#
perl
system 'DIR/bin.sys/pg_ctl stop -D DIR/pgsql/data -m immediate';
****
sleep 2
killdaemon
sudo rm -fr DIR/pgsql
