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