30#include "local_proto.h"
32#include <gdal_version.h>
38#define NOPG_UNUSED UNUSED
61static int name2sql(
char *
name)
70 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z'))) {
72 _(
"Name <%s> is not SQL compliant. Must start with a letter."),
80 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z') ||
81 (*s >=
'0' && *s <=
'9') || *s ==
'_')) {
82 G_debug(2,
"Character '%c' not allowed.", *s);
116 const char *table,
const char *key,
const char *
db,
122 G_warning(
_(
"Layer number must be 1 or greater"));
127 G_warning(
_(
"Unable to add attribute link, vector map is "
128 "not opened in WRITE mode"));
140 G_warning(
_(
"Unable to write attribute links"));
164 for (i = 0; i <
links->n_fields; i++) {
166 for (
j = i;
j <
links->n_fields - 1;
j++) {
171 links->field[
j].database =
links->field[
j + 1].database;
185 G_warning(
_(
"Unable to write database links"));
210 G_warning(
_(
"Database connection not defined. Skipping."));
214 Fi->database,
Fi->driver);
217 G_warning(
_(
"More DB links defined for input vector map. "
218 "Using only first DB link for output."));
251 G_debug(3,
"Vect_check_dblink: field %d, name %s", field,
255 if (p->
field[i].number == field) {
281 const char *
table,
const char *
key,
const char *
db,
362 G_debug(1,
"Vect_default_field_info(): map = %s field = %d",
Map->
name,
383 "driver: %s\ndatabase: %s"),
403 if (!name2sql(fi->
name)) {
416 if (!name2sql(buf)) {
437#ifdef TEMPORARY_MAP_DB
438 if (
Map->temporary) {
441 strcat(buf,
"/sqlite.db");
474 G_debug(1,
"Vect_get_dblink(): link = %d", link);
476 if (link >=
Map->dblnk->n_fields) {
477 G_warning(
_(
"Requested dblink %d, maximum link number %d"), link,
478 Map->dblnk->n_fields - 1);
483 fi->
number =
Map->dblnk->field[link].number;
515 G_debug(1,
"Vect_get_field(): field = %d", field);
517 for (i = 0; i <
Map->dblnk->n_fields; i++) {
518 if (
Map->dblnk->field[i].number == field) {
542 G_debug(1,
"Vect_get_field_by_name(): field = %s", field);
544 for (i = 0; i <
Map->dblnk->n_fields; i++) {
571 G_debug(1,
"Vect_get_field2(): field = %s", field);
607 G_debug(1,
"Vect_get_field_number(): field = %s", field);
609 if (
strcmp(field,
"-1") == 0)
645 char file[1024], buf[2001];
660 G_debug(1,
"Cannot open vector database definition file");
666 while (
G_getl2(buf, 2000, fd)) {
671 c = (
char *)
strchr(buf,
'#');
678#ifdef NOT_ABLE_TO_READ_GRASS_6
683 if (
ndef < 2 || (
ndef < 5 && rule < 1)) {
691 if (
ntok < 2 || (
ntok < 5 && rule < 1)) {
703 for (i = 4; i <
ntok - 1; i++) {
725 "field = %d name = %s, table = %s, key = %s, database = %s, "
744 G_debug(3,
"Searching for FID column in OGR DB");
750 if (
Map->fInfo.ogr.ds ==
NULL) {
756 if (
Map->fInfo.ogr.ds ==
NULL) {
757 G_warning(
_(
"Unable to open OGR data source '%s'"),
762 if (
Map->fInfo.ogr.layer ==
NULL) {
769 G_debug(3,
"Trying to open OGR layer: %s",
Map->fInfo.ogr.layer_name);
770 if (
Map->fInfo.ogr.layer_name) {
772 Map->fInfo.ogr.ds,
Map->fInfo.ogr.layer_name);
773 if (
Map->fInfo.ogr.layer ==
NULL) {
777 Map->fInfo.ogr.layer_name);
803 G_warning(
_(
"Feature table <%s> has no primary key defined. "
804 "Unable to define DB links."),
822 G_warning(
_(
"GRASS not compiled with PostgreSQL support"));
839 G_debug(1,
"Vect_read_dblinks(): map = %s, mapset = %s",
Map->
name,
845 return read_dblinks_nat(
Map);
849 return read_dblinks_ogr(
Map);
852 return read_dblinks_pg(
Map);
880 G_debug(1,
"Vect_write_dblinks(): map = %s, mapset = %s",
Map->
name,
889 _(
"Unable to create database definition file for vector map <%s>"),
894 for (i = 0; i < dbl->
n_fields; i++) {
901 fprintf(fd,
"%s|%s|%s|%s|%s\n", buf, dbl->
field[i].table,
903 dbl->
field[i].driver);
906 dbl->
field[i].driver);
926 char buf[1000], str[1000];
928 G_debug(3,
"Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
942 c = (
char *)
strstr(buf,
"$GISDBASE");
945 snprintf(str,
sizeof(str),
"%s%s%s", buf,
Map->gisdbase, c + 9);
949 c = (
char *)
strstr(buf,
"$LOCATION_NAME");
952 snprintf(str,
sizeof(str),
"%s%s%s", buf,
Map->location, c + 14);
956 c = (
char *)
strstr(buf,
"$MAPSET");
959 snprintf(str,
sizeof(str),
"%s%s%s", buf,
Map->mapset, c + 7);
963 c = (
char *)
strstr(buf,
"$MAP");
987 _(
"Bug: attempt to update map which is not in current mapset"));
AMI_err name(char **stream_name)
Main header of GRASS DataBase Management Interface.
#define DB_DEFAULT_DRIVER
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
int db_set_default_connection(void)
Sets up database connection settings using GRASS default from dbmi.h.
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
char ** G_tokenize(const char *, const char *)
Tokenize string.
void G_free_tokens(char **)
Free memory allocated to tokens.
void void void G_important_message(const char *,...) __attribute__((format(printf
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int G_number_of_tokens(char **)
Return number of tokens.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
char * G_chop(char *)
Chop leading and trailing white spaces.
char * G_store(const char *)
Copy string to allocated memory.
char * G_str_replace(const char *, const char *, const char *)
Replace all occurrences of old_str in buffer with new_str.
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
const char * Vect_get_name(struct Map_info *)
Get name of vector map.
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
#define GV_FORMAT_POSTGIS
PostGIS format.
#define GV_1TABLE
One table linked to vector map.
#define GV_MODE_WRITE
Write vector map open mode.
#define GV_DBLN_ELEMENT
Native format, link to database.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
#define GV_FORMAT_OGR
OGR format.
#define GV_MODE_RW
Read-write vector map open mode.
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
void Vect_set_db_updated(struct Map_info *Map)
Rewrite 'dbln' file.
int Vect_map_del_dblink(struct Map_info *Map, int field)
Delete db connection from Map_info structure.
int Vect_get_field_number(struct Map_info *Map, const char *field)
Get field number of given field.
void Vect_reset_dblinks(struct dblinks *p)
Reset dblinks structure (number of fields)
struct field_info * Vect_get_field2(struct Map_info *Map, const char *field)
Get information about link to database (by layer number or layer name)
struct field_info * Vect_default_field_info(struct Map_info *Map, int field, const char *field_name, int type)
Get default information about link to database for new dblink.
struct field_info * Vect_get_dblink(struct Map_info *Map, int link)
Get information about link to database.
int Vect_add_dblink(struct dblinks *p, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
Add new DB connection to dblinks structure.
int Vect_check_dblink(const struct dblinks *p, int field, const char *name)
Check if DB connection exists in dblinks structure.
int Vect_read_dblinks(struct Map_info *Map)
Read dblinks to existing structure.
char * Vect_subst_var(const char *in, struct Map_info *Map)
Substitute variable in string.
int Vect_map_add_dblink(struct Map_info *Map, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
Add new db connection to Map_info structure.
int Vect_write_dblinks(struct Map_info *Map)
Write dblinks to file.
struct field_info * Vect_get_field_by_name(struct Map_info *Map, const char *field)
Get information about link to database (by layer name)
void Vect_destroy_field_info(struct field_info *fi)
Free a struct field_info and all memory associated with it.
int Vect_map_check_dblink(struct Map_info *Map, int field, const char *name)
Check if DB connection exists in dblinks structure.
struct field_info * Vect_get_field(struct Map_info *Map, int field)
Get information about link to database (by layer number)
void Vect_copy_map_dblinks(struct Map_info *In, struct Map_info *Out, int first_only)
Copy DB links from input vector map to output vector map.
#define GV_KEY_COLUMN
Name of default key column.
int n_fields
Number of available layers (old: fields)
int alloc_fields
Number of allocated slots.
struct field_info * field
Pointer to the first field_info structure.
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * name
Layer name (optional)
char * key
Name of key column (usually 'cat')
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)
char * Vect__get_path(char *path, struct Map_info *Map)
Get map directory name (internal use only)