Skip to content

Commit 95ad0ca

Browse files
committed
mappostgis.c: factor out and apply bind values substitution in missing spots
1 parent f681af9 commit 95ad0ca

File tree

1 file changed

+55
-63
lines changed

1 file changed

+55
-63
lines changed

Diff for: mappostgis.c

+55-63
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,54 @@ int msPostGISLayerInitItemInfo(layerObj *layer)
26892689
#endif
26902690
}
26912691

2692+
#ifdef USE_POSTGIS
2693+
static const char** buildBindValues(layerObj *layer, int* p_num_bind_values)
2694+
{
2695+
/* try to get the first bind value */
2696+
char bind_key[20];
2697+
const char* bind_value = msLookupHashTable(&layer->bindvals, "1");
2698+
int num_bind_values = 0;
2699+
const char** layer_bind_values = (const char**)(bind_value ? msSmallMalloc(sizeof(const char*) * 1000) : NULL);
2700+
while(bind_value != NULL && num_bind_values < 1000) {
2701+
/* put the bind value on the stack */
2702+
layer_bind_values[num_bind_values] = bind_value;
2703+
/* increment the counter */
2704+
num_bind_values++;
2705+
/* create a new lookup key */
2706+
sprintf(bind_key, "%d", num_bind_values+1);
2707+
/* get the bind_value */
2708+
bind_value = msLookupHashTable(&layer->bindvals, bind_key);
2709+
}
2710+
*p_num_bind_values = num_bind_values;
2711+
return layer_bind_values;
2712+
}
2713+
2714+
static void freeBindValues(const char** layer_bind_values)
2715+
{
2716+
free((void*)layer_bind_values);
2717+
}
2718+
2719+
static PGresult* runPQexecParamsWithBindSubstitution(layerObj *layer, const char* strSQL, int binary)
2720+
{
2721+
PGresult *pgresult = NULL;
2722+
msPostGISLayerInfo* layerinfo = (msPostGISLayerInfo*) layer->layerinfo;
2723+
2724+
int num_bind_values = 0;
2725+
const char** layer_bind_values = buildBindValues(layer, &num_bind_values);
2726+
2727+
if(num_bind_values > 0) {
2728+
pgresult = PQexecParams(layerinfo->pgconn, strSQL, num_bind_values, NULL, layer_bind_values, NULL, NULL, binary);
2729+
} else {
2730+
pgresult = PQexecParams(layerinfo->pgconn, strSQL, 0, NULL, NULL, NULL, NULL, binary);
2731+
}
2732+
2733+
/* free bind values */
2734+
freeBindValues(layer_bind_values);
2735+
2736+
return pgresult;
2737+
}
2738+
#endif
2739+
26922740
/*
26932741
** msPostGISLayerWhichShapes()
26942742
**
@@ -2700,11 +2748,6 @@ int msPostGISLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
27002748
msPostGISLayerInfo *layerinfo = NULL;
27012749
char *strSQL = NULL;
27022750
PGresult *pgresult = NULL;
2703-
const char** layer_bind_values = NULL;
2704-
const char* bind_value;
2705-
char* bind_key = NULL;
2706-
2707-
int num_bind_values = 0;
27082751

27092752
assert(layer != NULL);
27102753
assert(layer->layerinfo != NULL);
@@ -2718,21 +2761,6 @@ int msPostGISLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
27182761
return MS_FAILURE;
27192762
}
27202763

2721-
/* try to get the first bind value */
2722-
layer_bind_values = (const char**)msSmallMalloc(sizeof(const char*) * 1000);
2723-
bind_key = (char*)msSmallMalloc(3);
2724-
bind_value = msLookupHashTable(&layer->bindvals, "1");
2725-
while(bind_value != NULL) {
2726-
/* put the bind value on the stack */
2727-
layer_bind_values[num_bind_values] = bind_value;
2728-
/* increment the counter */
2729-
num_bind_values++;
2730-
/* create a new lookup key */
2731-
sprintf(bind_key, "%d", num_bind_values+1);
2732-
/* get the bind_value */
2733-
bind_value = msLookupHashTable(&layer->bindvals, bind_key);
2734-
}
2735-
27362764
/*
27372765
** This comes *after* parsedata, because parsedata fills in
27382766
** layer->layerinfo.
@@ -2750,17 +2778,7 @@ int msPostGISLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
27502778
msDebug("msPostGISLayerWhichShapes query: %s\n", strSQL);
27512779
}
27522780

2753-
// fprintf(stderr, "SQL: %s\n", strSQL);
2754-
2755-
if(num_bind_values > 0) {
2756-
pgresult = PQexecParams(layerinfo->pgconn, strSQL, num_bind_values, NULL, layer_bind_values, NULL, NULL, RESULTSET_TYPE);
2757-
} else {
2758-
pgresult = PQexecParams(layerinfo->pgconn, strSQL,0, NULL, NULL, NULL, NULL, RESULTSET_TYPE);
2759-
}
2760-
2761-
/* free bind values */
2762-
free(bind_key);
2763-
free((void*)layer_bind_values);
2781+
pgresult = runPQexecParamsWithBindSubstitution(layer, strSQL, RESULTSET_TYPE);
27642782

