source: branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c @ 851

Last change on this file since 851 was 851, checked in by djay, 7 years ago

Invoke callback asynchronously. Still the ZOO-Kernel has still to wait for every requests to finish before stoping its execution.

  • Property svn:keywords set to Id
File size: 18.6 KB
RevLine 
[644]1/*
[822]2 * Authors : David Saggiorato
3 *           Gérald Fenoy
[652]4 *  Copyright 2015 GeoLabs SARL. All rights reserved.
[644]5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
[652]25#include "ogr_api.h"
26#include "ogrsf_frmts.h"
27#include "ogr_p.h"
[785]28#include "response_print.h"
[784]29#if GDAL_VERSION_MAJOR >= 2
30#include <gdal_priv.h>
31#endif
[652]32
[644]33#include "sqlapi.h"
34#include <fcgi_stdio.h>
35#include <stdlib.h>
36
37/**
[652]38 * Global GDALDataset pointer
[644]39 */
[784]40#if GDAL_VERSION_MAJOR >=2
41GDALDataset
42#else
43OGRDataSource
44#endif
[839]45 **zoo_DS = NULL;
[652]46
47/**
48 * Global OGRLayer pointer pointing to the lastest result set
49 */
50OGRLayer *zoo_ResultSet = NULL;
51
52/**
[822]53 * Create a GDAL / OGR string for connecting to a db backend defined in the
54 * key section.
[652]55 *
56 * @param conf the maps containing the setting of the main.cfg file
[822]57 * @param key the name of the section containing the connection setting
58 * @return the OGR connection string
[652]59 */
[822]60char* _createInitString(maps* conf,const char* key){
[652]61  char* res=NULL;
[822]62  char keywords[6][14]={
[652]63    "dbname",
64    "host",
65    "port",
66    "user",
[822]67    "password",
68    "active_schema"   
[652]69  };
70  int i=0;
[822]71  maps* cconf=getMaps(conf,key);
[839]72  if(cconf==NULL)
73    return "-1";
[652]74  int len=0;
[822]75  for(i=0;i<6;i++){
[652]76    map* tmp=getMap(cconf->content,keywords[i]);
77    if(tmp!=NULL){
78      if(res==NULL){
79        res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char));
80        sprintf(res,"%s='%s'",keywords[i],tmp->value);
81        len+=strlen(res);
82      }else{
83        char* res1=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
84        sprintf(res1," %s='%s'",keywords[i],tmp->value);
85        res=(char*)realloc(res,(len+strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
86        memcpy(res+len,res1,(strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
87        len+=strlen(res1);
88        res[len]=0;
89        free(res1);
90      }
91    }
92  }
93  map* tmp=getMap(cconf->content,"type");
94  if(tmp!=NULL){
95    char* fres=(char*)malloc((strlen(res)+strlen(tmp->value)+2)*sizeof(char));
96    sprintf(fres,"%s:%s",tmp->value,res);
97    free(res);
98    return fres;
99  }
[644]100  return res;
101}
[652]102
103/**
[822]104 * Create a GDAL / OGR string for connecting to the db backend
[652]105 *
106 * @param conf the maps containing the setting of the main.cfg file
[822]107 * @return the OGR connection string
108 */
109char* createInitString(maps* conf){
110  return _createInitString(conf,"database");
111}
112
113/**
114 * Connect to a db backend.
115 *
116 * @param conf the maps containing the setting of the main.cfg file
[652]117 * @see createInitString
118 */
[839]119int _init_sql(maps* conf,const char* key){
[822]120  char* sqlInitString=_createInitString(conf,key);
[839]121  if(strncmp(sqlInitString,"-1",2)==0)
122    return -1;
[652]123  OGRSFDriver *poDriver = NULL;
124  OGRRegisterAll();
[839]125  int zoo_ds_nb=0;
126  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
127  if(dsNb==NULL){
128    setMapInMaps(conf,"lenv","ds_nb","1");
129  }else{
130    zoo_ds_nb=atoi(dsNb->value);
131    char* tmp=(char*)malloc(11*sizeof(char));
132    sprintf(tmp,"%d",zoo_ds_nb+1);
133    setMapInMaps(conf,"lenv","ds_nb",(const char*)tmp);
134    free(tmp);
135  }
136  if(zoo_DS==NULL)
137    zoo_DS=
[784]138#if GDAL_VERSION_MAJOR >= 2
[839]139      (GDALDataset**) malloc(sizeof(GDALDataset*))
140#else
141      (OGRDataSource**) malloc(sizeof(OGRDataSource*))
142#endif
143      ;
144  else
145    zoo_DS=     
146#if GDAL_VERSION_MAJOR >= 2
147      (GDALDataset**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(GDALDataset*))
148#else
149      (OGRDataSource**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(OGRDataSource*))
150#endif
151      ;
152 
153#if GDAL_VERSION_MAJOR >= 2
154  zoo_DS[zoo_ds_nb] = (GDALDataset*) GDALOpenEx( sqlInitString,
[784]155                                      GDAL_OF_UPDATE | GDAL_OF_VECTOR,
156                                      NULL, NULL, NULL );
157#else
[839]158  zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
[784]159#endif
[839]160  if( zoo_DS[zoo_ds_nb] == NULL ){
[652]161#ifdef DEBUG
162    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
163    fflush(stderr);
164#endif
165    free(sqlInitString);
[839]166    setMapInMaps(conf,"lenv","dbIssue","1");
167    setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend"));
168    return -2;
[652]169  }
170#ifdef DEBUG
171  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
172  fflush(stderr);
173#endif
[839]174  fprintf(stderr,"sqlInitString: %s SUCEED %d !\n",sqlInitString,zoo_ds_nb);
175  fflush(stderr); 
[652]176  free(sqlInitString);
[839]177  zoo_ds_nb++;
178  return zoo_ds_nb;
[652]179}
180
181/**
[822]182 * Connect to the db backend.
183 *
184 * @param conf the maps containing the setting of the main.cfg file
185 * @see createInitString
186 */
[839]187int init_sql(maps* conf){
[822]188  return _init_sql(conf,"database");
189}
190
191/**
[652]192 * Close any connection to the db backend.
193 *
194 * @param conf the maps containing the setting of the main.cfg file
195 */
[839]196void close_sql(maps* conf,int cid){
[652]197  if( zoo_ResultSet != NULL )
[839]198    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
[652]199  if(zoo_DS!=NULL){
[784]200#if GDAL_VERSION_MAJOR >= 2
[839]201    GDALClose(zoo_DS[cid]);
[784]202#else
[839]203    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
[784]204#endif
[839]205    zoo_DS[cid]=NULL;
[652]206  }
207}
208
209/**
210 * Call OGRCleanupAll.
211 *
212 */
213void end_sql(){
214  OGRCleanupAll();
215}
216
217/**
[822]218 * Fetch a tuple set by executing a SQL query to the Database Backend.
219 *
220 * @param conf the maps containing the setting of the main.cfg file
221 * @param sqlQuery the SQL query to run
222 * @return NULL in case of failure or an OGRLayer pointer if the query succeed.
223 */
[839]224OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){
225  if(zoo_DS[cid]==NULL)
226    return NULL;
[822]227  OGRLayer *res=NULL;
[839]228#ifdef DEBUG
229  fprintf(stderr,"************************* %s %s %d\n\n",sqlQuery,__FILE__,__LINE__);
230  fflush(stderr);
231#endif
232  res = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
[822]233  return res;
234}
235
[839]236void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){
[822]237  if( objects != NULL ){
[839]238    zoo_DS[cid]->ReleaseResultSet( objects );
[822]239    objects=NULL;
240  }
241}
242
243/**
[652]244 * Execute a SQL query to the SQL Database Backend.
245 *
246 * @param conf the maps containing the setting of the main.cfg file
247 * @param sqlQuery the SQL query to run
248 * @return -1 in case of failure and 1 if the query succeed.
249 */
[839]250int execSql(maps* conf,int cid,const char* sqlQuery){
[822]251  int res=-1;
[839]252  zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
[652]253  if( zoo_ResultSet != NULL ){
[822]254    res=1;
[652]255  }
[822]256  return res;
[652]257}
258
259/**
260 * Clean any memory allocated by executing a request
261 *
262 * @param conf the maps containing the setting of the main.cfg file
263 * @param sqlQuery the SQL query to run
264 * @return -1 in case of failure and 1 if the query succeed.
265 */
[839]266void cleanUpResultSet(const maps* conf,int cid){
[652]267  if( zoo_ResultSet != NULL ){
[839]268    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
[652]269    zoo_ResultSet=NULL;
270  }
271}
272
[839]273#ifdef RELY_ON_DB
274int getCurrentId(maps* conf){
275  int res=0;
276  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
277  if(dsNb!=NULL)
278    res=atoi(dsNb->value);
279  return res;
280}
281
[652]282/**
283 * Record a file stored during ZOO-Kernel execution
284 *
285 * @param conf the maps containing the setting of the main.cfg file
286 * @param filename the file's name
287 * @param type the type (Intput,Output,Response)
288 * @param name the maps containing the setting of the main.cfg file
289 */
290void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
[839]291  int zoo_ds_nb=getCurrentId(conf);
[652]292  map *uusid=getMapFromMaps(conf,"lenv","usid");
293  map *schema=getMapFromMaps(conf,"database","schema");
294  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(uusid->value)+strlen(filename)+strlen(type)+(name!=NULL?strlen(name):2)+68+1)*sizeof(char));
295  if(name!=NULL)
296    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s','%s');",schema->value,uusid->value,filename,type,name);
297  else
298    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
[839]299  execSql(conf,zoo_ds_nb-1,sqlQuery);
[734]300  free(sqlQuery);
[839]301  cleanUpResultSet(conf,zoo_ds_nb-1);
[652]302}
303
304/**
305 * Insert the reference tuple corresponding to the running service
306 *
307 * @param conf the maps containing the setting of the main.cfg file
308 */
309void recordServiceStatus(maps* conf){
[839]310  int zoo_ds_nb=getCurrentId(conf);
[652]311  map *sid=getMapFromMaps(conf,"lenv","sid");
312  map *osid=getMapFromMaps(conf,"lenv","osid");
313  map *uusid=getMapFromMaps(conf,"lenv","usid");
314  map *schema=getMapFromMaps(conf,"database","schema");
315  char *sqlQuery=(char*)malloc((strlen(schema->value)+
316                                strlen(uusid->value)+
317                                strlen(osid->value)+
[785]318                                strlen(sid->value)+
319                                strlen(wpsStatus[2])+66+1)*sizeof(char));
[652]320  sprintf(sqlQuery,
[785]321          "INSERT INTO %s.services (uuid,sid,osid,fstate)"
322          "VALUES ('%s','%s','%s','%s');",
[652]323          schema->value,
[785]324          uusid->value,
325          sid->value,
326          osid->value,
327          wpsStatus[2]);
[839]328  execSql(conf,zoo_ds_nb-1,sqlQuery);
[734]329  free(sqlQuery);
[839]330  cleanUpResultSet(conf,zoo_ds_nb-1);
[652]331}
332
333/**
334 * Store the content of the result file
335 *
336 * @param conf the maps containing the setting of the main.cfg file
337 * @param filename the file's name
338 */
339void recordResponse(maps* conf,char* filename){
[839]340  int zoo_ds_nb=getCurrentId(conf);
[652]341  FILE *file = fopen (filename, "rb");
342  fseek (file, 0, SEEK_END);
343  long flen = ftell (file);
344  fseek (file, 0, SEEK_SET);
345  char *tmps = (char *) malloc ((flen + 1) * sizeof (char));
346  fread (tmps, flen, 1, file);
347  tmps[flen]=0;
348  fclose(file);
349  map *sid=getMapFromMaps(conf,"lenv","usid");
350  map *schema=getMapFromMaps(conf,"database","schema");
[786]351  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
[654]352  sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
[839]353  execSql(conf,zoo_ds_nb-1,sqlQuery);
[734]354  free(sqlQuery);
355  free(tmps);
[839]356  cleanUpResultSet(conf,zoo_ds_nb-1);
[652]357}
358
359/**
360 * Update the current status of the running service.
361 *
362 * @param conf the map containing the setting of the main.cfg file
363 * @return 0 on success, -2 if shmget failed, -1 if shmat failed
364 */
365int _updateStatus(maps* conf){
[839]366  int zoo_ds_nb=getCurrentId(conf);
[652]367  map *sid=getMapFromMaps(conf,"lenv","usid");
368  map *p=getMapFromMaps(conf,"lenv","status");
369  map *msg=getMapFromMaps(conf,"lenv","message");
370  map *schema=getMapFromMaps(conf,"database","schema");
371  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
372  sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
[839]373  if( zoo_ds_nb==
374#ifdef META_DB
375      1
376#else
377      0
378#endif
379      ){
[652]380    init_sql(conf);
[839]381    zoo_ds_nb++;
382  }
383  execSql(conf,zoo_ds_nb-1,sqlQuery);
384  cleanUpResultSet(conf,zoo_ds_nb-1);
[734]385  free(sqlQuery);
[652]386  return 0;
387}
388
389/**
390 * Get the ongoing status of a running service
391 *
392 * @param conf the maps containing the setting of the main.cfg file
393 * @param pid the service identifier (usid key from the [lenv] section)
394 * @return the reported status char* (MESSAGE|POURCENTAGE)
395 */
396char* _getStatus(maps* conf,char* pid){
[839]397  int zoo_ds_nb=getCurrentId(conf);
[850]398  int created=-1;
[652]399  map *schema=getMapFromMaps(conf,"database","schema");
400  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
401  sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
[839]402  if( zoo_ds_nb==
403#ifdef META_DB
404      1
405#else
406      0
407#endif
408      ){
[652]409    init_sql(conf);
[839]410    zoo_ds_nb++;
[850]411    created=1;
[839]412  }
413  execSql(conf,zoo_ds_nb-1,sqlQuery);
[652]414  OGRFeature  *poFeature = NULL;
415  const char *tmp1;
416  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
417    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
418      if( poFeature->IsFieldSet( iField ) ){
419        tmp1=strdup(poFeature->GetFieldAsString( iField ));
420      }
421      else
422        tmp1=NULL;
423    }
424    OGRFeature::DestroyFeature( poFeature );
425  }
[839]426  cleanUpResultSet(conf,zoo_ds_nb-1);
[734]427  free(sqlQuery);
[652]428  return (char*)tmp1;
429}
430
431/**
432 * Read the cache file of a running service
433 *
434 * @param conf the maps containing the setting of the main.cfg file
435 * @param pid the service identifier (usid key from the [lenv] section)
436 * @return the reported status char* (temporary/final result)
437 */
438char* _getStatusFile(maps* conf,char* pid){
[839]439  int zoo_ds_nb=getCurrentId(conf);
[652]440  map *schema=getMapFromMaps(conf,"database","schema");
[654]441  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char));
442  sprintf(sqlQuery,
443          "select content from %s.responses where uuid=$$%s$$"
444          " order by creation_time desc limit 1",schema->value,pid);
[839]445  if( zoo_ds_nb==
446#ifdef META_DB
447      1
448#else
449      0
450#endif
451      ){
[652]452    init_sql(conf);
[839]453    zoo_ds_nb++;
454  }
455  execSql(conf,zoo_ds_nb-1,sqlQuery);
[652]456  OGRFeature  *poFeature = NULL;
457  const char *tmp1;
458  int hasRes=-1;
[839]459  if(zoo_ResultSet!=NULL){
460      while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
461        for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
462          if( poFeature->IsFieldSet( iField ) ){
463            tmp1=strdup(poFeature->GetFieldAsString( iField ));
464            hasRes=1;
465          }
466          else
467            tmp1=NULL;
468        }
469        OGRFeature::DestroyFeature( poFeature );
[652]470      }
471  }
472  if(hasRes<0)
473    tmp1=NULL;
[839]474  else
475    cleanUpResultSet(conf,zoo_ds_nb-1);
[734]476  free(sqlQuery);
[652]477  return (char*)tmp1;
478}
479
480/**
[654]481 * Delete a service reference from the database.
482 *
483 * @param conf the map containing the setting of the main.cfg file
484 * @param pid the service identifier (usid key from the [lenv] section)
485 */
486void removeService(maps* conf,char* pid){
[839]487  int zoo_ds_nb=getCurrentId(conf);
[654]488  map *schema=getMapFromMaps(conf,"database","schema");
489  char *sqlQuery=(char*)
490    malloc((strlen(pid)+strlen(schema->value)+38+1)
491           *sizeof(char));
[839]492  if( zoo_ds_nb==
493#ifdef META_DB
494      1
495#else
496      0
497#endif
498      ){
[654]499    init_sql(conf);
[839]500    zoo_ds_nb++;
501  }
[654]502  sprintf(sqlQuery,
503          "DELETE FROM %s.services where uuid=$$%s$$;",
504          schema->value,pid);
[839]505  execSql(conf,zoo_ds_nb-1,sqlQuery);
506  cleanUpResultSet(conf,zoo_ds_nb-1);
507  close_sql(conf,zoo_ds_nb-1);
[734]508  free(sqlQuery);
[654]509  end_sql();
510}
511
512/**
[652]513 * Stop handling status repport.
514 *
515 * @param conf the map containing the setting of the main.cfg file
516 */
517void unhandleStatus(maps* conf){
[839]518  int zoo_ds_nb=getCurrentId(conf);
[654]519  map *schema=getMapFromMaps(conf,"database","schema");
[652]520  map *sid=getMapFromMaps(conf,"lenv","usid");
[654]521  map *fstate=getMapFromMaps(conf,"lenv","fstate");
522  char *sqlQuery=(char*)malloc((strlen(sid->value)+
523                                strlen(schema->value)+
524                                (fstate!=NULL?
525                                 strlen(fstate->value):
526                                 6)
527                                +66+1)*sizeof(char));
528  sprintf(sqlQuery,
529          "UPDATE %s.services set end_time=now(), fstate=$$%s$$"
530          " where uuid=$$%s$$;",
531          schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value);
[839]532  execSql(conf,zoo_ds_nb-1,sqlQuery);
533  cleanUpResultSet(conf,zoo_ds_nb-1);
[851]534  //close_sql(conf,zoo_ds_nb-1);
[734]535  free(sqlQuery);
[652]536  end_sql();
537}
538
[654]539/**
540 * Read the sid identifier attached of a service if any
541 *
542 * @param conf the maps containing the setting of the main.cfg file
543 * @param pid the service identifier (usid key from the [lenv] section)
544 * @return the sid value
545 */
546char* getStatusId(maps* conf,char* pid){
[839]547  int zoo_ds_nb=getCurrentId(conf);
[654]548  map *schema=getMapFromMaps(conf,"database","schema");
549  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
550  sprintf(sqlQuery,
551          "select osid from %s.services where uuid=$$%s$$",
552          schema->value,pid);
[839]553  if( zoo_ds_nb==0 ){
[654]554    init_sql(conf);
[839]555    zoo_ds_nb++;
556  }
557  execSql(conf,zoo_ds_nb-1,sqlQuery);
[654]558  OGRFeature  *poFeature = NULL;
559  const char *tmp1;
560  int hasRes=-1;
561  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
562    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
563      if( poFeature->IsFieldSet( iField ) ){
564        tmp1=zStrdup(poFeature->GetFieldAsString( iField ));
565        hasRes=1;
566        break;
567      }
568    }
569    OGRFeature::DestroyFeature( poFeature );
570  }
571  if(hasRes<0)
572    tmp1=NULL;
[734]573  free(sqlQuery);
[839]574  cleanUpResultSet(conf,zoo_ds_nb-1);
[654]575  return (char*)tmp1;
576}
577
578/**
579 * Read the Result file (.res).
580 *
581 * @param conf the maps containing the setting of the main.cfg file
582 * @param pid the service identifier (usid key from the [lenv] section)
583 */
584void readFinalRes(maps* conf,char* pid,map* statusInfo){
[839]585  int zoo_ds_nb=getCurrentId(conf);
[654]586  map *schema=getMapFromMaps(conf,"database","schema");
587  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
588  sprintf(sqlQuery,
589          "select fstate from %s.services where uuid=$$%s$$",
590          schema->value,pid);
591  if( zoo_DS == NULL )
592    init_sql(conf);
[839]593  execSql(conf,zoo_ds_nb-1,sqlQuery);
[654]594  OGRFeature  *poFeature = NULL;
595  int hasRes=-1;
596  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
597    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
598      if( poFeature->IsFieldSet( iField ) ){
599        addToMap(statusInfo,"Status",poFeature->GetFieldAsString( iField ));
600        hasRes=1;
601        break;
602      }
603    }
604    OGRFeature::DestroyFeature( poFeature );
605  }
[851]606  cleanUpResultSet(conf,zoo_ds_nb-1);
[654]607  if(hasRes<0)
608    addToMap(statusInfo,"Status","Failed");
[734]609  free(sqlQuery);
[654]610  return;
611}
612
613/**
614 * Check if a service is running.
615 *
616 * @param conf the maps containing the setting of the main.cfg file
617 * @param pid the unique service identifier (usid from the lenv section)
618 * @return 1 in case the service is still running, 0 otherwise
619 */
620int isRunning(maps* conf,char* pid){
621  int res=0;
[839]622  int zoo_ds_nb=getCurrentId(conf);
[654]623  map *schema=getMapFromMaps(conf,"database","schema");
624  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char));
625  sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid);
[839]626  if( zoo_ds_nb == 0 ){
[654]627    init_sql(conf);
[839]628    zoo_ds_nb++;
629  }
630  execSql(conf,zoo_ds_nb-1,sqlQuery);
[654]631  OGRFeature  *poFeature = NULL;
632  const char *tmp1;
633  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
634    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
635      if( poFeature->IsFieldSet( iField ) && 
636          atoi(poFeature->GetFieldAsString( iField ))>0 ){
637        res=1;
638        break;
639      }
640    }
641    OGRFeature::DestroyFeature( poFeature );
642  }
[839]643  cleanUpResultSet(conf,zoo_ds_nb-1);
[734]644  free(sqlQuery);
[654]645  return res;
646}
647
[652]648#endif
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png