@@ -40,8 +40,10 @@ struct mapcache_dimension_postgresql {
4040 char * dbconnection ;
4141 char * get_values_for_entry_query ;
4242 char * get_all_values_query ;
43+ char * get_default_value_query ;
4344 apr_hash_t * get_values_indexes ;
4445 apr_hash_t * get_all_indexes ;
46+ apr_hash_t * get_default_value_indexes ;
4547};
4648
4749struct postgresql_dimension_conn {
@@ -74,9 +76,10 @@ static int qparam(mapcache_context *ctx, char *qstring, const char *param, int i
7476static void parse_queries (mapcache_context * ctx , mapcache_dimension_postgresql * dim ) {
7577 const char * keys [9 ] = {":tileset" ,":dim" ,":gridsrs" ,":minx" ,":maxx" ,":miny" ,":maxy" ,":start_timestamp" ,":end_timestamp" };
7678 int i ;
77- int gaidx = 1 ,gvidx = 1 ;
79+ int gaidx = 1 ,gvidx = 1 , gdidx = 1 ;
7880 dim -> get_all_indexes = apr_hash_make (ctx -> pool );
7981 dim -> get_values_indexes = apr_hash_make (ctx -> pool );
82+ dim -> get_default_value_indexes = apr_hash_make (ctx -> pool );
8083 for (i = 0 ;i < 9 ;i ++ ) {
8184 if (qparam (ctx ,dim -> get_all_values_query ,keys [i ],gaidx )) {
8285 apr_hash_set (dim -> get_all_indexes ,keys [i ],APR_HASH_KEY_STRING ,INT2VOIDP (gaidx ));
@@ -86,6 +89,12 @@ static void parse_queries(mapcache_context *ctx, mapcache_dimension_postgresql *
8689 apr_hash_set (dim -> get_values_indexes ,keys [i ],APR_HASH_KEY_STRING ,INT2VOIDP (gvidx ));
8790 gvidx ++ ;
8891 }
92+ if (dim -> get_default_value_query ){
93+ if (qparam (ctx ,dim -> get_default_value_query ,keys [i ],gdidx )) {
94+ apr_hash_set (dim -> get_default_value_indexes ,keys [i ],APR_HASH_KEY_STRING ,INT2VOIDP (gdidx ));
95+ gdidx ++ ;
96+ }
97+ }
8998 }
9099}
91100
@@ -220,6 +229,14 @@ void mapcache_postgresql_dimension_connection_constructor(mapcache_context *ctx,
220229 * conn_ = NULL ;
221230 return ;
222231 }
232+ if (dim -> get_default_value_query ){
233+ prepare_query (ctx ,conn -> pgconn , "get_default_value" , dim -> get_default_value_query , dim -> get_default_value_indexes );
234+ if (GC_HAS_ERROR (ctx )) {
235+ PQfinish (conn -> pgconn );
236+ * conn_ = NULL ;
237+ return ;
238+ }
239+ }
223240}
224241
225242void mapcache_postgresql_dimension_connection_destructor (void * conn_ )
@@ -330,6 +347,47 @@ static apr_array_header_t* _mapcache_dimension_postgresql_get_all_entries(mapcac
330347
331348}
332349
350+ static apr_array_header_t * _mapcache_dimension_postgresql_get_default_entries (mapcache_context * ctx , mapcache_dimension * dim ,
351+ mapcache_tileset * tileset , mapcache_extent * extent , mapcache_grid * grid ) {
352+ mapcache_dimension_postgresql * sdim = (mapcache_dimension_postgresql * )dim ;
353+ PGresult * res ;
354+ apr_array_header_t * time_ids = NULL ;
355+ mapcache_pooled_connection * pc ;
356+ struct postgresql_dimension_conn * conn ;
357+ int nParams , * paramLengths ,* paramFormats ,i ;
358+ char * * paramValues ;
359+
360+ if (sdim -> get_default_value_query == NULL ){
361+ return NULL ;
362+ }
363+ pc = _postgresql_dimension_get_conn (ctx ,tileset ,sdim );
364+ if (GC_HAS_ERROR (ctx )) {
365+ return NULL ;
366+ }
367+ conn = pc -> connection ;
368+ _mapcache_dimension_postgresql_bind_parameters (ctx ,sdim -> get_all_indexes ,NULL ,tileset ,extent ,grid ,0 ,0 ,& nParams ,& paramValues ,& paramLengths ,& paramFormats );
369+ if (GC_HAS_ERROR (ctx )) {
370+ _postgresql_dimension_release_conn (ctx , pc );
371+ return NULL ;
372+ }
373+ res = PQexecPrepared (conn -> pgconn ,"get_default_value" ,nParams ,(const char * const * )paramValues ,paramLengths ,paramFormats ,0 );
374+ if (PQresultStatus (res ) != PGRES_TUPLES_OK ) {
375+ //ctx->set_error(ctx, 500, "postgresql query: %s", PQerrorMessage(conn->pgconn));
376+ PQclear (res );
377+ _postgresql_dimension_release_conn (ctx , pc );
378+ return NULL ;
379+ }
380+
381+ time_ids = apr_array_make (ctx -> pool ,0 ,sizeof (char * ));
382+ for (i = 0 ;i < PQntuples (res );i ++ ) {
383+ APR_ARRAY_PUSH (time_ids , char * ) = apr_pstrdup (ctx -> pool , PQgetvalue (res ,i ,0 ));
384+ }
385+ PQclear (res );
386+ _postgresql_dimension_release_conn (ctx , pc );
387+ return time_ids ;
388+
389+ }
390+
333391static void _mapcache_dimension_postgresql_parse_xml (mapcache_context * ctx , mapcache_dimension * dim ,
334392 ezxml_t node )
335393{
@@ -359,6 +417,14 @@ static void _mapcache_dimension_postgresql_parse_xml(mapcache_context *ctx, mapc
359417 ctx -> set_error (ctx ,400 ,"postgresql dimension \"%s\" has no <list_query> node" , dim -> name );
360418 return ;
361419 }
420+ child = ezxml_child (node ,"default_query" );
421+ if (child ) {
422+ dimension -> get_default_value_query = apr_pstrdup (ctx -> pool , child -> txt );
423+ } else {
424+ dimension -> get_default_value_query = NULL ;
425+ // ctx->set_error(ctx,400,"postgresql dimension \"%s\" has no <default_query> node", dim->name);
426+ // return;
427+ }
362428 parse_queries (ctx ,dimension );
363429 //printf("q1: %s\n",dimension->get_all_values_query);
364430 //printf("q2: %s\n",dimension->get_values_for_entry_query);
@@ -377,6 +443,7 @@ mapcache_dimension* mapcache_dimension_postgresql_create(mapcache_context *ctx,
377443 dimension -> dimension .configuration_parse_xml = _mapcache_dimension_postgresql_parse_xml ;
378444 dimension -> dimension .get_all_entries = _mapcache_dimension_postgresql_get_all_entries ;
379445 dimension -> dimension .get_all_ogc_formatted_entries = _mapcache_dimension_postgresql_get_all_entries ;
446+ dimension -> dimension .get_default_value = _mapcache_dimension_postgresql_get_default_entries ;
380447 return (mapcache_dimension * )dimension ;
381448#else
382449 ctx -> set_error (ctx ,400 ,"postgresql dimension support requires POSTGRESQL support to be built in" );
0 commit comments