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