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