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