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
|
#include <stdlib.h>
#include <string.h>
#include <gis.h>
#include <dbmi.h>
#include "globals.h"
#include "proto.h"
int db__driver_open_database(handle)
dbHandle *handle;
{
char *name, *schema, *user, *password, buf[500];
dbConnection connection;
PGCONN pgconn;
PGresult *res;
int row;
init_error();
db_get_connection(&connection);
name = db_get_handle_dbname(handle);
/* if name is empty use connection.databaseName */
if (strlen(name) == 0)
name = connection.databaseName;
G_debug(3, "db_driver_open_database() driver=pg database definition = '%s'", name );
if ( parse_conn ( name, &pgconn ) == DB_FAILED ) {
report_error();
return DB_FAILED;
}
G_debug(3, "host = %s, port = %s, options = %s, tty = %s, dbname = %s, user = %s, password = %s, "
"schema = %s", pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
pgconn.dbname, pgconn.user, pgconn.password, pgconn.schema );
db_get_login ( "pg", name, &user, &password );
pg_conn = PQsetdbLogin( pgconn.host, pgconn.port, pgconn.options, pgconn.tty,
pgconn.dbname, user, password );
free ( user );
free ( password );
if (PQstatus(pg_conn) == CONNECTION_BAD) {
append_error ( "Cannot connect to Postgres: " );
append_error ( PQerrorMessage(pg_conn) );
report_error ();
PQfinish(pg_conn);
return DB_FAILED;
}
/* Set schema */
schema = db_get_handle_dbschema (handle);
/* Cannot use default schema because link to table can point to different database */
/*
if ( schema )
schema = connection.schemaName;
*/
if ( pgconn.schema ) {
schema = pgconn.schema;
}
if ( schema && strlen(schema) > 0 ) {
sprintf ( buf, "set search_path to %s", schema );
res = PQexec ( pg_conn, buf );
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
append_error ( "Cannot set schema: " );
append_error ( schema );
report_error();
PQclear(res);
return DB_FAILED;
}
}
/* Read internal codes */
res = PQexec(pg_conn, "select oid, typname from pg_type where typname in ( "
"'int2', 'int4', 'int8', 'serial', 'oid', "
"'float4', 'float8', 'numeric', "
"'char', 'bpchar', 'varchar', 'text', "
"'time', 'date', 'timestamp', "
"'bool' ) order by oid" );
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
append_error ( "Cannot select data types" );
report_error();
PQclear(res);
return DB_FAILED;
}
pg_ntypes = PQntuples(res);
pg_types = (int *) G_realloc ( pg_types, 2 * pg_ntypes * sizeof(int) );
for ( row = 0; row < pg_ntypes; row++ ) {
int pgtype, type;
pgtype = atoi ( PQgetvalue(res, row, 0) );
pg_types[row][0] = pgtype;
if ( strcmp( PQgetvalue(res, row, 1), "int2" ) == 0 )
type = PG_TYPE_INT2;
else if ( strcmp( PQgetvalue(res, row, 1), "int4" ) == 0 )
type = PG_TYPE_INT4;
else if ( strcmp( PQgetvalue(res, row, 1), "int8" ) == 0 )
type = PG_TYPE_INT8;
else if ( strcmp( PQgetvalue(res, row, 1), "serial" ) == 0 )
type = PG_TYPE_SERIAL;
else if ( strcmp( PQgetvalue(res, row, 1), "oid" ) == 0 )
type = PG_TYPE_OID;
else if ( strcmp( PQgetvalue(res, row, 1), "float4" ) == 0 )
type = PG_TYPE_FLOAT4;
else if ( strcmp( PQgetvalue(res, row, 1), "float8" ) == 0 )
type = PG_TYPE_FLOAT8;
else if ( strcmp( PQgetvalue(res, row, 1), "numeric" ) == 0 )
type = PG_TYPE_NUMERIC;
else if ( strcmp( PQgetvalue(res, row, 1), "char" ) == 0 )
type = PG_TYPE_CHAR;
else if ( strcmp( PQgetvalue(res, row, 1), "bpchar" ) == 0 )
type = PG_TYPE_BPCHAR;
else if ( strcmp( PQgetvalue(res, row, 1), "varchar" ) == 0 )
type = PG_TYPE_VARCHAR;
else if ( strcmp( PQgetvalue(res, row, 1), "text" ) == 0 )
type = PG_TYPE_TEXT;
else if ( strcmp( PQgetvalue(res, row, 1), "date" ) == 0 )
type = PG_TYPE_DATE;
else if ( strcmp( PQgetvalue(res, row, 1), "time" ) == 0 )
type = PG_TYPE_TIME;
else if ( strcmp( PQgetvalue(res, row, 1), "timestamp" ) == 0 )
type = PG_TYPE_TIMESTAMP;
else if ( strcmp( PQgetvalue(res, row, 1), "bool" ) == 0 )
type = PG_TYPE_BOOL;
else
type = PG_TYPE_UNKNOWN;
G_debug ( 3, "pgtype = %d, \tname = %s -> \ttype = %d", pgtype, PQgetvalue(res, row, 1), type );
pg_types[row][1] = type;
}
PQclear(res);
return DB_OK;
}
int db__driver_close_database()
{
init_error();
PQfinish(pg_conn);
return DB_OK;
}
|