field.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*!
  2. \file vector/Vlib/field.c
  3. \brief Vector library - field (layer) related fns.
  4. Higher level functions for reading/writing/manipulating vectors.
  5. \todo see Vect_read_dblinks(); activate auto-FID detection once
  6. OGR_L_GetFIDColumn() is working or solution found if FID not
  7. available
  8. (C) 2001-2009 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  12. \author Update to GRASS 5.7 by Radim Blazek and David D. Gray.
  13. \author Various updates by Martin Landa <landa.martin gmail.com>, 2009
  14. */
  15. #include <grass/config.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <grass/glocale.h>
  20. #include <grass/gis.h>
  21. #include <grass/dbmi.h>
  22. #include <grass/vector.h>
  23. #ifdef HAVE_GDAL
  24. #include <gdal_version.h> /* needed for FID detection */
  25. #endif /* HAVE_GDAL */
  26. /*!
  27. \brief Create and init new dblinks structure
  28. \return pointer to new dblinks structure
  29. */
  30. struct dblinks *Vect_new_dblinks_struct(void)
  31. {
  32. struct dblinks *p;
  33. p = (struct dblinks *)G_malloc(sizeof(struct dblinks));
  34. if (p) {
  35. p->alloc_fields = p->n_fields = 0;
  36. p->field = NULL;
  37. }
  38. return p;
  39. }
  40. /*!
  41. \brief Reset dblinks structure
  42. \param p pointer to existing dblinks structure
  43. */
  44. void Vect_reset_dblinks(struct dblinks *p)
  45. {
  46. p->n_fields = 0;
  47. }
  48. /*!
  49. \brief Add new db connection to Map_info structure
  50. \param Map pointer to Map_info structure
  51. \param number layer number
  52. \param name layer name
  53. \param table table name
  54. \param key key name
  55. \param db database name
  56. \param driver driver name
  57. \return 0 on success
  58. \return -1 on failure
  59. */
  60. int Vect_map_add_dblink(struct Map_info *Map, int number, const char *name,
  61. const char *table, const char *key, const char *db,
  62. const char *driver)
  63. {
  64. int ret;
  65. if (number == 0) {
  66. G_warning(_("Layer number must be 1 or greater"));
  67. return -1;
  68. }
  69. if (Map->mode != GV_MODE_WRITE && Map->mode != GV_MODE_RW) {
  70. G_warning(_("Unable to add database link, map is not opened in WRITE mode"));
  71. return -1;
  72. }
  73. ret = Vect_add_dblink(Map->dblnk, number, name, table, key, db, driver);
  74. if (ret == -1) {
  75. G_warning(_("Unable to add database link"));
  76. return -1;
  77. }
  78. /* write it immediately otherwise it is lost if module crashes */
  79. ret = Vect_write_dblinks(Map);
  80. if (ret == -1) {
  81. G_warning(_("Unable to write database links"));
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. /*!
  87. \brief Delete db connection from Map_info structure
  88. \param Map pointer to Map_info structure
  89. \param field layer number
  90. \return 0 deleted
  91. \return -1 error
  92. */
  93. int Vect_map_del_dblink(struct Map_info *Map, int field)
  94. {
  95. int i, j, ret;
  96. struct dblinks *links;
  97. G_debug(4, "Vect_map_del_dblink() field = %d", field);
  98. links = Map->dblnk;
  99. ret = -1;
  100. for (i = 0; i < links->n_fields; i++) {
  101. if (links->field[i].number == field) { /* field found */
  102. for (j = i; j < links->n_fields - 1; j++) {
  103. links->field[j].number = links->field[j + 1].number;
  104. links->field[j].name = links->field[j + 1].name;
  105. links->field[j].table = links->field[j + 1].table;
  106. links->field[j].key = links->field[j + 1].key;
  107. links->field[j].database = links->field[j + 1].database;
  108. links->field[j].driver = links->field[j + 1].driver;
  109. }
  110. ret = 0;
  111. links->n_fields--;
  112. }
  113. }
  114. if (ret == -1)
  115. return -1;
  116. /* write it immediately otherwise it is lost if module crashes */
  117. ret = Vect_write_dblinks(Map);
  118. if (ret == -1) {
  119. G_warning(_("Unable to write database links"));
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. /*!
  125. \brief Check if DB connection exists in dblinks structure
  126. \param Map pointer to Map_info structure
  127. \param field layer number
  128. \return 1 dblink for field exists
  129. \return 0 dblink does not exist for field
  130. */
  131. int Vect_map_check_dblink(const struct Map_info *Map, int field, const char *name)
  132. {
  133. return Vect_check_dblink(Map->dblnk, field, name);
  134. }
  135. /*!
  136. \brief Check if DB connection exists in dblinks structure
  137. \param p pointer to existing dblinks structure
  138. \param field layer number
  139. \return 1 dblink for field exists
  140. \return 0 dblink does not exist for field
  141. */
  142. int Vect_check_dblink(const struct dblinks *p, int field, const char *name)
  143. {
  144. int i;
  145. G_debug(3, "Vect_check_dblink: field %d, name %s", field, (name != NULL ? name : "not given"));
  146. for (i = 0; i < p->n_fields; i++) {
  147. if (p->field[i].number == field) {
  148. return 1;
  149. }
  150. if (name != NULL && p->field[i].name != NULL) {
  151. if (strcmp(p->field[i].name, name) == 0)
  152. return 1;
  153. }
  154. }
  155. return 0;
  156. }
  157. /*!
  158. \brief Add new DB connection to dblinks structure
  159. \param p pointer to existing dblinks structure
  160. \param number layer number
  161. \param name layer name
  162. \param key key name
  163. \param db database name
  164. \param driver driver name
  165. \return 0 on success
  166. \return -1 error
  167. */
  168. int Vect_add_dblink(struct dblinks *p, int number, const char *name,
  169. const char *table, const char *key, const char *db,
  170. const char *driver)
  171. {
  172. int ret;
  173. G_debug(3, "Field number <%d>, name <%s>", number, name);
  174. ret = Vect_check_dblink(p, number, name);
  175. if (ret == 1) {
  176. G_warning(_("Layer number %d or name <%s> already exists"), number,
  177. name);
  178. return -1;
  179. }
  180. if (p->n_fields == p->alloc_fields) {
  181. p->alloc_fields += 10;
  182. p->field = (struct field_info *)G_realloc((void *)p->field,
  183. p->alloc_fields *
  184. sizeof(struct field_info));
  185. }
  186. p->field[p->n_fields].number = number;
  187. if (name != NULL) {
  188. p->field[p->n_fields].name = G_store(name);
  189. /* replace all spaces with underscore, otherwise dbln can't be read */
  190. G_strchg(p->field[p->n_fields].name, ' ', '_');
  191. }
  192. else
  193. p->field[p->n_fields].name = NULL;
  194. if (table != NULL)
  195. p->field[p->n_fields].table = G_store(table);
  196. else
  197. p->field[p->n_fields].table = NULL;
  198. if (key != NULL)
  199. p->field[p->n_fields].key = G_store(key);
  200. else
  201. p->field[p->n_fields].key = NULL;
  202. if (db != NULL)
  203. p->field[p->n_fields].database = G_store(db);
  204. else
  205. p->field[p->n_fields].database = NULL;
  206. if (driver != NULL)
  207. p->field[p->n_fields].driver = G_store(driver);
  208. else
  209. p->field[p->n_fields].driver = NULL;
  210. p->n_fields++;
  211. return 0;
  212. }
  213. /*!
  214. \brief Get default information about link to database for new dblink
  215. \param Map pointer to Map_info structure
  216. \param field layer number
  217. \param field_name layer name
  218. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  219. \return pointer to new field_info structure
  220. */
  221. struct field_info
  222. *Vect_default_field_info(struct Map_info *Map,
  223. int field, const char *field_name, int type)
  224. {
  225. struct field_info *fi;
  226. char buf[1000], buf2[1000];
  227. const char *schema;
  228. const char *drv, *db;
  229. dbConnection connection;
  230. G_debug(1, "Vect_default_field_info(): map = %s field = %d", Map->name,
  231. field);
  232. db_get_connection(&connection);
  233. drv = G__getenv2("DB_DRIVER", G_VAR_MAPSET);
  234. db = G__getenv2("DB_DATABASE", G_VAR_MAPSET);
  235. G_debug(2, "drv = %s db = %s", drv, db);
  236. if (!connection.driverName && !connection.databaseName) {
  237. /* Set default values and create dbf db dir */
  238. db_set_default_connection();
  239. db_get_connection(&connection);
  240. G_important_message(_("Default driver / database set to:\n"
  241. "driver: %s\ndatabase: %s"), connection.driverName,
  242. connection.databaseName);
  243. }
  244. /* they must be a matched pair, so if one is set but not the other
  245. then give up and let the user figure it out */
  246. else if (!connection.driverName) {
  247. G_fatal_error(_("Default driver is not set"));
  248. }
  249. else if (!connection.databaseName) {
  250. G_fatal_error(_("Default database is not set"));
  251. }
  252. drv = connection.driverName;
  253. db = connection.databaseName;
  254. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  255. fi->number = field;
  256. if (field_name != NULL)
  257. fi->name = G_store(field_name);
  258. else
  259. fi->name = NULL;
  260. /* Table name */
  261. if (type == GV_1TABLE) {
  262. sprintf(buf, "%s", Map->name);
  263. }
  264. else {
  265. if (field_name != NULL && strlen(field_name) > 0)
  266. sprintf(buf, "%s_%s", Map->name, field_name);
  267. else
  268. sprintf(buf, "%s_%d", Map->name, field);
  269. }
  270. schema = connection.schemaName;
  271. if (schema && strlen(schema) > 0) {
  272. sprintf(buf2, "%s.%s", schema, buf);
  273. fi->table = G_store(buf2);
  274. }
  275. else {
  276. fi->table = G_store(buf);
  277. }
  278. fi->key = G_store("cat"); /* Should be: id/fid/gfid/... ? */
  279. fi->database = G_store(db);
  280. fi->driver = G_store(drv);
  281. return (fi);
  282. }
  283. /*!
  284. \brief Get information about link to database.
  285. Variables are substituted by values, link is index to array of
  286. dblinks.
  287. \param Map pointer to Map_info structure
  288. \param link link id
  289. \return pointer to new field_info structure
  290. */
  291. struct field_info *Vect_get_dblink(const struct Map_info *Map, int link)
  292. {
  293. struct field_info *fi;
  294. G_debug(1, "Vect_get_dblink(): link = %d", link);
  295. if (link >= Map->dblnk->n_fields) {
  296. G_warning(_("Requested dblink %d, maximum link number %d"), link,
  297. Map->dblnk->n_fields - 1);
  298. return NULL;
  299. }
  300. fi = (struct field_info *)malloc(sizeof(struct field_info));
  301. fi->number = Map->dblnk->field[link].number;
  302. if (Map->dblnk->field[link].name != NULL)
  303. fi->name = G_store(Map->dblnk->field[link].name);
  304. else
  305. fi->name = NULL;
  306. fi->table = G_store(Map->dblnk->field[link].table);
  307. fi->key = G_store(Map->dblnk->field[link].key);
  308. fi->database = Vect_subst_var(Map->dblnk->field[link].database, Map);
  309. fi->driver = G_store(Map->dblnk->field[link].driver);
  310. return fi;
  311. }
  312. /*!
  313. \brief Get information about link to database (by layer number)
  314. Variables are substituted by values, field is number of requested
  315. field.
  316. \param Map pointer to Map_info structure
  317. \param field layer number
  318. \return pointer to new field_info structure
  319. \return NULL if not found
  320. */
  321. struct field_info *Vect_get_field(const struct Map_info *Map, int field)
  322. {
  323. int i;
  324. struct field_info *fi = NULL;
  325. G_debug(1, "Vect_get_field(): field = %d", field);
  326. for (i = 0; i < Map->dblnk->n_fields; i++) {
  327. if (Map->dblnk->field[i].number == field) {
  328. fi = Vect_get_dblink(Map, i);
  329. break;
  330. }
  331. }
  332. return fi;
  333. }
  334. /*!
  335. \brief Get information about link to database (by layer name)
  336. \param Map pointer to Map_info structure
  337. \param field layer name
  338. \return pointer to new field_info structure
  339. \return NULL if not found
  340. */
  341. struct field_info *Vect_get_field_by_name(const struct Map_info *Map, const char *field)
  342. {
  343. int i;
  344. struct field_info *fi = NULL;
  345. G_debug(1, "Vect_get_field_by_name(): field = %s", field);
  346. for (i = 0; i < Map->dblnk->n_fields; i++) {
  347. if (Map->dblnk->field[i].name == field) {
  348. fi = Vect_get_dblink(Map, i);
  349. break;
  350. }
  351. }
  352. return fi;
  353. }
  354. /*!
  355. \brief Read dblinks to existing structure.
  356. Variables are not substituted by values.
  357. \param Map pointer to Map_info structure
  358. \return number of links read
  359. \return -1 on error
  360. */
  361. int Vect_read_dblinks(struct Map_info *Map)
  362. {
  363. int ndef;
  364. FILE *fd;
  365. char file[1024], buf[2001];
  366. char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
  367. int fld;
  368. char *c;
  369. int row, rule;
  370. struct dblinks *dbl;
  371. G_debug(1, "Vect_read_dblinks(): map = %s, mapset = %s", Map->name,
  372. Map->mapset);
  373. dbl = Map->dblnk;
  374. Vect_reset_dblinks(dbl);
  375. G_debug(3, "Searching for FID column in OGR DB");
  376. if (Map->format == GV_FORMAT_OGR) {
  377. #ifndef HAVE_GDAL
  378. G_fatal_error(_("The support for OGR vector maps wasn't"
  379. " compiled in"));
  380. #else
  381. #if GDAL_VERSION_NUM > 1320 /* seems to be fixed after 1320 release */
  382. int layer, nLayers;
  383. OGRDataSourceH Ogr_ds;
  384. OGRLayerH Ogr_layer = NULL;
  385. OGRFeatureDefnH Ogr_featuredefn;
  386. char ogr_fid_col[1024];
  387. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  388. /* we open the connection to fetch the FID column name */
  389. OGRRegisterAll();
  390. /*Data source handle */
  391. Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  392. if (Ogr_ds == NULL)
  393. G_fatal_error(_("Unable to open OGR data source '%s'"),
  394. Map->fInfo.ogr.dsn);
  395. Map->fInfo.ogr.ds = Ogr_ds;
  396. /* Layer number */
  397. layer = -1;
  398. nLayers = OGR_DS_GetLayerCount(Ogr_ds); /* Layers = Maps in OGR DB */
  399. G_debug(3, "%d layers (maps) found in data source", nLayers);
  400. G_debug(3, "Trying to open OGR layer: %s", Map->fInfo.ogr.layer_name);
  401. if (Map->fInfo.ogr.layer_name) {
  402. Ogr_layer = OGR_DS_GetLayerByName(Ogr_ds, Map->fInfo.ogr.layer_name);
  403. if (Ogr_layer == NULL) {
  404. OGR_DS_Destroy(Ogr_ds);
  405. G_fatal_error(_("Unable to open OGR layer '%s'"),
  406. Map->fInfo.ogr.layer_name);
  407. }
  408. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  409. G_debug(3, "layer %s, FID col name: %s",
  410. OGR_FD_GetName(Ogr_featuredefn),
  411. OGR_L_GetFIDColumn(Ogr_layer));
  412. Map->fInfo.ogr.layer = Ogr_layer;
  413. G_debug(3, "OGR Map->fInfo.ogr.layer %p opened",
  414. Map->fInfo.ogr.layer);
  415. /* TODO what to do if OGR_L_GetFIDColumn() doesn't return FID name */
  416. sprintf(ogr_fid_col, "%s", OGR_L_GetFIDColumn(Map->fInfo.ogr.layer));
  417. G_debug(3, "Using FID column <%s> in OGR DB", ogr_fid_col);
  418. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name, ogr_fid_col,
  419. Map->fInfo.ogr.dsn, "ogr");
  420. }
  421. #else
  422. dbDriver *driver;
  423. dbCursor cursor;
  424. dbString sql;
  425. int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
  426. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  427. /* FID is not available for all OGR drivers */
  428. db_init_string(&sql);
  429. driver = db_start_driver_open_database("ogr", Map->fInfo.ogr.dsn);
  430. if (driver == NULL) {
  431. G_warning(_("Unable to open OGR DBMI driver"));
  432. return -1;
  433. }
  434. /* this is a bit stupid, but above FID auto-detection doesn't work yet...: */
  435. db_auto_print_errors(0);
  436. sprintf(buf, "select FID from %s where FID > 0",
  437. Map->fInfo.ogr.layer_name);
  438. db_set_string(&sql, buf);
  439. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  440. DB_OK) {
  441. /* FID not available, so we try ogc_fid */
  442. G_debug(3, "Failed. Now searching for ogc_fid column in OGR DB");
  443. sprintf(buf, "select ogc_fid from %s where ogc_fid > 0",
  444. Map->fInfo.ogr.layer_name);
  445. db_set_string(&sql, buf);
  446. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  447. DB_OK) {
  448. /* Neither FID nor ogc_fid available, so we try ogr_fid */
  449. G_debug(3,
  450. "Failed. Now searching for ogr_fid column in OGR DB");
  451. sprintf(buf, "select ogr_fid from %s where ogr_fid > 0",
  452. Map->fInfo.ogr.layer_name);
  453. db_set_string(&sql, buf);
  454. if (db_open_select_cursor
  455. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  456. /* Neither FID nor ogc_fid available, so we try gid */
  457. G_debug(3,
  458. "Failed. Now searching for gid column in OGR DB");
  459. sprintf(buf, "select gid from %s where gid > 0",
  460. Map->fInfo.ogr.layer_name);
  461. db_set_string(&sql, buf);
  462. if (db_open_select_cursor
  463. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  464. /* neither FID nor ogc_fid nor ogr_fid nor gid available */
  465. G_warning(_("All FID tests failed. Neither 'FID' nor 'ogc_fid' "
  466. "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
  467. db_close_database_shutdown_driver(driver);
  468. return 0;
  469. }
  470. else
  471. GID = 1;
  472. }
  473. else
  474. OGR_FID = 1;
  475. }
  476. else
  477. OGC_FID = 1;
  478. }
  479. else
  480. FID = 1;
  481. G_debug(3, "FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
  482. OGR_FID, GID);
  483. db_close_cursor(&cursor);
  484. db_close_database_shutdown_driver(driver);
  485. db_auto_print_errors(1);
  486. if (FID) {
  487. G_debug(3, "Using FID column in OGR DB");
  488. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name, "FID",
  489. Map->fInfo.ogr.dsn, "ogr");
  490. }
  491. else {
  492. if (OGC_FID) {
  493. G_debug(3, "Using ogc_fid column in OGR DB");
  494. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name,
  495. "ogc_fid", Map->fInfo.ogr.dsn, "ogr");
  496. }
  497. else {
  498. if (OGR_FID) {
  499. G_debug(3, "Using ogr_fid column in OGR DB");
  500. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name,
  501. "ogr_fid", Map->fInfo.ogr.dsn, "ogr");
  502. }
  503. else {
  504. if (GID) {
  505. G_debug(3, "Using gid column in OGR DB");
  506. Vect_add_dblink(dbl, 1, NULL,
  507. Map->fInfo.ogr.layer_name, "gid",
  508. Map->fInfo.ogr.dsn, "ogr");
  509. }
  510. }
  511. }
  512. }
  513. #endif /* GDAL_VERSION_NUM > 1320 */
  514. return (1);
  515. #endif /* HAVE_GDAL */
  516. }
  517. else if (Map->format != GV_FORMAT_NATIVE) {
  518. G_fatal_error(_("Don't know how to read links for format %d"),
  519. Map->format);
  520. }
  521. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  522. Map->mapset, GV_DIRECTORY, Map->name,
  523. GV_DBLN_ELEMENT);
  524. G_debug(1, "dbln file: %s", file);
  525. fd = fopen(file, "r");
  526. if (fd == NULL) { /* This may be correct, no tables defined */
  527. G_debug(1, "Cannot open vector database definition file");
  528. return (-1);
  529. }
  530. row = 0;
  531. rule = 0;
  532. while (G_getl2(buf, 2000, fd)) {
  533. row++;
  534. G_chop(buf);
  535. G_debug(1, "dbln: %s", buf);
  536. c = (char *)strchr(buf, '#');
  537. if (c != NULL)
  538. *c = '\0';
  539. if (strlen(buf) == 0)
  540. continue;
  541. ndef = sscanf(buf, "%s %s %s %s %s", fldstr, tab, col, db, drv);
  542. if (ndef < 2 || (ndef < 5 && rule < 1)) {
  543. G_warning(_("Error in rule on row %d in %s"), row, file);
  544. continue;
  545. }
  546. /* get field and field name */
  547. fldname = strchr(fldstr, '/');
  548. if (fldname != NULL) { /* field has name */
  549. fldname[0] = 0;
  550. fldname++;
  551. }
  552. fld = atoi(fldstr);
  553. Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv);
  554. G_debug(1,
  555. "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
  556. fld, fldname, tab, col, db, drv);
  557. rule++;
  558. }
  559. fclose(fd);
  560. G_debug(1, "Dblinks read");
  561. return (rule);
  562. }
  563. /*!
  564. \brief Write dblinks to file
  565. \param Map pointer to Map_info structure
  566. \return 0 on success
  567. \return -1 on error
  568. */
  569. int Vect_write_dblinks(struct Map_info *Map)
  570. {
  571. int i;
  572. FILE *fd;
  573. char file[GPATH_MAX], buf[GPATH_MAX];
  574. struct dblinks *dbl;
  575. G_debug(1, "Vect_write_dblinks(): map = %s, mapset = %s", Map->name,
  576. Map->mapset);
  577. dbl = Map->dblnk;
  578. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  579. Map->mapset, GV_DIRECTORY, Map->name,
  580. GV_DBLN_ELEMENT);
  581. G_debug(1, "dbln file: %s", file);
  582. fd = fopen(file, "w");
  583. if (fd == NULL) { /* This may be correct, no tables defined */
  584. G_warning(_("Unable to open vector database definition file '%s'"),
  585. file);
  586. return (-1);
  587. }
  588. for (i = 0; i < dbl->n_fields; i++) {
  589. if (dbl->field[i].name != NULL)
  590. sprintf(buf, "%d/%s", dbl->field[i].number, dbl->field[i].name);
  591. else
  592. sprintf(buf, "%d", dbl->field[i].number);
  593. fprintf(fd, "%s %s %s %s %s\n", buf, dbl->field[i].table,
  594. dbl->field[i].key, dbl->field[i].database,
  595. dbl->field[i].driver);
  596. G_debug(1, "%s %s %s %s %s", buf, dbl->field[i].table,
  597. dbl->field[i].key, dbl->field[i].database,
  598. dbl->field[i].driver);
  599. }
  600. fclose(fd);
  601. G_debug(1, "Dblinks written");
  602. return 0;
  603. }
  604. /*!
  605. \brief Substitute variable in string
  606. \param in current string
  607. \param Map pointer to Map_info structure
  608. \return pointer to new string
  609. */
  610. char *Vect_subst_var(const char *in, const struct Map_info *Map)
  611. {
  612. char *c;
  613. char buf[1000], str[1000];
  614. G_debug(3, "Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
  615. Map->name, Map->mapset);
  616. strcpy(str, in);
  617. strcpy(buf, str);
  618. c = (char *)strstr(buf, "$GISDBASE");
  619. if (c != NULL) {
  620. *c = '\0';
  621. sprintf(str, "%s%s%s", buf, Map->gisdbase, c + 9);
  622. }
  623. strcpy(buf, str);
  624. c = (char *)strstr(buf, "$LOCATION_NAME");
  625. if (c != NULL) {
  626. *c = '\0';
  627. sprintf(str, "%s%s%s", buf, Map->location, c + 14);
  628. }
  629. strcpy(buf, str);
  630. c = (char *)strstr(buf, "$MAPSET");
  631. if (c != NULL) {
  632. *c = '\0';
  633. sprintf(str, "%s%s%s", buf, Map->mapset, c + 7);
  634. }
  635. strcpy(buf, str);
  636. c = (char *)strstr(buf, "$MAP");
  637. if (c != NULL) {
  638. *c = '\0';
  639. sprintf(str, "%s%s%s", buf, Map->name, c + 4);
  640. }
  641. G_debug(3, " -> %s", str);
  642. return (G_store(str));
  643. }
  644. /*!
  645. \brief Rewrite 'dbln' file
  646. Should be used by GRASS modules which update database tables, so
  647. that other applications know that tables were changed and can
  648. reload data.
  649. \param Map pointer to Map_info structure
  650. */
  651. void Vect_set_db_updated(struct Map_info *Map)
  652. {
  653. if (strcmp(Map->mapset, G_mapset()) != 0) {
  654. G_fatal_error(_("Bug: attempt to update map which is not in current mapset"));
  655. }
  656. Vect_write_dblinks(Map);
  657. }