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
|
#include <dbmi.h>
#include "globals.h"
#include "proto.h"
int db__driver_list_databases(dbpath, npaths, dblist, dbcount)
dbString *dbpath;
int npaths;
dbHandle **dblist;
int *dbcount;
{
int i;
PGCONN pgconn;
PGresult *res;
int rec_num = 0;
dbHandle *list;
init_error();
*dblist = NULL;
*dbcount = 0;
/* TODO: the solution below is not good as user usually does not have permissions for "template1" */
append_error( "db_driver_list_databases() is not implemented in pg driver" );
report_error();
return DB_FAILED;
if ( npaths > 0 ) {
G_debug (3, "location: %s", db_get_string ( dbpath ) );
if ( parse_conn ( db_get_string(dbpath), &pgconn ) == DB_FAILED ) {
report_error();
return DB_FAILED;
}
}
G_debug(3, "host = %s, port = %s, options = %s, tty = %s",
pgconn.host, pgconn.port, pgconn.options, pgconn.tty);
pg_conn = PQsetdb( pgconn.host, pgconn.port, pgconn.options, pgconn.tty, "template1");
if (PQstatus(pg_conn) == CONNECTION_BAD) {
append_error( "Cannot connect to Postgres:\n" );
append_error( PQerrorMessage(pg_conn) );
report_error();
PQfinish (pg_conn);
return DB_FAILED;
}
res = PQexec(pg_conn, "select datname from pg_database");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
append_error( "Cannot select from Postgres:\n" );
append_error( PQerrorMessage(pg_conn) );
report_error();
PQclear(res);
PQfinish(pg_conn);
return DB_FAILED;
}
rec_num = PQntuples(res);
list = db_alloc_handle_array(rec_num);
if (list == NULL) {
append_error ( "Cannot db_alloc_handle_array()" );
report_error();
return DB_FAILED;
}
for (i = 0; i < rec_num; i++) {
db_init_handle(&list[i]);
if (db_set_handle(&list[i], PQgetvalue(res, i, 0), NULL) != DB_OK) {
append_error( "db_set_handle()" );
report_error();
db_free_handle_array(list, rec_num);
return DB_FAILED;
}
}
PQclear(res);
PQfinish(pg_conn);
*dblist = list;
*dbcount = rec_num;
return DB_OK;
}
|