27652783
if ( layer->debug > 1 ) {
27662784
msDebug("msPostGISLayerWhichShapes query status: %s (%d)\n", PQresStatus(PQresultStatus(pgresult)), PQresultStatus(pgresult));
@@ -2862,10 +2880,6 @@ int msPostGISLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *re
28622880
char *strSQL = NULL;
28632881
char *strSQLCount = NULL;
28642882
PGresult *pgresult = NULL;
2865-
const char** layer_bind_values = NULL;
2866-
const char* bind_value;
2867-
char* bind_key = NULL;
2868-
int num_bind_values = 0;
28692883
int nCount = 0;
28702884
int rectSRID = -1;
28712885
rectObj searchrectInLayerProj = rect;
@@ -2908,21 +2922,6 @@ int msPostGISLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *re
29082922
return -1;
29092923
}
29102924

2911-
/* try to get the first bind value */
2912-
layer_bind_values = (const char**)msSmallMalloc(sizeof(const char*) * 1000);
2913-
bind_value = msLookupHashTable(&layer->bindvals, "1");
2914-
bind_key = (char*)msSmallMalloc(3);
2915-
while(bind_value != NULL) {
2916-
/* put the bind value on the stack */
2917-
layer_bind_values[num_bind_values] = bind_value;
2918-
/* increment the counter */
2919-
num_bind_values++;
2920-
/* create a new lookup key */
2921-
sprintf(bind_key, "%d", num_bind_values+1);
2922-
/* get the bind_value */
2923-
bind_value = msLookupHashTable(&layer->bindvals, bind_key);
2924-
}
2925-
29262925
/*
29272926
** This comes *after* parsedata, because parsedata fills in
29282927
** layer->layerinfo.
@@ -2948,15 +2947,7 @@ int msPostGISLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *re
29482947
msDebug("msPostGISLayerGetShapeCount query: %s\n", strSQLCount);
29492948
}
29502949

2951-
if(num_bind_values > 0) {
2952-
pgresult = PQexecParams(layerinfo->pgconn, strSQLCount, num_bind_values, NULL, layer_bind_values, NULL, NULL, 1);
2953-
} else {
2954-
pgresult = PQexecParams(layerinfo->pgconn, strSQLCount,0, NULL, NULL, NULL, NULL, 0);
2955-
}
2956-
2957-
/* free bind values */
2958-
free(bind_key);
2959-
free((void*)layer_bind_values);
2950+
pgresult = runPQexecParamsWithBindSubstitution(layer, strSQLCount, 0);
29602951

29612952
if ( layer->debug > 1 ) {
29622953
msDebug("msPostGISLayerWhichShapes query status: %s (%d)\n",
@@ -3085,7 +3076,7 @@ int msPostGISLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
30853076
msDebug("msPostGISLayerGetShape query: %s\n", strSQL);
30863077
}
30873078

3088-
pgresult = PQexecParams(layerinfo->pgconn, strSQL,0, NULL, NULL, NULL, NULL, RESULTSET_TYPE);
3079+
pgresult = runPQexecParamsWithBindSubstitution(layer, strSQL, RESULTSET_TYPE);
30893080

30903081
/* Something went wrong. */
30913082
if ( (!pgresult) || (PQresultStatus(pgresult) != PGRES_TUPLES_OK) ) {
@@ -3317,7 +3308,7 @@ int msPostGISLayerGetItems(layerObj *layer)
33173308
msDebug("msPostGISLayerGetItems executing SQL: %s\n", sql);
33183309
}
33193310

3320-
pgresult = PQexecParams(layerinfo->pgconn, sql,0, NULL, NULL, NULL, NULL, 0);
3311+
pgresult = runPQexecParamsWithBindSubstitution(layer, sql, 0);
33213312

33223313
if ( (!pgresult) || (PQresultStatus(pgresult) != PGRES_TUPLES_OK) ) {
33233314
msDebug("msPostGISLayerGetItems(): Error (%s) executing SQL: %s\n", PQerrorMessage(layerinfo->pgconn), sql);
@@ -3459,7 +3450,7 @@ int msPostGISLayerGetExtent(layerObj *layer, rectObj *extent)
34593450
}
34603451

34613452
/* executing the query */
3462-
pgresult = PQexecParams(layerinfo->pgconn, strSQL,0, NULL, NULL, NULL, NULL, 0);
3453+
pgresult = runPQexecParamsWithBindSubstitution(layer, strSQL, 0);
34633454

34643455
msFree(strSQL);
34653456

@@ -3591,7 +3582,8 @@ int msPostGISLayerGetNumFeatures(layerObj *layer)
35913582
}
35923583

35933584
/* executing the query */
3594-
pgresult = PQexecParams(layerinfo->pgconn, strSQL, 0, NULL, NULL, NULL, NULL, 0);
3585+
3586+
pgresult = runPQexecParamsWithBindSubstitution(layer, strSQL, 0);
35953587

35963588
msFree(strSQL);
35973589

0 commit comments

Comments
 (0)