open_pg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*!
  2. \file lib/vector/Vlib/open_pg.c
  3. \brief Vector library - Open PostGIS layer as vector map layer
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2011-2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <grass/vector.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. #ifdef HAVE_POSTGRES
  16. #include "pg_local_proto.h"
  17. static char *get_key_column(struct Format_info_pg *);
  18. static SF_FeatureType ftype_from_string(const char *);
  19. static int drop_table(struct Format_info_pg *);
  20. static int check_schema(const struct Format_info_pg*);
  21. static int create_table(struct Format_info_pg *, const struct field_info *);
  22. #endif
  23. /*!
  24. \brief Open existing PostGIS feature table (level 1 - without topology)
  25. \todo Check database instead of geometry_columns
  26. \param[in,out] Map pointer to Map_info structure
  27. \param update TRUE for write mode, otherwise read-only
  28. \return 0 success
  29. \return -1 error
  30. */
  31. int V1_open_old_pg(struct Map_info *Map, int update)
  32. {
  33. #ifdef HAVE_POSTGRES
  34. int found;
  35. char stmt[DB_SQL_MAX];
  36. PGresult *res;
  37. struct Format_info_pg *pg_info;
  38. pg_info = &(Map->fInfo.pg);
  39. if (!pg_info->conninfo) {
  40. G_warning(_("Connection string not defined"));
  41. return -1;
  42. }
  43. if (!pg_info->table_name) {
  44. G_warning(_("PostGIS feature table not defined"));
  45. return -1;
  46. }
  47. G_debug(1, "V1_open_old_pg(): conninfo='%s' table='%s'", pg_info->conninfo,
  48. pg_info->table_name);
  49. /* connect database */
  50. pg_info->conn = PQconnectdb(pg_info->conninfo);
  51. G_debug(2, " PQconnectdb(): %s", pg_info->conninfo);
  52. if (PQstatus(pg_info->conn) == CONNECTION_BAD)
  53. G_fatal_error("%s\n%s", _("Connection ton PostgreSQL database failed."),
  54. PQerrorMessage(pg_info->conn));
  55. /* get DB name */
  56. pg_info->db_name = G_store(PQdb(pg_info->conn));
  57. if (!pg_info->db_name) {
  58. G_warning(_("Unable to get database name"));
  59. return -1;
  60. }
  61. /* if schema not defined, use 'public' */
  62. if (!pg_info->schema_name) {
  63. pg_info->schema_name = G_store("public");
  64. }
  65. /* get fid and geometry column */
  66. sprintf(stmt, "SELECT f_geometry_column, coord_dimension, srid, type "
  67. "FROM geometry_columns WHERE f_table_schema = '%s' AND "
  68. "f_table_name = '%s'",
  69. pg_info->schema_name, pg_info->table_name);
  70. G_debug(2, "SQL: %s", stmt);
  71. res = PQexec(pg_info->conn, stmt);
  72. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
  73. G_fatal_error("%s\n%s", _("No feature tables found in database."),
  74. PQresultErrorMessage(res));
  75. found = PQntuples(res) > 0 ? TRUE : FALSE;
  76. if (found) {
  77. /* geometry column */
  78. pg_info->geom_column = G_store(PQgetvalue(res, 0, 0));
  79. G_debug(3, "\t-> table = %s column = %s", pg_info->table_name,
  80. pg_info->geom_column);
  81. /* fid column */
  82. pg_info->fid_column = get_key_column(pg_info);
  83. /* coordinates dimension */
  84. pg_info->coor_dim = atoi(PQgetvalue(res, 0, 1));
  85. /* SRS ID */
  86. pg_info->srid = atoi(PQgetvalue(res, 0, 2));
  87. /* feature type */
  88. pg_info->feature_type = ftype_from_string(PQgetvalue(res, 0, 3));
  89. }
  90. PQclear(res);
  91. /* no feature in cache */
  92. pg_info->cache.fid = -1;
  93. if (!found) {
  94. G_warning(_("Feature table <%s> not found in 'geometry_columns'"),
  95. pg_info->table_name);
  96. return -1;
  97. }
  98. return 0;
  99. #else
  100. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  101. return -1;
  102. #endif
  103. }
  104. /*!
  105. \brief Open existing PostGIS layer (level 2 - feature index)
  106. \param[in,out] Map pointer to Map_info structure
  107. \return 0 success
  108. \return -1 error
  109. */
  110. int V2_open_old_pg(struct Map_info *Map)
  111. {
  112. #ifdef HAVE_POSTGRES
  113. G_debug(3, "V2_open_old_pg(): name = %s mapset = %s", Map->name,
  114. Map->mapset);
  115. if (Vect_open_fidx(Map, &(Map->fInfo.pg.offset)) != 0) {
  116. G_warning(_("Unable to open feature index file for vector map <%s>"),
  117. Vect_get_full_name(Map));
  118. G_zero(&(Map->fInfo.pg.offset), sizeof(struct Format_info_offset));
  119. }
  120. return 0;
  121. #else
  122. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  123. return -1;
  124. #endif
  125. }
  126. /*!
  127. \brief Prepare PostGIS database for creating new feature table
  128. (level 1)
  129. \todo To implement
  130. \param[out] Map pointer to Map_info structure
  131. \param name name of PostGIS feature table to create
  132. \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
  133. \return 0 success
  134. \return -1 error
  135. */
  136. int V1_open_new_pg(struct Map_info *Map, const char *name, int with_z)
  137. {
  138. #ifdef HAVE_POSTGRES
  139. char stmt[DB_SQL_MAX];
  140. struct Format_info_pg *pg_info;
  141. PGresult *res;
  142. pg_info = &(Map->fInfo.pg);
  143. if (!pg_info->conninfo) {
  144. G_warning(_("Connection string not defined"));
  145. return -1;
  146. }
  147. if (!pg_info->table_name) {
  148. G_warning(_("PostGIS feature table not defined"));
  149. return -1;
  150. }
  151. G_debug(1, "V1_open_new_pg(): conninfo='%s' table='%s'", pg_info->conninfo,
  152. pg_info->table_name);
  153. /* connect database */
  154. pg_info->conn = PQconnectdb(pg_info->conninfo);
  155. G_debug(2, " PQconnectdb(): %s", pg_info->conninfo);
  156. if (PQstatus(pg_info->conn) == CONNECTION_BAD)
  157. G_fatal_error("%s\n%s", _("Connection ton PostgreSQL database failed."),
  158. PQerrorMessage(pg_info->conn));
  159. /* get DB name */
  160. pg_info->db_name = G_store(PQdb(pg_info->conn));
  161. if (!pg_info->db_name) {
  162. G_warning(_("Unable to get database name"));
  163. return -1;
  164. }
  165. /* if schema not defined, use 'public' */
  166. if (!pg_info->schema_name) {
  167. pg_info->schema_name = G_store("public");
  168. }
  169. /* if fid_column not defined, use 'ogc_fid' */
  170. if (!pg_info->fid_column) {
  171. pg_info->fid_column = G_store("ogc_fid");
  172. }
  173. /* if geom_column not defined, use 'wkb_geometry' */
  174. if (!pg_info->geom_column) {
  175. pg_info->geom_column = G_store("wkb_geometry");
  176. }
  177. /* check if feature table already exists */
  178. sprintf(stmt, "SELECT * FROM pg_tables "
  179. "WHERE schemaname = '%s' AND tablename = '%s'",
  180. pg_info->schema_name, pg_info->table_name);
  181. G_debug(2, "SQL: %s", stmt);
  182. res = PQexec(pg_info->conn, stmt);
  183. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
  184. G_fatal_error("%s\n%s", _("No feature tables found in database."),
  185. PQresultErrorMessage(res));
  186. if (PQntuples(res) > 0) {
  187. /* table found */
  188. if (G_get_overwrite()) {
  189. G_warning(_("PostGIS layer <%s.%s> already exists and will be overwritten"),
  190. pg_info->schema_name, pg_info->table_name);
  191. if (drop_table(pg_info) == -1) {
  192. G_warning(_("Unable to delete PostGIS layer <%s>"),
  193. pg_info->table_name);
  194. return -1;
  195. }
  196. }
  197. else {
  198. G_fatal_error(_("PostGIS layer <%s.%s> already exists in database '%s'"),
  199. pg_info->schema_name, pg_info->table_name,
  200. pg_info->db_name);
  201. return -1;
  202. }
  203. }
  204. /* no feature in cache */
  205. pg_info->cache.fid = -1;
  206. /* unknown feature type */
  207. pg_info->feature_type = SF_UNKNOWN;
  208. PQclear(res);
  209. return 0;
  210. #else
  211. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  212. return -1;
  213. #endif
  214. }
  215. /*!
  216. \brief Create new PostGIS layer in given database (level 2)
  217. V1_open_new_pg() is required to be called before this function.
  218. List of currently supported types:
  219. - GV_POINT (SF_POINT)
  220. - GV_LINE (SF_LINESTRING)
  221. - GV_BOUNDARY (SF_POLYGON)
  222. \param[in,out] Map pointer to Map_info structure
  223. \param type feature type (GV_POINT, GV_LINE, ...)
  224. \return 0 success
  225. \return -1 error
  226. */
  227. int V2_open_new_pg(struct Map_info *Map, int type)
  228. {
  229. #ifdef HAVE_POSTGRES
  230. int ndblinks;
  231. struct Format_info_pg *pg_info;
  232. struct field_info *Fi;
  233. struct Key_Value *projinfo, *projunits;
  234. Fi = NULL;
  235. pg_info = &(Map->fInfo.pg);
  236. if (!pg_info->conninfo) {
  237. G_warning(_("Connection string not defined"));
  238. return -1;
  239. }
  240. if (!pg_info->table_name) {
  241. G_warning(_("PostGIS feature table not defined"));
  242. return -1;
  243. }
  244. G_debug(1, "V2_open_new_pg(): conninfo='%s' table='%s' -> type = %d",
  245. pg_info->conninfo, pg_info->table_name, type);
  246. /* get spatial reference */
  247. projinfo = G_get_projinfo();
  248. projunits = G_get_projunits();
  249. pg_info->srid = 0; /* TODO */
  250. // Ogr_spatial_ref = GPJ_grass_to_osr(projinfo, projunits);
  251. G_free_key_value(projinfo);
  252. G_free_key_value(projunits);
  253. /* determine geometry type */
  254. switch(type) {
  255. case GV_POINT:
  256. pg_info->feature_type = SF_POINT;
  257. break;
  258. case GV_LINE:
  259. pg_info->feature_type = SF_LINESTRING;
  260. break;
  261. case GV_BOUNDARY:
  262. pg_info->feature_type = SF_POLYGON;
  263. break;
  264. default:
  265. G_warning(_("Unsupported geometry type (%d)"), type);
  266. return -1;
  267. }
  268. /* coordinate dimension */
  269. pg_info->coor_dim = Vect_is_3d(Map) ? 3 : 2;
  270. /* create new PostGIS table */
  271. ndblinks = Vect_get_num_dblinks(Map);
  272. if (ndblinks > 0) {
  273. Fi = Vect_get_dblink(Map, 0);
  274. if (Fi) {
  275. if (ndblinks > 1)
  276. G_warning(_("More layers defined, using driver <%s> and "
  277. "database <%s>"), Fi->driver, Fi->database);
  278. }
  279. else {
  280. G_warning(_("Database connection not defined. "
  281. "Unable to write attributes."));
  282. }
  283. }
  284. if (create_table(pg_info, Fi) == -1) {
  285. G_warning(_("Unable to create new PostGIS table"));
  286. return -1;
  287. }
  288. if (Fi)
  289. G_free(Fi);
  290. return 0;
  291. #else
  292. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  293. return -1;
  294. #endif
  295. }
  296. #ifdef HAVE_POSTGRES
  297. char *get_key_column(struct Format_info_pg *pg_info)
  298. {
  299. char *key_column;
  300. char stmt[DB_SQL_MAX];
  301. PGresult *res;
  302. sprintf(stmt,
  303. "SELECT kcu.column_name "
  304. "FROM INFORMATION_SCHEMA.TABLES t "
  305. "LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc "
  306. "ON tc.table_catalog = t.table_catalog "
  307. "AND tc.table_schema = t.table_schema "
  308. "AND tc.table_name = t.table_name "
  309. "AND tc.constraint_type = 'PRIMARY KEY' "
  310. "LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu "
  311. "ON kcu.table_catalog = tc.table_catalog "
  312. "AND kcu.table_schema = tc.table_schema "
  313. "AND kcu.table_name = tc.table_name "
  314. "AND kcu.constraint_name = tc.constraint_name "
  315. "WHERE t.table_schema = '%s' AND t.table_name = '%s'",
  316. pg_info->schema_name, pg_info->table_name);
  317. G_debug(2, "SQL: %s", stmt);
  318. res = PQexec(pg_info->conn, stmt);
  319. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK ||
  320. PQntuples(res) != 1 || strlen(PQgetvalue(res, 0, 0)) < 1) {
  321. G_warning(_("No key column detected."));
  322. if (res)
  323. PQclear(res);
  324. return NULL;
  325. }
  326. key_column = G_store(PQgetvalue(res, 0, 0));
  327. PQclear(res);
  328. return key_column;
  329. }
  330. SF_FeatureType ftype_from_string(const char *type)
  331. {
  332. SF_FeatureType sf_type;
  333. if (G_strcasecmp(type, "POINT") == 0)
  334. return SF_POINT;
  335. else if (G_strcasecmp(type, "LINESTRING") == 0)
  336. return SF_LINESTRING;
  337. else if (G_strcasecmp(type, "POLYGON") == 0)
  338. return SF_POLYGON;
  339. else if (G_strcasecmp(type, "MULTIPOINT") == 0)
  340. return SF_MULTIPOINT;
  341. else if (G_strcasecmp(type, "MULTILINESTRING") == 0)
  342. return SF_MULTILINESTRING;
  343. else if (G_strcasecmp(type, "MULTIPOLYGON") == 0)
  344. return SF_MULTIPOLYGON;
  345. else if (G_strcasecmp(type, "GEOMETRYCOLLECTION") == 0)
  346. return SF_GEOMETRYCOLLECTION;
  347. return SF_UNKNOWN;
  348. G_debug(3, "ftype_from_string(): type='%s' -> %d", type, sf_type);
  349. return sf_type;
  350. }
  351. int drop_table(struct Format_info_pg *pg_info)
  352. {
  353. char stmt[DB_SQL_MAX];
  354. sprintf(stmt, "DROP TABLE \"%s\".\"%s\"",
  355. pg_info->schema_name, pg_info->table_name);
  356. G_debug(2, "SQL: %s", stmt);
  357. if (execute(pg_info->conn, stmt) == -1) {
  358. return -1;
  359. }
  360. return 0;
  361. }
  362. int check_schema(const struct Format_info_pg *pg_info)
  363. {
  364. int i, found, nschema;
  365. char stmt[DB_SQL_MAX];
  366. PGresult *result;
  367. /* add geometry column */
  368. sprintf(stmt, "SELECT nspname FROM pg_namespace");
  369. G_debug(2, "SQL: %s", stmt);
  370. result = PQexec(pg_info->conn, stmt);
  371. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
  372. PQclear(result);
  373. execute(pg_info->conn, "ROLLBACK");
  374. return -1;
  375. }
  376. found = FALSE;
  377. nschema = PQntuples(result);
  378. for (i = 0; i < nschema && !found; i++) {
  379. if (strcmp(pg_info->schema_name, PQgetvalue(result, i, 0)) == 0)
  380. found = TRUE;
  381. }
  382. PQclear(result);
  383. if (!found) {
  384. sprintf(stmt, "CREATE SCHEMA %s", pg_info->schema_name);
  385. if (execute(pg_info->conn, stmt) == -1) {
  386. execute(pg_info->conn, "ROLLBACK");
  387. return -1;
  388. }
  389. G_warning(_("Schema <%s> doesn't exist, created"), pg_info->schema_name);
  390. }
  391. return 0;
  392. }
  393. int create_table(struct Format_info_pg *pg_info, const struct field_info *Fi)
  394. {
  395. int spatial_index, primary_key;
  396. char stmt[DB_SQL_MAX], *geom_type;
  397. PGresult *result;
  398. /* by default create spatial index & add primary key */
  399. spatial_index = primary_key = TRUE;
  400. if (G_find_file2("", "PG", G_mapset())) {
  401. FILE *fp;
  402. const char *p;
  403. struct Key_Value *key_val;
  404. fp = G_fopen_old("", "PG", G_mapset());
  405. if (!fp) {
  406. G_fatal_error(_("Unable to open PG file"));
  407. }
  408. key_val = G_fread_key_value(fp);
  409. fclose(fp);
  410. /* disable spatial index ? */
  411. p = G_find_key_value("spatial_index", key_val);
  412. if (p && G_strcasecmp(p, "off") == 0)
  413. spatial_index = FALSE;
  414. /* disable primary key ? */
  415. p = G_find_key_value("primary_key", key_val);
  416. if (p && G_strcasecmp(p, "off") == 0)
  417. primary_key = FALSE;
  418. }
  419. /* create schema if not exists */
  420. if (G_strcasecmp(pg_info->schema_name, "public") != 0) {
  421. if (check_schema(pg_info) != 0)
  422. return -1;
  423. }
  424. /* prepare CREATE TABLE statement */
  425. sprintf(stmt, "CREATE TABLE \"%s\".\"%s\" (%s SERIAL",
  426. pg_info->schema_name, pg_info->table_name,
  427. pg_info->fid_column);
  428. if (Fi) {
  429. /* append attributes */
  430. int col, ncols, sqltype, length, ctype;
  431. char stmt_col[DB_SQL_MAX];
  432. const char *colname;
  433. dbString dbstmt;
  434. dbHandle handle;
  435. dbDriver *driver;
  436. dbCursor cursor;
  437. dbTable *table;
  438. dbColumn *column;
  439. db_init_string(&dbstmt);
  440. db_init_handle(&handle);
  441. pg_info->dbdriver = driver = db_start_driver(Fi->driver);
  442. if (!driver) {
  443. G_warning(_("Unable to start driver <%s>"), Fi->driver);
  444. return -1;
  445. }
  446. db_set_handle(&handle, Fi->database, NULL);
  447. if (db_open_database(driver, &handle) != DB_OK) {
  448. G_warning(_("Unable to open database <%s> by driver <%s>"),
  449. Fi->database, Fi->driver);
  450. db_close_database_shutdown_driver(driver);
  451. pg_info->dbdriver = NULL;
  452. return -1;
  453. }
  454. /* describe table */
  455. db_set_string(&dbstmt, "select * from ");
  456. db_append_string(&dbstmt, Fi->table);
  457. db_append_string(&dbstmt, " where 0 = 1");
  458. if (db_open_select_cursor(driver, &dbstmt,
  459. &cursor, DB_SEQUENTIAL) != DB_OK) {
  460. G_warning(_("Unable to open select cursor: '%s'"),
  461. db_get_string(&dbstmt));
  462. db_close_database_shutdown_driver(driver);
  463. pg_info->dbdriver = NULL;
  464. return -1;
  465. }
  466. table = db_get_cursor_table(&cursor);
  467. ncols = db_get_table_number_of_columns(table);
  468. G_debug(3, "copying attributes: driver = %s database = %s table = %s cols = %d",
  469. Fi->driver, Fi->database, Fi->table, ncols);
  470. for (col = 0; col < ncols; col++) {
  471. column = db_get_table_column(table, col);
  472. colname = db_get_column_name(column);
  473. sqltype = db_get_column_sqltype(column);
  474. ctype = db_sqltype_to_Ctype(sqltype);
  475. length = db_get_column_length(column);
  476. G_debug(3, "\tcolumn = %d name = %s type = %d length = %d",
  477. col, colname, sqltype, length);
  478. if (strcmp(pg_info->fid_column, colname) == 0) {
  479. /* skip fid column if exists */
  480. G_debug(3, "\t%s skipped", pg_info->fid_column);
  481. continue;
  482. }
  483. /* append column */
  484. sprintf(stmt_col, ",%s %s", colname, db_sqltype_name(sqltype));
  485. strcat(stmt, stmt_col);
  486. if (ctype == DB_C_TYPE_STRING) {
  487. /* length only for string columns */
  488. sprintf(stmt_col, "(%d)", length);
  489. strcat(stmt, stmt_col);
  490. }
  491. }
  492. db_free_string(&dbstmt);
  493. }
  494. strcat(stmt, ")"); /* close CREATE TABLE statement */
  495. /* begin transaction (create table) */
  496. if (execute(pg_info->conn, "BEGIN") == -1) {
  497. return -1;
  498. }
  499. /* create table */
  500. G_debug(2, "SQL: %s", stmt);
  501. if (execute(pg_info->conn, stmt) == -1) {
  502. execute(pg_info->conn, "ROLLBACK");
  503. return -1;
  504. }
  505. /* add primary key ? */
  506. if (primary_key) {
  507. sprintf(stmt, "ALTER TABLE \"%s\".\"%s\" ADD PRIMARY KEY (%s)",
  508. pg_info->schema_name, pg_info->table_name,
  509. pg_info->fid_column);
  510. G_debug(2, "SQL: %s", stmt);
  511. if (execute(pg_info->conn, stmt) == -1) {
  512. execute(pg_info->conn, "ROLLBACK");
  513. return -1;
  514. }
  515. }
  516. /* determine geometry type (string) */
  517. switch(pg_info->feature_type) {
  518. case (SF_POINT):
  519. geom_type = "POINT";
  520. break;
  521. case (SF_LINESTRING):
  522. geom_type = "LINESTRING";
  523. break;
  524. case (SF_POLYGON):
  525. geom_type = "POLYGON";
  526. break;
  527. default:
  528. G_warning(_("Unsupported feature type %d"), pg_info->feature_type);
  529. execute(pg_info->conn, "ROLLBACK");
  530. return -1;
  531. }
  532. /* add geometry column */
  533. sprintf(stmt, "SELECT AddGeometryColumn('%s', '%s', "
  534. "'%s', %d, '%s', %d)",
  535. pg_info->schema_name, pg_info->table_name,
  536. pg_info->geom_column, pg_info->srid,
  537. geom_type, pg_info->coor_dim);
  538. G_debug(2, "SQL: %s", stmt);
  539. result = PQexec(pg_info->conn, stmt);
  540. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
  541. PQclear(result);
  542. execute(pg_info->conn, "ROLLBACK");
  543. return -1;
  544. }
  545. /* create index ? */
  546. if (spatial_index) {
  547. sprintf(stmt, "CREATE INDEX %s_%s_idx ON \"%s\".\"%s\" USING GIST (%s)",
  548. pg_info->table_name, pg_info->geom_column,
  549. pg_info->schema_name, pg_info->table_name,
  550. pg_info->geom_column);
  551. G_debug(2, "SQL: %s", stmt);
  552. if (execute(pg_info->conn, stmt) == -1) {
  553. execute(pg_info->conn, "ROLLBACK");
  554. return -1;
  555. }
  556. }
  557. /* close transaction (create table) */
  558. if (execute(pg_info->conn, "COMMIT") == -1) {
  559. return -1;
  560. }
  561. return 0;
  562. }
  563. #endif