[mapserver-commits] r9236 - trunk/mapserver

svn at osgeo.org svn at osgeo.org
Thu Jul 30 15:28:35 EDT 2009


Author: pramsey
Date: 2009-07-30 15:28:35 -0400 (Thu, 30 Jul 2009)
New Revision: 9236

Modified:
   trunk/mapserver/mappostgis.c
Log:
Implement result set get shape for single-pass query (#3069)


Modified: trunk/mapserver/mappostgis.c
===================================================================
--- trunk/mapserver/mappostgis.c	2009-07-29 03:43:15 UTC (rev 9235)
+++ trunk/mapserver/mappostgis.c	2009-07-30 19:28:35 UTC (rev 9236)
@@ -1291,7 +1291,7 @@
 
 }
 
-int msPostGISReadShape(layerObj *layer, shapeObj *shape) {
+int msPostGISReadShape(layerObj *layer, shapeObj *shape, int random) {
 
     char *wkbstr = NULL;
     unsigned char *wkb = NULL;
@@ -1388,17 +1388,22 @@
                 msDebug("msPostGISReadShape: [%s] \"%s\"\n", layer->items[t], shape->values[t]);
             }
         }
-        /* t is the geometry, t+1 is the uid */
-        tmp = PQgetvalue(layerinfo->pgresult, layerinfo->rownum, t + 1);
-        if( tmp ) {
-            uid = strtol( tmp, NULL, 10 );
+        
+        if( random ) {
+            /* t is the geometry, t+1 is the uid */
+            tmp = PQgetvalue(layerinfo->pgresult, layerinfo->rownum, t + 1);
+            if( tmp ) {
+                uid = strtol( tmp, NULL, 10 );
+            }
+            else {
+                uid = 0;
+            }
+
+            shape->index = uid;
+        } else {
+            shape->index = layerinfo->rownum;
         }
-        else {
-            uid = 0;
-        }
 
-        shape->index = uid;
-
         if( layer->debug > 2 ) {
             msDebug("msPostGISReadShape: [index] %d\n",  shape->index);
         }
@@ -1700,7 +1705,7 @@
     pgresult = PQexec(layerinfo->pgconn, strSQL);
 
     if ( layer->debug > 1 ) {
-        msDebug("msPostGISLayerWhichShapes query status: %d\n", PQresultStatus(pgresult));
+        msDebug("msPostGISLayerWhichShapes query status: %s (%d)\n", PQresStatus(PQresultStatus(pgresult)), PQresultStatus(pgresult)); 
     }
 
     /* Something went wrong. */
@@ -1762,8 +1767,8 @@
     while (shape->type == MS_SHAPE_NULL) {
         if (layerinfo->rownum < PQntuples(layerinfo->pgresult)) {
             int rv;
-            /* Retrieve this shape. */
-            rv = msPostGISReadShape(layer, shape);
+            /* Retrieve this shape, random access mode. */
+            rv = msPostGISReadShape(layer, shape, 1);
             if( shape->type != MS_SHAPE_NULL ) {
                 (layerinfo->rownum)++; /* move to next shape */
                 return MS_SUCCESS;
@@ -1788,6 +1793,71 @@
 }
 
 /*
+** msPostGISLayerResultsGetShape()
+**
+** Registered vtable->LayerGetShape function. For pulling from a prepared and 
+** undisposed result set. We ignore the 'tile' parameter, as it means nothing to us.
+*/
+int msPostGISLayerResultsGetShape(layerObj *layer, shapeObj *shape, int tile, long record) {
+#ifdef USE_POSTGIS
+    
+    PGresult *pgresult = NULL;
+    msPostGISLayerInfo *layerinfo = NULL;
+	int result = MS_SUCCESS;
+
+    assert(layer != NULL);
+    assert(layer->layerinfo != NULL);
+
+    if (layer->debug) {
+        msDebug("msPostGISLayerGetShape called for record = %i\n", record);
+    }
+
+    layerinfo = (msPostGISLayerInfo*) layer->layerinfo;
+
+    /* Check the validity of the open result. */
+    pgresult = layerinfo->pgresult;
+    if ( ! pgresult ) {
+        msSetError( MS_MISCERR,
+                    "PostgreSQL result set is null.",
+                    "msPostGISLayerResultsGetShape()");
+        return MS_FAILURE;
+    }
+    if ( layer->debug > 1 ) {
+        msDebug("msPostGISLayerResultsGetShape query status: %s (%d)\n", PQresStatus(PQresultStatus(pgresult)), PQresultStatus(pgresult));
+    }    
+    if( PQresultStatus(pgresult) != PGRES_COMMAND_OK ) {
+        msSetError( MS_MISCERR,
+                    "PostgreSQL result set is not ready.",
+                    "msPostGISLayerResultsGetShape()");
+        return MS_FAILURE;
+    }
+
+    /* Check the validity of the requested record number. */
+    if( record >= PQntuples(pgresult) ) {
+        msSetError( MS_MISCERR,
+                    "PostgreSQL result set is not ready.",
+                    "msPostGISLayerResultsGetShape()");
+        return MS_FAILURE;
+    }
+
+    layerinfo->rownum = record; /* Only return one result. */
+
+    /* We don't know the shape type until we read the geometry. */
+    shape->type = MS_SHAPE_NULL;
+
+	/* Return the shape, result set mode. */
+    result = msPostGISReadShape(layer, shape, 0);
+
+    return (shape->type == MS_SHAPE_NULL) ? MS_FAILURE : MS_SUCCESS;
+#else
+    msSetError( MS_MISCERR,
+                "PostGIS support is not available.",
+                "msPostGISLayerResultsGetShape()");
+    return MS_FAILURE;
+#endif
+}
+
+/*
 ** msPostGISLayerGetShape()
 **
 ** Registered vtable->LayerGetShape function. We ignore the 'tile' 
@@ -1862,7 +1932,8 @@
     }
 
     if (num_tuples > 0) {
-         result = msPostGISReadShape(layer, shape);
+        /* Get shape in random access mode. */
+        result = msPostGISReadShape(layer, shape, 1);
     }
 
     return (shape->type == MS_SHAPE_NULL) ? MS_FAILURE : ( (num_tuples > 0) ? MS_SUCCESS : MS_DONE );
@@ -2397,7 +2468,7 @@
     layer->vtable->LayerIsOpen = msPostGISLayerIsOpen;
     layer->vtable->LayerWhichShapes = msPostGISLayerWhichShapes;
     layer->vtable->LayerNextShape = msPostGISLayerNextShape;
-    layer->vtable->LayerResultsGetShape = msPostGISLayerGetShape; /* no special version, use ...GetShape() */
+    layer->vtable->LayerResultsGetShape = msPostGISLayerResultsGetShape; 
     layer->vtable->LayerGetShape = msPostGISLayerGetShape;
     layer->vtable->LayerClose = msPostGISLayerClose;
     layer->vtable->LayerGetItems = msPostGISLayerGetItems;



More information about the mapserver-commits mailing list