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