]> gitweb.factorcode.org Git - factor.git/blob - libs/postgresql/postgresql-test.factor
04c97893197cb8bb5455e8bc4fede380a76fb7b4
[factor.git] / libs / postgresql / postgresql-test.factor
1 ! You will need to run  'createdb factor-test' to create the database.
2 ! Set username and password in  the 'connect' word.
3
4 IN: postgresql-test
5 USING: kernel postgresql alien errors io prettyprint sequences namespaces ;
6
7
8 : test-connection ( host port pgopts pgtty db user pass -- bool )
9     [ [ ] with-postgres ] catch "Error connecting!" "Connected!" ? print ;
10
11 ! just a basic demo
12
13 "localhost" "" "" "" "test" "postgres" "" [
14     "drop table animal" do-command
15
16     "create table animal (id serial not null primary key, species varchar(256), name varchar(256), age integer)" do-command
17     "insert into animal (species, name, age) values ('lion', 'Mufasa', 5)"
18     do-command
19
20     "select * from animal where name = 'Mufasa'" [ ] do-query
21     "select * from animal where name = 'Mufasa'"
22     [
23         result>seq length 1 = [ "...there can only be one Mufasa..." throw ] unless
24     ] do-query
25
26     "insert into animal (species, name, age) values ('lion', 'Simba', 1)"
27     do-command
28
29     "select * from animal" 
30     [
31           "Animal table:" print
32           result>seq print-table
33     ] do-query
34
35     ! intentional errors
36     ! [ "select asdf from animal"
37     ! [ ] do-query ] catch [ "caught: " write print ] when*
38     ! "select asdf from animal" [ ] do-query 
39     ! "aofijweafew" do-command
40 ] with-postgres-catch
41