-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.scm
executable file
·153 lines (131 loc) · 4.71 KB
/
example.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(import (scheme base)
(scheme write)
(postgresql))
(define (print . args) (for-each display args) (newline))
;; user: postgres
;; pass: postgres
(define conn (make-postgresql-connection
"localhost" "5432" #f "postgres" "postgres"))
(print "open connection")
;; open the connection
(postgresql-open-connection! conn)
;; login
(print "login")
(postgresql-login! conn)
(print "create tables")
;; may not be there yet (causes an error if there isn't)
(guard (e (else #t)) (postgresql-execute-sql! conn "drop table test"))
(guard (e (else (print (error-object-message e))))
(postgresql-execute-sql! conn
"create table test (id integer, name varchar(50))"))
(postgresql-terminate! conn)
(postgresql-open-connection! conn)
(postgresql-login! conn)
(print "simple query")
(let ((r (postgresql-execute-sql! conn "select * from test")))
(print (postgresql-query-descriptions r))
(print (postgresql-fetch-query! r)))
(postgresql-execute-sql! conn
"insert into test (id, name) values (1, 'name')")
(postgresql-execute-sql! conn
"insert into test (id, name) values (2, 'test name')")
(postgresql-execute-sql! conn
"insert into test (id, name) values (-1, 'test name2')")
(postgresql-execute-sql! conn "commit")
(print "insert with prepared statement")
(let ((p (postgresql-prepared-statement
conn "insert into test (id, name) values ($1, $2)")))
(print (postgresql-prepared-statement-sql p))
(print (postgresql-bind-parameters! p 3 "name"))
(let ((q (postgresql-execute! p)))
(print q))
(postgresql-close-prepared-statement! p))
(let ((p (postgresql-prepared-statement
conn "insert into test (id, name) values ($1, $2)")))
(print (postgresql-prepared-statement-sql p))
(print (postgresql-bind-parameters! p 3 '()))
(let ((q (postgresql-execute! p)))
(print q))
(postgresql-close-prepared-statement! p))
(print "select * from test")
(let ((r (postgresql-execute-sql! conn "select * from test")))
(print (postgresql-query-descriptions r))
(print (postgresql-fetch-query! r))
(print (postgresql-fetch-query! r))
(print (postgresql-fetch-query! r))
(print (postgresql-fetch-query! r))
(print (postgresql-fetch-query! r)))
(let ((p (postgresql-prepared-statement
conn "select * from test where name = $1")))
(print (postgresql-prepared-statement-sql p))
(print (postgresql-bind-parameters! p "name"))
(let ((q (postgresql-execute! p)))
(print q)
(print (postgresql-fetch-query! q))
(print (postgresql-fetch-query! q)))
(postgresql-close-prepared-statement! p))
(let ((p (postgresql-prepared-statement
conn "select * from test where id = $1")))
(print (postgresql-prepared-statement-sql p))
(print (postgresql-bind-parameters! p 1))
(let ((q (postgresql-execute! p)))
(print q)
(print (postgresql-fetch-query! q))
(print (postgresql-fetch-query! q)))
(postgresql-close-prepared-statement! p))
;; delete
(print "delete")
(print (postgresql-execute-sql! conn "delete from test"))
;; max column test
(let ((p (postgresql-prepared-statement
conn "insert into test (id, name) values ($1, $2)")))
(let loop ((i 0))
(unless (= i 100)
(postgresql-bind-parameters! p i "name")
(postgresql-execute! p)
(loop (+ i 1))))
(postgresql-close-prepared-statement! p))
(let ((p (postgresql-prepared-statement
conn "select * from test where name = $1")))
(print (postgresql-prepared-statement-sql p))
(print (postgresql-bind-parameters! p "name"))
(let ((q (postgresql-execute! p)))
;; skip first 50
(print "skip 50")
(do ((i 0 (+ i 1)))
((= i 50))
(postgresql-fetch-query! q))
;; 51
(print "get 51st")
(print (postgresql-fetch-query! q))
;; skip next 50
(do ((i 0 (+ i 1)))
((= i 50))
(postgresql-fetch-query! q))
(print (postgresql-fetch-query! q)))
(postgresql-close-prepared-statement! p))
(let ((q (postgresql-execute-sql! conn "select * from test")))
(do ((i 0 (+ i 1)))
((= i 60))
(postgresql-fetch-query! q))
(print (postgresql-fetch-query! q)))
(postgresql-execute-sql! conn "drop table test")
(print "droping non existing table")
(guard (e (else (print (error-object-message e))))
(postgresql-execute-sql! conn "drop table test"))
;; issue #2 re-creation of prepared statement
;; ? is not a valid placeholder in PostgreSQL
(guard (e (else (print e)))
(let ((ps (postgresql-prepared-statement
conn "select * from foo where a = ?")))
(print ps)
(postgresql-close-prepared-statement! ps)))
;; this hanged
(guard (e (else (print e)))
(let ((ps (postgresql-prepared-statement
conn "select * from foo where a = ?")))
(print ps)
(postgresql-close-prepared-statement! ps)))
;; terminate and close connection
(print "terminate")
(postgresql-terminate! conn)