28#include "local_proto.h"
30#include <gdal_version.h>
36#define NOPG_UNUSED G_UNUSED
59static int name2sql(
char *
name)
68 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z'))) {
70 _(
"Name <%s> is not SQL compliant. Must start with a letter."),
78 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z') ||
79 (*s >=
'0' && *s <=
'9') || *s ==
'_')) {
80 G_debug(2,
"Character '%c' not allowed.", *s);
114 const char *table,
const char *key,
const char *
db,
120 G_warning(
_(
"Layer number must be 1 or greater"));
125 G_warning(
_(
"Unable to add attribute link, vector map is "
126 "not opened in WRITE mode"));
138 G_warning(
_(
"Unable to write attribute links"));
162 for (i = 0; i <
links->n_fields; i++) {
164 for (
j = i;
j <
links->n_fields - 1;
j++) {
169 links->field[
j].database =
links->field[
j + 1].database;
183 G_warning(
_(
"Unable to write database links"));
208 G_warning(
_(
"Database connection not defined. Skipping."));
212 Fi->database,
Fi->driver);
215 G_warning(
_(
"More DB links defined for input vector map. "
216 "Using only first DB link for output."));
249 G_debug(3,
"Vect_check_dblink: field %d, name %s", field,
253 if (p->
field[i].number == field) {
279 const char *
table,
const char *
key,
const char *
db,
360 G_debug(1,
"Vect_default_field_info(): map = %s field = %d",
Map->
name,
381 "driver: %s\ndatabase: %s"),
401 if (!name2sql(fi->
name)) {
414 if (!name2sql(buf)) {
435#ifdef TEMPORARY_MAP_DB
436 if (
Map->temporary) {
439 strcat(buf,
"/sqlite.db");
472 G_debug(1,
"Vect_get_dblink(): link = %d", link);
474 if (link >=
Map->dblnk->n_fields) {
475 G_warning(
_(
"Requested dblink %d, maximum link number %d"), link,
476 Map->dblnk->n_fields - 1);
481 fi->
number =
Map->dblnk->field[link].number;
513 G_debug(1,
"Vect_get_field(): field = %d", field);
515 for (i = 0; i <
Map->dblnk->n_fields; i++) {
516 if (
Map->dblnk->field[i].number == field) {
540 G_debug(1,
"Vect_get_field_by_name(): field = %s", field);
542 for (i = 0; i <
Map->dblnk->n_fields; i++) {
569 G_debug(1,
"Vect_get_field2(): field = %s", field);
605 G_debug(1,
"Vect_get_field_number(): field = %s", field);
607 if (
strcmp(field,
"-1") == 0)
643 char file[1024], buf[2001];
658 G_debug(1,
"Cannot open vector database definition file");
664 while (
G_getl2(buf, 2000, fd)) {
669 c = (
char *)
strchr(buf,
'#');
676#ifdef NOT_ABLE_TO_READ_GRASS_6
681 if (
ndef < 2 || (
ndef < 5 && rule < 1)) {
689 if (
ntok < 2 || (
ntok < 5 && rule < 1)) {
701 for (i = 4; i <
ntok - 1; i++) {
723 "field = %d name = %s, table = %s, key = %s, database = %s, "
742 G_debug(3,
"Searching for FID column in OGR DB");
748 if (
Map->fInfo.ogr.ds ==
NULL) {
754 if (
Map->fInfo.ogr.ds ==
NULL) {
755 G_warning(
_(
"Unable to open OGR data source '%s'"),
760 if (
Map->fInfo.ogr.layer ==
NULL) {
767 G_debug(3,
"Trying to open OGR layer: %s",
Map->fInfo.ogr.layer_name);
768 if (
Map->fInfo.ogr.layer_name) {
770 Map->fInfo.ogr.ds,
Map->fInfo.ogr.layer_name);
771 if (
Map->fInfo.ogr.layer ==
NULL) {
775 Map->fInfo.ogr.layer_name);
801 G_warning(
_(
"Feature table <%s> has no primary key defined. "
802 "Unable to define DB links."),
820 G_warning(
_(
"GRASS not compiled with PostgreSQL support"));
837 G_debug(1,
"Vect_read_dblinks(): map = %s, mapset = %s",
Map->
name,
843 return read_dblinks_nat(
Map);
847 return read_dblinks_ogr(
Map);
850 return read_dblinks_pg(
Map);
878 G_debug(1,
"Vect_write_dblinks(): map = %s, mapset = %s",
Map->
name,
887 _(
"Unable to create database definition file for vector map <%s>"),
892 for (i = 0; i < dbl->
n_fields; i++) {
899 fprintf(fd,
"%s|%s|%s|%s|%s\n", buf, dbl->
field[i].table,
901 dbl->
field[i].driver);
904 dbl->
field[i].driver);
924 char buf[1000], str[1000];
926 G_debug(3,
"Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
940 c = (
char *)
strstr(buf,
"$GISDBASE");
943 snprintf(str,
sizeof(str),
"%s%s%s", buf,
Map->gisdbase, c + 9);
947 c = (
char *)
strstr(buf,
"$LOCATION_NAME");
950 snprintf(str,
sizeof(str),
"%s%s%s", buf,
Map->location, c + 14);
954 c = (
char *)
strstr(buf,
"$MAPSET");
957 snprintf(str,
sizeof(str),
"%s%s%s", buf,
Map->mapset, c + 7);
961 c = (
char *)
strstr(buf,
"$MAP");
985 _(
"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)