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