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

Last change on this file since 933 was 933, checked in by knut, 3 years ago

Resolve certain string-related type conflicts, e.g., char* vs. const char* or char[N], that may cause errors in some build environments. Resolve a problem with the include order of fcgi_stdio.h (note: should verify that this harmonizes with resolution of same problem in trunk).

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

Search

Context Navigation

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