2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 2000 Electrotechnical Laboratory, JAPAN
4 @c Licensed to the Free Software Foundation
5 @c See the file lispref.texi for copying conditions.
6 @c Thank you Oscar Figueiredo! This file was shamelessly cloned from
8 @setfilename ../../info/postgresql.info
9 @node PostgreSQL Support, Internationalization, LDAP Support, top
10 @chapter PostgreSQL Support
13 XEmacs can be linked with PostgreSQL libpq run-time support to provide
14 relational database access from Emacs Lisp code.
17 * Building XEmacs with PostgreSQL support::
18 * XEmacs PostgreSQL libpq API::
19 * XEmacs PostgreSQL libpq Examples::
22 @node Building XEmacs with PostgreSQL support, XEmacs PostgreSQL libpq API, ,PostgreSQL Support
23 @comment node-name, next, previous, up
24 @section Building XEmacs with PostgreSQL support
26 XEmacs PostgreSQL support requires linking to the PostgreSQL libpq
27 library. Describing how to build and install PostgreSQL is beyond the
28 scope of this document. See the PostgreSQL manual for details.
30 If you have installed XEmacs from one of the binary kits on
31 (@url{ftp://ftp.xemacs.org/}), or are using an XEmacs binary from a CD
32 ROM, you may have XEmacs PostgreSQL support by default. @code{M-x
33 describe-installation} will tell you if you do.
35 If you are building XEmacs from source, you need to install PostgreSQL
36 first. On some systems, PostgreSQL will come pre-installed in /usr. In
37 this case, it should be autodetected when you run configure. If
38 PostgreSQL is installed into its default location,
39 @file{/usr/local/pgsql}, you must specify
40 @code{--site-prefixes=/usr/local/pgsql} when you run configure. If
41 PostgreSQL is installed into another location, use that instead of
42 @file{/usr/local/pgsql} when specifying @code{--site-prefixes}.
44 As of XEmacs 21.2, PostgreSQL versions 6.5.3 and 7.0 are supported.
45 XEmacs Lisp support for V7.0 is somewhat more extensive than support for
46 V6.5. In particular, asynchronous queries are supported.
48 @node XEmacs PostgreSQL libpq API, XEmacs PostgreSQL libpq Examples, Building XEmacs with PostgreSQL support, PostgreSQL Support
49 @comment node-name, next, previous, up
50 @section XEmacs PostgreSQL libpq API
52 The XEmacs PostgreSQL API is intended to be a policy-free, low-level
53 binding to libpq. The intent is to provide all the basic functionality
54 and then let high level Lisp code decide its own policies.
56 This documentation assumes that the reader has knowledge of SQL, but
57 requires no prior knowledge of libpq.
59 There are many examples in this manual and some setup will be required.
60 In order to run most of the following examples, the following code needs
61 to be executed. In addition to the data is in this table, nearly all of
62 the examples will assume that the free variable @code{P} refers to this
63 database connection. The examples in the original edition of this
64 manual were run against Postgres 7.0beta1.
68 (setq P (pq-connectdb ""))
69 ;; id is the primary key, shikona is a Japanese word that
70 ;; means `the professional name of a Sumo wrestler', and
71 ;; rank is the Sumo rank name.
72 (pq-exec P (concat "CREATE TABLE xemacs_test"
73 " (id int, shikona text, rank text);"))
74 (pq-exec P "COPY xemacs_test FROM stdin;")
75 (pq-put-line P "1\tMusashimaru\tYokuzuna\n")
76 (pq-put-line P "2\tDejima\tOozeki\n")
77 (pq-put-line P "3\tMusoyama\tSekiwake\n")
78 (pq-put-line P "4\tMiyabiyama\tSekiwake\n")
79 (pq-put-line P "5\tWakanoyama\tMaegashira\n")
80 (pq-put-line P "\\.\n")
86 * libpq Lisp Variables::
87 * libpq Lisp Symbols and DataTypes::
88 * Synchronous Interface Functions::
89 * Asynchronous Interface Functions::
90 * Large Object Support::
91 * Other libpq Functions::
92 * Unimplemented libpq Functions::
95 @node libpq Lisp Variables, libpq Lisp Symbols and DataTypes, XEmacs PostgreSQL libpq API, XEmacs PostgreSQL libpq API
96 @comment node-name, next, previous, up
97 @subsection libpq Lisp Variables
99 Various Unix environment variables are used by libpq to provide defaults
100 to the many different parameters. In the XEmacs Lisp API, these
101 environment variables are bound to Lisp variables to provide more
102 convenient access to Lisp Code. These variables are passed to the
103 backend database server during the establishment of a database
104 connection and when the @code{pq-setenv} call is made.
107 Initialized from the @code{PGHOST} environment variable. The default
112 Initialized from the @code{PGUSER} environment variable. The default
117 Initialized from the @code{PGOPTIONS} environment variable. Default
118 additional server options.
122 Initialized from the @code{PGPORT} environment variable. The default
123 TCP port to connect to.
127 Initialized from the @code{PGTTY} environment variable. The default
130 Compatibility note: Debugging TTYs are turned off in the XEmacs Lisp
135 Initialized from the @code{PGDATABASE} environment variable. The
136 default database to connect to.
140 Initialized from the @code{PGREALM} environment variable. The default
144 @defvar pg:client-encoding
145 Initialized from the @code{PGCLIENTENCODING} environment variable. The
146 default client encoding.
148 Compatibility note: This variable is not present in non-Mule XEmacsen.
149 This variable is not present in versions of libpq prior to 7.0.
150 In the current implementation, client encoding is equivalent to the
151 @code{file-name-coding-system} format.
156 Initialized from the @code{PGAUTHTYPE} environment variable. The
157 default authentication scheme used.
159 Compatibility note: This variable is unused in versions of libpq after
160 6.5. It is not implemented at all in the XEmacs Lisp binding.
164 Initialized from the @code{PGGEQO} environment variable. Genetic
168 @defvar pg:cost-index
169 Initialized from the @code{PGCOSTINDEX} environment variable. Cost
174 Initialized from the @code{PGCOSTHEAP} environment variable. Cost heap
179 Initialized from the @code{PGTZ} environment variable. Default
183 @defvar pg:date-style
184 Initialized from the @code{PGDATESTYLE} environment variable. Default
185 date style in returned date objects.
188 @defvar pg-coding-system
189 This is a variable controlling which coding system is used to encode
190 non-ASCII strings sent to the database.
192 Compatibility Note: This variable is not present in InfoDock.
195 @node libpq Lisp Symbols and DataTypes, Synchronous Interface Functions, libpq Lisp Variables, XEmacs PostgreSQL libpq API
196 @comment node-name, next, previous, up
197 @subsection libpq Lisp Symbols and Datatypes
199 The following set of symbols are used to represent the intermediate
200 states involved in the asynchronous interface.
202 @defvr {Symbol} pgres::polling-failed
203 Undocumented. A fatal error has occurred during processing of an
204 asynchronous operation.
207 @defvr {Symbol} pgres::polling-reading
208 An intermediate status return during an asynchronous operation. It
209 indicates that one may use @code{select} before polling again.
212 @defvr {Symbol} pgres::polling-writing
213 An intermediate status return during an asynchronous operation. It
214 indicates that one may use @code{select} before polling again.
217 @defvr {Symbol} pgres::polling-ok
218 An asynchronous operation has successfully completed.
221 @defvr {Symbol} pgres::polling-active
222 An intermediate status return during an asynchronous operation. One can
223 call the poll function again immediately.
226 @defun pq-pgconn conn field
227 @var{conn} A database connection object.
228 @var{field} A symbol indicating which field of PGconn to fetch. Possible
229 values are shown in the following table.
236 Database user's password
238 Hostname database server is running on
240 TCP port number used in the connection
244 Compatibility note: Debugging TTYs are not used in the XEmacs Lisp API.
246 Additional server options
248 Connection status. Possible return values are shown in the following
251 @item pg::connection-ok
252 The normal, connected status.
253 @item pg::connection-bad
254 The connection is not open and the PGconn object needs to be deleted by
256 @item pg::connection-started
257 An asynchronous connection has been started, but is not yet complete.
258 @item pg::connection-made
259 An asynchronous connect has been made, and there is data waiting to be sent.
260 @item pg::connection-awaiting-response
261 Awaiting data from the backend during an asynchronous connection.
262 @item pg::connection-auth-ok
263 Received authentication, waiting for the backend to start up.
264 @item pg::connection-setenv
265 Negotiating environment during an asynchronous connection.
267 @item pq::error-message
268 The last error message that was delivered to this connection.
269 @item pq::backend-pid
270 The process ID of the backend database server.
274 The @code{PGresult} object is used by libpq to encapsulate the results
275 of queries. The printed representation takes on four forms. When the
276 PGresult object contains tuples from an SQL @code{SELECT} it will look
280 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
281 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
284 The number in brackets indicates how many rows of data are available.
285 When the PGresult object is the result of a command query that doesn't
286 return anything, it will look like:
289 (pq-exec P "CREATE TABLE a_new_table (i int);")
290 @result{} #<PGresult PGRES_COMMAND_OK - CREATE>
293 When either the query is a command-type query that can affect a number
294 of different rows, but doesn't return any of them it will look like:
298 (pq-exec P "INSERT INTO a_new_table VALUES (1);")
299 (pq-exec P "INSERT INTO a_new_table VALUES (2);")
300 (pq-exec P "INSERT INTO a_new_table VALUES (3);")
301 (setq R (pq-exec P "DELETE FROM a_new_table;")))
302 @result{} #<PGresult PGRES_COMMAND_OK[3] - DELETE 3>
305 Lastly, when the underlying PGresult object has been deallocated
306 directly by @code{pq-clear} the printed representation will look like:
310 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
313 @result{} #<PGresult DEAD>
316 The following set of functions are accessors to various data in the PGresult
319 @defun pq-result-status result
320 Return status of a query result.
321 @var{result} is a PGresult object. The return value is one of the
322 symbols in the following table.
324 @item pgres::empty-query
325 A query contained no text. This is usually the result of a recoverable
326 error, or a minor programming error.
327 @item pgres::command-ok
328 A query command that doesn't return anything was executed properly by
330 @item pgres::tuples-ok
331 A query command that returns tuples was executed properly by the
333 @item pgres::copy-out
334 Copy Out data transfer is in progress.
336 Copy In data transfer is in progress.
337 @item pgres::bad-response
338 An unexpected response was received from the backend.
339 @item pgres::nonfatal-error
340 Undocumented. This value is returned when the libpq function
341 @code{PQresultStatus} is called with a @code{NULL} pointer.
342 @item pgres::fatal-error
343 Undocumented. An error has occurred in processing the query and the
344 operation was not completed.
348 @defun pq-res-status result
349 Return the query result status as a string, not a symbol.
350 @var{result} is a PGresult object.
353 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
354 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
356 @result{} "PGRES_TUPLES_OK"
360 @defun pq-result-error-message result
361 Return an error message generated by the query, if any.
362 @var{result} is a PGresult object.
365 (setq R (pq-exec P "SELECT * FROM xemacs-test;"))
366 @result{} <A fatal error is signaled in the echo area>
367 (pq-result-error-message R)
368 @result{} "ERROR: parser: parse error at or near \"-\"
373 @defun pq-ntuples result
374 Return the number of tuples in the query result.
375 @var{result} is a PGresult object.
378 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
379 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
385 @defun pq-nfields result
386 Return the number of fields in each tuple of the query result.
387 @var{result} is a PGresult object.
390 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
391 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
397 @defun pq-binary-tuples result
398 Returns t if binary tuples are present in the results, nil otherwise.
399 @var{result} is a PGresult object.
402 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
403 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
409 @defun pq-fname result field-index
410 Returns the name of a specific field.
411 @var{result} is a PGresult object.
412 @var{field-index} is the number of the column to select from. The first
413 column is number zero.
417 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
418 (setq i (pq-nfields R))
419 (while (>= (decf i) 0)
420 (push (pq-fname R i) l))
422 @result{} ("id" "shikona" "rank")
426 @defun pq-fnumber result field-name
427 Return the field number corresponding to the given field name.
428 -1 is returned on a bad field name.
429 @var{result} is a PGresult object.
430 @var{field-name} is a string representing the field name to find.
432 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
433 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
436 (pq-fnumber R "Not a field")
441 @defun pq-ftype result field-num
442 Return an integer code representing the data type of the specified column.
443 @var{result} is a PGresult object.
444 @var{field-num} is the field number.
446 The return value of this function is the Object ID (Oid) in the database
447 of the type. Further queries need to be made to various system tables
448 in order to convert this value into something useful.
451 @defun pq-fmod result field-num
452 Return the type modifier code associated with a field. Field numbers
454 @var{result} is a PGresult object.
455 @var{field-index} selects which field to use.
458 @defun pq-fsize result field-index
459 Return size of the given field.
460 @var{result} is a PGresult object.
461 @var{field-index} selects which field to use.
465 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
466 (setq i (pq-nfields R))
467 (while (>= (decf i) 0)
468 (push (list (pq-ftype R i) (pq-fsize R i)) l))
470 @result{} ((23 23) (25 25) (25 25))
474 @defun pq-get-value result tup-num field-num
475 Retrieve a return value.
476 @var{result} is a PGresult object.
477 @var{tup-num} selects which tuple to fetch from.
478 @var{field-num} selects which field to fetch from.
480 Both tuples and fields are numbered from zero.
483 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
484 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
486 @result{} "Musashimaru"
494 @defun pq-get-length result tup-num field-num
495 Return the length of a specific value.
496 @var{result} is a PGresult object.
497 @var{tup-num} selects which tuple to fetch from.
498 @var{field-num} selects which field to fetch from.
501 (setq R (pq-exec P "SELECT * FROM xemacs_test;"))
502 @result{} #<PGresult PGRES_TUPLES_OK[5] - SELECT>
503 (pq-get-length R 0 1)
505 (pq-get-length R 1 1)
507 (pq-get-length R 2 1)
512 @defun pq-get-is-null result tup-num field-num
513 Return t if the specific value is the SQL @code{NULL}.
514 @var{result} is a PGresult object.
515 @var{tup-num} selects which tuple to fetch from.
516 @var{field-num} selects which field to fetch from.
519 @defun pq-cmd-status result
520 Return a summary string from the query.
521 @var{result} is a PGresult object.
523 @comment This example was written on day 3 of the 2000 Haru Basho.
524 (setq R (pq-exec P "INSERT INTO xemacs_test
525 VALUES (6, 'Wakanohana', 'Yokozuna');"))
526 @result{} #<PGresult PGRES_COMMAND_OK[1] - INSERT 542086 1>
528 @result{} "INSERT 542086 1"
529 (setq R (pq-exec P "UPDATE xemacs_test SET rank='retired'
530 WHERE shikona='Wakanohana';"))
531 @result{} #<PGresult PGRES_COMMAND_OK[1] - UPDATE 1>
536 Note that the first number returned from an insertion, like in the
537 example, is an object ID number and will almost certainly vary from
538 system to system since object ID numbers in Postgres must be unique
539 across all databases.
542 @defun pq-cmd-tuples result
543 Return the number of tuples if the last command was an INSERT/UPDATE/DELETE.
544 If the last command was something else, the empty string is returned.
545 @var{result} is a PGresult object.
548 (setq R (pq-exec P "INSERT INTO xemacs_test VALUES
549 (7, 'Takanohana', 'Yokuzuna');"))
550 @result{} #<PGresult PGRES_COMMAND_OK[1] - INSERT 38688 1>
553 (setq R (pq-exec P "SELECT * from xemacs_test;"))
554 @result{} #<PGresult PGRES_TUPLES_OK[7] - SELECT>
557 (setq R (pq-exec P "DELETE FROM xemacs_test
558 WHERE shikona LIKE '%hana';"))
559 @result{} #<PGresult PGRES_COMMAND_OK[2] - DELETE 2>
565 @defun pq-oid-value result
566 Return the object id of the insertion if the last command was an INSERT.
567 0 is returned if the last command was not an insertion.
568 @var{result} is a PGresult object.
570 In the first example, the numbers you will see on your local system will
571 almost certainly be different, however the second number from the right
572 in the unprintable PGresult object and the number returned by
573 @code{pq-oid-value} should match.
575 (setq R (pq-exec P "INSERT INTO xemacs_test VALUES
576 (8, 'Terao', 'Maegashira');"))
577 @result{} #<PGresult PGRES_COMMAND_OK[1] - INSERT 542089 1>
580 (setq R (pq-exec P "SELECT shikona FROM xemacs_test
581 WHERE rank='Maegashira';"))
582 @result{} #<PGresult PGRES_TUPLES_OK[2] - SELECT>
588 @defun pq-make-empty-pgresult conn status
589 Create an empty pgresult with the given status.
590 @var{conn} a database connection object
591 @var{status} a value that can be returned by @code{pq-result-status}.
593 The caller is responsible for making sure the return value gets properly
597 @node Synchronous Interface Functions, Asynchronous Interface Functions, libpq Lisp Symbols and DataTypes, XEmacs PostgreSQL libpq API
598 @comment node-name, next, previous, up
599 @subsection Synchronous Interface Functions
601 @defun pq-connectdb conninfo
602 Establish a (synchronous) database connection.
603 @var{conninfo} A string of blank separated options. Options are of the
604 form ``@var{option} = @var{value}''. If @var{value} contains blanks, it
605 must be single quoted. Blanks around the equal sign are optional.
606 Multiple option assignments are blank separated.
608 (pq-connectdb "dbname=japanese port = 25432")
609 @result{} #<PGconn localhost:25432 steve/japanese>
611 The printed representation of a database connection object has four
612 fields. The first field is the hostname where the database server is
613 running (in this case localhost), the second field is the port number,
614 the third field is the database user name, and the fourth field is the
615 name of the database.
617 Database connection objects which have been disconnected and will
618 generate an immediate error if they are used look like:
622 Bad connections can be reestablished with @code{pq-reset}, or deleted
623 entirely with @code{pq-finish}.
625 A database connection object that has been deleted looks like:
627 (let ((P1 (pq-connectdb "")))
630 @result{} #<PGconn DEAD>
633 Note that database connection objects are the most heavy weight objects
634 in XEmacs Lisp at this writing, usually representing as much as several
635 megabytes of virtual memory on the machine the database server is
636 running on. It is wisest to explicitly delete them when you are
637 finished with them, rather than letting garbage collection do it. An
641 (let ((P (pq-connectiondb "")))
644 (...)) ; access database here
648 The following options are available in the options string:
651 Authentication type. Same as @code{PGAUTHTYPE}. This is no longer used.
653 Database user name. Same as @code{PGUSER}.
657 Database name. Same as @code{PGDATABASE}
659 Symbolic hostname. Same as @code{PGHOST}.
661 Host address as four octets (eg. like 192.168.1.1).
663 TCP port to connect to. Same as @code{PGPORT}.
665 Debugging TTY. Same as @code{PGTTY}. This value is suppressed in the
668 Extra backend database options. Same as @code{PGOPTIONS}.
670 A database connection object is returned regardless of whether a
671 connection was established or not.
675 Reestablish database connection.
676 @var{conn} A database connection object.
678 This function reestablishes a database connection using the original
679 connection parameters. This is useful if something has happened to the
680 TCP link and it has become broken.
683 @defun pq-exec conn query
684 Make a synchronous database query.
685 @var{conn} A database connection object.
686 @var{query} A string containing an SQL query.
687 A PGresult object is returned, which in turn may be queried by its many
688 accessor functions to retrieve state out of it. If the query string
689 contains multiple SQL commands, only results from the final command are
693 (setq R (pq-exec P "SELECT * FROM xemacs_test;
694 DELETE FROM xemacs_test WHERE id=8;"))
695 @result{} #<PGresult PGRES_COMMAND_OK[1] - DELETE 1>
699 @defun pq-notifies conn
700 Return the latest async notification that has not yet been handled.
701 @var{conn} A database connection object.
702 If there has been a notification, then a list of two elements will be returned.
703 The first element contains the relation name being notified, the second
704 element contains the backend process ID number. nil is returned if there
705 aren't any notifications to process.
709 Synchronous transfer of environment variables to a backend
710 @var{conn} A database connection object.
712 Environment variable transfer is done as a normal part of database
715 Compatibility note: This function was present but not documented in versions
716 of libpq prior to 7.0.
719 @node Asynchronous Interface Functions, Large Object Support, Synchronous Interface Functions, XEmacs PostgreSQL libpq API
720 @comment node-name, next, previous, up
721 @subsection Asynchronous Interface Functions
723 Making command by command examples is too complex with the asynchronous
724 interface functions. See the examples section for complete calling
727 @defun pq-connect-start conninfo
728 Begin establishing an asynchronous database connection.
729 @var{conninfo} A string containing the connection options. See the
730 documentation of @code{pq-connectdb} for a listing of all the available
734 @defun pq-connect-poll conn
735 An intermediate function to be called during an asynchronous database
737 @var{conn} A database connection object.
738 The result codes are documented in a previous section.
741 @defun pq-is-busy conn
742 Returns t if @code{pq-get-result} would block waiting for input.
743 @var{conn} A database connection object.
746 @defun pq-consume-input conn
747 Consume any available input from the backend.
748 @var{conn} A database connection object.
750 Nil is returned if anything bad happens.
753 @defun pq-reset-start conn
754 Reset connection to the backend asynchronously.
755 @var{conn} A database connection object.
758 @defun pq-reset-poll conn
759 Poll an asynchronous reset for completion
760 @var{conn} A database connection object.
763 @defun pq-reset-cancel conn
764 Attempt to request cancellation of the current operation.
765 @var{conn} A database connection object.
767 The return value is t if the cancel request was successfully
768 dispatched, nil if not (in which case conn->errorMessage is set).
769 Note: successful dispatch is no guarantee that there will be any effect at
770 the backend. The application must read the operation result as usual.
773 @defun pq-send-query conn query
774 Submit a query to Postgres and don't wait for the result.
775 @var{conn} A database connection object.
776 Returns: t if successfully submitted
777 nil if error (conn->errorMessage is set)
780 @defun pq-get-result conn
781 Retrieve an asynchronous result from a query.
782 @var{conn} A database connection object.
784 @code{nil} is returned when no more query work remains.
787 @defun pq-set-nonblocking conn arg
788 Sets the PGconn's database connection non-blocking if the arg is TRUE
789 or makes it non-blocking if the arg is FALSE, this will not protect
790 you from PQexec(), you'll only be safe when using the non-blocking API.
791 @var{conn} A database connection object.
794 @defun pq-is-nonblocking conn
795 Return the blocking status of the database connection
796 @var{conn} A database connection object.
800 Force the write buffer to be written (or at least try)
801 @var{conn} A database connection object.
804 @defun PQsetenvStart conn
805 Start asynchronously passing environment variables to a backend.
806 @var{conn} A database connection object.
808 Compatibility note: this function is only available with libpq-7.0.
811 @defun PQsetenvPoll conn
812 Check an asynchronous environment variables transfer for completion.
813 @var{conn} A database connection object.
815 Compatibility note: this function is only available with libpq-7.0.
818 @defun PQsetenvAbort conn
819 Attempt to terminate an asynchronous environment variables transfer.
820 @var{conn} A database connection object.
822 Compatibility note: this function is only available with libpq-7.0.
825 @node Large Object Support, Other libpq Functions, Asynchronous Interface Functions, XEmacs PostgreSQL libpq API
826 @comment node-name, next, previous, up
827 @subsection Large Object Support
829 @defun pq-lo-import conn filename
830 Import a file as a large object into the database.
831 @var{conn} a database connection object
832 @var{filename} filename to import
834 On success, the object id is returned.
837 @defun pq-lo-export conn oid filename
838 Copy a large object in the database into a file.
839 @var{conn} a database connection object.
840 @var{oid} object id number of a large object.
841 @var{filename} filename to export to.
844 @node Other libpq Functions, Unimplemented libpq Functions, Large Object Support, XEmacs PostgreSQL libpq API
845 @comment node-name, next, previous, up
846 @subsection Other libpq Functions
848 @defun pq-finish conn
849 Destroy a database connection object by calling free on it.
850 @var{conn} a database connection object
852 It is possible to not call this routine because the usual XEmacs garbage
853 collection mechanism will call the underlying libpq routine whenever it
854 is releasing stale @code{PGconn} objects. However, this routine is
855 useful in @code{unwind-protect} clauses to make connections go away
856 quickly when unrecoverable errors have occurred.
858 After calling this routine, the printed representation of the XEmacs
859 wrapper object will contain the string ``DEAD''.
862 @defun pq-client-encoding conn
863 Return the client encoding as an integer code.
864 @var{conn} a database connection object
867 (pq-client-encoding P)
871 Compatibility note: This function did not exist prior to libpq-7.0 and
872 does not exist in a non-Mule XEmacs.
875 @defun pq-set-client-encoding conn encoding
876 Set client coding system.
877 @var{conn} a database connection object
878 @var{encoding} a string representing the desired coding system
881 (pq-set-client-encoding P "EUC_JP")
885 The current idiom for ensuring proper coding system conversion is the
886 following (illustrated for EUC Japanese encoding):
888 (setq P (pq-connectdb "..."))
889 (let ((file-name-coding-system 'euc-jp)
890 (pg-coding-system 'euc-jp))
891 (pq-set-client-encoding "EUC_JP")
895 Compatibility note: This function did not exist prior to libpq-7.0 and
896 does not exist in a non-Mule XEmacs.
899 @defun pq-env-2-encoding
900 Return the integer code representing the coding system in
901 @code{PGCLIENTENCODING}.
907 Compatibility note: This function did not exist prior to libpq-7.0 and
908 does not exist in a non-Mule XEmacs.
912 Destroy a query result object by calling free() on it.
913 @var{res} a query result object
915 Note: The memory allocation systems of libpq and XEmacs are different.
916 The XEmacs representation of a query result object will have both the
917 XEmacs version and the libpq version freed at the next garbage collection
918 when the object is no longer being referenced. Calling this function does
919 not release the XEmacs object, it is still subject to the usual rules for
920 Lisp objects. The printed representation of the XEmacs object will contain
921 the string ``DEAD'' after this routine is called indicating that it is no
922 longer useful for anything.
925 @defun pq-conn-defaults
926 Return a data structure that represents the connection defaults.
927 The data is returned as a list of lists, where each sublist contains
928 info regarding a single option.
931 @node Unimplemented libpq Functions, , Other libpq Functions, XEmacs PostgreSQL libpq API
932 @comment node-name, next, previous, up
933 @subsection Unimplemented libpq Functions
935 @deftypefn {Unimplemented Function} PGconn *PQsetdbLogin (char *pghost, char *pgport, char *pgoptions, char *pgtty, char *dbName, char *login, char *pwd)
936 Synchronous database connection.
937 @var{pghost} is the hostname of the PostgreSQL backend to connect to.
938 @var{pgport} is the TCP port number to use.
939 @var{pgoptions} specifies other backend options.
940 @var{pgtty} specifies the debugging tty to use.
941 @var{dbName} specifies the database name to use.
942 @var{login} specifies the database user name.
943 @var{pwd} specifies the database user's password.
945 This routine is deprecated as of libpq-7.0, and its functionality can be
946 replaced by external Lisp code if needed.
949 @deftypefn {Unimplemented Function} PGconn *PQsetdb (char *pghost, char *pgport, char *pgoptions, char *pgtty, char *dbName)
950 Synchronous database connection.
951 @var{pghost} is the hostname of the PostgreSQL backend to connect to.
952 @var{pgport} is the TCP port number to use.
953 @var{pgoptions} specifies other backend options.
954 @var{pgtty} specifies the debugging tty to use.
955 @var{dbName} specifies the database name to use.
957 This routine was deprecated in libpq-6.5.
960 @deftypefn {Unimplemented Function} int PQsocket (PGconn *conn)
961 Return socket file descriptor to a backend database process.
962 @var{conn} database connection object.
965 @deftypefn {Unimplemented Function} void PQprint (FILE *fout, PGresult *res, PGprintOpt *ps)
966 Print out the results of a query to a designated C stream.
967 @var{fout} C stream to print to
968 @var{res} the query result object to print
969 @var{ps} the print options structure.
971 This routine is deprecated as of libpq-7.0 and cannot be sensibly exported
975 @deftypefn {Unimplemented Function} void PQdisplayTuples (PGresult *res, FILE *fp, int fillAlign, char *fieldSep, int printHeader, int quiet)
976 @var{res} query result object to print
977 @var{fp} C stream to print to
978 @var{fillAlign} pad the fields with spaces
979 @var{fieldSep} field separator
980 @var{printHeader} display headers?
983 This routine was deprecated in libpq-6.5.
986 @deftypefn {Unimplemented Function} void PQprintTuples (PGresult *res, FILE *fout, int printAttName, int terseOutput, int width)
987 @var{res} query result object to print
988 @var{fout} C stream to print to
989 @var{printAttName} print attribute names
990 @var{terseOutput} delimiter bars
991 @var{width} width of column, if 0, use variable width
993 This routine was deprecated in libpq-6.5.
996 @deftypefn {Unimplemented Function} int PQmblen (char *s, int encoding)
997 Determine length of a multibyte encoded char at @code{*s}.
998 @var{s} encoded string
999 @var{encoding} type of encoding
1001 Compatibility note: This function was introduced in libpq-7.0.
1004 @deftypefn {Unimplemented Function} void PQtrace (PGconn *conn, FILE *debug_port)
1005 Enable tracing on @code{debug_port}.
1006 @var{conn} database connection object.
1007 @var{debug_port} C output stream to use.
1010 @deftypefn {Unimplemented Function} void PQuntrace (PGconn *conn)
1012 @var{conn} database connection object.
1015 @deftypefn {Unimplemented Function} char *PQoidStatus (PGconn *conn)
1016 Return the object id as a string of the last tuple inserted.
1017 @var{conn} database connection object.
1019 Compatibility note: This function is deprecated in libpq-7.0, however it
1020 is used internally by the XEmacs binding code when linked against versions
1024 @deftypefn {Unimplemented Function} PGresult *PQfn (PGconn *conn, int fnid, int *result_buf, int *result_len, int result_is_int, PQArgBlock *args, int nargs)
1025 ``Fast path'' interface --- not really recommended for application use
1026 @var{conn} A database connection object.
1035 The following set of very low level large object functions aren't
1036 appropriate to be exported to Lisp.
1038 @deftypefn {Unimplemented Function} int pq-lo-open (PGconn *conn, int lobjid, int mode)
1039 @var{conn} a database connection object.
1040 @var{lobjid} a large object ID.
1041 @var{mode} opening modes.
1044 @deftypefn {Unimplemented Function} int pq-lo-close (PGconn *conn, int fd)
1045 @var{conn} a database connection object.
1046 @var{fd} a large object file descriptor
1049 @deftypefn {Unimplemented Function} int pq-lo-read (PGconn *conn, int fd, char *buf, int len)
1050 @var{conn} a database connection object.
1051 @var{fd} a large object file descriptor.
1052 @var{buf} buffer to read into.
1053 @var{len} size of buffer.
1056 @deftypefn {Unimplemented Function} int pq-lo-write (PGconn *conn, int fd, char *buf, size_t len)
1057 @var{conn} a database connection object.
1058 @var{fd} a large object file descriptor.
1059 @var{buf} buffer to write from.
1060 @var{len} size of buffer.
1063 @deftypefn {Unimplemented Function} int pq-lo-lseek (PGconn *conn, int fd, int offset, int whence)
1064 @var{conn} a database connection object.
1065 @var{fd} a large object file descriptor.
1070 @deftypefn {Unimplemented Function} int pq-lo-creat (PGconn *conn, int mode)
1071 @var{conn} a database connection object.
1072 @var{mode} opening modes.
1075 @deftypefn {Unimplemented Function} int pq-lo-tell (PGconn *conn, int fd)
1076 @var{conn} a database connection object.
1077 @var{fd} a large object file descriptor.
1080 @deftypefn {Unimplemented Function} int pq-lo-unlink (PGconn *conn, int lobjid)
1081 @var{conn} a database connection object.
1082 @var{lbojid} a large object ID.
1085 @node XEmacs PostgreSQL libpq Examples, , XEmacs PostgreSQL libpq API, PostgreSQL Support
1086 @comment node-name, next, previous, up
1087 @section XEmacs PostgreSQL libpq Examples
1089 This is an example of one method of establishing an asynchronous
1093 (defun database-poller (P)
1094 (message "%S before poll" (pq-pgconn P 'pq::status))
1096 (message "%S after poll" (pq-pgconn P 'pq::status))
1097 (if (eq (pq-pgconn P 'pq::status) 'pg::connection-ok)
1099 (add-timeout .1 'database-poller P)))
1100 @result{} database-poller
1102 (setq P (pq-connect-start ""))
1103 (add-timeout .1 'database-poller P))
1104 @result{} pg::connection-started before poll
1105 @result{} pg::connection-made after poll
1106 @result{} pg::connection-made before poll
1107 @result{} pg::connection-awaiting-response after poll
1108 @result{} pg::connection-awaiting-response before poll
1109 @result{} pg::connection-auth-ok after poll
1110 @result{} pg::connection-auth-ok before poll
1111 @result{} pg::connection-setenv after poll
1112 @result{} pg::connection-setenv before poll
1113 @result{} pg::connection-ok after poll
1116 @result{} #<PGconn localhost:25432 steve/steve>
1119 Here is an example of one method of doing an asynchronous reset.
1122 (defun database-poller (P)
1124 (message "%S before poll" (pq-pgconn P 'pq::status))
1125 (setq PS (pq-reset-poll P))
1126 (message "%S after poll [%S]" (pq-pgconn P 'pq::status) PS)
1127 (if (eq (pq-pgconn P 'pq::status) 'pg::connection-ok)
1129 (add-timeout .1 'database-poller P))))
1130 @result{} database-poller
1133 (add-timeout .1 'database-poller P))
1134 @result{} pg::connection-started before poll
1135 @result{} pg::connection-made after poll [pgres::polling-writing]
1136 @result{} pg::connection-made before poll
1137 @result{} pg::connection-awaiting-response after poll [pgres::polling-reading]
1138 @result{} pg::connection-awaiting-response before poll
1139 @result{} pg::connection-setenv after poll [pgres::polling-reading]
1140 @result{} pg::connection-setenv before poll
1141 @result{} pg::connection-ok after poll [pgres::polling-ok]
1144 @result{} #<PGconn localhost:25432 steve/steve>
1147 And finally, an asynchronous query.
1150 (defun database-poller (P)
1152 (pq-consume-input P)
1154 (add-timeout .1 'database-poller P)
1155 (setq R (pq-get-result P))
1158 (push R result-list)
1159 (add-timeout .1 'database-poller P))))))
1160 @result{} database-poller
1161 (when (pq-send-query P "SELECT * FROM xemacs_test;")
1162 (setq result-list nil)
1163 (add-timeout .1 'database-poller P))
1167 @result{} (#<PGresult PGRES_TUPLES_OK - SELECT>)
1170 Here is an example showing how multiple SQL statements in a single query
1171 can have all their results collected.
1173 ;; Using the same @code{database-poller} function from the previous example
1174 (when (pq-send-query P "SELECT * FROM xemacs_test;
1175 SELECT * FROM pg_database;
1176 SELECT * FROM pg_user;")
1177 (setq result-list nil)
1178 (add-timeout .1 'database-poller P))
1182 @result{} (#<PGresult PGRES_TUPLES_OK - SELECT> #<PGresult PGRES_TUPLES_OK - SELECT> #<PGresult PGRES_TUPLES_OK - SELECT>)
1185 Here is an example which illustrates collecting all data from a query,
1186 including the field names.
1189 (defun pg-util-query-results (results)
1190 "Retrieve results of last SQL query into a list structure."
1191 (let ((i (1- (pq-ntuples R)))
1194 (setq j (1- (pq-nfields R)))
1197 (push (pq-get-value R i j) l2)
1201 (setq j (1- (pq-nfields R)))
1204 (push (pq-fname R j) l2)
1208 @result{} pg-util-query-results
1209 (setq R (pq-exec P "SELECT * FROM xemacs_test ORDER BY field2 DESC;"))
1210 @result{} #<PGresult PGRES_TUPLES_OK - SELECT>
1211 (pg-util-query-results R)
1212 @result{} (("f1" "field2") ("a" "97") ("b" "97") ("stuff" "42") ("a string" "12") ("foo" "10") ("string" "2") ("text" "1"))
1215 Here is an example of a query that uses a database cursor.
1219 (setq R (pq-exec P "BEGIN;"))
1220 (setq R (pq-exec P "DECLARE k_cursor CURSOR FOR SELECT * FROM xemacs_test ORDER BY f1 DESC;"))
1222 (setq R (pq-exec P "FETCH k_cursor;"))
1223 (while (eq (pq-ntuples R) 1)
1224 (push (list (pq-get-value R 0 0) (pq-get-value R 0 1)) data)
1225 (setq R (pq-exec P "FETCH k_cursor;")))
1226 (setq R (pq-exec P "END;"))
1228 @result{} (("a" "97") ("a string" "12") ("b" "97") ("foo" "10") ("string" "2") ("stuff" "42") ("text" "1"))
1231 Here's another example of cursors, this time with a Lisp macro to
1232 implement a mapping function over a table.
1235 (defmacro map-db (P table condition callout)
1237 (pq-exec ,P "BEGIN;")
1238 (pq-exec ,P (concat "DECLARE k_cursor CURSOR FOR SELECT * FROM "
1242 " ORDER BY f1 DESC;"))
1243 (setq R (pq-exec P "FETCH k_cursor;"))
1244 (while (eq (pq-ntuples R) 1)
1245 (,callout (pq-get-value R 0 0) (pq-get-value R 0 1))
1246 (setq R (pq-exec P "FETCH k_cursor;")))
1247 (pq-exec P "END;")))
1249 (defun callback (arg1 arg2)
1250 (message "arg1 = %s, arg2 = %s" arg1 arg2))
1252 (map-db P "xemacs_test" "WHERE field2 > 10" callback)
1253 @result{} arg1 = stuff, arg2 = 42
1254 @result{} arg1 = b, arg2 = 97
1255 @result{} arg1 = a string, arg2 = 12
1256 @result{} arg1 = a, arg2 = 97
1257 @result{} #<PGresult PGRES_COMMAND_OK - COMMIT>