Changeset 917 for trunk/zoo-project/zoo-kernel/sqlapi.c
- Timestamp:
- May 7, 2019, 2:17:08 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
False
/branches/prototype-v0 merged eligible
-
Property
svn:mergeinfo
set to
False
-
trunk/zoo-project/zoo-kernel/sqlapi.c
r786 r917 1 1 /* 2 * Author : David Saggiorato3 * Gérald Fenoy2 * Authors : David Saggiorato 3 * Gérald Fenoy 4 4 * Copyright 2015 GeoLabs SARL. All rights reserved. 5 5 * … … 23 23 */ 24 24 25 #ifdef RELY_ON_DB26 25 #include "ogr_api.h" 27 26 #include "ogrsf_frmts.h" … … 44 43 OGRDataSource 45 44 #endif 46 * zoo_DS = NULL;45 **zoo_DS = NULL; 47 46 48 47 /** … … 52 51 53 52 /** 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 */ 60 char* _createInitString(maps* conf,const char* key){ 60 61 char* res=NULL; 61 char keywords[ 5][9]={62 char keywords[6][14]={ 62 63 "dbname", 63 64 "host", 64 65 "port", 65 66 "user", 66 "password" 67 "password", 68 "active_schema" 67 69 }; 68 70 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 } 70 76 int len=0; 71 for(i=0;i< 5;i++){77 for(i=0;i<6;i++){ 72 78 map* tmp=getMap(cconf->content,keywords[i]); 73 79 if(tmp!=NULL){ … … 98 104 99 105 /** 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 */ 111 char* createInitString(maps* conf){ 112 return _createInitString(conf,"database"); 113 } 114 115 /** 116 * Connect to a db backend. 101 117 * 102 118 * @param conf the maps containing the setting of the main.cfg file 103 119 * @see createInitString 104 120 */ 105 void init_sql(maps* conf){ 106 char* sqlInitString=createInitString(conf); 121 int _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; 107 129 OGRSFDriver *poDriver = NULL; 108 130 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= 110 144 #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, 112 161 GDAL_OF_UPDATE | GDAL_OF_VECTOR, 113 162 NULL, NULL, NULL ); 114 163 #else 115 zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);116 #endif 117 if( zoo_DS == NULL ){118 #ifdef DEBUG164 zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver); 165 #endif 166 if( zoo_DS[zoo_ds_nb] == NULL ){ 167 #ifdef SQL_DEBUG 119 168 fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString); 120 169 fflush(stderr); 121 170 #endif 122 171 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 127 177 fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString); 128 178 fflush(stderr); 129 179 #endif 130 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 */ 191 int init_sql(maps* conf){ 192 return _init_sql(conf,"database"); 131 193 } 132 194 … … 136 198 * @param conf the maps containing the setting of the main.cfg file 137 199 */ 138 void close_sql(maps* conf){ 139 if( zoo_ResultSet != NULL ) 140 zoo_DS->ReleaseResultSet( zoo_ResultSet ); 141 if(zoo_DS!=NULL){ 200 void 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){ 142 206 #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; 148 212 } 149 213 } … … 158 222 159 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 */ 230 OGRLayer *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 242 void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){ 243 if( objects != NULL ){ 244 zoo_DS[cid]->ReleaseResultSet( objects ); 245 objects=NULL; 246 } 247 } 248 249 /** 160 250 * Execute a SQL query to the SQL Database Backend. 161 251 * … … 164 254 * @return -1 in case of failure and 1 if the query succeed. 165 255 */ 166 int execSql(maps* conf,const char* sqlQuery){ 167 zoo_ResultSet = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL); 256 int 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); 168 261 if( zoo_ResultSet != NULL ){ 169 re turn1;170 } 171 return -1;262 res=1; 263 } 264 return res; 172 265 } 173 266 … … 179 272 * @return -1 in case of failure and 1 if the query succeed. 180 273 */ 181 void cleanUpResultSet(const maps* conf ){274 void cleanUpResultSet(const maps* conf,int cid){ 182 275 if( zoo_ResultSet != NULL ){ 183 zoo_DS ->ReleaseResultSet( zoo_ResultSet );276 zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet ); 184 277 zoo_ResultSet=NULL; 185 278 } 279 } 280 281 #ifdef RELY_ON_DB 282 int 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; 186 288 } 187 289 … … 195 297 */ 196 298 void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){ 299 int zoo_ds_nb=getCurrentId(conf); 197 300 map *uusid=getMapFromMaps(conf,"lenv","usid"); 198 301 map *schema=getMapFromMaps(conf,"database","schema"); … … 202 305 else 203 306 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); 207 310 } 208 311 … … 213 316 */ 214 317 void recordServiceStatus(maps* conf){ 318 int zoo_ds_nb=getCurrentId(conf); 215 319 map *sid=getMapFromMaps(conf,"lenv","sid"); 216 320 map *osid=getMapFromMaps(conf,"lenv","osid"); … … 230 334 osid->value, 231 335 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); 235 339 } 236 340 … … 242 346 */ 243 347 void recordResponse(maps* conf,char* filename){ 348 int zoo_ds_nb=getCurrentId(conf); 244 349 FILE *file = fopen (filename, "rb"); 245 350 fseek (file, 0, SEEK_END); … … 254 359 char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char)); 255 360 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); 257 362 free(sqlQuery); 258 363 free(tmps); 259 cleanUpResultSet(conf );364 cleanUpResultSet(conf,zoo_ds_nb-1); 260 365 } 261 366 … … 267 372 */ 268 373 int _updateStatus(maps* conf){ 374 int zoo_ds_nb=getCurrentId(conf); 269 375 map *sid=getMapFromMaps(conf,"lenv","usid"); 270 376 map *p=getMapFromMaps(conf,"lenv","status"); … … 273 379 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char)); 274 380 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 ){ 277 382 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); 280 387 free(sqlQuery); 281 388 return 0; … … 290 397 */ 291 398 char* _getStatus(maps* conf,char* pid){ 399 int zoo_ds_nb=getCurrentId(conf); 400 int created=-1; 292 401 map *schema=getMapFromMaps(conf,"database","schema"); 293 402 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); 294 403 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 ){ 296 411 init_sql(conf); 297 execSql(conf,sqlQuery); 412 zoo_ds_nb++; 413 created=1; 414 } 415 execSql(conf,zoo_ds_nb-1,sqlQuery); 298 416 OGRFeature *poFeature = NULL; 299 417 const char *tmp1; … … 301 419 for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ 302 420 if( poFeature->IsFieldSet( iField ) ){ 303 tmp1= strdup(poFeature->GetFieldAsString( iField ));421 tmp1=zStrdup(poFeature->GetFieldAsString( iField )); 304 422 } 305 423 else … … 308 426 OGRFeature::DestroyFeature( poFeature ); 309 427 } 310 cleanUpResultSet(conf );428 cleanUpResultSet(conf,zoo_ds_nb-1); 311 429 free(sqlQuery); 312 430 return (char*)tmp1; … … 321 439 */ 322 440 char* _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; 324 446 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char)); 325 447 sprintf(sqlQuery, 326 448 "select content from %s.responses where uuid=$$%s$$" 327 449 " 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 ){ 329 457 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 ); 339 472 } 340 else341 tmp1=NULL;342 }343 OGRFeature::DestroyFeature( poFeature );344 473 } 345 474 if(hasRes<0) 346 475 tmp1=NULL; 347 cleanUpResultSet(conf );476 cleanUpResultSet(conf,zoo_ds_nb-1); 348 477 free(sqlQuery); 349 478 return (char*)tmp1; … … 357 486 */ 358 487 void removeService(maps* conf,char* pid){ 488 int zoo_ds_nb=getCurrentId(conf); 359 489 map *schema=getMapFromMaps(conf,"database","schema"); 360 490 char *sqlQuery=(char*) 361 491 malloc((strlen(pid)+strlen(schema->value)+38+1) 362 492 *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 ){ 364 500 init_sql(conf); 501 zoo_ds_nb++; 502 } 365 503 sprintf(sqlQuery, 366 504 "DELETE FROM %s.services where uuid=$$%s$$;", 367 505 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); 371 509 free(sqlQuery); 372 510 end_sql(); … … 379 517 */ 380 518 void unhandleStatus(maps* conf){ 519 int zoo_ds_nb=getCurrentId(conf); 381 520 map *schema=getMapFromMaps(conf,"database","schema"); 382 521 map *sid=getMapFromMaps(conf,"lenv","usid"); … … 392 531 " where uuid=$$%s$$;", 393 532 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(); 399 538 } 400 539 … … 407 546 */ 408 547 char* getStatusId(maps* conf,char* pid){ 548 int zoo_ds_nb=getCurrentId(conf); 409 549 map *schema=getMapFromMaps(conf,"database","schema"); 410 550 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); … … 412 552 "select osid from %s.services where uuid=$$%s$$", 413 553 schema->value,pid); 414 if( zoo_ DS == NULL )554 if( zoo_ds_nb==0 ){ 415 555 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; 417 560 OGRFeature *poFeature = NULL; 418 561 const char *tmp1; … … 431 574 tmp1=NULL; 432 575 free(sqlQuery); 576 cleanUpResultSet(conf,zoo_ds_nb-1); 433 577 return (char*)tmp1; 434 578 } … … 441 585 */ 442 586 void readFinalRes(maps* conf,char* pid,map* statusInfo){ 587 int zoo_ds_nb=getCurrentId(conf); 443 588 map *schema=getMapFromMaps(conf,"database","schema"); 444 589 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); … … 448 593 if( zoo_DS == NULL ) 449 594 init_sql(conf); 450 execSql(conf, sqlQuery);595 execSql(conf,zoo_ds_nb-1,sqlQuery); 451 596 OGRFeature *poFeature = NULL; 452 597 int hasRes=-1; … … 461 606 OGRFeature::DestroyFeature( poFeature ); 462 607 } 608 cleanUpResultSet(conf,zoo_ds_nb-1); 463 609 if(hasRes<0) 464 610 addToMap(statusInfo,"Status","Failed"); … … 476 622 int isRunning(maps* conf,char* pid){ 477 623 int res=0; 624 int zoo_ds_nb=getCurrentId(conf); 478 625 map *schema=getMapFromMaps(conf,"database","schema"); 479 626 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char)); 480 627 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 ){ 482 629 init_sql(conf); 483 execSql(conf,sqlQuery); 630 zoo_ds_nb++; 631 } 632 execSql(conf,zoo_ds_nb-1,sqlQuery); 484 633 OGRFeature *poFeature = NULL; 485 634 const char *tmp1; … … 494 643 OGRFeature::DestroyFeature( poFeature ); 495 644 } 496 cleanUpResultSet(conf );645 cleanUpResultSet(conf,zoo_ds_nb-1); 497 646 free(sqlQuery); 498 647 return res;
Note: See TracChangeset
for help on using the changeset viewer.