[GRASS-SVN] r56455 - grass/trunk/lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 28 06:35:48 PDT 2013


Author: martinl
Date: 2013-05-28 06:35:47 -0700 (Tue, 28 May 2013)
New Revision: 56455

Modified:
   grass/trunk/lib/vector/Vlib/build_pg.c
Log:
vlib(pg): fix writing nodes/areas/isles into DB for large array (allocate dynamic buffer for SQL command)


Modified: grass/trunk/lib/vector/Vlib/build_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build_pg.c	2013-05-28 12:55:35 UTC (rev 56454)
+++ grass/trunk/lib/vector/Vlib/build_pg.c	2013-05-28 13:35:47 UTC (rev 56455)
@@ -534,9 +534,8 @@
                 const struct Format_info_pg *pg_info)
 {
     int i, node_id;
-    size_t stmt_lines_size, stmt_angles_size;
-    char *stmt_lines, *stmt_angles;
-    char stmt[DB_SQL_MAX];
+    size_t stmt_lines_size, stmt_angles_size, stmt_size;
+    char *stmt_lines, *stmt_angles, *stmt;
         
     const struct P_node *Node;
     const struct Format_info_offset *offset;
@@ -545,6 +544,9 @@
     if (plus->n_nodes != offset->array_num)
         return -1;
     
+    stmt_size = 2 * DB_SQL_MAX + 512;
+    stmt = (char *) G_malloc(stmt_size);
+    
     stmt_lines = stmt_angles = NULL;
     for (i = 1; i <= plus->n_nodes; i++) {
         Node = plus->Node[i];
@@ -559,6 +561,10 @@
         build_stmt_id(Node->angles, Node->n_lines, FALSE, NULL, &stmt_angles, &stmt_angles_size);
         
         /* build SQL statement to add new node into 'node_grass' */
+        if (stmt_lines_size + stmt_angles_size + 512 > stmt_size) {
+            stmt_size = stmt_lines_size + stmt_angles_size + 512;
+            stmt = (char *) G_realloc(stmt, stmt_size);
+        }
         sprintf(stmt, "INSERT INTO \"%s\".%s VALUES ("
                 "%d, '{%s}', '{%s}')", pg_info->toposchema_name, TOPO_TABLE_NODE,
                 node_id, stmt_lines, stmt_angles);
@@ -569,6 +575,7 @@
 
     G_free(stmt_lines);
     G_free(stmt_angles);
+    G_free(stmt);
     
     return 0;
 }
@@ -591,13 +598,15 @@
                 const struct Format_info_pg *pg_info)
 {
     int area, centroid;
-    size_t stmt_lines_size, stmt_isles_size;
-    char *stmt_lines, *stmt_isles;
-    char stmt[DB_SQL_MAX];
+    size_t stmt_lines_size, stmt_isles_size, stmt_size;
+    char *stmt_lines, *stmt_isles, *stmt;
         
     const struct P_line *Line;
     const struct P_area *Area;
 
+    stmt_size = 2 * DB_SQL_MAX + 512;
+    stmt = (char *) G_malloc(stmt_size);
+    
     stmt_lines = stmt_isles = NULL;
     for (area = 1; area <= plus->n_areas; area++) {
         Area = plus->Area[area];
@@ -625,6 +634,10 @@
         }
         
         /* build SQL statement to add new node into 'node_grass' */
+        if (stmt_lines_size + stmt_isles_size + 512 > stmt_size) {
+            stmt_size = stmt_lines_size + stmt_isles_size + 512;
+            stmt = (char *) G_realloc(stmt, stmt_size);
+        }
         sprintf(stmt, "INSERT INTO \"%s\".%s VALUES ("
                 "%d, '{%s}', %d, '{%s}')", pg_info->toposchema_name, TOPO_TABLE_AREA,
                 area, stmt_lines, centroid, stmt_isles);
@@ -635,6 +648,7 @@
 
     G_free(stmt_lines);
     G_free(stmt_isles);
+    G_free(stmt);
     
     return 0;
 }
@@ -656,12 +670,14 @@
                 const struct Format_info_pg *pg_info)
 {
     int isle;
-    size_t stmt_lines_size;
-    char *stmt_lines;
-    char stmt[DB_SQL_MAX];
+    size_t stmt_lines_size, stmt_size;
+    char *stmt_lines, *stmt;
         
     const struct P_isle *Isle;
 
+    stmt_size = DB_SQL_MAX + 512;
+    stmt = (char *) G_malloc(stmt_size);
+
     stmt_lines = NULL;
     for (isle = 1; isle <= plus->n_isles; isle++) {
         Isle = plus->Isle[isle];
@@ -672,6 +688,10 @@
         build_stmt_id(Isle->lines, Isle->n_lines, TRUE, NULL, &stmt_lines, &stmt_lines_size);
         
         /* build SQL statement to add new node into 'node_grass' */
+        if (stmt_lines_size + 512 > stmt_size) {
+            stmt_size = stmt_lines_size + 512;
+            stmt = (char *) G_realloc(stmt, stmt_size);
+        }
         sprintf(stmt, "INSERT INTO \"%s\".%s VALUES ("
                 "%d, '{%s}', %d)", pg_info->toposchema_name, TOPO_TABLE_ISLE,
                 isle, stmt_lines, Isle->area);
@@ -681,7 +701,8 @@
     }
 
     G_free(stmt_lines);
-    
+    G_free(stmt);
+
     return 0;
 }
 



More information about the grass-commit mailing list