@@ -2689,6 +2689,54 @@ int msPostGISLayerInitItemInfo(layerObj *layer)
2689
2689
#endif
2690
2690
}
2691
2691
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
+
2692
2740
/*
2693
2741
** msPostGISLayerWhichShapes()
2694
2742
**
@@ -2700,11 +2748,6 @@ int msPostGISLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
2700
2748
msPostGISLayerInfo * layerinfo = NULL ;
2701
2749
char * strSQL = NULL ;
2702
2750
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 ;
2708
2751
2709
2752
assert (layer != NULL );
2710
2753
assert (layer -> layerinfo != NULL );
@@ -2718,21 +2761,6 @@ int msPostGISLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
2718
2761
return MS_FAILURE ;
2719
2762
}
2720
2763
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
-
2736
2764
/*
2737
2765
** This comes *after* parsedata, because parsedata fills in
2738
2766
** layer->layerinfo.
@@ -2750,17 +2778,7 @@ int msPostGISLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
2750
2778
msDebug ("msPostGISLayerWhichShapes query: %s\n" , strSQL );
2751
2779
}
2752
2780
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 );
2764
2782
2765
2783
if ( layer -> debug > 1 ) {
2766
2784
msDebug ("msPostGISLayerWhichShapes query status: %s (%d)\n" , PQresStatus (PQresultStatus (pgresult )), PQresultStatus (pgresult ));
@@ -2862,10 +2880,6 @@ int msPostGISLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *re
2862
2880
char * strSQL = NULL ;
2863
2881
char * strSQLCount = NULL ;
2864
2882
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 ;
2869
2883
int nCount = 0 ;
2870
2884
int rectSRID = -1 ;
2871
2885
rectObj searchrectInLayerProj = rect ;
@@ -2908,21 +2922,6 @@ int msPostGISLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *re
2908
2922
return -1 ;
2909
2923
}
2910
2924
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
-
2926
2925
/*
2927
2926
** This comes *after* parsedata, because parsedata fills in
2928
2927
** layer->layerinfo.
@@ -2948,15 +2947,7 @@ int msPostGISLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *re
2948
2947
msDebug ("msPostGISLayerGetShapeCount query: %s\n" , strSQLCount );
2949
2948
}
2950
2949
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 );
2960
2951
2961
2952
if ( layer -> debug > 1 ) {
2962
2953
msDebug ("msPostGISLayerWhichShapes query status: %s (%d)\n" ,
@@ -3085,7 +3076,7 @@ int msPostGISLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
3085
3076
msDebug ("msPostGISLayerGetShape query: %s\n" , strSQL );
3086
3077
}
3087
3078
3088
- pgresult = PQexecParams ( layerinfo -> pgconn , strSQL , 0 , NULL , NULL , NULL , NULL , RESULTSET_TYPE );
3079
+ pgresult = runPQexecParamsWithBindSubstitution ( layer , strSQL , RESULTSET_TYPE );
3089
3080
3090
3081
/* Something went wrong. */
3091
3082
if ( (!pgresult ) || (PQresultStatus (pgresult ) != PGRES_TUPLES_OK ) ) {
@@ -3317,7 +3308,7 @@ int msPostGISLayerGetItems(layerObj *layer)
3317
3308
msDebug ("msPostGISLayerGetItems executing SQL: %s\n" , sql );
3318
3309
}
3319
3310
3320
- pgresult = PQexecParams ( layerinfo -> pgconn , sql , 0 , NULL , NULL , NULL , NULL , 0 );
3311
+ pgresult = runPQexecParamsWithBindSubstitution ( layer , sql , 0 );
3321
3312
3322
3313
if ( (!pgresult ) || (PQresultStatus (pgresult ) != PGRES_TUPLES_OK ) ) {
3323
3314
msDebug ("msPostGISLayerGetItems(): Error (%s) executing SQL: %s\n" , PQerrorMessage (layerinfo -> pgconn ), sql );
@@ -3459,7 +3450,7 @@ int msPostGISLayerGetExtent(layerObj *layer, rectObj *extent)
3459
3450
}
3460
3451
3461
3452
/* executing the query */
3462
- pgresult = PQexecParams ( layerinfo -> pgconn , strSQL , 0 , NULL , NULL , NULL , NULL , 0 );
3453
+ pgresult = runPQexecParamsWithBindSubstitution ( layer , strSQL , 0 );
3463
3454
3464
3455
msFree (strSQL );
3465
3456
@@ -3591,7 +3582,8 @@ int msPostGISLayerGetNumFeatures(layerObj *layer)
3591
3582
}
3592
3583
3593
3584
/* executing the query */
3594
- pgresult = PQexecParams (layerinfo -> pgconn , strSQL , 0 , NULL , NULL , NULL , NULL , 0 );
3585
+
3586
+ pgresult = runPQexecParamsWithBindSubstitution (layer , strSQL , 0 );
3595
3587
3596
3588
msFree (strSQL );
3597
3589
0 commit comments