Slightly better basic type detection.
[sxemacs] / info / lispref / postgresql.texi
1 @c -*-texinfo-*-
2 @c This is part of the SXEmacs Lisp Reference Manual.
3 @c Copyright (C) 2000 Electrotechnical Laboratory, JAPAN
4 @c Copyright (C) 2005 Sebastian Freundt
5 @c See the file lispref.texi for copying conditions.
6 @c Thank you Oscar Figueiredo!  This file was shamelessly cloned from
7 @c  ldap.texi.
8 @setfilename ../../info/postgresql.info
9
10 @node PostgreSQL Support, OpenSSL Support, LDAP Support, top
11 @chapter PostgreSQL Support
12 @cindex PostgreSQL
13
14 SXEmacs can be linked with PostgreSQL libpq run-time support to provide
15 relational database access from Emacs Lisp code.
16
17 This implementation has been successfully tested with PostgreSQL versions
18 6.5, 7.x and 8.x.
19
20 @menu
21 * Building SXEmacs with PostgreSQL support::
22 * SXEmacs PostgreSQL libpq API::
23 * SXEmacs PostgreSQL libpq Examples::
24 @end menu
25
26
27 @node Building SXEmacs with PostgreSQL support, SXEmacs PostgreSQL libpq API,  ,PostgreSQL Support
28 @comment  node-name,  next,  previous,  up
29 @section Building SXEmacs with PostgreSQL support
30
31 SXEmacs PostgreSQL support requires linking to the PostgreSQL libpq
32 library.  We have copied the short version of how to build and setup
33 PostgreSQL here:
34
35 @example
36 ./configure
37 gmake
38 su
39 gmake install
40 adduser postgres
41 mkdir /usr/local/pgsql/data
42 chown postgres /usr/local/pgsql/data
43 su - postgres
44 /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
45 /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &
46 @end example
47
48 With this installation you can try to create a database @samp{test}:
49
50 @example
51 /usr/local/pgsql/bin/createdb test
52 /usr/local/pgsql/bin/psql test
53 @end example
54
55
56 If you have installed SXEmacs from one of the binary kits on
57 (@url{ftp://ftp.sxemacs.org/}), or are using an SXEmacs binary from
58 your favourite distributor, you may have SXEmacs PostgreSQL support
59 by default.  @code{M-x describe-installation} will tell you if you do.
60
61 If you are building SXEmacs from source, you need to install PostgreSQL
62 first (see above example).  On some systems, PostgreSQL will come 
63 pre-installed in /usr or /usr/local.  In this case, it should be
64 autodetected when you run configure.
65
66 If PostgreSQL is installed into its default location (for example when
67 passing no @samp{--prefix} to the configure; @file{/usr/local/pgsql} is
68 default) you must specify @code{--site-prefixes=/usr/local/pgsql} when
69 you run configure.  If PostgreSQL is installed into another location, 
70 use that instead of @file{/usr/local/pgsql} when specifying
71 @code{--site-prefixes}.
72
73 All versions of SXEmacs have been reported to work with PostgreSQL
74 versions 6.5, 7.x, and 8.x.  SXEmacs Lisp support for V7.x and V8.x is
75 somewhat more extensive than support for V6.5.  In particular, 
76 asynchronous queries are supported.
77
78
79 @node SXEmacs PostgreSQL libpq API, SXEmacs PostgreSQL libpq Examples, Building SXEmacs with PostgreSQL support, PostgreSQL Support
80 @comment  node-name,  next,  previous,  up
81 @section SXEmacs PostgreSQL libpq API
82
83 The SXEmacs PostgreSQL API is intended to be a policy-free, low-level
84 binding to libpq.  That is to provide all the basic functionality
85 and then let high level Lisp code decide its own policies.
86
87 This documentation assumes that the reader has knowledge of SQL, but
88 requires no prior knowledge of libpq.
89
90 There are many examples in this manual and some setup will be required.
91 In order to run most of the following examples, the following code needs
92 to be executed.  The first example establishes a database connection and
93 then creates a table @samp{sxemacs_codenames} in your default database.
94
95 It may happen for you that this code fails.  The code heavily depends
96 on your settings in @file{pg_hba.conf} (host-based access controls).  On
97 the other hand, nearly all of the examples during this documentation will
98 assume that the free variable @code{P} refers to the database
99 connection.
100
101 @example
102 @group
103 ;; @r{establish a connection to @samp{$PGDATABASE} as @samp{$PGUSER}}
104 ;; @r{on @samp{$PGHOST} at port @samp{$PGPORT}}
105 (setq P (pq-connectdb ""))
106   @result{} #<PGconn kantdb:5432 freundt/freundt>
107 @end group
108
109 @group
110 ;; @r{establish a connection to a password-protected db}
111 (setq P (pq-connectdb "password=foo234bar"))
112   @result{} #<PGconn kantdb:5432 freundt/freundt>
113 @end group
114
115 @group
116 ;; @r{now create a test table}
117 (pq-exec P (concat "CREATE TABLE sxemacs_codenames"
118                    " (id int, version text, codename text);"))
119   @result{} #<PGresult PGRES_COMMAND_OK - CREATE TABLE>
120 @end group
121 @end example
122
123 Now we transfer following data to the table created.
124
125 @example
126 @group
127 (progn
128   (pq-exec P "COPY sxemacs_codenames FROM stdin;")
129   (pq-put-line P "1\t22.1.0\tAlfa Romeo\n")
130   (pq-put-line P "2\t22.1.1\tAston Martin\n")
131   (pq-put-line P "3\t22.1.2\tAudi\n")
132   (pq-put-line P "4\t22.1.3\tBMW\n")
133   (pq-put-line P "5\t22.1.4\tBentley Turbo\n")
134   (pq-put-line P "\\.\n")
135   (pq-end-copy P))
136      @result{} nil
137 @end group
138 @end example
139
140 @menu
141 * libpq Lisp Variables::
142 * libpq Lisp Symbols and DataTypes::
143 * Synchronous Interface Functions::
144 * Asynchronous Interface Functions::
145 * Large Object Support::
146 * Other libpq Functions::
147 * Unimplemented libpq Functions::
148 @end menu
149
150
151 @node libpq Lisp Variables, libpq Lisp Symbols and DataTypes, SXEmacs PostgreSQL libpq API, SXEmacs PostgreSQL libpq API
152 @comment  node-name,  next,  previous,  up
153 @subsection libpq Lisp Variables
154
155 Various Unix environment variables are used by libpq to provide defaults
156 to the many different parameters.  In the SXEmacs Lisp API, these
157 environment variables are bound to Lisp variables to provide more
158 convenient access to Lisp Code.  These variables are passed to the
159 backend database server during the establishment of a database
160 connection and when the @code{pq-setenv} call is made.
161
162 @defvar pg:host
163 Initialized from the @code{PGHOST} environment variable.  The default
164 host to connect to.
165 @end defvar
166
167 @defvar pg:user
168 Initialized from the @code{PGUSER} environment variable.  The default
169 database user name.
170 @end defvar
171
172 @defvar pg:options
173 Initialized from the @code{PGOPTIONS} environment variable.  Default
174 additional server options.
175 @end defvar
176
177 @defvar pg:port
178 Initialized from the @code{PGPORT} environment variable.  The default
179 TCP port to connect to.
180 @end defvar
181
182 @defvar pg:tty
183 Initialized from the @code{PGTTY} environment variable.  The default
184 debugging TTY.
185
186 Compatibility note:  Debugging TTYs are turned off in the SXEmacs Lisp
187 binding.
188 @end defvar
189
190 @defvar pg:database
191 Initialized from the @code{PGDATABASE} environment variable.  The
192 default database to connect to.
193 @end defvar
194
195 @defvar pg:realm
196 Initialized from the @code{PGREALM} environment variable.  The default
197 Kerberos realm.
198 @end defvar
199
200 @defvar pg:client-encoding
201 Initialized from the @code{PGCLIENTENCODING} environment variable.  The
202 default client encoding.
203
204 Compatibility note:  This variable is not present in non-Mule SXEmacsen.
205 This variable is not present in versions of libpq prior to 7.0.
206 In the current implementation, client encoding is equivalent to the
207 @code{file-name-coding-system} format.
208 @end defvar
209
210 @c unused
211 @defvar pg:authtype
212 Initialized from the @code{PGAUTHTYPE} environment variable.  The
213 default authentication scheme used.
214
215 Compatibility note:  This variable is unused in versions of libpq after
216 6.5.  It is not implemented at all in the SXEmacs Lisp binding.
217 @end defvar
218
219 @defvar pg:geqo
220 Initialized from the @code{PGGEQO} environment variable.  Genetic
221 optimizer options.
222 @end defvar
223
224 @defvar pg:cost-index
225 Initialized from the @code{PGCOSTINDEX} environment variable.  Cost
226 index options.
227 @end defvar
228
229 @defvar pg:cost-heap
230 Initialized from the @code{PGCOSTHEAP} environment variable.  Cost heap
231 options.
232 @end defvar
233
234 @defvar pg:tz
235 Initialized from the @code{PGTZ} environment variable.  Default
236 timezone.
237 @end defvar
238
239 @defvar pg:date-style
240 Initialized from the @code{PGDATESTYLE} environment variable.  Default
241 date style in returned date objects.
242 @end defvar
243
244 @defvar pg-coding-system
245 This is a variable controlling which coding system is used to encode
246 non-ASCII strings sent to the database.
247
248 Compatibility Note: This variable is not present in InfoDock.
249 @end defvar
250
251
252 @node libpq Lisp Symbols and DataTypes, Synchronous Interface Functions, libpq Lisp Variables, SXEmacs PostgreSQL libpq API
253 @comment  node-name,  next,  previous,  up
254 @subsection libpq Lisp Symbols and Datatypes
255
256 The following set of symbols are used to represent the intermediate
257 states involved in the asynchronous interface.
258
259 @defvr {Symbol} pgres::polling-failed
260 Undocumented.  A fatal error has occurred during processing of an
261 asynchronous operation.
262 @end defvr
263
264 @defvr {Symbol} pgres::polling-reading
265 An intermediate status return during an asynchronous operation.  It
266 indicates that one may use @code{select} before polling again.
267 @end defvr
268
269 @defvr {Symbol} pgres::polling-writing
270 An intermediate status return during an asynchronous operation.  It
271 indicates that one may use @code{select} before polling again.
272 @end defvr
273
274 @defvr {Symbol} pgres::polling-ok
275 An asynchronous operation has successfully completed.
276 @end defvr
277
278 @defvr {Symbol} pgres::polling-active
279 An intermediate status return during an asynchronous operation.  One can
280 call the poll function again immediately.
281 @end defvr
282
283 @defun pq-pgconn conn field
284 @var{conn} A database connection object.
285 @var{field} A symbol indicating which field of PGconn to fetch.  Possible
286 values are shown in the following table.
287 @table @code
288 @item pq::db
289 Database name
290 @item pq::user
291 Database user name
292 @item pq::pass
293 Database user's password
294 @item pq::host
295 Hostname database server is running on
296 @item pq::port
297 TCP port number used in the connection
298 @item pq::tty
299 Debugging TTY
300
301 Compatibility note:  Debugging TTYs are not used in the SXEmacs Lisp API.
302 @item pq::options
303 Additional server options
304 @item pq::status
305 Connection status.  Possible return values are shown in the following
306 table.
307 @table @code
308 @item pg::connection-ok
309 The normal, connected status.
310 @item pg::connection-bad
311 The connection is not open and the PGconn object needs to be deleted by
312 @code{pq-finish}.
313 @item pg::connection-started
314 An asynchronous connection has been started, but is not yet complete.
315 @item pg::connection-made
316 An asynchronous connect has been made, and there is data waiting to be sent.
317 @item pg::connection-awaiting-response
318 Awaiting data from the backend during an asynchronous connection.
319 @item pg::connection-auth-ok
320 Received authentication, waiting for the backend to start up.
321 @item pg::connection-setenv
322 Negotiating environment during an asynchronous connection.
323 @end table
324 @item pq::error-message
325 The last error message that was delivered to this connection.
326 @item pq::backend-pid
327 The process ID of the backend database server.
328 @item pq::getssl
329 The SSL session of the database connection, @code{nil} for none.
330 @end table
331 @end defun
332
333 The @code{PGresult} object is used by libpq to encapsulate the results
334 of queries.  The printed representation takes on four forms.  When the
335 PGresult object contains tuples from an SQL @code{SELECT} it will look
336 like:
337
338 @example
339 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
340      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
341 @end example
342
343 The number in brackets indicates how many rows of data are available.
344 When the PGresult object is the result of a command query that doesn't
345 return anything, it will look like:
346
347 @example
348 (pq-exec P "CREATE TABLE a_new_table (i int);")
349      @result{} #<PGresult PGRES_COMMAND_OK - CREATE>
350 @end example
351
352 When either the query is a command-type query that can affect a number
353 of different rows, but doesn't return any of them it will look like:
354
355 @example
356 (progn
357   (pq-exec P "INSERT INTO a_new_table VALUES (1);")
358   (pq-exec P "INSERT INTO a_new_table VALUES (2);")
359   (pq-exec P "INSERT INTO a_new_table VALUES (3);")
360   (setq R (pq-exec P "DELETE FROM a_new_table;")))
361      @result{} #<PGresult PGRES_COMMAND_OK[3] - DELETE 3>
362 @end example
363
364 Lastly, when the underlying PGresult object has been deallocated
365 directly by @code{pq-clear} the printed representation will look like:
366
367 @example
368 (progn
369   (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
370   (pq-clear R)
371   R)
372      @result{} #<PGresult DEAD>
373 @end example
374
375 The following set of functions are accessors to various data in the PGresult
376 object.
377
378 @defun pq-result-status result
379 Return status of a query result.
380 @var{result} is a PGresult object.  The return value is one of the
381 symbols in the following table.
382 @table @code
383 @item pgres::empty-query
384 A query contained no text.  This is usually the result of a recoverable
385 error, or a minor programming error.
386 @item pgres::command-ok
387 A query command that doesn't return anything was executed properly by
388 the backend.
389 @item pgres::tuples-ok
390 A query command that returns tuples was executed properly by the
391 backend.
392 @item pgres::copy-out
393 Copy Out data transfer is in progress.
394 @item pgres::copy-in
395 Copy In data transfer is in progress.
396 @item pgres::bad-response
397 An unexpected response was received from the backend.
398 @item pgres::nonfatal-error
399 Undocumented.  This value is returned when the libpq function
400 @code{PQresultStatus} is called with a @code{NULL} pointer.
401 @item pgres::fatal-error
402 Undocumented.  An error has occurred in processing the query and the
403 operation was not completed.
404 @end table
405 @end defun
406
407 @defun pq-res-status result
408 Return the query result status as a string, not a symbol.
409 @var{result} is a PGresult object.
410
411 @example
412 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
413      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
414 (pq-res-status R)
415      @result{} "PGRES_TUPLES_OK"
416 @end example
417 @end defun
418
419 @defun pq-result-error-message result
420 Return an error message generated by the query, if any.
421 @var{result} is a PGresult object.
422
423 @example
424 (setq R (pq-exec P "SELECT * FROM sxemacs-codenames;"))
425      @result{} <A fatal error is signaled in the echo area>
426 (pq-result-error-message R)
427      @result{} "ERROR:  parser: parse error at or near \"-\""
428 @end example
429 @end defun
430
431 @defun pq-ntuples result
432 Return the number of tuples in the query result.
433 @var{result} is a PGresult object.
434
435 @example
436 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
437      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
438 (pq-ntuples R)
439      @result{} 5
440 @end example
441 @end defun
442
443 @defun pq-nfields result
444 Return the number of fields in each tuple of the query result.
445 @var{result} is a PGresult object.
446
447 @example
448 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
449      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
450 (pq-nfields R)
451      @result{} 3
452 @end example
453 @end defun
454
455 @defun pq-binary-tuples result
456 Returns t if binary tuples are present in the results, nil otherwise.
457 @var{result} is a PGresult object.
458
459 @example
460 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
461      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
462 (pq-binary-tuples R)
463      @result{} nil
464 @end example
465 @end defun
466
467 @defun pq-fname result field-index
468 Returns the name of a specific field.
469 @var{result} is a PGresult object.
470 @var{field-index} is the number of the column to select from.  The first
471 column is number zero.
472
473 @example
474 (let (i l)
475   (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
476   (setq i (pq-nfields R))
477   (while (>= (decf i) 0)
478     (push (pq-fname R i) l))
479   l)
480      @result{} ("id" "version" "codename")
481 @end example
482 @end defun
483
484 @defun pq-fnumber result field-name
485 Return the field number corresponding to the given field name.
486 -1 is returned on a bad field name.
487 @var{result} is a PGresult object.
488 @var{field-name} is a string representing the field name to find.
489 @example
490 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
491      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
492 (pq-fnumber R "id")
493      @result{} 0
494 (pq-fnumber R "Not a field")
495      @result{} -1
496 @end example
497 @end defun
498
499 @defun pq-ftype result field-num
500 Return an integer code representing the data type of the specified column.
501 @var{result} is a PGresult object.
502 @var{field-num} is the field number.
503
504 The return value of this function is the Object ID (Oid) in the database
505 of the type.  Further queries need to be made to various system tables
506 in order to convert this value into something useful.
507 @end defun
508
509 @defun pq-fmod result field-num
510 Return the type modifier code associated with a field.  Field numbers
511 start at zero.
512 @var{result} is a PGresult object.
513 @var{field-index} selects which field to use.
514 @end defun
515
516 @defun pq-fsize result field-index
517 Return size of the given field.
518 @var{result} is a PGresult object.
519 @var{field-index} selects which field to use.
520
521 @example
522 (let (i l)
523   (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
524   (setq i (pq-nfields R))
525   (while (>= (decf i) 0)
526     (push (list (pq-ftype R i) (pq-fsize R i)) l))
527   l)
528      @result{} ((23 23) (25 25) (25 25))
529 @end example
530 @end defun
531
532 @defun pq-get-value result tup-num field-num
533 Retrieve a return value.
534 @var{result} is a PGresult object.
535 @var{tup-num} selects which tuple to fetch from.
536 @var{field-num} selects which field to fetch from.
537
538 Both tuples and fields are numbered from zero.
539
540 @example
541 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
542      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
543 (pq-get-value R 0 1)
544      @result{} "22.1.0"
545 (pq-get-value R 1 1)
546      @result{} "22.1.1"
547 (pq-get-value R 1 2)
548      @result{} "Aston Martin"
549 @end example
550 @end defun
551
552 @defun pq-get-length result tup-num field-num
553 Return the length of a specific value.
554 @var{result} is a PGresult object.
555 @var{tup-num} selects which tuple to fetch from.
556 @var{field-num} selects which field to fetch from.
557
558 @example
559 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;"))
560      @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
561 (pq-get-length R 0 1)
562      @result{} 6
563 (pq-get-length R 1 1)
564      @result{} 6
565 (pq-get-length R 1 2)
566      @result{} 12
567 @end example
568 @end defun
569
570 @defun pq-get-is-null result tup-num field-num
571 Return t if the specific value is the SQL @code{NULL}.
572 @var{result} is a PGresult object.
573 @var{tup-num} selects which tuple to fetch from.
574 @var{field-num} selects which field to fetch from.
575 @end defun
576
577 @defun pq-cmd-status result
578 Return a summary string from the query.
579 @var{result} is a PGresult object.
580 @example
581 @comment This example was written on day 3 of the 2000 Haru Basho.
582 (setq R (pq-exec P "INSERT INTO sxemacs_codenames
583                    VALUES (6, '22.1.5', 'Bugatto');"))
584      @result{} #<PGresult PGRES_COMMAND_OK[1] - INSERT 542086 1>
585 (pq-cmd-status R)
586      @result{} "INSERT 2069411 1"
587 (setq R (pq-exec P "UPDATE sxemacs_codenames SET codename='Bugatti'
588                     WHERE version='22.1.5';"))
589      @result{} #<PGresult PGRES_COMMAND_OK[1] - UPDATE 1>
590 (pq-cmd-status R)
591      @result{} "UPDATE 1"
592 @end example
593
594 Note that the first number returned from an insertion, like in the
595 example, is an object ID number and will almost certainly vary from
596 system to system since object ID numbers in Postgres must be unique
597 across all databases.
598 @end defun
599
600 @defun pq-cmd-tuples result
601 Return the number of tuples if the last command was an INSERT/UPDATE/DELETE.
602 If the last command was something else, the empty string is returned.
603 @var{result} is a PGresult object.
604
605 @example
606 (setq R (pq-exec P "INSERT INTO sxemacs_codenames VALUES
607                     (7, '22.1.6', 'Cadillac');"))
608      @result{} #<PGresult PGRES_COMMAND_OK[1] - INSERT 38688 1>
609 (pq-cmd-tuples R)
610      @result{} "1"
611 (setq R (pq-exec P "SELECT * from sxemacs_codenames;"))
612      @result{} #<PGresult PGRES_TUPLES_OK[7] - SELECT>
613 (pq-cmd-tuples R)
614      @result{} ""
615 (setq R (pq-exec P "DELETE FROM sxemacs_codenames
616                     WHERE codename LIKE '%urbo';"))
617      @result{} #<PGresult PGRES_COMMAND_OK[1] - DELETE 1>
618 (pq-cmd-tuples R)
619      @result{} "1"
620 @end example
621 @end defun
622
623 @defun pq-oid-value result
624 Return the object id of the insertion if the last command was an INSERT.
625 0 is returned if the last command was not an insertion.
626 @var{result} is a PGresult object.
627
628 In the first example, the numbers you will see on your local system will
629 almost certainly be different, however the second number from the right
630 in the unprintable PGresult object and the number returned by
631 @code{pq-oid-value} should match.
632 @example
633 (setq R (pq-exec P "INSERT INTO sxemacs_codenames VALUES
634                     (8, '22.1.7', 'Celica');"))
635      @result{} #<PGresult PGRES_COMMAND_OK[1] - INSERT 542089 1>
636 (pq-oid-value R)
637      @result{} 542089
638 (setq R (pq-exec P "SELECT version FROM sxemacs_codenames
639                     WHERE codename='Bugatti';"))
640      @result{} #<PGresult PGRES_TUPLES_OK[1] - SELECT>
641 (pq-oid-value R)
642      @result{} 0
643 @end example
644 @end defun
645
646 @defun pq-make-empty-pgresult conn status
647 Create an empty pgresult with the given status.
648 @var{conn} a database connection object
649 @var{status} a value that can be returned by @code{pq-result-status}.
650
651 The caller is responsible for making sure the return value gets properly
652 freed.
653 @end defun
654
655
656 @node Synchronous Interface Functions, Asynchronous Interface Functions, libpq Lisp Symbols and DataTypes, SXEmacs PostgreSQL libpq API
657 @comment  node-name,  next,  previous,  up
658 @subsection Synchronous Interface Functions
659
660 @defun pq-connectdb conninfo
661 Establish a (synchronous) database connection.
662 @var{conninfo} A string of blank separated options.  Options are of the
663 form ``@var{option} = @var{value}''.  If @var{value} contains blanks, it
664 must be single quoted.  Blanks around the equal sign are optional.
665 Multiple option assignments are blank separated.
666 @example
667 (pq-connectdb "dbname=kantdb port=5432")
668      @result{} #<PGconn kantdb:5432 freundt/freundt>
669 @end example
670 The printed representation of a database connection object has four
671 fields.  The first field is the hostname where the database server is
672 running (in this case localhost), the second field is the port number,
673 the third field is the database user name, and the fourth field is the
674 name of the database.
675
676 Database connection objects which have been disconnected and will
677 generate an immediate error if they are used look like:
678 @example
679   #<PGconn BAD>
680 @end example
681 Bad connections can be reestablished with @code{pq-reset}, or deleted
682 entirely with @code{pq-finish}.
683
684 A database connection object that has been deleted looks like:
685 @example
686 (let ((P1 (pq-connectdb "")))
687   (pq-finish P1)
688   P1)
689      @result{} #<PGconn DEAD>
690 @end example
691
692 Note that database connection objects are the most heavy weight objects
693 in SXEmacs Lisp at this writing, usually representing as much as several
694 megabytes of virtual memory on the machine the database server is
695 running on.  It is wisest to explicitly delete them when you are
696 finished with them, rather than letting garbage collection do it.  An
697 example idiom is:
698
699 @example
700 (let ((P (pq-connectiondb "")))
701   (unwind-protect
702       (progn
703         (...)) ; access database here
704     (pq-finish P)))
705 @end example
706
707 The following options are available in the options string:
708 @table @code
709 @item authtype
710 Authentication type.  Same as @code{PGAUTHTYPE}.  This is no longer used.
711 @item user
712 Database user name.  Same as @code{PGUSER}.
713 @item password
714 Database password.
715 @item dbname
716 Database name.  Same as @code{PGDATABASE}
717 @item host
718 Symbolic hostname.  Same as @code{PGHOST}.
719 @item hostaddr
720 Host address as four octets (eg. like 192.168.1.1).
721 @item port
722 TCP port to connect to.  Same as @code{PGPORT}.
723 @item tty
724 Debugging TTY.  Same as @code{PGTTY}.  This value is suppressed in the
725 SXEmacs Lisp API.
726 @item options
727 Extra backend database options.  Same as @code{PGOPTIONS}.
728 @end table
729 A database connection object is returned regardless of whether a
730 connection was established or not.
731 @end defun
732
733 @defun pq-reset conn
734 Reestablish database connection.
735 @var{conn} A database connection object.
736
737 This function reestablishes a database connection using the original
738 connection parameters.  This is useful if something has happened to the
739 TCP link and it has become broken.
740 @end defun
741
742 @defun pq-exec conn query
743 Make a synchronous database query.
744 @var{conn} A database connection object.
745 @var{query} A string containing an SQL query.
746 A PGresult object is returned, which in turn may be queried by its many
747 accessor functions to retrieve state out of it.  If the query string
748 contains multiple SQL commands, only results from the final command are
749 returned.
750
751 @example
752 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames;
753 DELETE FROM sxemacs_codenames WHERE id=8;"))
754      @result{} #<PGresult PGRES_COMMAND_OK[1] - DELETE 1>
755 @end example
756 @end defun
757
758 @defun pq-notifies conn
759 Return the latest async notification that has not yet been handled.
760 @var{conn} A database connection object.
761 If there has been a notification, then a list of two elements will be returned.
762 The first element contains the relation name being notified, the second
763 element contains the backend process ID number.  nil is returned if there
764 aren't any notifications to process.
765 @end defun
766
767 @defun PQsetenv conn
768 Synchronous transfer of environment variables to a backend
769 @var{conn} A database connection object.
770
771 Environment variable transfer is done as a normal part of database
772 connection.
773
774 Compatibility note: This function was present but not documented in versions
775 of libpq prior to 7.0.
776 @end defun
777
778
779 @node Asynchronous Interface Functions, Large Object Support, Synchronous Interface Functions, SXEmacs PostgreSQL libpq API
780 @comment  node-name,  next,  previous,  up
781 @subsection Asynchronous Interface Functions
782
783 Making command by command examples is too complex with the asynchronous
784 interface functions.  See the examples section for complete calling
785 sequences.
786
787 @defun pq-connect-start conninfo
788 Begin establishing an asynchronous database connection.
789 @var{conninfo} A string containing the connection options.  See the
790 documentation of @code{pq-connectdb} for a listing of all the available
791 flags.
792 @end defun
793
794 @defun pq-connect-poll conn
795 An intermediate function to be called during an asynchronous database
796 connection.
797 @var{conn} A database connection object.
798 The result codes are documented in a previous section.
799 @end defun
800
801 @defun pq-is-busy conn
802 Returns t if @code{pq-get-result} would block waiting for input.
803 @var{conn} A database connection object.
804 @end defun
805
806 @defun pq-consume-input conn
807 Consume any available input from the backend.
808 @var{conn} A database connection object.
809
810 Nil is returned if anything bad happens.
811 @end defun
812
813 @defun pq-reset-start conn
814 Reset connection to the backend asynchronously.
815 @var{conn} A database connection object.
816 @end defun
817
818 @defun pq-reset-poll conn
819 Poll an asynchronous reset for completion
820 @var{conn} A database connection object.
821 @end defun
822
823 @defun pq-reset-cancel conn
824 Attempt to request cancellation of the current operation.
825 @var{conn} A database connection object.
826
827 The return value is t if the cancel request was successfully
828 dispatched, nil if not (in which case conn->errorMessage is set).
829 Note: successful dispatch is no guarantee that there will be any effect at
830 the backend.  The application must read the operation result as usual.
831 @end defun
832
833 @defun pq-send-query conn query
834 Submit a query to Postgres and don't wait for the result.
835 @var{conn} A database connection object.
836 Returns: t if successfully submitted
837          nil if error (conn->errorMessage is set)
838 @end defun
839
840 @defun pq-get-result conn
841 Retrieve an asynchronous result from a query.
842 @var{conn} A database connection object.
843
844 @code{nil} is returned when no more query work remains.
845 @end defun
846
847 @defun pq-set-nonblocking conn arg
848 Sets the PGconn's database connection non-blocking if the arg is TRUE
849 or makes it non-blocking if the arg is FALSE, this will not protect
850 you from PQexec(), you'll only be safe when using the non-blocking API.
851 @var{conn} A database connection object.
852 @end defun
853
854 @defun pq-is-nonblocking conn
855 Return the blocking status of the database connection
856 @var{conn} A database connection object.
857 @end defun
858
859 @defun pq-flush conn
860 Force the write buffer to be written (or at least try)
861 @var{conn} A database connection object.
862 @end defun
863
864 @defun PQsetenvStart conn
865 Start asynchronously passing environment variables to a backend.
866 @var{conn} A database connection object.
867
868 Compatibility note: this function is only available with libpq-7.0.
869 @end defun
870
871 @defun PQsetenvPoll conn
872 Check an asynchronous environment variables transfer for completion.
873 @var{conn} A database connection object.
874
875 Compatibility note: this function is only available with libpq-7.0.
876 @end defun
877
878 @defun PQsetenvAbort conn
879 Attempt to terminate an asynchronous environment variables transfer.
880 @var{conn} A database connection object.
881
882 Compatibility note: this function is only available with libpq-7.0.
883 @end defun
884
885
886 @node Large Object Support, Other libpq Functions, Asynchronous Interface Functions, SXEmacs PostgreSQL libpq API
887 @comment  node-name,  next,  previous,  up
888 @subsection Large Object Support
889
890 @defun pq-lo-import conn filename
891 Import a file as a large object into the database.
892 @var{conn} a database connection object
893 @var{filename} filename to import
894
895 On success, the object id is returned.
896 @end defun
897
898 @defun pq-lo-export conn oid filename
899 Copy a large object in the database into a file.
900 @var{conn} a database connection object.
901 @var{oid} object id number of a large object.
902 @var{filename} filename to export to.
903 @end defun
904
905
906 @node Other libpq Functions, Unimplemented libpq Functions, Large Object Support, SXEmacs PostgreSQL libpq API
907 @comment  node-name,  next,  previous,  up
908 @subsection Other libpq Functions
909
910 @defun pq-finish conn
911 Destroy a database connection object by calling free on it.
912 @var{conn} a database connection object
913
914 It is possible to not call this routine because the usual SXEmacs garbage
915 collection mechanism will call the underlying libpq routine whenever it
916 is releasing stale @code{PGconn} objects.  However, this routine is
917 useful in @code{unwind-protect} clauses to make connections go away
918 quickly when unrecoverable errors have occurred.
919
920 After calling this routine, the printed representation of the SXEmacs
921 wrapper object will contain the string ``DEAD''.
922 @end defun
923
924 @defun pq-client-encoding conn
925 Return the client encoding as an integer code.
926 @var{conn} a database connection object
927
928 @example
929 (pq-client-encoding P)
930      @result{} 1
931 @end example
932
933 Compatibility note: This function did not exist prior to libpq-7.0 and
934 does not exist in a non-Mule SXEmacs.
935 @end defun
936
937 @defun pq-set-client-encoding conn encoding
938 Set client coding system.
939 @var{conn} a database connection object
940 @var{encoding} a string representing the desired coding system
941
942 @example
943 (pq-set-client-encoding P "EUC_JP")
944      @result{} 0
945 @end example
946
947 The current idiom for ensuring proper coding system conversion is the
948 following (illustrated for EUC Japanese encoding):
949 @example
950 (setq P (pq-connectdb "..."))
951 (let ((file-name-coding-system 'euc-jp)
952       (pg-coding-system 'euc-jp))
953   (pq-set-client-encoding "EUC_JP")
954   ...)
955 (pq-finish P)
956 @end example
957 Compatibility note: This function did not exist prior to libpq-7.0 and
958 does not exist in a non-Mule SXEmacs.
959 @end defun
960
961 @defun pq-env-2-encoding
962 Return the integer code representing the coding system in
963 @code{PGCLIENTENCODING}.
964
965 @example
966 (pq-env-2-encoding)
967      @result{} 0
968 @end example
969 Compatibility note: This function did not exist prior to libpq-7.0 and
970 does not exist in a non-Mule SXEmacs.
971 @end defun
972
973 @defun pq-clear res
974 Destroy a query result object by calling free() on it.
975 @var{res} a query result object
976
977 Note:  The memory allocation systems of libpq and SXEmacs are different.
978 The SXEmacs representation of a query result object will have both the
979 SXEmacs version and the libpq version freed at the next garbage collection
980 when the object is no longer being referenced.  Calling this function does
981 not release the SXEmacs object, it is still subject to the usual rules for
982 Lisp objects.  The printed representation of the SXEmacs object will contain
983 the string ``DEAD'' after this routine is called indicating that it is no
984 longer useful for anything.
985 @end defun
986
987 @defun pq-conn-defaults
988 Return a data structure that represents the connection defaults.
989 The data is returned as a list of lists, where each sublist contains
990 info regarding a single option.
991 @end defun
992
993
994 @node Unimplemented libpq Functions, , Other libpq Functions, SXEmacs PostgreSQL libpq API
995 @comment  node-name,  next,  previous,  up
996 @subsection Unimplemented libpq Functions
997
998 @deftypefn {Unimplemented Function} PGconn *PQsetdbLogin (char *pghost, char *pgport, char *pgoptions, char *pgtty, char *dbName, char *login, char *pwd)
999 Synchronous database connection.
1000 @var{pghost} is the hostname of the PostgreSQL backend to connect to.
1001 @var{pgport} is the TCP port number to use.
1002 @var{pgoptions} specifies other backend options.
1003 @var{pgtty} specifies the debugging tty to use.
1004 @var{dbName} specifies the database name to use.
1005 @var{login} specifies the database user name.
1006 @var{pwd} specifies the database user's password.
1007
1008 This routine is deprecated as of libpq-7.0, and its functionality can be
1009 replaced by external Lisp code if needed.
1010 @end deftypefn
1011
1012 @deftypefn {Unimplemented Function} PGconn *PQsetdb (char *pghost, char *pgport, char *pgoptions, char *pgtty, char *dbName)
1013 Synchronous database connection.
1014 @var{pghost} is the hostname of the PostgreSQL backend to connect to.
1015 @var{pgport} is the TCP port number to use.
1016 @var{pgoptions} specifies other backend options.
1017 @var{pgtty} specifies the debugging tty to use.
1018 @var{dbName} specifies the database name to use.
1019
1020 This routine was deprecated in libpq-6.5.
1021 @end deftypefn
1022
1023 @deftypefn {Unimplemented Function} int PQsocket (PGconn *conn)
1024 Return socket file descriptor to a backend database process.
1025 @var{conn} database connection object.
1026 @end deftypefn
1027
1028 @deftypefn {Unimplemented Function} void PQprint (FILE *fout, PGresult *res, PGprintOpt *ps)
1029 Print out the results of a query to a designated C stream.
1030 @var{fout} C stream to print to
1031 @var{res} the query result object to print
1032 @var{ps} the print options structure.
1033
1034 This routine is deprecated as of libpq-7.0 and cannot be sensibly exported
1035 to SXEmacs Lisp.
1036 @end deftypefn
1037
1038 @deftypefn {Unimplemented Function} void PQdisplayTuples (PGresult *res, FILE *fp, int fillAlign, char *fieldSep, int printHeader, int quiet)
1039 @var{res} query result object to print
1040 @var{fp} C stream to print to
1041 @var{fillAlign} pad the fields with spaces
1042 @var{fieldSep} field separator
1043 @var{printHeader} display headers?
1044 @var{quiet}
1045
1046 This routine was deprecated in libpq-6.5.
1047 @end deftypefn
1048
1049 @deftypefn {Unimplemented Function} void PQprintTuples (PGresult *res, FILE *fout, int printAttName, int terseOutput, int width)
1050 @var{res} query result object to print
1051 @var{fout} C stream to print to
1052 @var{printAttName} print attribute names
1053 @var{terseOutput} delimiter bars
1054 @var{width} width of column, if 0, use variable width
1055
1056 This routine was deprecated in libpq-6.5.
1057 @end deftypefn
1058
1059 @deftypefn {Unimplemented Function} int PQmblen (char *s, int encoding)
1060 Determine length of a multibyte encoded char at @code{*s}.
1061 @var{s} encoded string
1062 @var{encoding} type of encoding
1063
1064 Compatibility note:  This function was introduced in libpq-7.0.
1065 @end deftypefn
1066
1067 @deftypefn {Unimplemented Function} void PQtrace (PGconn *conn, FILE *debug_port)
1068 Enable tracing on @code{debug_port}.
1069 @var{conn} database connection object.
1070 @var{debug_port} C output stream to use.
1071 @end deftypefn
1072
1073 @deftypefn {Unimplemented Function} void PQuntrace (PGconn *conn)
1074 Disable tracing.
1075 @var{conn} database connection object.
1076 @end deftypefn
1077
1078 @deftypefn {Unimplemented Function} char *PQoidStatus (PGconn *conn)
1079 Return the object id as a string of the last tuple inserted.
1080 @var{conn} database connection object.
1081
1082 Compatibility note: This function is deprecated since libpq-7.0, however it
1083 is used internally by the SXEmacs binding code when linked against versions
1084 prior to 7.0.
1085 @end deftypefn
1086
1087 @deftypefn {Unimplemented Function} PGresult *PQfn (PGconn *conn, int fnid, int *result_buf, int *result_len, int result_is_int, PQArgBlock *args, int nargs)
1088 ``Fast path'' interface --- not really recommended for application use
1089 @var{conn} A database connection object.
1090 @var{fnid}
1091 @var{result_buf}
1092 @var{result_len}
1093 @var{result_is_int}
1094 @var{args}
1095 @var{nargs}
1096 @end deftypefn
1097
1098 The following set of very low level large object functions aren't
1099 appropriate to be exported to Lisp.
1100
1101 @deftypefn {Unimplemented Function} int pq-lo-open (PGconn *conn, int lobjid, int mode)
1102 @var{conn} a database connection object.
1103 @var{lobjid} a large object ID.
1104 @var{mode} opening modes.
1105 @end deftypefn
1106
1107 @deftypefn {Unimplemented Function} int pq-lo-close (PGconn *conn, int fd)
1108 @var{conn} a database connection object.
1109 @var{fd} a large object file descriptor
1110 @end deftypefn
1111
1112 @deftypefn {Unimplemented Function} int pq-lo-read (PGconn *conn, int fd, char *buf, int len)
1113 @var{conn} a database connection object.
1114 @var{fd} a large object file descriptor.
1115 @var{buf} buffer to read into.
1116 @var{len} size of buffer.
1117 @end deftypefn
1118
1119 @deftypefn {Unimplemented Function} int pq-lo-write (PGconn *conn, int fd, char *buf, size_t len)
1120 @var{conn} a database connection object.
1121 @var{fd} a large object file descriptor.
1122 @var{buf} buffer to write from.
1123 @var{len} size of buffer.
1124 @end deftypefn
1125
1126 @deftypefn {Unimplemented Function} int pq-lo-lseek (PGconn *conn, int fd, int offset, int whence)
1127 @var{conn} a database connection object.
1128 @var{fd} a large object file descriptor.
1129 @var{offset}
1130 @var{whence}
1131 @end deftypefn
1132
1133 @deftypefn {Unimplemented Function} int pq-lo-creat (PGconn *conn, int mode)
1134 @var{conn} a database connection object.
1135 @var{mode} opening modes.
1136 @end deftypefn
1137
1138 @deftypefn {Unimplemented Function} int pq-lo-tell (PGconn *conn, int fd)
1139 @var{conn} a database connection object.
1140 @var{fd} a large object file descriptor.
1141 @end deftypefn
1142
1143 @deftypefn {Unimplemented Function} int pq-lo-unlink (PGconn *conn, int lobjid)
1144 @var{conn} a database connection object.
1145 @var{lbojid} a large object ID.
1146 @end deftypefn
1147
1148
1149 @node SXEmacs PostgreSQL libpq Examples,  , SXEmacs PostgreSQL libpq API, PostgreSQL Support
1150 @comment  node-name,  next,  previous,  up
1151 @section SXEmacs PostgreSQL libpq Examples
1152
1153 This is an example of one method of establishing an asynchronous
1154 connection.
1155
1156 @example
1157 (defun database-poller (P)
1158   (message "%S before poll" (pq-pgconn P 'pq::status))
1159   (pq-connect-poll P)
1160   (message "%S after poll" (pq-pgconn P 'pq::status))
1161   (if (eq (pq-pgconn P 'pq::status) 'pg::connection-ok)
1162       (message "Done!")
1163     (add-timeout .1 'database-poller P)))
1164      @result{} database-poller
1165 (progn
1166   (setq P (pq-connect-start ""))
1167   (add-timeout .1 'database-poller P))
1168      @result{} pg::connection-started before poll
1169      @result{} pg::connection-made after poll
1170      @result{} pg::connection-made before poll
1171      @result{} pg::connection-awaiting-response after poll
1172      @result{} pg::connection-awaiting-response before poll
1173      @result{} pg::connection-auth-ok after poll
1174      @result{} pg::connection-auth-ok before poll
1175      @result{} pg::connection-setenv after poll
1176      @result{} pg::connection-setenv before poll
1177      @result{} pg::connection-ok after poll
1178      @result{} Done!
1179 P
1180      @result{} #<PGconn kantdb:5432 freundt/freundt>
1181 @end example
1182
1183 Here is an example of one method of doing an asynchronous reset.
1184
1185 @example
1186 (defun database-poller (P)
1187   (let (PS)
1188     (message "%S before poll" (pq-pgconn P 'pq::status))
1189     (setq PS (pq-reset-poll P))
1190     (message "%S after poll [%S]" (pq-pgconn P 'pq::status) PS)
1191     (if (eq (pq-pgconn P 'pq::status) 'pg::connection-ok)
1192         (message "Done!")
1193       (add-timeout .1 'database-poller P))))
1194      @result{} database-poller
1195 (progn
1196   (pq-reset-start P)
1197   (add-timeout .1 'database-poller P))
1198      @result{} pg::connection-started before poll
1199      @result{} pg::connection-made after poll [pgres::polling-writing]
1200      @result{} pg::connection-made before poll
1201      @result{} pg::connection-awaiting-response after poll [pgres::polling-reading]
1202      @result{} pg::connection-awaiting-response before poll
1203      @result{} pg::connection-setenv after poll [pgres::polling-reading]
1204      @result{} pg::connection-setenv before poll
1205      @result{} pg::connection-ok after poll [pgres::polling-ok]
1206      @result{} Done!
1207 P
1208      @result{} #<PGconn kantdb:5432 freundt/freundt>
1209 @end example
1210
1211 And finally, an asynchronous query.
1212
1213 @example
1214 (defun database-poller (P)
1215   (let (R)
1216     (pq-consume-input P)
1217     (if (pq-is-busy P)
1218         (add-timeout .1 'database-poller P)
1219       (setq R (pq-get-result P))
1220       (if R
1221           (progn
1222             (push R result-list)
1223             (add-timeout .1 'database-poller P))))))
1224      @result{} database-poller
1225 (when (pq-send-query P "SELECT * FROM sxemacs_codenames;")
1226   (setq result-list nil)
1227   (add-timeout .1 'database-poller P))
1228      @result{} 1910971
1229 ;; wait a moment
1230 result-list
1231      @result{} (#<PGresult PGRES_TUPLES_OK[7] - SELECT>)
1232 @end example
1233
1234 Here is an example showing how multiple SQL statements in a single query
1235 can have all their results collected.
1236 @example
1237 ;; Using the same @code{database-poller} function from the previous example
1238 (when (pq-send-query P "SELECT * FROM sxemacs_codenames;
1239 SELECT * FROM pg_database;
1240 SELECT * FROM pg_user;")
1241   (setq result-list nil)
1242   (add-timeout .1 'database-poller P))
1243      @result{} 1911150
1244 ;; wait a moment
1245 result-list
1246      @result{} (#<PGresult PGRES_TUPLES_OK[8] - SELECT> #<PGresult PGRES_TUPLES_OK[10] - SELECT> #<PGresult PGRES_TUPLES_OK[7] - SELECT>)
1247 @end example
1248
1249 Here is an example which illustrates collecting all data from a query,
1250 including the field names.
1251
1252 @example
1253 (defun pg-util-query-results (results)
1254   "Retrieve results of last SQL query into a list structure."
1255   (let ((i (1- (pq-ntuples R)))
1256         j l1 l2)
1257     (while (>= i 0)
1258       (setq j (1- (pq-nfields R)))
1259       (setq l2 nil)
1260       (while (>= j 0)
1261         (push (pq-get-value R i j) l2)
1262         (decf j))
1263       (push l2 l1)
1264       (decf i))
1265     (setq j (1- (pq-nfields R)))
1266     (setq l2 nil)
1267     (while (>= j 0)
1268       (push (pq-fname R j) l2)
1269       (decf j))
1270     (push l2 l1)
1271     l1))
1272      @result{} pg-util-query-results
1273 (setq R (pq-exec P "SELECT * FROM sxemacs_codenames ORDER BY codename DESC;"))
1274   @result{} #<PGresult PGRES_TUPLES_OK[7] - SELECT>
1275 (pg-util-query-results R)
1276   @result{} (("id" "version" "codename") ("8" "22.1.7" "Celica") ("7" "22.1.6" "Cadillac") ("6" "22.1.5" "Bugatti") ("4" "22.1.3" "BMW") ("3" "22.1.2" "Audi") ("2" "22.1.1" "Aston Martin") ("1" "22.1.0" "Alfa Romeo"))
1277 @end example
1278
1279 Here is an example of a query that uses a database cursor.
1280
1281 @example
1282 (let (data R)
1283   (setq R (pq-exec P "BEGIN;"))
1284   (setq R (pq-exec P "DECLARE k_cursor CURSOR FOR SELECT * FROM sxemacs_codenames ORDER BY version DESC;"))
1285
1286   (setq R (pq-exec P "FETCH k_cursor;"))
1287   (while (eq (pq-ntuples R) 1)
1288     (push (list (pq-get-value R 0 0) (pq-get-value R 0 1)) data)
1289     (setq R (pq-exec P "FETCH k_cursor;")))
1290   (setq R (pq-exec P "END;"))
1291   data)
1292   @result{} (("1" "22.1.0") ("2" "22.1.1") ("3" "22.1.2") ("4" "22.1.3") ("6" "22.1.5") ("7" "22.1.6") ("8" "22.1.7"))
1293 @end example
1294
1295 Here's another example of cursors, this time with a Lisp macro to
1296 implement a mapping function over a table.
1297
1298 @example
1299 (defmacro map-db (P table condition callout)
1300   `(let (R)
1301      (pq-exec ,P "BEGIN;")
1302      (pq-exec ,P (concat "DECLARE k_cursor CURSOR FOR SELECT * FROM "
1303                          ,table
1304                          " "
1305                          ,condition
1306                          " ORDER BY codename DESC;"))
1307      (setq R (pq-exec P "FETCH k_cursor;"))
1308      (while (eq (pq-ntuples R) 1)
1309        (,callout (pq-get-value R 0 0) (pq-get-value R 0 1))
1310        (setq R (pq-exec P "FETCH k_cursor;")))
1311      (pq-exec P "END;")))
1312      @result{} map-db
1313 (defun callback (arg1 arg2)
1314   (message "arg1 = %s, arg2 = %s" arg1 arg2))
1315      @result{} callback
1316 (map-db P "sxemacs_codenames" "WHERE version > '22.1.0'" callback)
1317   @result{} arg1 = 8, arg2 = 22.1.7
1318   @result{} arg1 = 7, arg2 = 22.1.6
1319   @result{} arg1 = 6, arg2 = 22.1.5
1320   @result{} arg1 = 4, arg2 = 22.1.3
1321   @result{} arg1 = 3, arg2 = 22.1.2
1322   @result{} arg1 = 2, arg2 = 22.1.1
1323   @result{} #<PGresult PGRES_COMMAND_OK - COMMIT>
1324 @end example