15dfecf96Smrg;; Postgresql C library interface, example program 3, using the xedit
25dfecf96Smrg;; lisp interface
35dfecf96Smrg
45dfecf96Smrg;;  Test the binary cursor interface
55dfecf96Smrg;;
65dfecf96Smrg;; populate a database by doing the following:
75dfecf96Smrg;;
85dfecf96Smrg;; CREATE TABLE test1 (i int4, d real, p polygon);
95dfecf96Smrg;;
105dfecf96Smrg;; INSERT INTO test1 values (1, 3.567, polygon '(3.0, 4.0, 1.0, 2.0)');
115dfecf96Smrg;;
125dfecf96Smrg;; INSERT INTO test1 values (2, 89.05, polygon '(4.0, 3.0, 2.0, 1.0)');
135dfecf96Smrg;;
145dfecf96Smrg;; the expected output is:
155dfecf96Smrg;;
165dfecf96Smrg;; tuple 0: got i = (4 bytes) 1, d = (4 bytes) 3.567000, p = (4
175dfecf96Smrg;; bytes) 2 points   boundbox = (hi=3.000000/4.000000, lo =
185dfecf96Smrg;; 1.000000,2.000000) tuple 1: got i = (4 bytes) 2, d = (4 bytes)
195dfecf96Smrg;; 89.050003, p = (4 bytes) 2 points   boundbox =
205dfecf96Smrg;; (hi=4.000000/3.000000, lo = 2.000000,1.000000)
215dfecf96Smrg
225dfecf96Smrg;;  Output of the lisp code:
235dfecf96Smrg;;
245dfecf96Smrg;; type[0] = 23, size[0] = 4
255dfecf96Smrg;; type[1] = 700, size[1] = 4
265dfecf96Smrg;; type[2] = 604, size[2] = -1
275dfecf96Smrg;; tuple 0: got
285dfecf96Smrg;;  i = (4 bytes) 1
295dfecf96Smrg;;  d = (4 bytes) 3.567
305dfecf96Smrg;;  p = (4 bytes) 2 points boundbox = (hi=3.0/4.0, lo = 1.0/2.0)
315dfecf96Smrg;; tuple 1: got
325dfecf96Smrg;;  i = (4 bytes) 2
335dfecf96Smrg;;  d = (4 bytes) 89.05
345dfecf96Smrg;;  p = (4 bytes) 2 points boundbox = (hi=4.0/3.0, lo = 2.0/1.0)
355dfecf96Smrg
365dfecf96Smrg
375dfecf96Smrg(require "psql")
385dfecf96Smrg
395dfecf96Smrg(defun exit-nicely (conn)
405dfecf96Smrg    (pq-finish conn)
415dfecf96Smrg    (quit 1)
425dfecf96Smrg)
435dfecf96Smrg
445dfecf96Smrg;; begin, by setting the parameters for a backend connection if the
455dfecf96Smrg;; parameters are null, then the system will try to use reasonable
465dfecf96Smrg;; defaults by looking up environment variables or, failing that,
475dfecf96Smrg;; using hardwired constants
485dfecf96Smrg(setq pghost nil)		; host name of the backend server
495dfecf96Smrg(setq pgport nil)		; port of the backend server
505dfecf96Smrg(setq pgoptions nil)		; special options to start up the backend server
515dfecf96Smrg(setq pgtty nil)		; debugging tty for the backend server
525dfecf96Smrg(setq pgdbname "test")		; change this to the name of your test database
535dfecf96Smrg				;; XXX Note: getenv not yet implemented in the
545dfecf96Smrg				 ; lisp interpreter
555dfecf96Smrg
565dfecf96Smrg;; make a connection to the database
575dfecf96Smrg(setq conn (pq-setdb pghost pgport pgoptions pgtty pgdbname))
585dfecf96Smrg
595dfecf96Smrg;; check to see that the backend connection was successfully made
605dfecf96Smrg(when (= (pq-status conn) pg-connection-bad)
615dfecf96Smrg    (format t "Connection to database '~A' failed.~%" pgdbname)
625dfecf96Smrg    (format t "~A" (pq-error-message conn))
635dfecf96Smrg    (exit-nicely conn))
645dfecf96Smrg
655dfecf96Smrg(setq res (pq-exec conn "BEGIN"))
665dfecf96Smrg(when (= (pq-status conn) pg-connection-bad)
675dfecf96Smrg    (format t "BEGIN command failed~%")
685dfecf96Smrg    (pq-clear res)
695dfecf96Smrg    (exit-nicely conn))
705dfecf96Smrg
715dfecf96Smrg;; Should PQclear PGresult whenever it is no longer needed to avoid memory leaks
725dfecf96Smrg(pq-clear res)
735dfecf96Smrg
745dfecf96Smrg(setq res (pq-exec conn "DECLARE mycursor BINARY CURSOR FOR select * from test1"))
755dfecf96Smrg(when (= (pq-status conn) pg-connection-bad)
765dfecf96Smrg    (format t "DECLARE CURSOR command failed~%")
775dfecf96Smrg    (pq-clear res)
785dfecf96Smrg    (exit-nicely conn))
795dfecf96Smrg(pq-clear res)
805dfecf96Smrg
815dfecf96Smrg(setq res (pq-exec conn "FETCH ALL in mycursor"))
825dfecf96Smrg(when (or (null res) (not (= (pq-result-status res) pgres-tuples-ok)))
835dfecf96Smrg    (format t "FETCH ALL command didn't return tuples properly~%")
845dfecf96Smrg    (pq-clear res)
855dfecf96Smrg    (exit-nicely conn))
865dfecf96Smrg
875dfecf96Smrg(setq i-fnum (pq-fnumber res "i"))
885dfecf96Smrg(setq d-fnum (pq-fnumber res "d"))
895dfecf96Smrg(setq p-fnum (pq-fnumber res "p"))
905dfecf96Smrg
915dfecf96Smrg(dotimes (i 3)
925dfecf96Smrg    (format t "type[~D] = ~D, size[~D] = ~D~%"
935dfecf96Smrg     i (pq-ftype res i) i (pq-fsize res i))
945dfecf96Smrg)
955dfecf96Smrg
965dfecf96Smrg(dotimes (i (pq-ntuples res))
975dfecf96Smrg    (setq i-val (pq-getvalue res i i-fnum 'int32))
985dfecf96Smrg    (setq d-val (pq-getvalue res i d-fnum 'float))
995dfecf96Smrg    (setq p-val (pq-getvalue res i p-fnum 'pg-polygon))
1005dfecf96Smrg    (format t "tuple ~D: got~%" i)
1015dfecf96Smrg    (format t " i = (~D bytes) ~D~%" (pq-getlength res i i-fnum) i-val)
1025dfecf96Smrg    (format t " d = (~D bytes) ~D~%" (pq-getlength res i d-fnum) d-val)
1035dfecf96Smrg    (format t " p = (~D bytes) ~D points~,8@Tboundbox = (hi=~F/~F, lo = ~F/~F)~%"
1045dfecf96Smrg     (pq-getlength res i d-fnum) (pg-polygon-num-points p-val)
1055dfecf96Smrg     (pg-point-x (pg-box-high (pg-polygon-boundbox p-val)))
1065dfecf96Smrg     (pg-point-y (pg-box-high (pg-polygon-boundbox p-val)))
1075dfecf96Smrg     (pg-point-x (pg-box-low (pg-polygon-boundbox p-val)))
1085dfecf96Smrg     (pg-point-y (pg-box-low (pg-polygon-boundbox p-val))))
1095dfecf96Smrg)
1105dfecf96Smrg(pq-clear res)
1115dfecf96Smrg
1125dfecf96Smrg(setq res (pq-exec conn "CLOSE mycursor"))
1135dfecf96Smrg(pq-clear res)
1145dfecf96Smrg
1155dfecf96Smrg(setq res (pq-exec conn "COMMIT"))
1165dfecf96Smrg(pq-clear res)
1175dfecf96Smrg
1185dfecf96Smrg(pq-finish conn)
119