442
|
1 /*
|
|
2 postgresql.c -- Emacs Lisp binding to libpq.so
|
|
3 Copyright (C) 2000 Electrotechnical Laboratory, JAPAN.
|
|
4 Licensed to the Free Software Foundation.
|
|
5
|
|
6 Author: SL Baur <steve@beopen.com>
|
|
7 Maintainer: SL Baur <steve@beopen.com>
|
|
8
|
|
9 Please send patches to this file to me first before submitting them to
|
|
10 xemacs-patches.
|
|
11
|
|
12
|
|
13 KNOWN PROBLEMS (Last update 15-March-2000)
|
|
14 + None.
|
|
15
|
|
16 Implementation notes:
|
|
17 0. Supported PostgreSQL versions
|
|
18 This code was developed against libpq-6.5.3 and libpq-7.0-beta1. Earlier
|
|
19 versions may work. V7 support is more complete than V6.5 support.
|
|
20 1. Mule
|
|
21 Non-ASCII databases have been tested on both 6.5 and 7.0.
|
|
22 2. Asynchronous Operation
|
|
23 Starting with libpq-7.0, an asynchronous interface is offered. This
|
|
24 binding supports the asynchronous calls to a limited extent. Since the
|
|
25 XEmacs 21.2 core does not support a sensible interface to add managed but
|
|
26 unreadable (by XEmacs) file descriptors to the main select code, polling
|
|
27 is required to drive the asynchronous calls. XtAppAddInput would work
|
|
28 fine, but we want to be able to use the database when running strictly in
|
|
29 tty mode.
|
|
30 3. Completeness
|
|
31 Various calls have been deliberately not exported to Lisp. The
|
|
32 unexported calls are either left-over backwards compatibility code that
|
|
33 aren't needed, calls that cannot be implemented sensibly, or calls that
|
|
34 cannot be implemented safely. A list of all global functions in libpq
|
|
35 but not exported to Lisp is below.
|
|
36 4. Policy
|
|
37 This interface tries very hard to not set any policy towards how database
|
|
38 code in Emacs Lisp will be written.
|
|
39 5. Documentation
|
|
40 For full lisp programming documentation, see the XEmacs Lisp Reference
|
|
41 Manual. For PostgreSQL documentation, see the PostgreSQL distribution.
|
|
42
|
|
43 TODO (in rough order of priority):
|
|
44 1. Asynchronous notifies need to be implemented to the extent they can be.
|
|
45 2. The large object interface needs work with Emacs buffers in addition
|
|
46 to files. Need two functions buffer->large_object, and large_object->
|
|
47 buffer.
|
|
48 */
|
|
49
|
|
50 /*
|
|
51 Unimplemented functions: [TODO]
|
|
52 PQsetNoticeProcessor
|
|
53
|
|
54 Implemented, but undocumented functions: [TODO]
|
|
55 PQgetline (copy in/out)
|
|
56 PQputline (copy in/out)
|
|
57 PQgetlineAsync (copy in/out Asynch.)
|
|
58 PQputnbytes (copy in/out Asynch.)
|
|
59 PQendcopy (copy in/out)
|
|
60
|
|
61 Unsupported functions:
|
|
62 PQsetdbLogin -- This function is deprecated, has a subset of the
|
|
63 functionality of PQconnectdb, and is better done in Lisp.
|
|
64 PQsetdb -- Same as for PQsetdbLogin
|
|
65 PQsocket -- Abstraction error, file descriptors should not be leaked
|
|
66 into Lisp code
|
|
67 PQprint -- print to a file descriptor, deprecated, better done in Lisp
|
|
68 PQdisplayTuples -- deprecated
|
|
69 PQprintTuples -- really, really deprecated
|
|
70 PQmblen -- Returns the length in bytes of multibyte character encoded
|
|
71 string.
|
|
72 PQtrace -- controls debug print tracing to a tty.
|
|
73 PQuntrace -- Ditto. I don't see any way to do this sensibly.
|
|
74 PQoidStatus -- deprecated and nearly identical to PQoidValue
|
|
75 PQfn -- "Fast path" interface
|
|
76 lo_open (large object) [*]
|
|
77 lo_close (large object) [*]
|
|
78 lo_read (large object) [*]
|
|
79 lo_write (large object) [*]
|
|
80 lo_lseek (large object) [*]
|
|
81 lo_creat (large object) [*]
|
|
82 lo_tell (large object) [*]
|
|
83 lo_unlink (large object) [*]
|
|
84 */
|
|
85
|
|
86 #include <config.h>
|
|
87
|
|
88 /* This must be portable with XEmacs 21.1 so long as it is the official
|
|
89 released version of XEmacs and provides the basis of InfoDock. The
|
|
90 interface to lcrecord handling has changed with 21.2, so unfortunately
|
|
91 we will need a few snippets of backwards compatibility code.
|
|
92 */
|
|
93 #if (EMACS_MAJOR_VERSION == 21) && (EMACS_MINOR_VERSION < 2)
|
|
94 #define RUNNING_XEMACS_21_1 1
|
|
95 #endif
|
|
96
|
|
97 /* #define POSTGRES_LO_IMPORT_IS_VOID 1 */
|
|
98
|
|
99 #include "lisp.h"
|
|
100 #include "sysdep.h"
|
872
|
101
|
442
|
102 #include "buffer.h"
|
|
103 #include "postgresql.h"
|
872
|
104 #include "process.h"
|
442
|
105
|
|
106 #ifdef RUNNING_XEMACS_21_1 /* handle interface changes */
|
|
107 #define PG_OS_CODING FORMAT_FILENAME
|
|
108 #define TO_EXTERNAL_FORMAT(a,from,b,to,c) GET_C_STRING_EXT_DATA_ALLOCA(from,FORMAT_FILENAME,to)
|
|
109 #else
|
|
110 #ifdef MULE
|
771
|
111 #define PG_OS_CODING get_coding_system_for_text_file (Vpg_coding_system, 1)
|
442
|
112 #else
|
|
113 #define PG_OS_CODING Qnative
|
|
114 #endif
|
|
115 Lisp_Object Vpg_coding_system;
|
|
116 #endif
|
|
117
|
563
|
118 #define CHECK_LIVE_CONNECTION(P) do { \
|
|
119 if (!P || (PQstatus (P) != CONNECTION_OK)) { \
|
|
120 char *e = "bad value"; \
|
|
121 if (P) e = PQerrorMessage (P); \
|
|
122 signal_ferror (Qprocess_error, "dead connection [%s]", e); \
|
|
123 } } while (0)
|
|
124 #define PUKE_IF_NULL(p) do { \
|
|
125 if (!p) signal_error (Qinvalid_argument, "bad value", Qunbound); \
|
|
126 } while (0)
|
442
|
127
|
|
128 static Lisp_Object VXPGHOST;
|
|
129 static Lisp_Object VXPGUSER;
|
|
130 static Lisp_Object VXPGOPTIONS;
|
|
131 static Lisp_Object VXPGPORT;
|
|
132 static Lisp_Object VXPGTTY; /* This needs to be blanked! */
|
|
133 static Lisp_Object VXPGDATABASE;
|
|
134 static Lisp_Object VXPGREALM;
|
|
135 #ifdef MULE
|
|
136 static Lisp_Object VXPGCLIENTENCODING;
|
|
137 #endif /* MULE */
|
|
138
|
|
139 /* Other variables:
|
|
140 PGAUTHTYPE -- not used after PostgreSQL 6.5
|
|
141 PGGEQO
|
|
142 PGCOSTINDEX
|
|
143 PGCOSTHEAP
|
|
144 PGTZ
|
|
145 PGDATESTYLE
|
|
146 */
|
|
147 #ifndef HAVE_POSTGRESQLV7
|
|
148 static Lisp_Object VXPGAUTHTYPE;
|
|
149 #endif
|
|
150 static Lisp_Object VXPGGEQO, VXPGCOSTINDEX, VXPGCOSTHEAP, VXPGTZ, VXPGDATESTYLE;
|
|
151
|
|
152 static Lisp_Object Qpostgresql;
|
|
153 static Lisp_Object Qpg_connection_ok, Qpg_connection_bad;
|
|
154 static Lisp_Object Qpg_connection_started, Qpg_connection_made;
|
|
155 static Lisp_Object Qpg_connection_awaiting_response, Qpg_connection_auth_ok;
|
|
156 static Lisp_Object Qpg_connection_setenv;
|
|
157
|
|
158 static Lisp_Object Qpqdb, Qpquser, Qpqpass, Qpqhost, Qpqport, Qpqtty;
|
|
159 static Lisp_Object Qpqoptions, Qpqstatus, Qpqerrormessage, Qpqbackendpid;
|
|
160
|
|
161 static Lisp_Object Qpgres_empty_query, Qpgres_command_ok, Qpgres_tuples_ok;
|
|
162 static Lisp_Object Qpgres_copy_out, Qpgres_copy_in, Qpgres_bad_response;
|
|
163 static Lisp_Object Qpgres_nonfatal_error, Qpgres_fatal_error;
|
|
164
|
|
165 static Lisp_Object Qpgres_polling_failed, Qpgres_polling_reading;
|
|
166 static Lisp_Object Qpgres_polling_writing, Qpgres_polling_ok;
|
|
167 static Lisp_Object Qpgres_polling_active;
|
|
168 /****/
|
|
169
|
|
170 /* PGconn is an opaque object and we need to be able to store them in
|
|
171 Lisp code because libpq supports multiple connections.
|
|
172 */
|
|
173 Lisp_Object Qpgconnp;
|
|
174
|
|
175 static Lisp_Object
|
|
176 make_pgconn (Lisp_PGconn *pgconn)
|
|
177 {
|
793
|
178 return wrap_pgconn (pgconn);
|
442
|
179 }
|
|
180
|
|
181 static Lisp_Object
|
|
182 #ifdef RUNNING_XEMACS_21_1
|
|
183 mark_pgconn (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
184 #else
|
|
185 mark_pgconn (Lisp_Object obj)
|
|
186 #endif
|
|
187 {
|
|
188 return Qnil;
|
|
189 }
|
|
190
|
|
191 static void
|
|
192 print_pgconn (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
193 {
|
|
194 char buf[256];
|
|
195 PGconn *P;
|
|
196 ConnStatusType cst;
|
|
197 char *host="", *db="", *user="", *port="";
|
|
198
|
|
199 P = (XPGCONN (obj))->pgconn;
|
|
200
|
|
201 if (!P) /* this may happen since we allow PQfinish() to be called */
|
|
202 strcpy (buf, "#<PGconn DEAD>"); /* evil! */
|
|
203 else if ((cst = PQstatus (P)) == CONNECTION_OK)
|
|
204 {
|
|
205 if (!(host = PQhost (P)))
|
|
206 host = "";
|
|
207 port = PQport (P);
|
|
208 db = PQdb (P);
|
|
209 if (!(user = PQuser (P)))
|
|
210 user = "";
|
|
211 sprintf (buf, "#<PGconn %s:%s %s/%s>", /* evil! */
|
|
212 !strlen (host) ? "localhost" : host,
|
|
213 port,
|
|
214 user,
|
|
215 db);
|
|
216 }
|
|
217 else if (cst == CONNECTION_BAD)
|
|
218 strcpy (buf, "#<PGconn BAD>"); /* evil! */
|
|
219 else
|
|
220 strcpy (buf, "#<PGconn connecting>"); /* evil! */
|
|
221
|
|
222 if (print_readably)
|
563
|
223 printing_unreadable_object ("%s", buf);
|
442
|
224 else
|
826
|
225 write_c_string (printcharfun, buf);
|
442
|
226 }
|
|
227
|
|
228 static Lisp_PGconn *
|
|
229 allocate_pgconn (void)
|
|
230 {
|
|
231 #ifdef RUNNING_XEMACS_21_1
|
|
232 Lisp_PGconn *pgconn = alloc_lcrecord_type (Lisp_PGconn,
|
|
233 lrecord_pgconn);
|
|
234 #else
|
|
235 Lisp_PGconn *pgconn = alloc_lcrecord_type (Lisp_PGconn,
|
|
236 &lrecord_pgconn);
|
|
237 #endif
|
|
238 pgconn->pgconn = (PGconn *)NULL;
|
|
239 return pgconn;
|
|
240 }
|
|
241
|
|
242 static void
|
|
243 finalize_pgconn (void *header, int for_disksave)
|
|
244 {
|
|
245 Lisp_PGconn *pgconn = (Lisp_PGconn *)header;
|
|
246
|
|
247 if (for_disksave)
|
563
|
248 invalid_operation ("Can't dump an emacs containing PGconn objects",
|
|
249 make_pgconn (pgconn));
|
442
|
250
|
|
251 if (pgconn->pgconn)
|
|
252 {
|
|
253 PQfinish (pgconn->pgconn);
|
|
254 pgconn->pgconn = (PGconn *)NULL;
|
|
255 }
|
|
256 }
|
|
257
|
|
258 #ifdef RUNNING_XEMACS_21_1
|
|
259 DEFINE_LRECORD_IMPLEMENTATION ("pgconn", pgconn,
|
|
260 mark_pgconn, print_pgconn, finalize_pgconn,
|
|
261 NULL, NULL,
|
|
262 Lisp_PGconn);
|
|
263 #else
|
|
264 DEFINE_LRECORD_IMPLEMENTATION ("pgconn", pgconn,
|
|
265 mark_pgconn, print_pgconn, finalize_pgconn,
|
|
266 NULL, NULL,
|
|
267 0,
|
|
268 Lisp_PGconn);
|
|
269 #endif
|
|
270 /****/
|
|
271
|
|
272 /* PGresult is an opaque object and we need to be able to store them in
|
|
273 Lisp code.
|
|
274 */
|
|
275 Lisp_Object Qpgresultp;
|
|
276
|
|
277 static Lisp_Object
|
|
278 make_pgresult (Lisp_PGresult *pgresult)
|
|
279 {
|
793
|
280 return wrap_pgresult (pgresult);
|
442
|
281 }
|
|
282
|
|
283 static Lisp_Object
|
|
284 #ifdef RUNNING_XEMACS_21_1
|
|
285 mark_pgresult (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
286 #else
|
|
287 mark_pgresult (Lisp_Object obj)
|
|
288 #endif
|
|
289 {
|
|
290 return Qnil;
|
|
291 }
|
|
292
|
|
293 #define RESULT_TUPLES_FMT "#<PGresult %s[%d] - %s>"
|
|
294 #define RESULT_CMD_TUPLES_FMT "#<PGresult %s[%s] - %s>"
|
|
295 #define RESULT_DEFAULT_FMT "#<PGresult %s - %s>"
|
|
296 static void
|
|
297 print_pgresult (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
298 {
|
|
299 char buf[1024];
|
|
300 PGresult *res;
|
|
301
|
|
302 res = (XPGRESULT (obj))->pgresult;
|
|
303
|
|
304 if (res)
|
|
305 {
|
|
306 switch (PQresultStatus (res))
|
|
307 {
|
|
308 case PGRES_TUPLES_OK:
|
|
309 /* Add number of tuples of result to output */
|
|
310 sprintf (buf, RESULT_TUPLES_FMT, /* evil! */
|
|
311 PQresStatus (PQresultStatus (res)),
|
|
312 PQntuples (res),
|
|
313 PQcmdStatus (res));
|
|
314 break;
|
|
315 case PGRES_COMMAND_OK:
|
|
316 /* Add number of tuples affected by output-less command */
|
|
317 if (!strlen (PQcmdTuples (res))) goto notuples;
|
|
318 sprintf (buf, RESULT_CMD_TUPLES_FMT, /* evil! */
|
|
319 PQresStatus (PQresultStatus (res)),
|
|
320 PQcmdTuples (res),
|
|
321 PQcmdStatus (res));
|
|
322 break;
|
|
323 default:
|
|
324 notuples:
|
|
325 /* No counts to print */
|
|
326 sprintf (buf, RESULT_DEFAULT_FMT, /* evil! */
|
|
327 PQresStatus (PQresultStatus (res)),
|
|
328 PQcmdStatus (res));
|
|
329 break;
|
|
330 }
|
|
331 }
|
|
332 else
|
|
333 strcpy (buf, "#<PGresult DEAD>"); /* evil! */
|
|
334
|
|
335 if (print_readably)
|
563
|
336 printing_unreadable_object ("%s", buf);
|
442
|
337 else
|
826
|
338 write_c_string (printcharfun, buf);
|
442
|
339 }
|
|
340
|
|
341 #undef RESULT_TUPLES_FMT
|
|
342 #undef RESULT_CMD_TUPLES_FMT
|
|
343 #undef RESULT_DEFAULT_FMT
|
|
344
|
|
345 static Lisp_PGresult *
|
|
346 allocate_pgresult (void)
|
|
347 {
|
|
348 #ifdef RUNNING_XEMACS_21_1
|
|
349 Lisp_PGresult *pgresult = alloc_lcrecord_type (Lisp_PGresult,
|
|
350 lrecord_pgresult);
|
|
351 #else
|
|
352 Lisp_PGresult *pgresult = alloc_lcrecord_type (Lisp_PGresult,
|
|
353 &lrecord_pgresult);
|
|
354 #endif
|
|
355 pgresult->pgresult = (PGresult *)NULL;
|
|
356 return pgresult;
|
|
357 }
|
|
358
|
|
359 static void
|
|
360 finalize_pgresult (void *header, int for_disksave)
|
|
361 {
|
|
362 Lisp_PGresult *pgresult = (Lisp_PGresult *)header;
|
|
363
|
|
364 if (for_disksave)
|
563
|
365 invalid_operation ("Can't dump an emacs containing PGresult objects",
|
|
366 make_pgresult (pgresult));
|
442
|
367
|
|
368 if (pgresult->pgresult)
|
|
369 {
|
|
370 PQclear (pgresult->pgresult);
|
|
371 pgresult->pgresult = (PGresult *)NULL;
|
|
372 }
|
|
373 }
|
|
374
|
|
375 #ifdef RUNNING_XEMACS_21_1
|
|
376 DEFINE_LRECORD_IMPLEMENTATION ("pgresult", pgresult,
|
|
377 mark_pgresult, print_pgresult, finalize_pgresult,
|
|
378 NULL, NULL,
|
|
379 Lisp_PGresult);
|
|
380 #else
|
|
381 DEFINE_LRECORD_IMPLEMENTATION ("pgresult", pgresult,
|
|
382 mark_pgresult, print_pgresult, finalize_pgresult,
|
|
383 NULL, NULL,
|
|
384 0,
|
|
385 Lisp_PGresult);
|
|
386 #endif
|
|
387
|
|
388 /***********************/
|
|
389
|
|
390 /* notices */
|
|
391 static void
|
|
392 xemacs_notice_processor (void *arg, const char *msg)
|
|
393 {
|
|
394 warn_when_safe (Qpostgresql, Qnotice, "%s", msg);
|
|
395 }
|
|
396
|
|
397 /* There are four ways (as of PostgreSQL v7) to connect to a database.
|
|
398 Two of them, PQsetdb and PQsetdbLogin, are deprecated. Both of those
|
|
399 routines take a number of positional parameters and are better done in Lisp.
|
|
400 Note that PQconnectStart does not exist prior to v7.
|
|
401 */
|
|
402
|
|
403 DEFUN ("pq-conn-defaults", Fpq_conn_defaults, 0, 0, 0, /*
|
|
404 Return a connection default structure.
|
|
405 */
|
|
406 ())
|
|
407 {
|
|
408 /* This function can GC */
|
|
409 PQconninfoOption *pcio;
|
|
410 Lisp_Object temp, temp1;
|
|
411 int i;
|
|
412
|
|
413 pcio = PQconndefaults();
|
|
414 if (!pcio) return Qnil; /* can never happen in libpq-7.0 */
|
|
415 temp = list1 (Fcons (build_ext_string (pcio[0].keyword, PG_OS_CODING),
|
|
416 Fcons (build_ext_string (pcio[0].envvar, PG_OS_CODING),
|
|
417 Fcons (build_ext_string (pcio[0].compiled, PG_OS_CODING),
|
|
418 Fcons (build_ext_string (pcio[0].val, PG_OS_CODING),
|
|
419 Fcons (build_ext_string (pcio[0].label, PG_OS_CODING),
|
|
420 Fcons (build_ext_string (pcio[0].dispchar, PG_OS_CODING),
|
|
421 Fcons (make_int (pcio[0].dispsize), Qnil))))))));
|
|
422
|
|
423 for (i = 1; pcio[i].keyword; i++)
|
|
424 {
|
|
425 temp1 = list1 (Fcons (build_ext_string (pcio[i].keyword, PG_OS_CODING),
|
|
426 Fcons (build_ext_string (pcio[i].envvar, PG_OS_CODING),
|
|
427 Fcons (build_ext_string (pcio[i].compiled, PG_OS_CODING),
|
|
428 Fcons (build_ext_string (pcio[i].val, PG_OS_CODING),
|
|
429 Fcons (build_ext_string (pcio[i].label, PG_OS_CODING),
|
|
430 Fcons (build_ext_string (pcio[i].dispchar, PG_OS_CODING),
|
|
431 Fcons (make_int (pcio[i].dispsize), Qnil))))))));
|
|
432 {
|
|
433 Lisp_Object args[2];
|
|
434 args[0] = temp;
|
|
435 args[1] = temp1;
|
|
436 /* Fappend GCPROs its arguments */
|
|
437 temp = Fappend (2, args);
|
|
438 }
|
|
439 }
|
|
440
|
|
441 return temp;
|
|
442 }
|
|
443
|
|
444 /* PQconnectdb Makes a new connection to a backend.
|
|
445 PGconn *PQconnectdb(const char *conninfo)
|
|
446 */
|
|
447
|
|
448 DEFUN ("pq-connectdb", Fpq_connectdb, 1, 1, 0, /*
|
|
449 Make a new connection to a PostgreSQL backend.
|
|
450 */
|
|
451 (conninfo))
|
|
452 {
|
|
453 PGconn *P;
|
|
454 Lisp_PGconn *lisp_pgconn;
|
|
455 char *error_message = "Out of Memory?";
|
|
456 char *c_conninfo;
|
|
457
|
|
458 CHECK_STRING (conninfo);
|
|
459
|
|
460 TO_EXTERNAL_FORMAT(LISP_STRING, conninfo,
|
|
461 C_STRING_ALLOCA, c_conninfo, Qnative);
|
|
462 P = PQconnectdb (c_conninfo);
|
|
463 if (P && (PQstatus (P) == CONNECTION_OK))
|
|
464 {
|
|
465 (void)PQsetNoticeProcessor (P, xemacs_notice_processor, NULL);
|
|
466 lisp_pgconn = allocate_pgconn();
|
|
467 lisp_pgconn->pgconn = P;
|
|
468 return make_pgconn (lisp_pgconn);
|
|
469 }
|
|
470 else
|
|
471 {
|
|
472 /* Connection failed. Destroy the connection and signal an error. */
|
|
473 char buf[BLCKSZ];
|
|
474 strcpy (buf, error_message);
|
|
475 if (P)
|
|
476 {
|
|
477 /* storage for the error message gets erased when call PQfinish */
|
|
478 /* so we must temporarily stash it somewhere */
|
|
479 strncpy (buf, PQerrorMessage (P), sizeof (buf));
|
|
480 buf[sizeof (buf) - 1] = '\0';
|
|
481 PQfinish (P);
|
|
482 }
|
563
|
483 signal_ferror (Qprocess_error, "libpq: %s", buf);
|
442
|
484 }
|
|
485 }
|
|
486
|
|
487 /* PQconnectStart Makes a new asynchronous connection to a backend.
|
|
488 PGconn *PQconnectStart(const char *conninfo)
|
|
489 */
|
|
490
|
|
491 #ifdef HAVE_POSTGRESQLV7
|
|
492 DEFUN ("pq-connect-start", Fpq_connect_start, 1, 1, 0, /*
|
|
493 Make a new asynchronous connection to a PostgreSQL backend.
|
|
494 */
|
|
495 (conninfo))
|
|
496 {
|
|
497 PGconn *P;
|
|
498 Lisp_PGconn *lisp_pgconn;
|
|
499 char *error_message = "Out of Memory?";
|
|
500 char *c_conninfo;
|
|
501
|
|
502 CHECK_STRING (conninfo);
|
|
503 TO_EXTERNAL_FORMAT (LISP_STRING, conninfo,
|
|
504 C_STRING_ALLOCA, c_conninfo, Qnative);
|
|
505 P = PQconnectStart (c_conninfo);
|
|
506
|
|
507 if (P && (PQstatus (P) != CONNECTION_BAD))
|
|
508 {
|
|
509 (void)PQsetNoticeProcessor (P, xemacs_notice_processor, NULL);
|
|
510 lisp_pgconn = allocate_pgconn();
|
|
511 lisp_pgconn->pgconn = P;
|
|
512
|
|
513 return make_pgconn (lisp_pgconn);
|
|
514 }
|
|
515 else
|
|
516 {
|
|
517 /* capture the error message before destroying the object */
|
|
518 char buf[BLCKSZ];
|
|
519 strcpy (buf, error_message);
|
|
520 if (P)
|
|
521 {
|
|
522 strncpy (buf, PQerrorMessage (P), sizeof (buf));
|
|
523 buf[sizeof (buf) - 1] = '\0';
|
|
524 PQfinish (P);
|
|
525 }
|
563
|
526 signal_ferror (Qprocess_error, "libpq: %s", buf);
|
442
|
527 }
|
|
528 }
|
|
529
|
|
530 DEFUN ("pq-connect-poll", Fpq_connect_poll, 1, 1, 0, /*
|
|
531 Poll an asynchronous connection for completion
|
|
532 */
|
|
533 (conn))
|
|
534 {
|
|
535 PGconn *P;
|
|
536 PostgresPollingStatusType polling_status;
|
|
537
|
|
538 CHECK_PGCONN (conn);
|
|
539
|
|
540 P = (XPGCONN (conn))->pgconn;
|
|
541 CHECK_LIVE_CONNECTION (P);
|
|
542
|
|
543 polling_status = PQconnectPoll (P);
|
|
544 switch (polling_status)
|
|
545 {
|
|
546 case PGRES_POLLING_FAILED:
|
|
547 /* Something Bad has happened */
|
|
548 {
|
|
549 char *e = PQerrorMessage (P);
|
563
|
550 signal_ferror (Qprocess_error, "libpq: %s", e);
|
442
|
551 }
|
|
552 case PGRES_POLLING_OK:
|
|
553 return Qpgres_polling_ok;
|
|
554 case PGRES_POLLING_READING:
|
|
555 return Qpgres_polling_reading;
|
|
556 case PGRES_POLLING_WRITING:
|
|
557 return Qpgres_polling_writing;
|
|
558 case PGRES_POLLING_ACTIVE:
|
|
559 return Qpgres_polling_active;
|
|
560 default:
|
|
561 /* they've added a new field we don't know about */
|
563
|
562 signal_ferror (Qprocess_error, "Help! Unknown status code %08x from backend!", polling_status);
|
442
|
563 }
|
|
564 }
|
|
565
|
|
566 #ifdef MULE
|
|
567 DEFUN ("pq-client-encoding", Fpq_client_encoding, 1, 1, 0, /*
|
|
568 Return client coding system.
|
|
569 */
|
|
570 (conn))
|
|
571 {
|
|
572 PGconn *P;
|
|
573
|
|
574 CHECK_PGCONN (conn);
|
|
575 P = (XPGCONN (conn))->pgconn;
|
|
576 CHECK_LIVE_CONNECTION (P);
|
|
577
|
|
578 return make_int (PQclientEncoding (P));
|
|
579 }
|
|
580
|
|
581 DEFUN ("pq-set-client-encoding", Fpq_set_client_encoding, 2, 2, 0, /*
|
|
582 Set client coding system.
|
|
583 */
|
|
584 (conn, encoding))
|
|
585 {
|
|
586 PGconn *P;
|
|
587 int rc;
|
|
588 char *c_encoding;
|
|
589
|
|
590 CHECK_PGCONN (conn);
|
|
591 CHECK_STRING (encoding);
|
|
592
|
|
593 P = (XPGCONN (conn))->pgconn;
|
|
594 CHECK_LIVE_CONNECTION (P);
|
|
595
|
|
596 TO_EXTERNAL_FORMAT (LISP_STRING, encoding,
|
|
597 C_STRING_ALLOCA, c_encoding, Qnative);
|
|
598
|
|
599 if ((rc = PQsetClientEncoding (P, c_encoding)) < 0)
|
563
|
600 signal_error (Qinvalid_argument, "bad encoding", Qunbound);
|
442
|
601 else
|
|
602 return make_int (rc);
|
|
603 }
|
|
604
|
|
605 #endif
|
|
606 #endif /* HAVE_POSTGRESQLV7 */
|
|
607
|
|
608 /* PQfinish Close the connection to the backend. Also frees memory
|
|
609 used by the PGconn object.
|
|
610 void PQfinish(PGconn *conn)
|
|
611 */
|
|
612 DEFUN ("pq-finish", Fpq_finish, 1, 1, 0, /*
|
|
613 Close the connection to the backend.
|
|
614 */
|
|
615 (conn))
|
|
616 {
|
|
617 PGconn *P;
|
|
618
|
|
619 CHECK_PGCONN (conn);
|
|
620 P = (XPGCONN (conn))->pgconn;
|
|
621 PUKE_IF_NULL (P);
|
|
622
|
|
623 PQfinish (P);
|
|
624 /* #### PQfinish deallocates the PGconn structure, so we now have a
|
|
625 dangling pointer. */
|
|
626 /* Genocided all @'s ... */
|
|
627 (XPGCONN (conn))->pgconn = (PGconn *)NULL; /* You feel DEAD inside */
|
|
628 return Qnil;
|
|
629 }
|
|
630
|
|
631 DEFUN ("pq-clear", Fpq_clear, 1, 1, 0, /*
|
|
632 Forcibly erase a PGresult object.
|
|
633 */
|
|
634 (res))
|
|
635 {
|
|
636 PGresult *R;
|
|
637
|
|
638 CHECK_PGRESULT (res);
|
|
639 R = (XPGRESULT (res))->pgresult;
|
|
640 PUKE_IF_NULL (R);
|
|
641
|
|
642 PQclear (R);
|
|
643 /* Genocided all @'s ... */
|
|
644 (XPGRESULT (res))->pgresult = (PGresult *)NULL; /* You feel DEAD inside */
|
|
645
|
|
646 return Qnil;
|
|
647 }
|
|
648
|
|
649 DEFUN ("pq-is-busy", Fpq_is_busy, 1, 1, 0, /*
|
|
650 Return t if PQgetResult would block waiting for input.
|
|
651 */
|
|
652 (conn))
|
|
653 {
|
|
654 PGconn *P;
|
|
655
|
|
656 CHECK_PGCONN (conn);
|
|
657 P = (XPGCONN (conn))->pgconn;
|
|
658 CHECK_LIVE_CONNECTION (P);
|
|
659
|
|
660 return PQisBusy (P) ? Qt : Qnil;
|
|
661 }
|
|
662
|
|
663 DEFUN ("pq-consume-input", Fpq_consume_input, 1, 1, 0, /*
|
|
664 Consume any available input from the backend.
|
|
665 Returns nil if something bad happened.
|
|
666 */
|
|
667 (conn))
|
|
668 {
|
|
669 PGconn *P;
|
|
670
|
|
671 CHECK_PGCONN (conn);
|
|
672 P = (XPGCONN (conn))->pgconn;
|
|
673 CHECK_LIVE_CONNECTION (P);
|
|
674
|
|
675 return PQconsumeInput (P) ? Qt : Qnil;
|
|
676 }
|
|
677
|
|
678 /* PQreset Reset the communication port with the backend.
|
|
679 void PQreset(PGconn *conn)
|
|
680 */
|
|
681 DEFUN ("pq-reset", Fpq_reset, 1, 1, 0, /*
|
|
682 Reset the connection to the backend.
|
|
683 This function will close the connection to the backend and attempt to
|
|
684 reestablish a new connection to the same postmaster, using all the same
|
|
685 parameters previously used. This may be useful for error recovery if a
|
|
686 working connection is lost.
|
|
687 */
|
|
688 (conn))
|
|
689 {
|
|
690 PGconn *P;
|
|
691
|
|
692 CHECK_PGCONN (conn);
|
|
693 P = (XPGCONN (conn))->pgconn;
|
|
694 PUKE_IF_NULL (P);/* we can resurrect a BAD connection, but not a dead one. */
|
|
695
|
|
696 PQreset (P);
|
|
697
|
|
698 return Qnil;
|
|
699 }
|
|
700
|
|
701 #ifdef HAVE_POSTGRESQLV7
|
|
702 DEFUN ("pq-reset-start", Fpq_reset_start, 1, 1, 0, /*
|
|
703 Reset connection to the backend asynchronously.
|
|
704 */
|
|
705 (conn))
|
|
706 {
|
|
707 PGconn *P;
|
|
708
|
|
709 CHECK_PGCONN (conn);
|
|
710 P = (XPGCONN (conn))->pgconn;
|
|
711 CHECK_LIVE_CONNECTION (P);
|
|
712
|
|
713 if (PQresetStart (P)) return Qt;
|
|
714 {
|
|
715 char *e = PQerrorMessage (P);
|
563
|
716 signal_ferror (Qprocess_error, "libpq: %s", e);
|
442
|
717 }
|
|
718 }
|
|
719
|
|
720 DEFUN ("pq-reset-poll", Fpq_reset_poll, 1, 1, 0, /*
|
|
721 Poll an asynchronous reset for completion.
|
|
722 */
|
|
723 (conn))
|
|
724 {
|
|
725 PGconn *P;
|
|
726 PostgresPollingStatusType polling_status;
|
|
727
|
|
728 CHECK_PGCONN (conn);
|
|
729
|
|
730 P = (XPGCONN (conn))->pgconn;
|
|
731 CHECK_LIVE_CONNECTION (P);
|
|
732
|
|
733 polling_status = PQresetPoll (P);
|
|
734 switch (polling_status)
|
|
735 {
|
|
736 case PGRES_POLLING_FAILED:
|
|
737 /* Something Bad has happened */
|
|
738 {
|
|
739 char *e = PQerrorMessage (P);
|
563
|
740 signal_ferror (Qprocess_error, "libpq: %s", e);
|
442
|
741 }
|
|
742 case PGRES_POLLING_OK:
|
|
743 return Qpgres_polling_ok;
|
|
744 case PGRES_POLLING_READING:
|
|
745 return Qpgres_polling_reading;
|
|
746 case PGRES_POLLING_WRITING:
|
|
747 return Qpgres_polling_writing;
|
|
748 case PGRES_POLLING_ACTIVE:
|
|
749 return Qpgres_polling_active;
|
|
750 default:
|
|
751 /* they've added a new field we don't know about */
|
563
|
752 signal_ferror (Qprocess_error, "Help! Unknown status code %08x from backend!", polling_status);
|
442
|
753 }
|
|
754 }
|
|
755 #endif
|
|
756
|
|
757 DEFUN ("pq-request-cancel", Fpq_request_cancel, 1, 1, 0, /*
|
|
758 Attempt to request cancellation of the current operation.
|
|
759
|
|
760 The return value is t if the cancel request was successfully
|
|
761 dispatched, nil if not (in which case conn->errorMessage is set).
|
|
762 Note: successful dispatch is no guarantee that there will be any effect at
|
|
763 the backend. The application must read the operation result as usual.
|
|
764 */
|
|
765 (conn))
|
|
766 {
|
|
767 PGconn *P;
|
|
768
|
|
769 CHECK_PGCONN (conn);
|
|
770 P = (XPGCONN (conn))->pgconn;
|
|
771 CHECK_LIVE_CONNECTION (P);
|
|
772
|
|
773 return PQrequestCancel (P) ? Qt : Qnil;
|
|
774 }
|
|
775
|
|
776 /* accessor function for the PGconn object */
|
|
777 DEFUN ("pq-pgconn", Fpq_pgconn, 2, 2, 0, /*
|
|
778 Accessor function for the PGconn object.
|
|
779 Currently recognized symbols for the field:
|
|
780 pq::db Database name
|
|
781 pq::user Database user name
|
|
782 pq::pass Database user's password
|
|
783 pq::host Hostname of PostgreSQL backend connected to
|
|
784 pq::port TCP port number of connection
|
|
785 pq::tty Debugging TTY (not used in Emacs)
|
|
786 pq::options Additional backend options
|
|
787 pq::status Connection status (either OK or BAD)
|
|
788 pq::error-message Last error message from the backend
|
|
789 pq::backend-pid Process ID of backend process
|
|
790 */
|
|
791 (conn, field))
|
|
792 {
|
|
793 PGconn *P;
|
|
794
|
|
795 CHECK_PGCONN (conn);
|
|
796 P = (XPGCONN (conn))->pgconn;
|
|
797 PUKE_IF_NULL (P); /* BAD connections still have state to query */
|
|
798
|
|
799 if (EQ(field, Qpqdb))
|
|
800 /* PQdb Returns the database name of the connection.
|
|
801 char *PQdb(PGconn *conn)
|
|
802 */
|
|
803 return build_ext_string (PQdb(P), PG_OS_CODING);
|
|
804 else if (EQ (field, Qpquser))
|
|
805 /* PQuser Returns the user name of the connection.
|
|
806 char *PQuser(PGconn *conn)
|
|
807 */
|
|
808 return build_ext_string (PQuser(P), PG_OS_CODING);
|
|
809 else if (EQ (field, Qpqpass))
|
|
810 /* PQpass Returns the password of the connection.
|
|
811 char *PQpass(PGconn *conn)
|
|
812 */
|
|
813 return build_ext_string (PQpass(P), PG_OS_CODING);
|
|
814 else if (EQ (field, Qpqhost))
|
|
815 /* PQhost Returns the server host name of the connection.
|
|
816 char *PQhost(PGconn *conn)
|
|
817 */
|
|
818 return build_ext_string (PQhost(P), PG_OS_CODING);
|
|
819 else if (EQ (field, Qpqport))
|
|
820 {
|
|
821 char *p;
|
|
822 /* PQport Returns the port of the connection.
|
|
823 char *PQport(PGconn *conn)
|
|
824 */
|
|
825 if ((p = PQport(P)))
|
|
826 return make_int(atoi(p));
|
|
827 else
|
|
828 return make_int(-1);
|
|
829 }
|
|
830 else if (EQ (field, Qpqtty))
|
|
831 /* PQtty Returns the debug tty of the connection.
|
|
832 char *PQtty(PGconn *conn)
|
|
833 */
|
|
834 return build_ext_string (PQtty(P), PG_OS_CODING);
|
|
835 else if (EQ (field, Qpqoptions))
|
|
836 /* PQoptions Returns the backend options used in the connection.
|
|
837 char *PQoptions(PGconn *conn)
|
|
838 */
|
|
839 return build_ext_string (PQoptions(P), PG_OS_CODING);
|
|
840 else if (EQ (field, Qpqstatus))
|
|
841 {
|
|
842 ConnStatusType cst;
|
|
843 /* PQstatus Returns the status of the connection. The status can be
|
|
844 CONNECTION_OK or CONNECTION_BAD.
|
|
845 ConnStatusType PQstatus(PGconn *conn)
|
|
846 */
|
|
847 switch ((cst = PQstatus (P)))
|
|
848 {
|
|
849 case CONNECTION_OK: return Qpg_connection_ok;
|
|
850 case CONNECTION_BAD: return Qpg_connection_bad;
|
|
851 #ifdef HAVE_POSTGRESQLV7
|
|
852 case CONNECTION_STARTED: return Qpg_connection_started;
|
|
853 case CONNECTION_MADE: return Qpg_connection_made;
|
|
854 case CONNECTION_AWAITING_RESPONSE: return Qpg_connection_awaiting_response;
|
|
855 case CONNECTION_AUTH_OK: return Qpg_connection_auth_ok;
|
|
856 case CONNECTION_SETENV: return Qpg_connection_setenv;
|
|
857 #endif /* HAVE_POSTGRESQLV7 */
|
|
858 default:
|
|
859 /* they've added a new field we don't know about */
|
563
|
860 signal_ferror (Qprocess_error, "Help! Unknown connection status code %08x from backend!", cst);
|
442
|
861 }
|
|
862 }
|
|
863 else if (EQ (field, Qpqerrormessage))
|
|
864 /* PQerrorMessage Returns the error message most recently generated
|
|
865 by an operation on the connection.
|
|
866 char *PQerrorMessage(PGconn* conn);
|
|
867 */
|
|
868 return build_ext_string (PQerrorMessage(P), PG_OS_CODING);
|
|
869 else if (EQ (field, Qpqbackendpid))
|
|
870 /* PQbackendPID Returns the process ID of the backend server handling
|
|
871 this connection.
|
|
872 int PQbackendPID(PGconn *conn);
|
|
873 */
|
|
874 return make_int (PQbackendPID(P));
|
|
875 else
|
563
|
876 signal_error (Qinvalid_argument, "bad PGconn accessor", Qunbound);
|
442
|
877 }
|
|
878
|
|
879 /* Query functions */
|
|
880 DEFUN ("pq-exec", Fpq_exec, 2, 2, 0, /*
|
|
881 Submit a query to Postgres and wait for the result.
|
|
882 */
|
|
883 (conn, query))
|
|
884 {
|
|
885 PGconn *P;
|
|
886 Lisp_PGresult *lisp_pgresult;
|
|
887 PGresult *R;
|
|
888 char *c_query;
|
|
889
|
|
890 CHECK_PGCONN (conn);
|
|
891 CHECK_STRING (query);
|
|
892
|
|
893 P = (XPGCONN (conn))->pgconn;
|
|
894 CHECK_LIVE_CONNECTION (P);
|
|
895
|
|
896 TO_EXTERNAL_FORMAT (LISP_STRING, query,
|
|
897 C_STRING_ALLOCA, c_query, Qnative);
|
|
898
|
|
899 R = PQexec (P, c_query);
|
|
900 {
|
|
901 char *tag, buf[BLCKSZ];
|
|
902
|
563
|
903 if (!R) out_of_memory ("query: out of memory", Qunbound);
|
442
|
904 else
|
|
905 switch (PQresultStatus (R))
|
|
906 {
|
|
907 case PGRES_BAD_RESPONSE:
|
|
908 tag = "bad response [%s]";
|
|
909 goto err;
|
|
910 case PGRES_NONFATAL_ERROR:
|
|
911 tag = "non-fatal error [%s]";
|
|
912 goto err;
|
|
913 case PGRES_FATAL_ERROR:
|
|
914 tag = "fatal error [%s]";
|
|
915 err:
|
|
916 strncpy (buf, PQresultErrorMessage (R), sizeof (buf));
|
|
917 buf [sizeof (buf) - 1] = '\0';
|
|
918 PQclear (R);
|
563
|
919 signal_ferror (Qprocess_error, tag, buf);
|
442
|
920 /*NOTREACHED*/
|
|
921 default:
|
|
922 break;
|
|
923 }
|
|
924 }
|
|
925
|
|
926 lisp_pgresult = allocate_pgresult ();
|
|
927 lisp_pgresult->pgresult = R;
|
|
928
|
|
929 return make_pgresult (lisp_pgresult);
|
|
930 }
|
|
931
|
|
932 DEFUN ("pq-send-query", Fpq_send_query, 2, 2, 0, /*
|
|
933 Submit a query to Postgres and don't wait for the result.
|
|
934 Returns: t if successfully submitted
|
|
935 nil if error (conn->errorMessage is set)
|
|
936 */
|
|
937 (conn, query))
|
|
938 {
|
|
939 PGconn *P;
|
|
940 char *c_query;
|
|
941
|
|
942 CHECK_PGCONN (conn);
|
|
943 CHECK_STRING (query);
|
|
944
|
|
945 P = (XPGCONN (conn))->pgconn;
|
|
946 CHECK_LIVE_CONNECTION (P);
|
|
947
|
|
948 TO_EXTERNAL_FORMAT (LISP_STRING, query,
|
|
949 C_STRING_ALLOCA, c_query, Qnative);
|
|
950
|
|
951 if (PQsendQuery (P, c_query)) return Qt;
|
563
|
952 else signal_ferror (Qprocess_error, "async query: %s", PQerrorMessage (P));
|
442
|
953 }
|
|
954
|
|
955 DEFUN ("pq-get-result", Fpq_get_result, 1, 1, 0, /*
|
|
956 Retrieve an asynchronous result from a query.
|
|
957 NIL is returned when no more query work remains.
|
|
958 */
|
|
959 (conn))
|
|
960 {
|
|
961 PGconn *P;
|
|
962 Lisp_PGresult *lisp_pgresult;
|
|
963 PGresult *R;
|
|
964
|
|
965 CHECK_PGCONN (conn);
|
|
966
|
|
967 P = (XPGCONN (conn))->pgconn;
|
|
968 CHECK_LIVE_CONNECTION (P);
|
|
969
|
|
970 R = PQgetResult (P);
|
|
971 if (!R) return Qnil; /* not an error, there's no more data to get */
|
|
972
|
|
973 {
|
|
974 char *tag, buf[BLCKSZ];
|
|
975
|
|
976 switch (PQresultStatus (R))
|
|
977 {
|
|
978 case PGRES_BAD_RESPONSE:
|
|
979 tag = "bad response [%s]";
|
|
980 goto err;
|
|
981 case PGRES_NONFATAL_ERROR:
|
|
982 tag = "non-fatal error [%s]";
|
|
983 goto err;
|
|
984 case PGRES_FATAL_ERROR:
|
|
985 tag = "fatal error [%s]";
|
|
986 err:
|
|
987 strncpy (buf, PQresultErrorMessage (R), sizeof (buf));
|
|
988 buf[sizeof (buf) - 1] = '\0';
|
|
989 PQclear (R);
|
563
|
990 signal_ferror (Qprocess_error, tag, buf);
|
442
|
991 /*NOTREACHED*/
|
|
992 default:
|
|
993 break;
|
|
994 }
|
|
995 }
|
|
996
|
|
997 lisp_pgresult = allocate_pgresult();
|
|
998 lisp_pgresult->pgresult = R;
|
|
999
|
|
1000 return make_pgresult (lisp_pgresult);
|
|
1001 }
|
|
1002
|
|
1003 DEFUN ("pq-result-status", Fpq_result_status, 1, 1, 0, /*
|
|
1004 Return result status of the query.
|
|
1005 */
|
|
1006 (result))
|
|
1007 {
|
|
1008 PGresult *R;
|
|
1009 ExecStatusType est;
|
|
1010
|
|
1011 CHECK_PGRESULT (result);
|
|
1012 R = (XPGRESULT (result))->pgresult;
|
|
1013 PUKE_IF_NULL (R);
|
|
1014
|
|
1015 switch ((est = PQresultStatus (R))) {
|
|
1016 case PGRES_EMPTY_QUERY: return Qpgres_empty_query;
|
|
1017 case PGRES_COMMAND_OK: return Qpgres_command_ok;
|
|
1018 case PGRES_TUPLES_OK: return Qpgres_tuples_ok;
|
|
1019 case PGRES_COPY_OUT: return Qpgres_copy_out;
|
|
1020 case PGRES_COPY_IN: return Qpgres_copy_in;
|
|
1021 case PGRES_BAD_RESPONSE: return Qpgres_bad_response;
|
|
1022 case PGRES_NONFATAL_ERROR: return Qpgres_nonfatal_error;
|
|
1023 case PGRES_FATAL_ERROR: return Qpgres_fatal_error;
|
|
1024 default:
|
|
1025 /* they've added a new field we don't know about */
|
563
|
1026 signal_ferror (Qprocess_error, "Help! Unknown exec status code %08x from backend!", est);
|
442
|
1027 }
|
|
1028 }
|
|
1029
|
|
1030 DEFUN ("pq-res-status", Fpq_res_status, 1, 1, 0, /*
|
|
1031 Return stringified result status of the query.
|
|
1032 */
|
|
1033 (result))
|
|
1034 {
|
|
1035 PGresult *R;
|
|
1036
|
|
1037 CHECK_PGRESULT (result);
|
|
1038 R = (XPGRESULT (result))->pgresult;
|
|
1039 PUKE_IF_NULL (R);
|
|
1040
|
|
1041 return build_ext_string (PQresStatus (PQresultStatus (R)), PG_OS_CODING);
|
|
1042 }
|
|
1043
|
|
1044 /* Sundry PGresult accessor functions */
|
|
1045 DEFUN ("pq-result-error-message", Fpq_result_error_message, 1, 1, 0, /*
|
|
1046 Return last message associated with the query.
|
|
1047 */
|
|
1048 (result))
|
|
1049 {
|
|
1050 PGresult *R;
|
|
1051
|
|
1052 CHECK_PGRESULT (result);
|
|
1053 R = (XPGRESULT (result))->pgresult;
|
|
1054 PUKE_IF_NULL (R);
|
|
1055
|
|
1056 return build_ext_string (PQresultErrorMessage (R), PG_OS_CODING);
|
|
1057 }
|
|
1058
|
|
1059 DEFUN ("pq-ntuples", Fpq_ntuples, 1, 1, 0, /*
|
|
1060 Return the number of tuples (instances) in the query result.
|
|
1061 */
|
|
1062 (result))
|
|
1063 {
|
|
1064 PGresult *R;
|
|
1065
|
|
1066 CHECK_PGRESULT (result);
|
|
1067 R = (XPGRESULT (result))->pgresult;
|
|
1068 PUKE_IF_NULL (R);
|
|
1069
|
|
1070 return make_int (PQntuples (R));
|
|
1071 }
|
|
1072
|
|
1073 DEFUN ("pq-nfields", Fpq_nfields, 1, 1, 0, /*
|
|
1074 Return the number of fields (attributes) in each tuple of the query result.
|
|
1075 */
|
|
1076 (result))
|
|
1077 {
|
|
1078 PGresult *R;
|
|
1079
|
|
1080 CHECK_PGRESULT (result);
|
|
1081 R = (XPGRESULT (result))->pgresult;
|
|
1082 PUKE_IF_NULL (R);
|
|
1083
|
|
1084 return make_int (PQnfields (R));
|
|
1085 }
|
|
1086
|
|
1087 DEFUN ("pq-binary-tuples", Fpq_binary_tuples, 1, 1, 0, /*
|
|
1088 Return t if the query result contains binary data, nil otherwise.
|
|
1089 */
|
|
1090 (result))
|
|
1091 {
|
|
1092 PGresult *R;
|
|
1093
|
|
1094 CHECK_PGRESULT (result);
|
|
1095 R = (XPGRESULT (result))->pgresult;
|
|
1096 PUKE_IF_NULL (R);
|
|
1097
|
|
1098 return (PQbinaryTuples (R)) ? Qt : Qnil;
|
|
1099 }
|
|
1100
|
|
1101 DEFUN ("pq-fname", Fpq_fname, 2, 2, 0, /*
|
|
1102 Return the field (attribute) name associated with the given field index.
|
|
1103 Field indices start at 0.
|
|
1104 */
|
|
1105 (result, field_index))
|
|
1106 {
|
|
1107 PGresult *R;
|
|
1108
|
|
1109 CHECK_PGRESULT (result);
|
|
1110 CHECK_INT (field_index);
|
|
1111 R = (XPGRESULT (result))->pgresult;
|
|
1112 PUKE_IF_NULL (R);
|
|
1113
|
|
1114 return build_ext_string (PQfname (R, XINT (field_index)), PG_OS_CODING);
|
|
1115 }
|
|
1116
|
|
1117 DEFUN ("pq-fnumber", Fpq_fnumber, 2, 2, 0, /*
|
|
1118 Return the number of fields (attributes) in each tuple of the query result.
|
|
1119 */
|
|
1120 (result, field_name))
|
|
1121 {
|
|
1122 PGresult *R;
|
|
1123 char *c_field_name;
|
|
1124
|
|
1125 CHECK_PGRESULT (result);
|
|
1126 CHECK_STRING (field_name);
|
|
1127 R = (XPGRESULT (result))->pgresult;
|
|
1128 PUKE_IF_NULL (R);
|
|
1129
|
|
1130 TO_EXTERNAL_FORMAT (LISP_STRING, field_name,
|
|
1131 C_STRING_ALLOCA, c_field_name, Qnative);
|
|
1132
|
|
1133 return make_int (PQfnumber (R, c_field_name));
|
|
1134 }
|
|
1135
|
|
1136 DEFUN ("pq-ftype", Fpq_ftype, 2, 2, 0, /*
|
|
1137 Return the field type associated with the given field index.
|
|
1138 The integer returned is the internal coding of the type. Field indices
|
|
1139 start at 0.
|
|
1140 */
|
|
1141 (result, field_num))
|
|
1142 {
|
|
1143 PGresult *R;
|
|
1144
|
|
1145 CHECK_PGRESULT (result);
|
|
1146 CHECK_INT (field_num);
|
|
1147 R = (XPGRESULT (result))->pgresult;
|
|
1148 PUKE_IF_NULL (R);
|
|
1149
|
|
1150 return make_int (PQftype (R, XINT (field_num)));
|
|
1151 }
|
|
1152
|
|
1153 DEFUN ("pq-fsize", Fpq_fsize, 2, 2, 0, /*
|
|
1154 Return the field size in bytes associated with the given field index.
|
|
1155 Field indices start at 0.
|
|
1156 */
|
|
1157 (result, field_index))
|
|
1158 {
|
|
1159 PGresult *R;
|
|
1160
|
|
1161 CHECK_PGRESULT (result);
|
|
1162 CHECK_INT (field_index);
|
|
1163 R = (XPGRESULT (result))->pgresult;
|
|
1164 PUKE_IF_NULL (R);
|
|
1165
|
|
1166 return make_int (PQftype (R, XINT (field_index)));
|
|
1167 }
|
|
1168
|
|
1169 DEFUN ("pq-fmod", Fpq_fmod, 2, 2, 0, /*
|
|
1170 Return the type modifier associated with a field.
|
|
1171 Field indices start at 0.
|
|
1172 */
|
|
1173 (result, field_index))
|
|
1174 {
|
|
1175 PGresult *R;
|
|
1176
|
|
1177 CHECK_PGRESULT (result);
|
|
1178 CHECK_INT (field_index);
|
|
1179 R = (XPGRESULT (result))->pgresult;
|
|
1180 PUKE_IF_NULL (R);
|
|
1181
|
|
1182 return make_int (PQfmod (R, XINT (field_index)));
|
|
1183 }
|
|
1184
|
|
1185 DEFUN ("pq-get-value", Fpq_get_value, 3, 3, 0, /*
|
|
1186 Return a single field (attribute) value of one tuple of a PGresult.
|
|
1187 Tuple and field indices start at 0.
|
|
1188 */
|
|
1189 (result, tup_num, field_num))
|
|
1190 {
|
|
1191 PGresult *R;
|
|
1192
|
|
1193 CHECK_PGRESULT (result);
|
|
1194 CHECK_INT (tup_num);
|
|
1195 CHECK_INT (field_num);
|
|
1196 R = (XPGRESULT (result))->pgresult;
|
|
1197 PUKE_IF_NULL (R);
|
|
1198
|
|
1199 return build_ext_string (PQgetvalue (R, XINT (tup_num), XINT (field_num)),
|
|
1200 PG_OS_CODING);
|
|
1201 }
|
|
1202
|
|
1203 DEFUN ("pq-get-length", Fpq_get_length, 3, 3, 0, /*
|
|
1204 Returns the length of a field value in bytes.
|
|
1205 If result is binary, i.e. a result of a binary portal, then the
|
|
1206 length returned does NOT include the size field of the varlena. (The
|
|
1207 data returned by PQgetvalue doesn't either.)
|
|
1208 */
|
|
1209 (result, tup_num, field_num))
|
|
1210 {
|
|
1211 PGresult *R;
|
|
1212
|
|
1213 CHECK_PGRESULT (result);
|
|
1214 CHECK_INT (tup_num);
|
|
1215 CHECK_INT (field_num);
|
|
1216 R = (XPGRESULT (result))->pgresult;
|
|
1217 PUKE_IF_NULL (R);
|
|
1218
|
|
1219 return make_int (PQgetlength (R, XINT (tup_num), XINT (field_num)));
|
|
1220 }
|
|
1221
|
|
1222 DEFUN ("pq-get-is-null", Fpq_get_is_null, 3, 3, 0, /*
|
|
1223 Returns the null status of a field value.
|
|
1224 */
|
|
1225 (result, tup_num, field_num))
|
|
1226 {
|
|
1227 PGresult *R;
|
|
1228
|
|
1229 CHECK_PGRESULT (result);
|
|
1230 CHECK_INT (tup_num);
|
|
1231 CHECK_INT (field_num);
|
|
1232 R = (XPGRESULT (result))->pgresult;
|
|
1233 PUKE_IF_NULL (R);
|
|
1234
|
|
1235 return PQgetisnull (R, XINT (tup_num), XINT (field_num)) ? Qt : Qnil;
|
|
1236 }
|
|
1237
|
|
1238 DEFUN ("pq-cmd-status", Fpq_cmd_status, 1, 1, 0, /*
|
|
1239 Returns the command status string from the SQL command that generated the result.
|
|
1240 */
|
|
1241 (result))
|
|
1242 {
|
|
1243 PGresult *R;
|
|
1244
|
|
1245 CHECK_PGRESULT (result);
|
|
1246 R = (XPGRESULT (result))->pgresult;
|
|
1247 PUKE_IF_NULL (R);
|
|
1248
|
|
1249 return build_ext_string (PQcmdStatus (R), PG_OS_CODING);
|
|
1250 }
|
|
1251
|
|
1252 DEFUN ("pq-cmd-tuples", Fpq_cmd_tuples, 1, 1, 0, /*
|
|
1253 Returns the number of rows affected by the SQL command.
|
|
1254 */
|
|
1255 (result))
|
|
1256 {
|
|
1257 PGresult *R;
|
|
1258
|
|
1259 CHECK_PGRESULT (result);
|
|
1260 R = (XPGRESULT (result))->pgresult;
|
|
1261 PUKE_IF_NULL (R);
|
|
1262
|
|
1263 return build_ext_string (PQcmdTuples (R), PG_OS_CODING);
|
|
1264 }
|
|
1265
|
|
1266 DEFUN ("pq-oid-value", Fpq_oid_value, 1, 1, 0, /*
|
|
1267 Returns the object id of the tuple inserted.
|
|
1268 */
|
|
1269 (result))
|
|
1270 {
|
|
1271 PGresult *R;
|
|
1272
|
|
1273 CHECK_PGRESULT (result);
|
|
1274 R = (XPGRESULT (result))->pgresult;
|
|
1275 PUKE_IF_NULL (R);
|
|
1276
|
|
1277 #ifdef HAVE_POSTGRESQLV7
|
|
1278 return make_int (PQoidValue (R));
|
|
1279 #else
|
|
1280 /* Use the old interface */
|
|
1281 return make_int (atoi (PQoidStatus (R)));
|
|
1282 #endif
|
|
1283 }
|
|
1284
|
|
1285 #ifdef HAVE_POSTGRESQLV7
|
|
1286 DEFUN ("pq-set-nonblocking", Fpq_set_nonblocking, 2, 2, 0, /*
|
|
1287 Sets the PGconn's database connection non-blocking if the arg is TRUE
|
|
1288 or makes it non-blocking if the arg is FALSE, this will not protect
|
|
1289 you from PQexec(), you'll only be safe when using the non-blocking API.
|
|
1290
|
|
1291 Needs to be called only on a connected database connection.
|
|
1292 */
|
|
1293 (conn, arg))
|
|
1294 {
|
|
1295 PGconn *P;
|
|
1296
|
|
1297 CHECK_PGCONN (conn);
|
|
1298 P = (XPGCONN (conn))->pgconn;
|
|
1299 CHECK_LIVE_CONNECTION (P);
|
|
1300
|
|
1301 return make_int (PQsetnonblocking (P, !NILP (arg)));
|
|
1302 }
|
|
1303
|
|
1304 DEFUN ("pq-is-nonblocking", Fpq_is_nonblocking, 1, 1, 0, /*
|
|
1305 Return the blocking status of the database connection.
|
|
1306 */
|
|
1307 (conn))
|
|
1308 {
|
|
1309 PGconn *P;
|
|
1310
|
|
1311 CHECK_PGCONN (conn);
|
|
1312 P = (XPGCONN (conn))->pgconn;
|
|
1313 CHECK_LIVE_CONNECTION (P);
|
|
1314
|
|
1315 return PQisnonblocking (P) ? Qt : Qnil;
|
|
1316 }
|
|
1317
|
|
1318 DEFUN ("pq-flush", Fpq_flush, 1, 1, 0, /*
|
|
1319 Force the write buffer to be written (or at least try).
|
|
1320 */
|
|
1321 (conn))
|
|
1322 {
|
|
1323 PGconn *P;
|
|
1324
|
|
1325 CHECK_PGCONN (conn);
|
|
1326 P = (XPGCONN (conn))->pgconn;
|
|
1327 CHECK_LIVE_CONNECTION (P);
|
|
1328
|
|
1329 return make_int (PQflush (P));
|
|
1330 }
|
|
1331 #endif
|
|
1332
|
|
1333 DEFUN ("pq-notifies", Fpq_notifies, 1, 1, 0, /*
|
|
1334 Return the latest async notification that has not yet been handled.
|
|
1335 If there has been a notification, then a list of two elements will be returned.
|
|
1336 The first element contains the relation name being notified, the second
|
|
1337 element contains the backend process ID number. nil is returned if there
|
|
1338 aren't any notifications to process.
|
|
1339 */
|
|
1340 (conn))
|
|
1341 {
|
|
1342 /* This function cannot GC */
|
|
1343 PGconn *P;
|
|
1344 PGnotify *PGN;
|
|
1345
|
|
1346 CHECK_PGCONN (conn);
|
|
1347 P = (XPGCONN (conn))->pgconn;
|
|
1348 CHECK_LIVE_CONNECTION (P);
|
|
1349
|
|
1350 PGN = PQnotifies (P);
|
|
1351 if (!PGN)
|
|
1352 return Qnil;
|
|
1353 else
|
|
1354 {
|
|
1355 Lisp_Object temp;
|
|
1356
|
|
1357 temp = list2 (build_ext_string (PGN->relname, PG_OS_CODING), make_int (PGN->be_pid));
|
|
1358 free ((void *)PGN);
|
|
1359 return temp;
|
|
1360 }
|
|
1361 }
|
|
1362
|
|
1363 #if defined (HAVE_POSTGRESQLV7) && defined(MULE)
|
|
1364 DEFUN ("pq-env-2-encoding", Fpq_env_2_encoding, 0, 0, 0, /*
|
|
1365 Get encoding id from environment variable PGCLIENTENCODING.
|
|
1366 */
|
|
1367 ())
|
|
1368 {
|
|
1369 return make_int (PQenv2encoding ());
|
|
1370 }
|
|
1371 #endif /* MULE */
|
|
1372
|
|
1373 DEFUN ("pq-lo-import", Fpq_lo_import, 2, 2, 0, /*
|
|
1374 */
|
|
1375 (conn, filename))
|
|
1376 {
|
|
1377 PGconn *P;
|
|
1378 char *c_filename;
|
|
1379
|
|
1380 CHECK_PGCONN (conn);
|
|
1381 CHECK_STRING (filename);
|
|
1382
|
|
1383 P = (XPGCONN (conn))->pgconn;
|
|
1384 CHECK_LIVE_CONNECTION (P);
|
|
1385
|
|
1386 TO_EXTERNAL_FORMAT (LISP_STRING, filename,
|
|
1387 C_STRING_ALLOCA, c_filename,
|
|
1388 Qfile_name);
|
|
1389
|
|
1390 return make_int ((int)lo_import (P, c_filename));
|
|
1391 }
|
|
1392
|
|
1393 DEFUN ("pq-lo-export", Fpq_lo_export, 3, 3, 0, /*
|
|
1394 */
|
|
1395 (conn, oid, filename))
|
|
1396 {
|
|
1397 PGconn *P;
|
|
1398 char *c_filename;
|
|
1399
|
|
1400 CHECK_PGCONN (conn);
|
|
1401 CHECK_INT (oid);
|
|
1402 CHECK_STRING (filename);
|
|
1403
|
|
1404 P = (XPGCONN (conn))->pgconn;
|
|
1405 CHECK_LIVE_CONNECTION (P);
|
|
1406
|
|
1407 TO_EXTERNAL_FORMAT (LISP_STRING, filename,
|
|
1408 C_STRING_ALLOCA, c_filename, Qfile_name);
|
|
1409
|
|
1410 return make_int ((int)lo_export (P, XINT (oid), c_filename));
|
|
1411 }
|
|
1412
|
|
1413 DEFUN ("pq-make-empty-pgresult", Fpq_make_empty_pgresult, 2, 2, 0, /*
|
|
1414 Make an empty PGresult object with the given status.
|
|
1415 */
|
|
1416 (conn, status))
|
|
1417 {
|
|
1418 PGconn *P;
|
|
1419 Lisp_PGresult *lpgr;
|
|
1420 PGresult *R;
|
|
1421 ExecStatusType est;
|
|
1422
|
|
1423 CHECK_PGCONN (conn);
|
|
1424 P = (XPGCONN (conn))->pgconn;
|
|
1425 CHECK_LIVE_CONNECTION (P); /* needed here? */
|
|
1426
|
|
1427 if (EQ (status, Qpgres_empty_query)) est = PGRES_EMPTY_QUERY;
|
|
1428 else if (EQ (status, Qpgres_command_ok)) est = PGRES_COMMAND_OK;
|
|
1429 else if (EQ (status, Qpgres_tuples_ok)) est = PGRES_TUPLES_OK;
|
|
1430 else if (EQ (status, Qpgres_copy_out)) est = PGRES_COPY_OUT;
|
|
1431 else if (EQ (status, Qpgres_copy_in)) est = PGRES_COPY_IN;
|
|
1432 else if (EQ (status, Qpgres_bad_response)) est = PGRES_BAD_RESPONSE;
|
|
1433 else if (EQ (status, Qpgres_nonfatal_error)) est = PGRES_NONFATAL_ERROR;
|
|
1434 else if (EQ (status, Qpgres_fatal_error)) est = PGRES_FATAL_ERROR;
|
563
|
1435 else invalid_constant ("bad status symbol", status);
|
442
|
1436
|
|
1437 R = PQmakeEmptyPGresult (P, est);
|
563
|
1438 if (!R) out_of_memory (0, Qunbound);
|
442
|
1439
|
|
1440 lpgr = allocate_pgresult ();
|
|
1441 lpgr->pgresult = R;
|
|
1442
|
|
1443 return make_pgresult (lpgr);
|
|
1444 }
|
|
1445
|
|
1446 DEFUN ("pq-get-line", Fpq_get_line, 1, 1, 0, /*
|
|
1447 Retrieve a line from server in copy in operation.
|
|
1448 The return value is a dotted pair where the cons cell is an integer code:
|
|
1449 -1: Copying is complete
|
|
1450 0: A record is complete
|
|
1451 1: A record is incomplete, it will be continued in the next `pq-get-line'
|
|
1452 operation.
|
|
1453 and the cdr cell is returned string data.
|
|
1454
|
|
1455 The copy operation is complete when the value `\.' (backslash dot) is
|
|
1456 returned.
|
|
1457 */
|
|
1458 (conn))
|
|
1459 {
|
|
1460 char buffer[BLCKSZ]; /* size of a Postgres disk block */
|
|
1461 PGconn *P;
|
|
1462 int ret;
|
|
1463
|
|
1464 CHECK_PGCONN (conn);
|
|
1465 P = (XPGCONN (conn))->pgconn;
|
|
1466 CHECK_LIVE_CONNECTION (P);
|
|
1467
|
|
1468 ret = PQgetline (P, buffer, sizeof (buffer));
|
|
1469
|
|
1470 return Fcons (make_int (ret), build_ext_string (buffer, PG_OS_CODING));
|
|
1471 }
|
|
1472
|
|
1473 DEFUN ("pq-put-line", Fpq_put_line, 2, 2, 0, /*
|
|
1474 Send a line to the server in copy out operation.
|
|
1475
|
|
1476 Returns t if the operation succeeded, nil otherwise.
|
|
1477 */
|
|
1478 (conn, string))
|
|
1479 {
|
|
1480 PGconn *P;
|
|
1481 char *c_string;
|
|
1482
|
|
1483 CHECK_PGCONN (conn);
|
|
1484 CHECK_STRING (string);
|
|
1485
|
|
1486 P = (XPGCONN (conn))->pgconn;
|
|
1487 CHECK_LIVE_CONNECTION (P);
|
|
1488 TO_EXTERNAL_FORMAT (LISP_STRING, string,
|
|
1489 C_STRING_ALLOCA, c_string, Qnative);
|
|
1490
|
|
1491 return !PQputline (P, c_string) ? Qt : Qnil;
|
|
1492 }
|
|
1493
|
|
1494 DEFUN ("pq-get-line-async", Fpq_get_line_async, 1, 1, 0, /*
|
|
1495 Get a line from the server in copy in operation asynchronously.
|
|
1496
|
|
1497 This routine is for applications that want to do "COPY <rel> to stdout"
|
|
1498 asynchronously, that is without blocking. Having issued the COPY command
|
|
1499 and gotten a PGRES_COPY_OUT response, the app should call PQconsumeInput
|
|
1500 and this routine until the end-of-data signal is detected. Unlike
|
|
1501 PQgetline, this routine takes responsibility for detecting end-of-data.
|
|
1502
|
|
1503 On each call, PQgetlineAsync will return data if a complete newline-
|
|
1504 terminated data line is available in libpq's input buffer, or if the
|
|
1505 incoming data line is too long to fit in the buffer offered by the caller.
|
|
1506 Otherwise, no data is returned until the rest of the line arrives.
|
|
1507
|
|
1508 If -1 is returned, the end-of-data signal has been recognized (and removed
|
|
1509 from libpq's input buffer). The caller *must* next call PQendcopy and
|
|
1510 then return to normal processing.
|
|
1511
|
|
1512 RETURNS:
|
|
1513 -1 if the end-of-copy-data marker has been recognized
|
|
1514 0 if no data is available
|
|
1515 >0 the number of bytes returned.
|
|
1516 The data returned will not extend beyond a newline character. If possible
|
|
1517 a whole line will be returned at one time. But if the buffer offered by
|
|
1518 the caller is too small to hold a line sent by the backend, then a partial
|
|
1519 data line will be returned. This can be detected by testing whether the
|
|
1520 last returned byte is '\n' or not.
|
|
1521 The returned string is *not* null-terminated.
|
|
1522 */
|
|
1523 (conn))
|
|
1524 {
|
|
1525 PGconn *P;
|
|
1526 char buffer[BLCKSZ];
|
|
1527 int ret;
|
|
1528
|
|
1529 CHECK_PGCONN (conn);
|
|
1530
|
|
1531 P = (XPGCONN (conn))->pgconn;
|
|
1532 CHECK_LIVE_CONNECTION (P);
|
|
1533
|
|
1534 ret = PQgetlineAsync (P, buffer, sizeof (buffer));
|
|
1535
|
|
1536 if (ret == -1) return Qt; /* done! */
|
|
1537 else if (!ret) return Qnil; /* no data yet */
|
|
1538 else return Fcons (make_int (ret),
|
|
1539 make_ext_string ((Extbyte *) buffer, ret, PG_OS_CODING));
|
|
1540 }
|
|
1541
|
|
1542 DEFUN ("pq-put-nbytes", Fpq_put_nbytes, 2, 2, 0, /*
|
|
1543 Asynchronous copy out.
|
|
1544 */
|
|
1545 (conn, data))
|
|
1546 {
|
|
1547 /* NULs are not allowed. I don't think this matters at this time. */
|
|
1548 PGconn *P;
|
|
1549 char *c_data;
|
|
1550
|
|
1551 CHECK_PGCONN (conn);
|
|
1552 CHECK_STRING (data);
|
|
1553
|
|
1554 P = (XPGCONN (conn))->pgconn;
|
|
1555 CHECK_LIVE_CONNECTION (P);
|
|
1556 TO_EXTERNAL_FORMAT (LISP_STRING, data,
|
|
1557 C_STRING_ALLOCA, c_data, Qnative);
|
|
1558
|
|
1559 return !PQputnbytes (P, c_data, strlen (c_data)) ? Qt : Qnil;
|
|
1560 }
|
|
1561
|
|
1562 DEFUN ("pq-end-copy", Fpq_end_copy, 1, 1, 0, /*
|
|
1563 End a copying operation.
|
|
1564 */
|
|
1565 (conn))
|
|
1566 {
|
|
1567 PGconn *P;
|
|
1568
|
|
1569 CHECK_PGCONN (conn);
|
|
1570 P = (XPGCONN (conn))->pgconn;
|
|
1571 CHECK_LIVE_CONNECTION (P);
|
|
1572
|
|
1573 return PQendcopy (P) ? Qt : Qnil;
|
|
1574 }
|
|
1575
|
|
1576 void
|
|
1577 syms_of_postgresql(void)
|
|
1578 {
|
|
1579 #ifndef RUNNING_XEMACS_21_1
|
|
1580 INIT_LRECORD_IMPLEMENTATION (pgconn);
|
|
1581 INIT_LRECORD_IMPLEMENTATION (pgresult);
|
|
1582 #endif
|
563
|
1583 DEFSYMBOL (Qpostgresql);
|
442
|
1584
|
|
1585 /* opaque exported types */
|
563
|
1586 DEFSYMBOL (Qpgconnp);
|
|
1587 DEFSYMBOL (Qpgresultp);
|
442
|
1588
|
|
1589 /* connection status types */
|
|
1590 defsymbol (&Qpg_connection_ok, "pg::connection-ok");
|
|
1591 defsymbol (&Qpg_connection_bad, "pg::connection-bad");
|
|
1592 defsymbol (&Qpg_connection_started, "pg::connection-started");
|
|
1593 defsymbol (&Qpg_connection_made, "pg::connection-made");
|
|
1594 defsymbol (&Qpg_connection_awaiting_response, "pg::connection-awaiting-response");
|
|
1595 defsymbol (&Qpg_connection_auth_ok, "pg::connection-auth-ok");
|
|
1596 defsymbol (&Qpg_connection_setenv, "pg::connection-setenv");
|
|
1597
|
|
1598 /* Fields of PGconn */
|
|
1599 defsymbol (&Qpqdb, "pq::db");
|
|
1600 defsymbol (&Qpquser, "pq::user");
|
|
1601 defsymbol (&Qpqpass, "pq::pass");
|
|
1602 defsymbol (&Qpqhost, "pq::host");
|
|
1603 defsymbol (&Qpqport, "pq::port");
|
|
1604 defsymbol (&Qpqtty, "pq::tty");
|
|
1605 defsymbol (&Qpqoptions, "pq::options");
|
|
1606 defsymbol (&Qpqstatus, "pq::status");
|
|
1607 defsymbol (&Qpqerrormessage, "pq::error-message");
|
|
1608 defsymbol (&Qpqbackendpid, "pq::backend-pid");
|
|
1609
|
|
1610 /* Query status results */
|
|
1611 defsymbol (&Qpgres_empty_query, "pgres::empty-query");
|
|
1612 defsymbol (&Qpgres_command_ok, "pgres::command-ok");
|
|
1613 defsymbol (&Qpgres_tuples_ok, "pgres::tuples-ok");
|
|
1614 defsymbol (&Qpgres_copy_out, "pgres::copy-out");
|
|
1615 defsymbol (&Qpgres_copy_in, "pgres::copy-in");
|
|
1616 defsymbol (&Qpgres_bad_response, "pgres::bad-response");
|
|
1617 defsymbol (&Qpgres_nonfatal_error, "pgres::nonfatal-error");
|
|
1618 defsymbol (&Qpgres_fatal_error, "pgres::fatal-error");
|
|
1619
|
|
1620 /* Poll status results */
|
|
1621 defsymbol (&Qpgres_polling_failed, "pgres::polling-failed");
|
|
1622 defsymbol (&Qpgres_polling_reading, "pgres::polling-reading");
|
|
1623 defsymbol (&Qpgres_polling_writing, "pgres::polling-writing");
|
|
1624 defsymbol (&Qpgres_polling_ok, "pgres::polling-ok");
|
|
1625 defsymbol (&Qpgres_polling_active, "pgres::polling-active");
|
|
1626
|
|
1627 #ifdef HAVE_POSTGRESQLV7
|
|
1628 DEFSUBR (Fpq_connect_start);
|
|
1629 DEFSUBR (Fpq_connect_poll);
|
|
1630 #ifdef MULE
|
|
1631 DEFSUBR (Fpq_client_encoding);
|
|
1632 DEFSUBR (Fpq_set_client_encoding);
|
|
1633 #endif /* MULE */
|
|
1634 #endif /* HAVE_POSTGRESQLV7 */
|
|
1635 DEFSUBR (Fpq_conn_defaults);
|
|
1636 DEFSUBR (Fpq_connectdb);
|
|
1637 DEFSUBR (Fpq_finish);
|
|
1638 DEFSUBR (Fpq_clear);
|
|
1639 DEFSUBR (Fpq_is_busy);
|
|
1640 DEFSUBR (Fpq_consume_input);
|
|
1641
|
|
1642 DEFSUBR (Fpq_reset);
|
|
1643 #ifdef HAVE_POSTGRESQLV7
|
|
1644 DEFSUBR (Fpq_reset_start);
|
|
1645 DEFSUBR (Fpq_reset_poll);
|
|
1646 #endif
|
|
1647 DEFSUBR (Fpq_request_cancel);
|
|
1648 DEFSUBR (Fpq_pgconn);
|
|
1649
|
|
1650 DEFSUBR (Fpq_exec);
|
|
1651 DEFSUBR (Fpq_send_query);
|
|
1652 DEFSUBR (Fpq_get_result);
|
|
1653 DEFSUBR (Fpq_result_status);
|
|
1654 DEFSUBR (Fpq_res_status);
|
|
1655 DEFSUBR (Fpq_result_error_message);
|
|
1656 DEFSUBR (Fpq_ntuples);
|
|
1657 DEFSUBR (Fpq_nfields);
|
|
1658 DEFSUBR (Fpq_binary_tuples);
|
|
1659 DEFSUBR (Fpq_fname);
|
|
1660 DEFSUBR (Fpq_fnumber);
|
|
1661 DEFSUBR (Fpq_ftype);
|
|
1662 DEFSUBR (Fpq_fsize);
|
|
1663 DEFSUBR (Fpq_fmod);
|
|
1664 /***/
|
|
1665 DEFSUBR (Fpq_get_value);
|
|
1666 DEFSUBR (Fpq_get_length);
|
|
1667 DEFSUBR (Fpq_get_is_null);
|
|
1668 DEFSUBR (Fpq_cmd_status);
|
|
1669 DEFSUBR (Fpq_cmd_tuples);
|
|
1670 DEFSUBR (Fpq_oid_value);
|
|
1671
|
|
1672 #ifdef HAVE_POSTGRESQLV7
|
|
1673 DEFSUBR (Fpq_set_nonblocking);
|
|
1674 DEFSUBR (Fpq_is_nonblocking);
|
|
1675 DEFSUBR (Fpq_flush);
|
|
1676 #endif
|
|
1677 DEFSUBR (Fpq_notifies);
|
|
1678
|
|
1679 #if defined (HAVE_POSTGRESQLV7) && defined(MULE)
|
|
1680 DEFSUBR (Fpq_env_2_encoding);
|
|
1681 #endif
|
|
1682
|
|
1683 DEFSUBR (Fpq_lo_import);
|
|
1684 DEFSUBR (Fpq_lo_export);
|
|
1685
|
|
1686 DEFSUBR (Fpq_make_empty_pgresult);
|
|
1687
|
|
1688 /* copy in/out functions */
|
|
1689 DEFSUBR (Fpq_get_line);
|
|
1690 DEFSUBR (Fpq_put_line);
|
|
1691 DEFSUBR (Fpq_get_line_async);
|
|
1692 DEFSUBR (Fpq_put_nbytes);
|
|
1693 DEFSUBR (Fpq_end_copy);
|
|
1694 }
|
|
1695
|
|
1696 void
|
|
1697 vars_of_postgresql(void)
|
|
1698 {
|
|
1699 Fprovide (Qpostgresql);
|
|
1700 #ifdef HAVE_POSTGRESQLV7
|
|
1701 Fprovide (intern ("postgresqlv7"));
|
|
1702 #endif
|
|
1703 #ifndef RUNNING_XEMACS_21_1
|
|
1704 Vpg_coding_system = Qnative;
|
|
1705 DEFVAR_LISP ("pg-coding-system", &Vpg_coding_system /*
|
|
1706 Default Postgres client coding system.
|
|
1707 */ );
|
|
1708 #endif
|
|
1709
|
446
|
1710 DEFVAR_LISP ("pg:host", &VXPGHOST /*
|
|
1711 Default PostgreSQL server name.
|
|
1712 If not set, the server running on the local host is used. The
|
|
1713 initial value is set from the PGHOST environment variable.
|
|
1714 */ );
|
|
1715
|
|
1716 DEFVAR_LISP ("pg:user", &VXPGUSER /*
|
|
1717 Default PostgreSQL user name.
|
|
1718 This value is used when connecting to a database for authentication.
|
|
1719 The initial value is set from the PGUSER environment variable.
|
|
1720 */ );
|
|
1721
|
|
1722 DEFVAR_LISP ("pg:options", &VXPGOPTIONS /*
|
|
1723 Default PostgreSQL user name.
|
|
1724 This value is used when connecting to a database for authentication.
|
|
1725 The initial value is set from the PGUSER environment variable.
|
|
1726 */ );
|
|
1727
|
|
1728 DEFVAR_LISP ("pg:port", &VXPGPORT /*
|
|
1729 Default port to connect to PostgreSQL backend.
|
|
1730 This value is used when connecting to a database.
|
|
1731 The initial value is set from the PGPORT environment variable.
|
|
1732 */ );
|
|
1733
|
|
1734 DEFVAR_LISP ("pg:tty", &VXPGTTY /*
|
|
1735 Default debugging TTY.
|
|
1736 There is no useful setting of this variable in the XEmacs Lisp API.
|
|
1737 The initial value is set from the PGTTY environment variable.
|
|
1738 */ );
|
|
1739
|
|
1740 DEFVAR_LISP ("pg:database", &VXPGDATABASE /*
|
|
1741 Default database to connect to.
|
|
1742 The initial value is set from the PGDATABASE environment variable.
|
|
1743 */ );
|
|
1744
|
|
1745 DEFVAR_LISP ("pg:realm", &VXPGREALM /*
|
|
1746 Default kerberos realm to use for authentication.
|
|
1747 The initial value is set from the PGREALM environment variable.
|
|
1748 */ );
|
|
1749
|
|
1750 #ifdef MULE
|
|
1751 /* It's not clear whether this is any use. My intent is to
|
|
1752 autodetect the coding system from the database. */
|
|
1753 DEFVAR_LISP ("pg:client-encoding", &VXPGCLIENTENCODING /*
|
|
1754 Default client encoding to use.
|
|
1755 The initial value is set from the PGCLIENTENCODING environment variable.
|
|
1756 */ );
|
|
1757 #endif
|
|
1758
|
|
1759 #if !defined(HAVE_POSTGRESQLV7)
|
|
1760 DEFVAR_LISP ("pg:authtype", &VXPGAUTHTYPE /*
|
|
1761 Default authentication to use.
|
|
1762 The initial value is set from the PGAUTHTYPE environment variable.
|
|
1763
|
|
1764 WARNING: This variable has gone away in versions of PostgreSQL newer
|
|
1765 than 6.5.
|
|
1766 */ );
|
|
1767 #endif
|
|
1768
|
|
1769 DEFVAR_LISP ("pg:geqo", &VXPGGEQO /*
|
|
1770 Genetic Query Optimizer options.
|
|
1771 The initial value is set from the PGGEQO environment variable.
|
|
1772 */ );
|
|
1773
|
|
1774 DEFVAR_LISP ("pg:cost-index", &VXPGCOSTINDEX /*
|
|
1775 Default cost index options.
|
|
1776 The initial value is set from the PGCOSTINDEX environment variable.
|
|
1777 */ );
|
|
1778
|
|
1779 DEFVAR_LISP ("pg:cost-heap", &VXPGCOSTHEAP /*
|
|
1780 Default cost heap options.
|
|
1781 The initial value is set from the PGCOSTHEAP environment variable.
|
|
1782 */ );
|
|
1783
|
|
1784 DEFVAR_LISP ("pg:tz", &VXPGTZ /*
|
|
1785 Default timezone to use.
|
|
1786 The initial value is set from the PGTZ environment variable.
|
|
1787 */ );
|
|
1788
|
|
1789 DEFVAR_LISP ("pg:date-style", &VXPGDATESTYLE /*
|
|
1790 Default date style to use.
|
|
1791 The initial value is set from the PGDATESTYLE environment variable.
|
|
1792 */ );
|
|
1793 }
|
|
1794
|
|
1795 /* These initializations should not be done at dump-time. */
|
|
1796 void
|
771
|
1797 init_postgresql_from_environment (void)
|
446
|
1798 {
|
867
|
1799 Ibyte *p;
|
442
|
1800
|
771
|
1801 #define FROB(envvar, var) \
|
|
1802 if ((p = egetenv (envvar))) \
|
|
1803 var = build_intstring (p); \
|
|
1804 else \
|
|
1805 var = Qnil
|
442
|
1806
|
771
|
1807 if (initialized)
|
442
|
1808 {
|
771
|
1809 FROB ("PGHOST", VXPGHOST);
|
|
1810 FROB ("PGUSER", VXPGUSER);
|
|
1811 FROB ("PGOPTIONS", VXPGOPTIONS);
|
442
|
1812
|
771
|
1813 if ((p = egetenv ("PGPORT")))
|
|
1814 VXPGPORT = make_int (atoi ((char *) p));
|
|
1815 else
|
|
1816 VXPGPORT = Qnil;
|
442
|
1817
|
771
|
1818 FROB ("PGTTY", VXPGTTY);
|
|
1819 FROB ("PGDATABASE", VXPGDATABASE);
|
|
1820 FROB ("PGREALM", VXPGREALM);
|
442
|
1821 #ifdef MULE
|
771
|
1822 /* It's not clear whether this is any use. My intent is to
|
|
1823 autodetect the coding system from the database. */
|
|
1824 FROB ("PGCLIENTENCODING", VXPGCLIENTENCODING);
|
442
|
1825 #endif
|
|
1826
|
|
1827 #if !defined(HAVE_POSTGRESQLV7)
|
771
|
1828 FROB ("PGAUTHTYPE", VXPGAUTHTYPE);
|
442
|
1829 #endif
|
|
1830
|
771
|
1831 FROB ("PGGEQO", VXPGGEQO);
|
|
1832 FROB ("PGCOSTINDEX", VXPGCOSTINDEX);
|
|
1833 FROB ("PGCOSTHEAP", VXPGCOSTHEAP);
|
|
1834 FROB ("PGTZ", VXPGTZ);
|
|
1835 FROB ("PGDATESTYLE", VXPGDATESTYLE);
|
|
1836 #undef FROB
|
442
|
1837 }
|
|
1838 }
|
446
|
1839
|