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
|
/*!
\file db/driver/postgres/db.c
\brief DBMI - Low Level PostgreSQL database driver - open/close database.
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Radim Blazek
\author Create/drop database by Martin Landa <landa.martin gmail.com>
*/
#include <stdlib.h>
#include <string.h>
#include <grass/dbmi.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include "globals.h"
#include "proto.h"
static void notice_processor(void *arg UNUSED, const char *message)
{
/* print notice messages only on verbose level */
if (G_verbose() > G_verbose_std()) {
fprintf(stderr, "%s", message);
}
}
static int create_delete_db(dbHandle *, int);
int db__driver_open_database(dbHandle *handle)
{
char buf[500];
const char *name, *schema, *user, *password, *host, *port;
dbConnection connection;
PGCONN pgconn;
PGresult *res;
int row;
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) {
db_d_report_error();
return DB_FAILED;
}
db_get_login2("pg", name, &user, &password, &host, &port);
pg_conn = PQsetdbLogin(host, port, pgconn.options, pgconn.tty,
pgconn.dbname, user, password);
G_debug(3,
"db_driver_open_database(): host = %s, port = %s, options = %s, "
"tty = %s, "
"dbname = %s, user = %s, password = %s "
"schema = %s",
host, port, pgconn.options, pgconn.tty, pgconn.dbname, user,
password, pgconn.schema);
if (PQstatus(pg_conn) == CONNECTION_BAD) {
db_d_append_error("%s\n%s", _("Connection failed."),
PQerrorMessage(pg_conn));
db_d_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;
}
/* set path to the 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) {
db_d_append_error("%s %s", _("Unable to set schema:"), schema);
db_d_report_error();
PQclear(res);
return DB_FAILED;
}
}
/* read internal codes */
res = PQexec(pg_conn, "select oid, typname from pg_type where typname in ( "
"'bit', 'int2', 'int4', 'int8', 'serial', 'oid', "
"'float4', 'float8', 'numeric', "
"'char', 'bpchar', 'varchar', 'text', "
"'time', 'date', 'timestamp', "
"'bool', 'geometry', 'topogeometry') order by oid");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
db_d_append_error(_("Unable to select data types"));
db_d_report_error();
PQclear(res);
return DB_FAILED;
}
pg_ntypes = PQntuples(res);
pg_types = 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;
G_debug(3, "row = %d value = %s", row, PQgetvalue(res, row, 1));
if (strcmp(PQgetvalue(res, row, 1), "bit") == 0)
type = PG_TYPE_BIT;
else 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 if (strcmp(PQgetvalue(res, row, 1), "geometry") == 0)
type = PG_TYPE_POSTGIS_GEOM;
else if (strcmp(PQgetvalue(res, row, 1), "topogeometry") == 0)
type = PG_TYPE_POSTGIS_TOPOGEOM;
else
type = PG_TYPE_UNKNOWN;
G_debug(
3, "db_driver_open_database(): pgtype = %d, name = %s -> type = %d",
pgtype, PQgetvalue(res, row, 1), type);
pg_types[row][1] = type;
}
/* print notice messages only on verbose level */
PQsetNoticeProcessor(pg_conn, notice_processor, NULL);
PQclear(res);
return DB_OK;
}
int db__driver_close_database(void)
{
PQfinish(pg_conn);
return DB_OK;
}
/*!
\brief Create new empty PostgreSQL database.
\param handle dbHandle
\return DB_OK on success
\return DB_FAILED on failure
*/
int db__driver_create_database(dbHandle *handle)
{
return create_delete_db(handle, TRUE);
}
/*!
\brief Drop existing PostgreSQL database.
\param handle dbHandle
\return DB_OK on success
\return DB_FAILED on failure
*/
int db__driver_delete_database(dbHandle *handle)
{
return create_delete_db(handle, FALSE);
}
/* create or drop database */
int create_delete_db(dbHandle *handle, int create)
{
dbString stmt;
const char *template_db, *name, *user, *password, *host, *port;
PGCONN pgconn;
PGresult *res;
db_init_string(&stmt);
template_db = "template1";
name = db_get_handle_dbname(handle); /* database to create */
if (parse_conn(template_db, &pgconn) == DB_FAILED) {
db_d_report_error();
return DB_FAILED;
}
G_debug(3,
"db_driver_create_database(): host = %s, port = %s, options = %s, "
"tty = %s, "
"dbname = %s, user = %s, password = %s, host = %s, port = %s"
"schema = %s",
pgconn.host, pgconn.port, pgconn.options, pgconn.tty, pgconn.dbname,
pgconn.user, pgconn.password, pgconn.host, pgconn.port,
pgconn.schema);
db_get_login2("pg", template_db, &user, &password, &host, &port);
pg_conn = PQsetdbLogin(host, port, pgconn.options, pgconn.tty,
pgconn.dbname, user, password);
if (PQstatus(pg_conn) == CONNECTION_BAD) {
db_d_append_error(_("Connection failed."));
db_d_append_error("\n");
db_d_append_error("%s", PQerrorMessage(pg_conn));
db_d_report_error();
PQfinish(pg_conn);
return DB_FAILED;
}
/* create new database */
if (create)
db_set_string(&stmt, "CREATE DATABASE ");
else
db_set_string(&stmt, "DROP DATABASE ");
db_append_string(&stmt, name);
res = PQexec(pg_conn, db_get_string(&stmt));
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
if (create)
db_d_append_error(_("Unable to create database <%s>"), name);
else
db_d_append_error(_("Unable to drop database <%s>"), name);
db_d_append_error("\n");
db_d_append_error("%s", PQerrorMessage(pg_conn));
db_d_report_error();
PQclear(res);
PQfinish(pg_conn);
return DB_FAILED;
}
PQclear(res);
PQfinish(pg_conn);
return DB_OK;
}
|