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

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

HPC support update. Add inputs for create options in Gdal_Dem.

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