1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
|
<HTML><HEAD>
<TITLE>C Examples -- Example 7: postgres</TITLE>
<LINK rel=Previous href="c-app6.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="dr-app.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="c-app6.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="dr-app.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="19981">
</a><h3>Example 7: postgres</h3>
<p><a name="19982">
</a>The following example shows how to create a database driver. This `postgres' module is a database services driver which allows the AOLserver to use the <i>Postgres95</i> database. </p>
<p><a name="19986">
</a>This example can be found in the <code>examples/c/postgres</code> directory.</p>
<pre> <a name="19987"></a>
<a name="19988"></a>#include "ns.h"
<a name="19989"></a>#include "nsdb.h"
<a name="19990"></a>#include "nstcl.h"
<a name="19991"></a>#include "libpq-fe.h"
<a name="19992"></a>#include <string.h>
<a name="19993"></a>#include <assert.h>
<a name="19994"></a>
<a name="19995"></a>/*-
<a name="19996"></a>
<a name="19997"></a>What is this?
<a name="19998"></a>------------
<a name="19999"></a>
<a name="20003"></a>This module implements a simple AOLserver database services driver. A
<a name="20007"></a>database driver is a module which interfaces between the AOLserver
<a name="20008"></a>database-independent nsdb module and the API of a particular DBMS. A
<a name="20009"></a>database driver's job is to open connections, send SQL statements, and
<a name="20010"></a>translate the results into the form used by nsdb. In this case, the
<a name="20011"></a>driver is for the Postgres95 DBMS from UC Berekely. Postgres95 can be
<a name="20012"></a>downloaded and installed on most Unix systems. To use this driver, you
<a name="20013"></a>must have Postgres95 installed on your system. For more information on
<a name="20014"></a>Postgres95 or to download the code, open:
<a name="20015"></a>
<a name="20016"></a> http://s2k-ftp.cs.berkeley.edu:8000/postgres95
<a name="20017"></a>
<a name="20018"></a>
<a name="20019"></a>How does it work?
<a name="20020"></a>----------------
<a name="20021"></a>
<a name="20025"></a>Driver modules look much like ordinary AOLserver modules but are
<a name="20026"></a>loaded differently. Instead of being listed with other modules in the
<a name="20027"></a>[ns\server\<server-name>\modules] configuration section, a database
<a name="20028"></a>driver is listed in the [ns\module\nsdb\drivers] section and nsdb does
<a name="20029"></a>the loading. The database driver initialization function normally does
<a name="20030"></a>little more than call the nsdb Ns_DbRegisterDriver() function with an
<a name="20031"></a>array of pointers to functions. The functions are then later used by
<a name="20032"></a>nsdb to open database connections and send and process queries.
<a name="20033"></a>In addition to open,
<a name="20034"></a>select, and getrow functions, the driver also provides system catalog
<a name="20035"></a>functions and a function for initializing a virtual server. The
<a name="20036"></a>virtual server initialization function is called each time nsdb is
<a name="20037"></a>loaded into a virtual server. In this case, the server initialization
<a name="20038"></a>function, Ns_PgServerInit, adds the "ns_pg" Tcl command to the server's
<a name="20039"></a>Tcl interpreters which can be used to fetch information about an active
<a name="20040"></a>Postgres95 connection in a Tcl script.
<a name="20041"></a>
<a name="20042"></a>*/
<a name="20043"></a>
<a name="20044"></a>
<a name="20045"></a>#define DRIVER_NAME "Postgres95"
<a name="20046"></a>
<a name="20047"></a>static char *Ns_PgName(Ns_DbHandle *handle);
<a name="20048"></a>static int Ns_PgOpenDb(Ns_DbHandle *dbhandle);
<a name="20049"></a>static int Ns_PgCloseDb(Ns_DbHandle *dbhandle);
<a name="20050"></a>static int Ns_PgCmd(Ns_DbHandle *handle, char *sql);
<a name="20051"></a>static int Ns_PgReconnect(Ns_DbHandle *handle);
<a name="20052"></a>static Ns_Set *Ns_PgSelect(Ns_DbHandle *handle, char *sql);
<a name="20053"></a>static int Ns_PgGetRow(Ns_DbHandle *handle, Ns_Set *row);
<a name="20054"></a>static int Ns_PgFlush(Ns_DbHandle *handle);
<a name="20055"></a>static Ns_DbTableInfo *Ns_PgGetTableInfo(Ns_DbHandle *handle, char *table);
<a name="20056"></a>static char *Ns_PgTableList(Ns_DString *pds, Ns_DbHandle *handle,
<a name="20057"></a> int includesystem);
<a name="20058"></a>static char *Ns_PgBestRowId(Ns_DString *pds, Ns_DbHandle *handle, char *table);
<a name="20059"></a>static int Ns_PgServerInit(char *hServer, char *hModule, char *hDriver);
<a name="20060"></a>static char *pgName = DRIVER_NAME;
<a name="20061"></a>static unsigned int pgCNum = 0;
<a name="20062"></a>
<a name="20063"></a>
<a name="20064"></a>/*-
<a name="20065"></a> *
<a name="20066"></a> * The NULL-terminated PgProcs[] array of Ns_DbProc structures is the
<a name="20067"></a> * method by which the function pointers are passed to the nsdb module
<a name="20068"></a> * through the Ns_DbRegisterDriver() function. Each Ns_DbProc includes
<a name="20069"></a> * the function id (i.e., DbFn_OpenDb, DbFn_CloseDb, etc.) and the
<a name="20070"></a> * cooresponding driver function pointer (i.e., Ns_PgOpendb, Ns_PgCloseDb,
<a name="20071"></a> * etc.). See nsdb.h for a complete list of function ids.
<a name="20072"></a> */
<a name="20073"></a>static Ns_DbProc PgProcs[] = {
<a name="20074"></a> {DbFn_Name, (void *) Ns_PgName},
<a name="20075"></a> {DbFn_OpenDb, (void *) Ns_PgOpenDb},
<a name="20076"></a> {DbFn_CloseDb, (void *) Ns_PgCloseDb},
<a name="20077"></a> {DbFn_DML, (void *) Ns_PgCmd},
<a name="20078"></a> {DbFn_Select, (void *) Ns_PgSelect},
<a name="20079"></a> {DbFn_GetRow, (void *) Ns_PgGetRow},
<a name="20080"></a> {DbFn_Flush, (void *) Ns_PgFlush},
<a name="20081"></a> {DbFn_Cancel, (void *) Ns_PgFlush},
<a name="20082"></a> {DbFn_GetTableInfo, (void *) Ns_PgGetTableInfo},
<a name="20083"></a> {DbFn_TableList, (void *) Ns_PgTableList},
<a name="20084"></a> {DbFn_BestRowId, (void *) Ns_PgBestRowId},
<a name="20085"></a> {DbFn_ServerInit, (void *) Ns_PgServerInit},
<a name="20086"></a> {0, NULL}
<a name="20087"></a>};
<a name="20088"></a>
<a name="20089"></a>
<a name="20090"></a>/*
<a name="20091"></a> * The NsPgConn structure is connection data specific
<a name="20092"></a> * to Postgres95.
<a name="20093"></a> */
<a name="20094"></a>typedef struct NsPgConn {
<a name="20095"></a> PGconn *conn;
<a name="20096"></a> unsigned int cNum;
<a name="20097"></a> PGresult *res;
<a name="20098"></a> int nCols;
<a name="20099"></a> int nTuples;
<a name="20100"></a> int curTuple;
<a name="20101"></a>} NsPgConn;
<a name="20102"></a>
<a name="20103"></a>
<a name="20104"></a>DllExport int Ns_ModuleVersion = 1;
<a name="20105"></a>
<a name="20106"></a>DllExport int
<a name="20107"></a>Ns_DbDriverInit(char *hDriver, char *configPath)
<a name="20108"></a>{
<a name="20109"></a> /*
<a name="20110"></a> * Register the Postgres95 driver functions with nsdb.
<a name="20111"></a> * Nsdb will later call the Ns_PgServerInit() function
<a name="20112"></a> * for each virtual server which utilizes nsdb.
<a name="20113"></a> */
<a name="20115"></a> if (Ns_DbRegisterDriver(hDriver, &(PgProcs[0])) != NS_OK) {
<a name="20117"></a> Ns_Log(Error, "Ns_DbDriverInit(%s): Could not register the %s driver.",
<a name="20118"></a> hDriver, pgName);
<a name="20119"></a> return NS_ERROR;
<a name="20120"></a> }
<a name="20121"></a> Ns_Log(Notice, "%s loaded.", pgName);
<a name="20122"></a> return NS_OK;
<a name="20123"></a>}
<a name="20124"></a>
<a name="20125"></a>
<a name="20126"></a>/*
<a name="20127"></a> * Ns_PgName - Return the string name which identifies the Postgres95 driver.
<a name="20128"></a> */
<a name="20129"></a>static char *
<a name="20130"></a>Ns_PgName(Ns_DbHandle *ignored)
<a name="20131"></a>{
<a name="20132"></a> return pgName;
<a name="20133"></a>}
<a name="20134"></a>
<a name="20135"></a>
<a name="20136"></a>
<a name="20137"></a>
<a name="20138"></a>
<a name="20139"></a>/*
<a name="20140"></a> * Ns_PgOpenDb - Open an Postgres95 connection on an nsdb handle. The
<a name="20141"></a> * datasource for Postgres95 is in the form "host:port:database".
<a name="20142"></a> */
<a name="20143"></a>static int
<a name="20144"></a>Ns_PgOpenDb(Ns_DbHandle *handle)
<a name="20145"></a>{
<a name="20146"></a> NsPgConn *nsConn;
<a name="20147"></a> PGconn *pgConn;
<a name="20148"></a> char *host;
<a name="20149"></a> char *port;
<a name="20150"></a> char *db;
<a name="20151"></a> int status;
<a name="20152"></a>
<a name="20153"></a> assert(handle != NULL);
<a name="20154"></a>
<a name="20155"></a> status = NS_ERROR;
<a name="20156"></a> host = handle->datasource;
<a name="20157"></a> port = strchr(handle->datasource, `:');
<a name="20158"></a> if (port == NULL || ((db = strchr(port + 1, `:')) == NULL)) {
<a name="20159"></a> Ns_Log(Error, "Ns_PgOpenDb(%s): Malformed datasource: %s",
<a name="20160"></a> handle->driver, handle->datasource);
<a name="20161"></a> } else {
<a name="20162"></a> *port++ = `\0';
<a name="20163"></a> *db++ = `\0';
<a name="20164"></a> Ns_Log(Notice, "Opening %s on %s, %s", db, host, port);
<a name="20165"></a> pgConn = PQsetdb(host, port, NULL, NULL, db);
<a name="20166"></a> *--db = `:';
<a name="20167"></a> *--port = `:';
<a name="20168"></a> if (PQstatus(pgConn) == CONNECTION_OK) {
<a name="20170"></a> Ns_Log(Notice, "Ns_PgOpenDb(%s): Openned connection to %s.",
<a name="20171"></a> handle->driver, handle->datasource);
<a name="20173"></a> nsConn = ns_malloc(sizeof(NsPgConn));
<a name="20174"></a> nsConn->cNum = pgCNum++;
<a name="20175"></a> nsConn->conn = pgConn;
<a name="20176"></a> nsConn->res = NULL;
<a name="20177"></a> nsConn->nCols = nsConn->nTuples = nsConn->curTuple = 0;
<a name="20178"></a> handle->connection = nsConn;
<a name="20179"></a> status = NS_OK;
<a name="20180"></a> } else {
<a name="20181"></a> Ns_Log(Error, "Ns_PgOpenDb(%s): Could not connect to %s: %s",
<a name="20182"></a> handle->driver, handle->datasource, PQerrorMessage(pgConn));
<a name="20183"></a> PQfinish(pgConn);
<a name="20184"></a> return NS_ERROR;
<a name="20185"></a> }
<a name="20186"></a> }
<a name="20187"></a> return NS_OK;
<a name="20188"></a>}
<a name="20189"></a>
<a name="20190"></a>
<a name="20191"></a>/*
<a name="20192"></a> * Ns_PgCloseDb - Close an Postgres95 connection on an nsdb handle.
<a name="20193"></a> */
<a name="20194"></a>static int
<a name="20195"></a>Ns_PgCloseDb(Ns_DbHandle *handle)
<a name="20196"></a>{
<a name="20197"></a> NsPgConn *nsConn;
<a name="20198"></a>
<a name="20199"></a> assert(handle != NULL);
<a name="20200"></a>
<a name="20201"></a> nsConn = handle->connection;
<a name="20202"></a> if (handle->verbose) {
<a name="20203"></a> Ns_Log(Notice, "Ns_PgCloseDb(%d): Closing connection: %s",
<a name="20204"></a> nsConn->cNum, handle->datasource);
<a name="20205"></a> }
<a name="20206"></a> PQfinish(nsConn->conn);
<a name="20207"></a> nsConn->conn = NULL;
<a name="20208"></a> nsConn->nCols = nsConn->nTuples = nsConn->curTuple = 0;
<a name="20210"></a> ns_free(nsConn);
<a name="20211"></a> handle->connection = NULL;
<a name="20212"></a> return NS_OK;
<a name="20213"></a>}
<a name="20214"></a>
<a name="20215"></a>
<a name="20216"></a>/*
<a name="20217"></a> * Ns_PgExec - Send a Postgres95 query. This function does not
<a name="20218"></a> * implement an nsdb function but is used internally by the Postgres95
<a name="20219"></a> * driver.
<a name="20220"></a> */
<a name="20221"></a>static int
<a name="20222"></a>Ns_PgExec(Ns_DbHandle *handle, char *sql)
<a name="20223"></a>{
<a name="20224"></a> NsPgConn *nsConn;
<a name="20225"></a> Ns_DString dsSql;
<a name="20226"></a> int status;
<a name="20227"></a>
<a name="20228"></a> assert(handle != NULL);
<a name="20229"></a> assert(sql != NULL);
<a name="20230"></a>
<a name="20231"></a> status = NS_ERROR;
<a name="20232"></a> nsConn = handle->connection;
<a name="20234"></a> Ns_DStringInit(&dsSql);
<a name="20236"></a> Ns_DStringAppend(&dsSql, sql);
<a name="20237"></a> while (dsSql.length > 0 && isspace(dsSql.string[dsSql.length - 1])) {
<a name="20238"></a> dsSql.string[--dsSql.length] = `\0';
<a name="20239"></a> }
<a name="20240"></a> if (dsSql.length > 0 && dsSql.string[dsSql.length - 1] != `;') {
<a name="20242"></a> Ns_DStringNAppend(&dsSql, ";", 1);
<a name="20243"></a> }
<a name="20244"></a> nsConn->res = PQexec(nsConn->conn, dsSql.string);
<a name="20246"></a> Ns_DStringFree(&dsSql);
<a name="20247"></a> if (nsConn->res == NULL) {
<a name="20249"></a> Ns_Log(Error, "Ns_PgExec(%s): Could not send query `%s': %s",
<a name="20250"></a> handle->datasource, sql, PQerrorMessage(nsConn->conn));
<a name="20251"></a> return NS_ERROR;
<a name="20252"></a> }
<a name="20253"></a> return NS_OK;
<a name="20254"></a>}
<a name="20255"></a>
<a name="20256"></a>
<a name="20257"></a>/*
<a name="20258"></a> * Ns_PgCmd - Send a query which should return a command result.
<a name="20259"></a> */
<a name="20260"></a>static int
<a name="20261"></a>Ns_PgCmd(Ns_DbHandle *handle, char *sql)
<a name="20262"></a>{
<a name="20263"></a> int status;
<a name="20264"></a> NsPgConn *nsConn;
<a name="20265"></a>
<a name="20266"></a> assert(handle != NULL);
<a name="20267"></a> assert(sql != NULL);
<a name="20268"></a>
<a name="20269"></a> nsConn = handle->connection;
<a name="20270"></a> status = Ns_PgExec(handle, sql);
<a name="20271"></a> if (status == NS_OK) {
<a name="20272"></a> if (PQresultStatus(nsConn->res) != PGRES_COMMAND_OK) {
<a name="20273"></a> Ns_Log(Error,
<a name="20274"></a> "Ns_PgCmd(%s): Query `%s' did not return PGRES_COMMAND_OK status.",
<a name="20275"></a> handle->datasource, sql);
<a name="20276"></a> status = NS_ERROR;
<a name="20277"></a> }
<a name="20278"></a> nsConn->nCols = 0;
<a name="20279"></a> PQclear(nsConn->res);
<a name="20280"></a> nsConn->res = NULL;
<a name="20281"></a> }
<a name="20282"></a> return status;
<a name="20283"></a>}
<a name="20284"></a>
<a name="20285"></a>
<a name="20286"></a>
<a name="20287"></a>/*
<a name="20288"></a> * Ns_PgSelect - Send a query which should return rows.
<a name="20289"></a> */
<a name="20290"></a>static Ns_Set *
<a name="20291"></a>Ns_PgSelect(Ns_DbHandle *handle, char *sql)
<a name="20292"></a>{
<a name="20293"></a> Ns_Set *row;
<a name="20294"></a> NsPgConn *nsConn;
<a name="20295"></a> int i;
<a name="20296"></a>
<a name="20297"></a> assert(handle != NULL);
<a name="20298"></a> assert(sql != NULL);
<a name="20299"></a>
<a name="20300"></a> row = NULL;
<a name="20301"></a> nsConn = handle->connection;
<a name="20302"></a> if (Ns_PgExec(handle, sql) == NS_OK) {
<a name="20303"></a> if (PQresultStatus(nsConn->res) == PGRES_TUPLES_OK) {
<a name="20304"></a> nsConn->curTuple = 0;
<a name="20305"></a> nsConn->nCols = PQnfields(nsConn->res);
<a name="20306"></a> nsConn->nTuples = PQntuples(nsConn->res);
<a name="20307"></a> row = handle->row;
<a name="20308"></a> for (i = 0; i < nsConn->nCols; ++i) {
<a name="20310"></a> Ns_SetPut(row, PQfname(nsConn->res, i), NULL);
<a name="20311"></a> }
<a name="20312"></a> } else {
<a name="20313"></a> Ns_Log(Error, "Ns_PgSelect(%s): Query did not return rows: %s",
<a name="20314"></a> handle->datasource, sql);
<a name="20315"></a> }
<a name="20316"></a> }
<a name="20317"></a> return row;
<a name="20318"></a>}
<a name="20319"></a>
<a name="20320"></a>
<a name="20321"></a>/*
<a name="20322"></a> * Ns_PgGetRow - Fetch rows after an Ns_PgSelect.
<a name="20323"></a> */
<a name="20324"></a>static int
<a name="20325"></a>Ns_PgGetRow(Ns_DbHandle *handle, Ns_Set *row)
<a name="20326"></a>{
<a name="20327"></a> NsPgConn *nsConn;
<a name="20328"></a> int status;
<a name="20329"></a> int i;
<a name="20330"></a>
<a name="20331"></a> assert(handle != NULL);
<a name="20332"></a> assert(row != NULL);
<a name="20333"></a> assert(handle->connection != NULL);
<a name="20334"></a>
<a name="20335"></a> nsConn = handle->connection;
<a name="20336"></a> if (nsConn->nCols == 0) {
<a name="20337"></a> Ns_Log(Error, "Ns_PgGetRow(%s): Get row called outside a fetch row loop.",
<a name="20338"></a> handle->datasource);
<a name="20339"></a> status = NS_ERROR;
<a name="20340"></a> } else if (nsConn->curTuple == nsConn->nTuples) {
<a name="20341"></a> PQclear(nsConn->res);
<a name="20342"></a> nsConn->res = NULL;
<a name="20343"></a> nsConn->nCols = nsConn->nTuples = nsConn->curTuple = 0;
<a name="20344"></a> status = NS_END_DATA;
<a name="20345"></a> } else {
<a name="20346"></a> for (i = 0; i < nsConn->nCols; i++) {
<a name="20348"></a> Ns_SetPutValue(row, (int) i, PQgetvalue(nsConn->res,
<a name="20349"></a> nsConn->curTuple, i));
<a name="20350"></a> }
<a name="20351"></a> ++nsConn->curTuple;
<a name="20352"></a> return NS_OK;
<a name="20353"></a> }
<a name="20354"></a> return status;
<a name="20355"></a>}
<a name="20356"></a>
<a name="20357"></a>
<a name="20358"></a>/*
<a name="20359"></a> * Ns_PgFlush - Flush any waiting rows not needed after an Ns_DbSelect().
<a name="20360"></a> */
<a name="20361"></a>static int
<a name="20362"></a>Ns_PgFlush(Ns_DbHandle *handle)
<a name="20363"></a>{
<a name="20364"></a> NsPgConn *nsConn;
<a name="20365"></a>
<a name="20366"></a> assert(handle != NULL);
<a name="20367"></a> assert(handle->connection != NULL);
<a name="20368"></a>
<a name="20369"></a> nsConn = handle->connection;
<a name="20370"></a> if (nsConn->nCols > 0) {
<a name="20371"></a> PQclear(nsConn->res);
<a name="20372"></a> nsConn->res = NULL;
<a name="20373"></a> nsConn->nCols = nsConn->nTuples = nsConn->curTuple = 0;
<a name="20374"></a> }
<a name="20375"></a> return NS_OK;
<a name="20376"></a>}
<a name="20377"></a>
<a name="20378"></a>
<a name="20379"></a>/*
<a name="20380"></a> * Ns_DbTableInfo - Return system catalog information (columns, types, etc.)
<a name="20381"></a> * about a table.
<a name="20382"></a> */
<a name="20383"></a>static Ns_DbTableInfo *
<a name="20384"></a>Ns_PgGetTableInfo(Ns_DbHandle *handle, char *table)
<a name="20385"></a>{
<a name="20386"></a> Ns_Set *row;
<a name="20387"></a> Ns_Set *col;
<a name="20388"></a> Ns_DString ds;
<a name="20389"></a> Ns_DbTableInfo *tinfo;
<a name="20390"></a> int status;
<a name="20391"></a> char *name;
<a name="20392"></a> char *type;
<a name="20393"></a>
<a name="20394"></a>
<a name="20396"></a> Ns_DStringInit(&ds);
<a name="20398"></a> Ns_DStringVarAppend(&ds, "SELECT a.attname, t.typname "
<a name="20399"></a> "FROM pg_class c, pg_attribute a, pg_type t "
<a name="20400"></a> "WHERE c.relname = `", table, "` "
<a name="20401"></a> "and a.attnum > 0 and a.attrelid = c.oid "
<a name="20402"></a> "and a.atttypid = t.oid ORDER BY attname", NULL);
<a name="20403"></a>
<a name="20405"></a> row = Ns_DbSelect(handle, ds.string);
<a name="20407"></a> Ns_DStringFree(&ds);
<a name="20408"></a> tinfo = NULL;
<a name="20409"></a> if (row != NULL) {
<a name="20410"></a> while ((status = Ns_PgGetRow(handle, row)) == NS_OK) {
<a name="20411"></a> name = row->fields[0].value;
<a name="20412"></a> type = row->fields[1].value;
<a name="20413"></a> if (name == NULL || type == NULL) {
<a name="20414"></a> Ns_Log(Error,
<a name="20415"></a> "Ns_PgGetTableInfo(%s): Invalid `pg_attribute' entry for table: %s",
<a name="20416"></a> handle->datasource, table);
<a name="20417"></a> break;
<a name="20418"></a> }
<a name="20419"></a>
<a name="20420"></a> /*
<a name="20421"></a> * NB: Move the fields directly from the row
<a name="20422"></a> * Ns_Set to the col Ns_Set to avoid a data copy.
<a name="20423"></a> */
<a name="20425"></a> col = Ns_SetCreate(NULL);
<a name="20426"></a> col->name = name;
<a name="20428"></a> Ns_SetPut(col, "type", NULL);
<a name="20429"></a> col->fields[0].value = type;
<a name="20430"></a> row->fields[0].value = NULL;
<a name="20431"></a> row->fields[1].value = NULL;
<a name="20432"></a> if (tinfo == NULL) {
<a name="20434"></a> tinfo = Ns_DbNewTableInfo(table);
<a name="20435"></a> }
<a name="20437"></a> Ns_DbAddColumnInfo(tinfo, col);
<a name="20438"></a> }
<a name="20439"></a> if (status != NS_END_DATA && tinfo != NULL) {
<a name="20441"></a> Ns_DbFreeTableInfo(tinfo);
<a name="20442"></a> tinfo = NULL;
<a name="20443"></a> }
<a name="20444"></a> }
<a name="20445"></a> return tinfo;
<a name="20446"></a>}
<a name="20447"></a>
<a name="20448"></a>
<a name="20449"></a>
<a name="20450"></a>
<a name="20451"></a>
<a name="20452"></a>/*
<a name="20453"></a> * Ns_PgTableList - Return a list of tables in the database.
<a name="20454"></a> */
<a name="20455"></a>static char *
<a name="20456"></a>Ns_PgTableList(Ns_DString *pds, Ns_DbHandle *handle, int fSystemTables)
<a name="20457"></a>{
<a name="20458"></a> Ns_Set *row;
<a name="20459"></a> Ns_DString ds;
<a name="20460"></a> char *table;
<a name="20461"></a> int status;
<a name="20462"></a>
<a name="20464"></a> Ns_DStringInit(&ds);
<a name="20466"></a> Ns_DStringAppend(&ds, "SELECT relname FROM pg_class "
<a name="20467"></a> "WHERE relkind = `r' and relname !~ `^Inv' ");
<a name="20468"></a> if (!fSystemTables) {
<a name="20469"></a> Ns_DStringAppend(&ds, "and relname !~ `^pg_' ");
<a name="20470"></a> }
<a name="20471"></a> Ns_DStringAppend(&ds, "ORDER BY relname");
<a name="20473"></a> row = Ns_DbSelect(handle, ds.string);
<a name="20474"></a> Ns_DStringFree(&ds);
<a name="20475"></a> status = NS_ERROR;
<a name="20476"></a> if (row != NULL) {
<a name="20478"></a> while ((status = Ns_DbGetRow(handle, row)) == NS_OK) {
<a name="20479"></a> table = row->fields[0].value;
<a name="20480"></a> if (table == NULL) {
<a name="20482"></a> Ns_Log(Warning,
<a name="20483"></a> "Ns_PgTableList(%s): NULL relname in `pg_class' table.",
<a name="20484"></a> handle->datasource);
<a name="20485"></a> } else {
<a name="20486"></a> Ns_DStringNAppend(pds, table, strlen(table) + 1);
<a name="20487"></a> }
<a name="20488"></a> }
<a name="20489"></a> }
<a name="20490"></a> if (status == NS_END_DATA) {
<a name="20491"></a> return pds->string;
<a name="20492"></a> }
<a name="20493"></a> return NULL;
<a name="20494"></a>}
<a name="20495"></a>
<a name="20496"></a>
<a name="20497"></a>/*
<a name="20498"></a> * Ns_PgBestRowId - Return the primary key of a table. If a table
<a name="20502"></a> * has a primary key, the AOLserver can perform row updates and
<a name="20503"></a> * deletes. In the case of Postgres95, the "oid" system column is alwasy
<a name="20504"></a> * unique so we just return it instead of looking for an actual
<a name="20505"></a> * primary key.
<a name="20506"></a> */
<a name="20507"></a>static char *
<a name="20508"></a>Ns_PgBestRowId(Ns_DString *pds, Ns_DbHandle *handle, char *table)
<a name="20509"></a>{
<a name="20511"></a> Ns_DStringNAppend(pds, "oid", 4);
<a name="20512"></a> return pds->string;
<a name="20513"></a>}
<a name="20514"></a>
<a name="20515"></a>
<a name="20516"></a>
<a name="20517"></a>/*
<a name="20518"></a> * PgCmd - This function implements the "ns_pg" Tcl command installed into
<a name="20519"></a> * each interpreter of each virtual server. It provides access to features
<a name="20520"></a> * specific to the Postgres95 driver.
<a name="20521"></a> */
<a name="20522"></a>static int
<a name="20523"></a>PgCmd(ClientData dummy, Tcl_Interp *interp, int argc, char **argv)
<a name="20524"></a>{
<a name="20525"></a> Ns_DbHandle *handle;
<a name="20526"></a> NsPgConn *pgconn;
<a name="20527"></a>
<a name="20528"></a> if (argc != 3) {
<a name="20530"></a> Tcl_AppendResult(interp, "wrong # args: should be \"",
<a name="20531"></a> argv[0], " command dbId\"", NULL);
<a name="20532"></a> return TCL_ERROR;
<a name="20533"></a> }
<a name="20535"></a> if (Ns_TclDbGetHandle(interp, argv[2], &handle) != TCL_OK) {
<a name="20536"></a> return TCL_ERROR;
<a name="20537"></a> }
<a name="20538"></a>
<a name="20539"></a> /*
<a name="20540"></a> * Make sure this is a Postgres95 handle before accessing
<a name="20541"></a> * handle->connection as an NsPgConn.
<a name="20542"></a> */
<a name="20544"></a> if (Ns_DbDriverName(handle) != pgName) {
<a name="20546"></a> Tcl_AppendResult(interp, "handle \"", argv[1], "\" is not of type \"",
<a name="20547"></a> pgName, "\"", NULL);
<a name="20548"></a> return TCL_ERROR;
<a name="20549"></a> }
<a name="20550"></a> pgconn = (NsPgConn *) handle->connection;
<a name="20551"></a> if (!strcmp(argv[1], "db")) {
<a name="20553"></a> Tcl_SetResult(interp, PQdb(pgconn->conn), TCL_STATIC);
<a name="20554"></a> } else if (!strcmp(argv[1], "host")) {
<a name="20555"></a> Tcl_SetResult(interp, PQhost(pgconn->conn), TCL_STATIC);
<a name="20556"></a> } else if (!strcmp(argv[1], "options")) {
<a name="20557"></a> Tcl_SetResult(interp, PQoptions(pgconn->conn), TCL_STATIC);
<a name="20558"></a> } else if (!strcmp(argv[1], "port")) {
<a name="20559"></a> Tcl_SetResult(interp, PQport(pgconn->conn), TCL_STATIC);
<a name="20560"></a> } else if (!strcmp(argv[1], "number")) {
<a name="20561"></a> sprintf(interp->result, "%u", pgconn->cNum);
<a name="20562"></a> } else if (!strcmp(argv[1], "error")) {
<a name="20563"></a> Tcl_SetResult(interp, PQerrorMessage(pgconn->conn), TCL_STATIC);
<a name="20564"></a> } else if (!strcmp(argv[1], "status")) {
<a name="20565"></a> if (PQstatus(pgconn->conn) == CONNECTION_OK) {
<a name="20566"></a> interp->result = "ok";
<a name="20567"></a> } else {
<a name="20568"></a> interp->result = "bad";
<a name="20569"></a> }
<a name="20570"></a> } else {
<a name="20572"></a> Tcl_AppendResult(interp, "unknown command \"", argv[2],
<a name="20573"></a> "\": should be db, host, options, port, error or status.", NULL);
<a name="20574"></a> return TCL_ERROR;
<a name="20575"></a> }
<a name="20576"></a> return TCL_OK;
<a name="20577"></a>}
<a name="20578"></a>
<a name="20579"></a>
<a name="20580"></a>/*
<a name="20581"></a> * Ns_PgInterpInit - Add the "ns_pg" command to a single Tcl interpreter.
<a name="20582"></a> */
<a name="20583"></a>static int
<a name="20584"></a>Ns_PgInterpInit(Tcl_Interp *interp, void *ignored)
<a name="20585"></a>{
<a name="20587"></a> Tcl_CreateCommand(interp, "ns_pg", PgCmd, NULL, NULL);
<a name="20588"></a> return NS_OK;
<a name="20589"></a>}
<a name="20590"></a>
<a name="20591"></a>
<a name="20592"></a>/*
<a name="20593"></a> * Ns_PgServerInit - Have Ns_PgInterpInit called for each interpreter in
<a name="20594"></a> * the virtual server which is being intialized.
<a name="20595"></a> */
<a name="20596"></a>static int
<a name="20597"></a>Ns_PgServerInit(char *hServer, char *hModule, char *hDriver)
<a name="20598"></a>{
<a name="20600"></a> return Ns_TclInitInterps(hServer, Ns_PgInterpInit, NULL);
<a name="20601"></a>}
<a name="17669"></a>
</pre><p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="c-app6.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="dr-app.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|