/*! \brief Open database (OGR datasource) \param handle pointer to dbHandle (db name and schema) \return DB_OK on success \return DB_FAILED on failure */ int db__driver_open_database(dbHandle * handle) { const char *name; dbConnection connection; 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() name = '%s'", name); OGRRegisterAll(); hDs = OGROpen(name, TRUE, NULL); if (hDs == NULL) { append_error(_("Unable to open OGR data source")); report_error(); return DB_FAILED; } G_debug(3, "Datasource opened"); return DB_OK; }
int db__driver_open_database(dbHandle * handle) { char buf[500]; const char *name, *schema, *user, *password; 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; } G_debug(3, "db_driver_open_database(): 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); 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; }
/* create or drop database */ int create_delete_db(dbHandle *handle, int create) { dbString stmt; const char *template_db, *name, *user, *password; 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, " "schema = %s", pgconn.host, pgconn.port, pgconn.options, pgconn.tty, pgconn.dbname, pgconn.user, pgconn.password, pgconn.schema); db_get_login("pg", template_db, &user, &password); pg_conn = PQsetdbLogin(pgconn.host, pgconn.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(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(PQerrorMessage(pg_conn)); db_d_report_error(); PQclear(res); PQfinish(pg_conn); return DB_FAILED; } PQclear(res); PQfinish(pg_conn); return DB_OK; }
int db__driver_open_database(dbHandle * handle) { const char *name; int len; dbConnection connection; char buf[1024]; DIR *dir; struct dirent *ent; char **tokens; int no_tokens, n; G_debug(2, "DBF: db__driver_open_database() name = '%s'", db_get_handle_dbname(handle)); db.name[0] = '\0'; db.tables = NULL; db.atables = 0; db.ntables = 0; db_get_connection(&connection); name = db_get_handle_dbname(handle); /* if name is empty use connection.databaseName */ if (strlen(name) == 0) { name = connection.databaseName; } strcpy(db.name, name); /* open database dir and read table ( *.dbf files ) names * to structure */ /* parse variables in db.name if present */ if (db.name[0] == '$') { tokens = G_tokenize(db.name, "/"); no_tokens = G_number_of_tokens(tokens); db.name[0] = '\0'; /* re-init */ for (n = 0; n < no_tokens; n++) { G_debug(3, "tokens[%d] = %s", n, tokens[n]); if (tokens[n][0] == '$') { G_strchg(tokens[n], '$', ' '); G_chop(tokens[n]); strcat(db.name, G__getenv(tokens[n])); G_debug(3, " -> %s", G__getenv(tokens[n])); } else strcat(db.name, tokens[n]); strcat(db.name, "/"); } G_free_tokens(tokens); } G_debug(2, "db.name = %s", db.name); errno = 0; dir = opendir(db.name); if (dir == NULL) { if (errno == ENOENT) { int status; status = G_mkdir(db.name); if (status != 0) { /* mkdir failed */ append_error("Cannot create dbf database: %s\n", name); report_error(); return DB_FAILED; } } else { /* some other problem */ append_error("Cannot open dbf database: %s\n", name); report_error(); return DB_FAILED; } } while ((ent = readdir(dir))) { len = strlen(ent->d_name) - 4; if ((len > 0) && (G_strcasecmp(ent->d_name + len, ".dbf") == 0)) { strcpy(buf, ent->d_name); buf[len] = '\0'; add_table(buf, ent->d_name); } } closedir(dir); return DB_OK; }
int db__driver_open_database(dbHandle * handle) { char *name; dbConnection default_connection; MYSQL *res; db_get_connection(&default_connection); name = G_store(db_get_handle_dbname(handle)); /* if name is empty use default_connection.databaseName */ if (strlen(name) == 0) name = default_connection.databaseName; G_debug(3, "db_driver_open_database() mysql: database definition = '%s'", name); /* Embedded version */ { char *datadir, *database; char *server_args[4]; char *buf; if (!replace_variables(name, &datadir, &database)) { db_d_append_error(_("Unable parse MySQL embedded database name")); db_d_append_error(mysql_error(connection)); db_d_report_error(); return DB_FAILED; } server_args[0] = "mesql"; /* this string is not used */ G_asprintf(&buf, "--datadir=%s", datadir); server_args[1] = buf; /* With InnoDB it is very slow to close the database */ server_args[2] = "--skip-innodb"; /* OK? */ /* Without --bootstrap it complains about missing * mysql.time_zone_leap_second table */ server_args[3] = "--bootstrap"; /* OK? */ if (mysql_server_init(4, server_args, NULL)) { db_d_append_error(_("Cannot initialize MySQL embedded server")); db_d_append_error(mysql_error(connection)); db_d_report_error(); free(datadir); free(database); return DB_FAILED; } connection = mysql_init(NULL); mysql_options(connection, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); res = mysql_real_connect(connection, NULL, NULL, NULL, database, 0, NULL, 0); free(datadir); free(database); if (res == NULL) { db_d_append_error(_("Unable to connect to MySQL embedded server: ")); db_d_append_error(mysql_error(connection)); db_d_report_error(); return DB_FAILED; } } return DB_OK; }