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