Ignore:
Timestamp:
May 7, 2019, 2:17:08 PM (5 years ago)
Author:
djay
Message:

Merge prototype-v0 branch in trunk

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/zoo-project/zoo-kernel/sqlapi.c

    r786 r917  
    11/*
    2  * Author : David Saggiorato
    3  *          Gérald Fenoy
     2 * Authors : David Saggiorato
     3 *           Gérald Fenoy
    44 *  Copyright 2015 GeoLabs SARL. All rights reserved.
    55 *
     
    2323 */
    2424
    25 #ifdef RELY_ON_DB
    2625#include "ogr_api.h"
    2726#include "ogrsf_frmts.h"
     
    4443OGRDataSource
    4544#endif
    46  *zoo_DS = NULL;
     45 **zoo_DS = NULL;
    4746
    4847/**
     
    5251
    5352/**
    54  * Create a GDAL / OGR string for connecting to the db backend
    55  * (do not support active_schema)
    56  *
    57  * @param conf the maps containing the setting of the main.cfg file
    58  */
    59 char* createInitString(maps* conf){
     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){
    6061  char* res=NULL;
    61   char keywords[5][9]={
     62  char keywords[6][14]={
    6263    "dbname",
    6364    "host",
    6465    "port",
    6566    "user",
    66     "password"
     67    "password",
     68    "active_schema"   
    6769  };
    6870  int i=0;
    69   maps* cconf=getMaps(conf,"database");
     71  maps* cconf=getMaps(conf,key);
     72  if(cconf==NULL){
     73    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     74    return "-1";
     75  }
    7076  int len=0;
    71   for(i=0;i<5;i++){
     77  for(i=0;i<6;i++){
    7278    map* tmp=getMap(cconf->content,keywords[i]);
    7379    if(tmp!=NULL){
     
    98104
    99105/**
    100  * Connect to the db backend.
     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.
    101117 *
    102118 * @param conf the maps containing the setting of the main.cfg file
    103119 * @see createInitString
    104120 */
    105 void init_sql(maps* conf){
    106   char* sqlInitString=createInitString(conf);
     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;
    107129  OGRSFDriver *poDriver = NULL;
    108130  OGRRegisterAll();
    109 
     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=
    110144#if GDAL_VERSION_MAJOR >= 2
    111   zoo_DS = (GDALDataset*) GDALOpenEx( sqlInitString,
     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,
    112161                                      GDAL_OF_UPDATE | GDAL_OF_VECTOR,
    113162                                      NULL, NULL, NULL );
    114163#else
    115   zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
    116 #endif
    117   if( zoo_DS == NULL ){
    118 #ifdef DEBUG
     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
    119168    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
    120169    fflush(stderr);
    121170#endif
    122171    free(sqlInitString);
    123     setMapInMaps(conf,"lenv","message","Failed to connect to the database backend");
    124     return;
    125   }
    126 #ifdef DEBUG
     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
    127177  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
    128178  fflush(stderr);
    129179#endif
    130180  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");
    131193}
    132194
     
    136198 * @param conf the maps containing the setting of the main.cfg file
    137199 */
    138 void close_sql(maps* conf){
    139   if( zoo_ResultSet != NULL )
    140     zoo_DS->ReleaseResultSet( zoo_ResultSet );
    141   if(zoo_DS!=NULL){
     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){
    142206#if GDAL_VERSION_MAJOR >= 2
    143     GDALClose(zoo_DS);
    144 #else
    145     OGRDataSource::DestroyDataSource( zoo_DS );
    146 #endif
    147     zoo_DS=NULL;
     207    GDALClose(zoo_DS[cid]);
     208#else
     209    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
     210#endif
     211    zoo_DS[cid]=NULL;
    148212  }
    149213}
     
    158222
    159223/**
     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/**
    160250 * Execute a SQL query to the SQL Database Backend.
    161251 *
     
    164254 * @return -1 in case of failure and 1 if the query succeed.
    165255 */
    166 int execSql(maps* conf,const char* sqlQuery){
    167   zoo_ResultSet = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
     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);
    168261  if( zoo_ResultSet != NULL ){
    169     return 1;
    170   }
    171   return -1;
     262    res=1;
     263  }
     264  return res;
    172265}
    173266
     
    179272 * @return -1 in case of failure and 1 if the query succeed.
    180273 */
    181 void cleanUpResultSet(const maps* conf){
     274void cleanUpResultSet(const maps* conf,int cid){
    182275  if( zoo_ResultSet != NULL ){
    183     zoo_DS->ReleaseResultSet( zoo_ResultSet );
     276    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
    184277    zoo_ResultSet=NULL;
    185278  }
     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;
    186288}
    187289
     
    195297 */
    196298void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
     299  int zoo_ds_nb=getCurrentId(conf);
    197300  map *uusid=getMapFromMaps(conf,"lenv","usid");
    198301  map *schema=getMapFromMaps(conf,"database","schema");
     
    202305  else
    203306    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
    204   execSql(conf,sqlQuery);
    205   free(sqlQuery);
    206   cleanUpResultSet(conf);
     307  execSql(conf,zoo_ds_nb-1,sqlQuery);
     308  free(sqlQuery);
     309  cleanUpResultSet(conf,zoo_ds_nb-1);
    207310}
    208311
     
    213316 */
    214317void recordServiceStatus(maps* conf){
     318  int zoo_ds_nb=getCurrentId(conf);
    215319  map *sid=getMapFromMaps(conf,"lenv","sid");
    216320  map *osid=getMapFromMaps(conf,"lenv","osid");
     
    230334          osid->value,
    231335          wpsStatus[2]);
    232   execSql(conf,sqlQuery);
    233   free(sqlQuery);
    234   cleanUpResultSet(conf);
     336  execSql(conf,zoo_ds_nb-1,sqlQuery);
     337  free(sqlQuery);
     338  cleanUpResultSet(conf,zoo_ds_nb-1);
    235339}
    236340
     
    242346 */
    243347void recordResponse(maps* conf,char* filename){
     348  int zoo_ds_nb=getCurrentId(conf);
    244349  FILE *file = fopen (filename, "rb");
    245350  fseek (file, 0, SEEK_END);
     
    254359  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
    255360  sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
    256   execSql(conf,sqlQuery);
     361  execSql(conf,zoo_ds_nb-1,sqlQuery);
    257362  free(sqlQuery);
    258363  free(tmps);
    259   cleanUpResultSet(conf);
     364  cleanUpResultSet(conf,zoo_ds_nb-1);
    260365}
    261366
     
    267372 */
    268373int _updateStatus(maps* conf){
     374  int zoo_ds_nb=getCurrentId(conf);
    269375  map *sid=getMapFromMaps(conf,"lenv","usid");
    270376  map *p=getMapFromMaps(conf,"lenv","status");
     
    273379  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
    274380  sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
    275   fflush(stderr);
    276   if( zoo_DS == NULL )
     381  if( zoo_DS == NULL || zoo_DS[zoo_ds_nb-1]==NULL ){
    277382    init_sql(conf);
    278   execSql(conf,sqlQuery);
    279   cleanUpResultSet(conf);
     383    zoo_ds_nb++;
     384  }
     385  execSql(conf,zoo_ds_nb-1,sqlQuery);
     386  cleanUpResultSet(conf,zoo_ds_nb-1);
    280387  free(sqlQuery);
    281388  return 0;
     
    290397 */
    291398char* _getStatus(maps* conf,char* pid){
     399  int zoo_ds_nb=getCurrentId(conf);
     400  int created=-1;
    292401  map *schema=getMapFromMaps(conf,"database","schema");
    293402  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
    294403  sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
    295   if( zoo_DS == NULL )
     404  if( zoo_ds_nb==
     405#ifdef META_DB
     406      1
     407#else
     408      0
     409#endif
     410      ){
    296411    init_sql(conf);
    297   execSql(conf,sqlQuery);
     412    zoo_ds_nb++;
     413    created=1;
     414  }
     415  execSql(conf,zoo_ds_nb-1,sqlQuery);
    298416  OGRFeature  *poFeature = NULL;
    299417  const char *tmp1;
     
    301419    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
    302420      if( poFeature->IsFieldSet( iField ) ){
    303         tmp1=strdup(poFeature->GetFieldAsString( iField ));
     421        tmp1=zStrdup(poFeature->GetFieldAsString( iField ));
    304422      }
    305423      else
     
    308426    OGRFeature::DestroyFeature( poFeature );
    309427  }
    310   cleanUpResultSet(conf);
     428  cleanUpResultSet(conf,zoo_ds_nb-1);
    311429  free(sqlQuery);
    312430  return (char*)tmp1;
     
    321439 */
    322440char* _getStatusFile(maps* conf,char* pid){
    323   map *schema=getMapFromMaps(conf,"database","schema");
     441  int zoo_ds_nb=getCurrentId(conf);
     442  map *schema=getMapFromMaps(conf,"database","schema");
     443  OGRFeature  *poFeature = NULL;
     444  const char *tmp1=NULL;
     445  int hasRes=-1;
    324446  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char));
    325447  sprintf(sqlQuery,
    326448          "select content from %s.responses where uuid=$$%s$$"
    327449          " order by creation_time desc limit 1",schema->value,pid);
    328   if( zoo_DS == NULL )
     450  if( zoo_ds_nb==
     451#ifdef META_DB
     452      1
     453#else
     454      0
     455#endif
     456      ){
    329457    init_sql(conf);
    330   execSql(conf,sqlQuery);
    331   OGRFeature  *poFeature = NULL;
    332   const char *tmp1;
    333   int hasRes=-1;
    334   while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
    335     for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
    336       if( poFeature->IsFieldSet( iField ) ){
    337         tmp1=strdup(poFeature->GetFieldAsString( iField ));
    338         hasRes=1;
     458    zoo_ds_nb++;
     459  }
     460  execSql(conf,zoo_ds_nb-1,sqlQuery);
     461  if(zoo_ResultSet!=NULL){
     462      while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
     463        for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
     464          if( poFeature->IsFieldSet( iField ) ){
     465            tmp1=zStrdup(poFeature->GetFieldAsString( iField ));
     466            hasRes=1;
     467          }
     468          else
     469            tmp1=NULL;
     470        }
     471        OGRFeature::DestroyFeature( poFeature );
    339472      }
    340       else
    341         tmp1=NULL;
    342     }
    343     OGRFeature::DestroyFeature( poFeature );
    344473  }
    345474  if(hasRes<0)
    346475    tmp1=NULL;
    347   cleanUpResultSet(conf);
     476  cleanUpResultSet(conf,zoo_ds_nb-1);
    348477  free(sqlQuery);
    349478  return (char*)tmp1;
     
    357486 */
    358487void removeService(maps* conf,char* pid){
     488  int zoo_ds_nb=getCurrentId(conf);
    359489  map *schema=getMapFromMaps(conf,"database","schema");
    360490  char *sqlQuery=(char*)
    361491    malloc((strlen(pid)+strlen(schema->value)+38+1)
    362492           *sizeof(char));
    363   if( zoo_DS == NULL )
     493  if( zoo_ds_nb==
     494#ifdef META_DB
     495      1
     496#else
     497      0
     498#endif
     499      ){
    364500    init_sql(conf);
     501    zoo_ds_nb++;
     502  }
    365503  sprintf(sqlQuery,
    366504          "DELETE FROM %s.services where uuid=$$%s$$;",
    367505          schema->value,pid);
    368   execSql(conf,sqlQuery);
    369   cleanUpResultSet(conf);
    370   close_sql(conf);
     506  execSql(conf,zoo_ds_nb-1,sqlQuery);
     507  cleanUpResultSet(conf,zoo_ds_nb-1);
     508  close_sql(conf,zoo_ds_nb-1);
    371509  free(sqlQuery);
    372510  end_sql();
     
    379517 */
    380518void unhandleStatus(maps* conf){
     519  int zoo_ds_nb=getCurrentId(conf);
    381520  map *schema=getMapFromMaps(conf,"database","schema");
    382521  map *sid=getMapFromMaps(conf,"lenv","usid");
     
    392531          " where uuid=$$%s$$;",
    393532          schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value);
    394   execSql(conf,sqlQuery);
    395   cleanUpResultSet(conf);
    396   close_sql(conf);
    397   free(sqlQuery);
    398   end_sql();
     533  execSql(conf,zoo_ds_nb-1,sqlQuery);
     534  cleanUpResultSet(conf,zoo_ds_nb-1);
     535  //close_sql(conf,zoo_ds_nb-1);
     536  free(sqlQuery);
     537  //end_sql();
    399538}
    400539
     
    407546 */
    408547char* getStatusId(maps* conf,char* pid){
     548  int zoo_ds_nb=getCurrentId(conf);
    409549  map *schema=getMapFromMaps(conf,"database","schema");
    410550  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
     
    412552          "select osid from %s.services where uuid=$$%s$$",
    413553          schema->value,pid);
    414   if( zoo_DS == NULL )
     554  if( zoo_ds_nb==0 ){
    415555    init_sql(conf);
    416   execSql(conf,sqlQuery);
     556    zoo_ds_nb++;
     557  }
     558  if(execSql(conf,zoo_ds_nb-1,sqlQuery)<0)
     559    return NULL;
    417560  OGRFeature  *poFeature = NULL;
    418561  const char *tmp1;
     
    431574    tmp1=NULL;
    432575  free(sqlQuery);
     576  cleanUpResultSet(conf,zoo_ds_nb-1);
    433577  return (char*)tmp1;
    434578}
     
    441585 */
    442586void readFinalRes(maps* conf,char* pid,map* statusInfo){
     587  int zoo_ds_nb=getCurrentId(conf);
    443588  map *schema=getMapFromMaps(conf,"database","schema");
    444589  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
     
    448593  if( zoo_DS == NULL )
    449594    init_sql(conf);
    450   execSql(conf,sqlQuery);
     595  execSql(conf,zoo_ds_nb-1,sqlQuery);
    451596  OGRFeature  *poFeature = NULL;
    452597  int hasRes=-1;
     
    461606    OGRFeature::DestroyFeature( poFeature );
    462607  }
     608  cleanUpResultSet(conf,zoo_ds_nb-1);
    463609  if(hasRes<0)
    464610    addToMap(statusInfo,"Status","Failed");
     
    476622int isRunning(maps* conf,char* pid){
    477623  int res=0;
     624  int zoo_ds_nb=getCurrentId(conf);
    478625  map *schema=getMapFromMaps(conf,"database","schema");
    479626  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char));
    480627  sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid);
    481   if( zoo_DS == NULL )
     628  if( zoo_ds_nb == 0 ){
    482629    init_sql(conf);
    483   execSql(conf,sqlQuery);
     630    zoo_ds_nb++;
     631  }
     632  execSql(conf,zoo_ds_nb-1,sqlQuery);
    484633  OGRFeature  *poFeature = NULL;
    485634  const char *tmp1;
     
    494643    OGRFeature::DestroyFeature( poFeature );
    495644  }
    496   cleanUpResultSet(conf);
     645  cleanUpResultSet(conf,zoo_ds_nb-1);
    497646  free(sqlQuery);
    498647  return res;
Note: See TracChangeset for help on using the changeset viewer.

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