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