15dfecf96Smrg;; Postgresql C library interface, example program 2, using the xedit
25dfecf96Smrg;; lisp interface
35dfecf96Smrg
45dfecf96Smrg;;  Test of the asynchronous notification interface
55dfecf96Smrg;;
65dfecf96Smrg;; Start this program, then from psql in another window do
75dfecf96Smrg;;   NOTIFY TBL2;
85dfecf96Smrg;;
95dfecf96Smrg;; Or, if you want to get fancy, try this:
105dfecf96Smrg;; Populate a database with the following:
115dfecf96Smrg;;
125dfecf96Smrg;;   CREATE TABLE TBL1 (i int4);
135dfecf96Smrg;;
145dfecf96Smrg;;   CREATE TABLE TBL2 (i int4);
155dfecf96Smrg;;
165dfecf96Smrg;;   CREATE RULE r1 AS ON INSERT TO TBL1 DO
175dfecf96Smrg;;     (INSERT INTO TBL2 values (new.i); NOTIFY TBL2);
185dfecf96Smrg;;
195dfecf96Smrg;; and do
205dfecf96Smrg;;
215dfecf96Smrg;;   INSERT INTO TBL1 values (10);
225dfecf96Smrg(require "psql")
235dfecf96Smrg
245dfecf96Smrg(defun exit-nicely (conn)
255dfecf96Smrg    (pq-finish conn)
265dfecf96Smrg    (quit 1)
275dfecf96Smrg)
285dfecf96Smrg
295dfecf96Smrg;; begin, by setting the parameters for a backend connection if the
305dfecf96Smrg;; parameters are null, then the system will try to use reasonable
315dfecf96Smrg;; defaults by looking up environment variables or, failing that,
325dfecf96Smrg;; using hardwired constants
335dfecf96Smrg(setq pghost nil)		; host name of the backend server
345dfecf96Smrg(setq pgport nil)		; port of the backend server
355dfecf96Smrg(setq pgoptions nil)		; special options to start up the backend server
365dfecf96Smrg(setq pgtty nil)		; debugging tty for the backend server
375dfecf96Smrg(setq pgdbname "test")		; change this to the name of your test database
385dfecf96Smrg				;; XXX Note: getenv not yet implemented in the
395dfecf96Smrg				 ; lisp interpreter
405dfecf96Smrg
415dfecf96Smrg;; make a connection to the database
425dfecf96Smrg(setq conn (pq-setdb pghost pgport pgoptions pgtty pgdbname))
435dfecf96Smrg
445dfecf96Smrg;; check to see that the backend connection was successfully made
455dfecf96Smrg(when (= (pq-status conn) pg-connection-bad)
465dfecf96Smrg    (format t "Connection to database '~A' failed.~%" pgdbname)
475dfecf96Smrg    (format t "~A" (pq-error-message conn))
485dfecf96Smrg    (exit-nicely conn))
495dfecf96Smrg
505dfecf96Smrg(setq res (pq-exec conn "LISTEN TBL2"))
515dfecf96Smrg(when (= (pq-status conn) pg-connection-bad)
525dfecf96Smrg    (format t "LISTEN command failed~%")
535dfecf96Smrg    (format t "~A" (pq-error-message conn))
545dfecf96Smrg    (exit-nicely conn))
555dfecf96Smrg
565dfecf96Smrg;; Should PQclear PGresult whenever it is no longer needed to avoid memory leaks
575dfecf96Smrg(pq-clear res)
585dfecf96Smrg
595dfecf96Smrg(loop
605dfecf96Smrg    ;; wait a little bit between checks; waiting with select()
615dfecf96Smrg    ;; would be more efficient.
625dfecf96Smrg	;; XXX Note: sleep not yet implemented in the lisp interpreter
635dfecf96Smrg
645dfecf96Smrg    ;; collect any asynchronous backend messages
655dfecf96Smrg    (pq-consume-input conn)
665dfecf96Smrg
675dfecf96Smrg    ;; check for asynchronous notify messages
685dfecf96Smrg    (when (setq notifies (pq-notifies conn))
695dfecf96Smrg	(format t "ASYNC NOTIFY of '~A' from backend pid '~D' received~%"
705dfecf96Smrg	 (pg-notify-relname notifies) (pg-notify-be-pid notifies))
715dfecf96Smrg    )
725dfecf96Smrg)
735dfecf96Smrg
745dfecf96Smrg(pq-finish conn)
75