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

Last change on this file since 863 was 863, checked in by djay, 6 years ago

Change the default ZOO-Kernel behavior, if an input has been passed by reference, the ZOO-Service will receive a cache_file map rather than the value field which was usually returned, same for array value apply. To use the previous behavior, one can add "memory=load" to the main section of the main.cfg file. Update ZOO-Services for using this new field if available.

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