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
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#ifdef SQL_DEBUG
124  fprintf(stderr,"Try to connect to: %s %s !\n",key,sqlInitString);
125  fflush(stderr); 
126#endif
127  if(strncmp(sqlInitString,"-1",2)==0)
128    return -1;
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 SQL_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 SQL_DEBUG
177  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
178  fflush(stderr);
179#endif
180  free(sqlInitString);
181  zoo_ds_nb++;
182  return zoo_ds_nb;
183}
184
185/**
186 * Connect to the db backend.
187 *
188 * @param conf the maps containing the setting of the main.cfg file
189 * @see createInitString
190 */
191int init_sql(maps* conf){
192  return _init_sql(conf,"database");
193}
194
195/**
196 * Close any connection to the db backend.
197 *
198 * @param conf the maps containing the setting of the main.cfg file
199 */
200void close_sql(maps* conf,int cid){
201  if( zoo_ResultSet != NULL ){
202    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
203    zoo_ResultSet=NULL;
204  }
205  if(zoo_DS!=NULL && zoo_DS[cid]!=NULL){
206#if GDAL_VERSION_MAJOR >= 2
207    GDALClose(zoo_DS[cid]);
208#else
209    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
210#endif
211    zoo_DS[cid]=NULL;
212  }
213}
214
215/**
216 * Call OGRCleanupAll.
217 *
218 */
219void end_sql(){
220  OGRCleanupAll();
221}
222
223/**
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 */
230OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){
231  if(zoo_DS==NULL || zoo_DS[cid]==NULL)
232    return NULL;
233  OGRLayer *res=NULL;
234#ifdef SQL_DEBUG
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);
239  return res;
240}
241
242void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){
243  if( objects != NULL ){
244    zoo_DS[cid]->ReleaseResultSet( objects );
245    objects=NULL;
246  }
247}
248
249/**
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 */
256int execSql(maps* conf,int cid,const char* sqlQuery){
257  int res=-1;
258  if(zoo_DS == NULL || zoo_DS[cid]==NULL)
259    return -1;
260  zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
261  if( zoo_ResultSet != NULL ){
262    res=1;
263  }
264  return res;
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 */
274void cleanUpResultSet(const maps* conf,int cid){
275  if( zoo_ResultSet != NULL ){
276    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
277    zoo_ResultSet=NULL;
278  }
279}
280
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
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){
299  int zoo_ds_nb=getCurrentId(conf);
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);
307  execSql(conf,zoo_ds_nb-1,sqlQuery);
308  free(sqlQuery);
309  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
318  int zoo_ds_nb=getCurrentId(conf);
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)+
326                                strlen(sid->value)+
327                                strlen(wpsStatus[2])+66+1)*sizeof(char));
328  sprintf(sqlQuery,
329          "INSERT INTO %s.services (uuid,sid,osid,fstate)"
330          "VALUES ('%s','%s','%s','%s');",
331          schema->value,
332          uusid->value,
333          sid->value,
334          osid->value,
335          wpsStatus[2]);
336  execSql(conf,zoo_ds_nb-1,sqlQuery);
337  free(sqlQuery);
338  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
348  int zoo_ds_nb=getCurrentId(conf);
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");
359  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
360  sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
361  execSql(conf,zoo_ds_nb-1,sqlQuery);
362  free(sqlQuery);
363  free(tmps);
364  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
374  int zoo_ds_nb=getCurrentId(conf);
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);
381  if( zoo_ds_nb == 0 ){
382    init_sql(conf);
383    zoo_ds_nb++;
384  }
385  execSql(conf,0,sqlQuery);
386  cleanUpResultSet(conf,0);
387  close_sql(conf,0);
388  free(sqlQuery);
389  setMapInMaps(conf,"lenv","ds_nb","0");
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){
401  int zoo_ds_nb=getCurrentId(conf);
402  int created=-1;
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);
406  if( zoo_ds_nb==
407#ifdef META_DB
408      1
409#else
410      0
411#endif
412      ){
413    init_sql(conf);
414    zoo_ds_nb++;
415    created=1;
416  }
417  execSql(conf,zoo_ds_nb-1,sqlQuery);
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  }
430  cleanUpResultSet(conf,zoo_ds_nb-1);
431  free(sqlQuery);
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){
443  int zoo_ds_nb=getCurrentId(conf);
444  map *schema=getMapFromMaps(conf,"database","schema");
445  OGRFeature  *poFeature = NULL;
446  const char *tmp1=NULL;
447  int hasRes=-1;
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);
452  if( zoo_ds_nb==
453#ifdef META_DB
454      1
455#else
456      0
457#endif
458      ){
459    init_sql(conf);
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 );
474      }
475  }
476  if(hasRes<0)
477    tmp1=NULL;
478  cleanUpResultSet(conf,zoo_ds_nb-1);
479  free(sqlQuery);
480  return (char*)tmp1;
481}
482
483/**
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){
490  int zoo_ds_nb=getCurrentId(conf);
491  map *schema=getMapFromMaps(conf,"database","schema");
492  char *sqlQuery=(char*)
493    malloc((strlen(pid)+strlen(schema->value)+38+1)
494           *sizeof(char));
495  if( zoo_ds_nb==
496#ifdef META_DB
497      1
498#else
499      0
500#endif
501      ){
502    init_sql(conf);
503    zoo_ds_nb++;
504  }
505  sprintf(sqlQuery,
506          "DELETE FROM %s.services where uuid=$$%s$$;",
507          schema->value,pid);
508  execSql(conf,zoo_ds_nb-1,sqlQuery);
509  cleanUpResultSet(conf,zoo_ds_nb-1);
510  close_sql(conf,zoo_ds_nb-1);
511  free(sqlQuery);
512  end_sql();
513}
514
515/**
516 * Stop handling status repport.
517 *
518 * @param conf the map containing the setting of the main.cfg file
519 */
520void unhandleStatus(maps* conf){
521  int zoo_ds_nb=getCurrentId(conf);
522  map *schema=getMapFromMaps(conf,"database","schema");
523  map *sid=getMapFromMaps(conf,"lenv","usid");
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);
535  execSql(conf,zoo_ds_nb-1,sqlQuery);
536  cleanUpResultSet(conf,zoo_ds_nb-1);
537  //close_sql(conf,zoo_ds_nb-1);
538  free(sqlQuery);
539  //end_sql();
540}
541
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){
550  int zoo_ds_nb=getCurrentId(conf);
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);
556  if( zoo_ds_nb==0 ){
557    init_sql(conf);
558    zoo_ds_nb++;
559  }
560  if(execSql(conf,zoo_ds_nb-1,sqlQuery)<0)
561    return NULL;
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;
577  free(sqlQuery);
578  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
589  int zoo_ds_nb=getCurrentId(conf);
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);
597  execSql(conf,zoo_ds_nb-1,sqlQuery);
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  }
610  cleanUpResultSet(conf,zoo_ds_nb-1);
611  if(hasRes<0)
612    addToMap(statusInfo,"Status","Failed");
613  free(sqlQuery);
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;
626  int zoo_ds_nb=getCurrentId(conf);
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);
630  if( zoo_ds_nb == 0 ){
631    init_sql(conf);
632    zoo_ds_nb++;
633  }
634  execSql(conf,zoo_ds_nb-1,sqlQuery);
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  }
647  cleanUpResultSet(conf,zoo_ds_nb-1);
648  free(sqlQuery);
649  return res;
650}
651
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