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