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