Changeset 839 for branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c
- Timestamp:
- Aug 7, 2017, 2:56:24 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c
r822 r839 43 43 OGRDataSource 44 44 #endif 45 * zoo_DS = NULL;45 **zoo_DS = NULL; 46 46 47 47 /** … … 70 70 int i=0; 71 71 maps* cconf=getMaps(conf,key); 72 if(cconf==NULL) 73 return "-1"; 72 74 int len=0; 73 75 for(i=0;i<6;i++){ 74 76 map* tmp=getMap(cconf->content,keywords[i]); 75 fprintf(stderr,"%s %d \n",__FILE__,__LINE__);76 fflush(stderr);77 77 if(tmp!=NULL){ 78 fprintf(stderr,"%s %d \n",__FILE__,__LINE__);79 fflush(stderr);80 78 if(res==NULL){ 81 79 res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char)); … … 89 87 len+=strlen(res1); 90 88 res[len]=0; 91 fprintf(stderr,"%s %d \n",__FILE__,__LINE__);92 fflush(stderr);93 89 free(res1); 94 90 } 95 91 } 96 fprintf(stderr,"%s %d \n",__FILE__,__LINE__); 97 fflush(stderr); 98 } 99 fprintf(stderr,"%s %d \n",__FILE__,__LINE__); 100 fflush(stderr); 92 } 101 93 map* tmp=getMap(cconf->content,"type"); 102 94 if(tmp!=NULL){ … … 125 117 * @see createInitString 126 118 */ 127 void_init_sql(maps* conf,const char* key){119 int _init_sql(maps* conf,const char* key){ 128 120 char* sqlInitString=_createInitString(conf,key); 121 if(strncmp(sqlInitString,"-1",2)==0) 122 return -1; 129 123 OGRSFDriver *poDriver = NULL; 130 124 OGRRegisterAll(); 131 125 int zoo_ds_nb=0; 126 map* dsNb=getMapFromMaps(conf,"lenv","ds_nb"); 127 if(dsNb==NULL){ 128 setMapInMaps(conf,"lenv","ds_nb","1"); 129 }else{ 130 zoo_ds_nb=atoi(dsNb->value); 131 char* tmp=(char*)malloc(11*sizeof(char)); 132 sprintf(tmp,"%d",zoo_ds_nb+1); 133 setMapInMaps(conf,"lenv","ds_nb",(const char*)tmp); 134 free(tmp); 135 } 136 if(zoo_DS==NULL) 137 zoo_DS= 132 138 #if GDAL_VERSION_MAJOR >= 2 133 zoo_DS = (GDALDataset*) GDALOpenEx( sqlInitString, 139 (GDALDataset**) malloc(sizeof(GDALDataset*)) 140 #else 141 (OGRDataSource**) malloc(sizeof(OGRDataSource*)) 142 #endif 143 ; 144 else 145 zoo_DS= 146 #if GDAL_VERSION_MAJOR >= 2 147 (GDALDataset**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(GDALDataset*)) 148 #else 149 (OGRDataSource**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(OGRDataSource*)) 150 #endif 151 ; 152 153 #if GDAL_VERSION_MAJOR >= 2 154 zoo_DS[zoo_ds_nb] = (GDALDataset*) GDALOpenEx( sqlInitString, 134 155 GDAL_OF_UPDATE | GDAL_OF_VECTOR, 135 156 NULL, NULL, NULL ); 136 157 #else 137 zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);138 #endif 139 if( zoo_DS == NULL ){158 zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver); 159 #endif 160 if( zoo_DS[zoo_ds_nb] == NULL ){ 140 161 #ifdef DEBUG 141 162 fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString); … … 143 164 #endif 144 165 free(sqlInitString); 145 setMapInMaps(conf,"lenv","message","Failed to connect to the database backend"); 146 return; 166 setMapInMaps(conf,"lenv","dbIssue","1"); 167 setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend")); 168 return -2; 147 169 } 148 170 #ifdef DEBUG … … 150 172 fflush(stderr); 151 173 #endif 174 fprintf(stderr,"sqlInitString: %s SUCEED %d !\n",sqlInitString,zoo_ds_nb); 175 fflush(stderr); 152 176 free(sqlInitString); 177 zoo_ds_nb++; 178 return zoo_ds_nb; 153 179 } 154 180 … … 159 185 * @see createInitString 160 186 */ 161 voidinit_sql(maps* conf){187 int init_sql(maps* conf){ 162 188 return _init_sql(conf,"database"); 163 189 } … … 168 194 * @param conf the maps containing the setting of the main.cfg file 169 195 */ 170 void close_sql(maps* conf ){196 void close_sql(maps* conf,int cid){ 171 197 if( zoo_ResultSet != NULL ) 172 zoo_DS ->ReleaseResultSet( zoo_ResultSet );198 zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet ); 173 199 if(zoo_DS!=NULL){ 174 200 #if GDAL_VERSION_MAJOR >= 2 175 GDALClose(zoo_DS );176 #else 177 OGRDataSource::DestroyDataSource( zoo_DS );178 #endif 179 zoo_DS =NULL;201 GDALClose(zoo_DS[cid]); 202 #else 203 OGRDataSource::DestroyDataSource( zoo_DS[cid] ); 204 #endif 205 zoo_DS[cid]=NULL; 180 206 } 181 207 } … … 196 222 * @return NULL in case of failure or an OGRLayer pointer if the query succeed. 197 223 */ 198 OGRLayer *fetchSql(maps* conf,const char* sqlQuery){ 224 OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){ 225 if(zoo_DS[cid]==NULL) 226 return NULL; 199 227 OGRLayer *res=NULL; 200 res = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL); 228 #ifdef DEBUG 229 fprintf(stderr,"************************* %s %s %d\n\n",sqlQuery,__FILE__,__LINE__); 230 fflush(stderr); 231 #endif 232 res = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL); 201 233 return res; 202 234 } 203 235 204 void cleanFetchSql(maps* conf, OGRLayer *objects){236 void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){ 205 237 if( objects != NULL ){ 206 zoo_DS ->ReleaseResultSet( objects );238 zoo_DS[cid]->ReleaseResultSet( objects ); 207 239 objects=NULL; 208 240 } … … 216 248 * @return -1 in case of failure and 1 if the query succeed. 217 249 */ 218 int execSql(maps* conf, const char* sqlQuery){250 int execSql(maps* conf,int cid,const char* sqlQuery){ 219 251 int res=-1; 220 zoo_ResultSet = zoo_DS ->ExecuteSQL( sqlQuery, NULL, NULL);252 zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL); 221 253 if( zoo_ResultSet != NULL ){ 222 254 res=1; 223 255 } 224 zoo_DS->ReleaseResultSet( zoo_ResultSet );225 256 return res; 226 257 } … … 233 264 * @return -1 in case of failure and 1 if the query succeed. 234 265 */ 235 void cleanUpResultSet(const maps* conf ){266 void cleanUpResultSet(const maps* conf,int cid){ 236 267 if( zoo_ResultSet != NULL ){ 237 zoo_DS ->ReleaseResultSet( zoo_ResultSet );268 zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet ); 238 269 zoo_ResultSet=NULL; 239 270 } 271 } 272 273 #ifdef RELY_ON_DB 274 int getCurrentId(maps* conf){ 275 int res=0; 276 map* dsNb=getMapFromMaps(conf,"lenv","ds_nb"); 277 if(dsNb!=NULL) 278 res=atoi(dsNb->value); 279 return res; 240 280 } 241 281 … … 249 289 */ 250 290 void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){ 291 int zoo_ds_nb=getCurrentId(conf); 251 292 map *uusid=getMapFromMaps(conf,"lenv","usid"); 252 293 map *schema=getMapFromMaps(conf,"database","schema"); … … 256 297 else 257 298 sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type); 258 execSql(conf, sqlQuery);259 free(sqlQuery); 260 cleanUpResultSet(conf );299 execSql(conf,zoo_ds_nb-1,sqlQuery); 300 free(sqlQuery); 301 cleanUpResultSet(conf,zoo_ds_nb-1); 261 302 } 262 303 … … 267 308 */ 268 309 void recordServiceStatus(maps* conf){ 310 int zoo_ds_nb=getCurrentId(conf); 269 311 map *sid=getMapFromMaps(conf,"lenv","sid"); 270 312 map *osid=getMapFromMaps(conf,"lenv","osid"); … … 284 326 osid->value, 285 327 wpsStatus[2]); 286 execSql(conf, sqlQuery);287 free(sqlQuery); 288 cleanUpResultSet(conf );328 execSql(conf,zoo_ds_nb-1,sqlQuery); 329 free(sqlQuery); 330 cleanUpResultSet(conf,zoo_ds_nb-1); 289 331 } 290 332 … … 296 338 */ 297 339 void recordResponse(maps* conf,char* filename){ 340 int zoo_ds_nb=getCurrentId(conf); 298 341 FILE *file = fopen (filename, "rb"); 299 342 fseek (file, 0, SEEK_END); … … 308 351 char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char)); 309 352 sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value); 310 execSql(conf, sqlQuery);353 execSql(conf,zoo_ds_nb-1,sqlQuery); 311 354 free(sqlQuery); 312 355 free(tmps); 313 cleanUpResultSet(conf); 314 } 315 316 #ifdef RELY_ON_DB 356 cleanUpResultSet(conf,zoo_ds_nb-1); 357 } 358 317 359 /** 318 360 * Update the current status of the running service. … … 322 364 */ 323 365 int _updateStatus(maps* conf){ 366 int zoo_ds_nb=getCurrentId(conf); 324 367 map *sid=getMapFromMaps(conf,"lenv","usid"); 325 368 map *p=getMapFromMaps(conf,"lenv","status"); … … 328 371 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char)); 329 372 sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value); 330 fflush(stderr); 331 if( zoo_DS == NULL ) 373 if( zoo_ds_nb== 374 #ifdef META_DB 375 1 376 #else 377 0 378 #endif 379 ){ 332 380 init_sql(conf); 333 execSql(conf,sqlQuery); 334 cleanUpResultSet(conf); 381 zoo_ds_nb++; 382 } 383 execSql(conf,zoo_ds_nb-1,sqlQuery); 384 cleanUpResultSet(conf,zoo_ds_nb-1); 335 385 free(sqlQuery); 336 386 return 0; … … 345 395 */ 346 396 char* _getStatus(maps* conf,char* pid){ 397 int zoo_ds_nb=getCurrentId(conf); 347 398 map *schema=getMapFromMaps(conf,"database","schema"); 348 399 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); 349 400 sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid); 350 if( zoo_DS == NULL ) 401 if( zoo_ds_nb== 402 #ifdef META_DB 403 1 404 #else 405 0 406 #endif 407 ){ 351 408 init_sql(conf); 352 execSql(conf,sqlQuery); 409 zoo_ds_nb++; 410 } 411 execSql(conf,zoo_ds_nb-1,sqlQuery); 353 412 OGRFeature *poFeature = NULL; 354 413 const char *tmp1; … … 363 422 OGRFeature::DestroyFeature( poFeature ); 364 423 } 365 cleanUpResultSet(conf );424 cleanUpResultSet(conf,zoo_ds_nb-1); 366 425 free(sqlQuery); 367 426 return (char*)tmp1; … … 376 435 */ 377 436 char* _getStatusFile(maps* conf,char* pid){ 437 int zoo_ds_nb=getCurrentId(conf); 378 438 map *schema=getMapFromMaps(conf,"database","schema"); 379 439 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char)); … … 381 441 "select content from %s.responses where uuid=$$%s$$" 382 442 " order by creation_time desc limit 1",schema->value,pid); 383 if( zoo_DS == NULL ) 443 if( zoo_ds_nb== 444 #ifdef META_DB 445 1 446 #else 447 0 448 #endif 449 ){ 384 450 init_sql(conf); 385 execSql(conf,sqlQuery); 451 zoo_ds_nb++; 452 } 453 execSql(conf,zoo_ds_nb-1,sqlQuery); 386 454 OGRFeature *poFeature = NULL; 387 455 const char *tmp1; 388 456 int hasRes=-1; 389 while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ 390 for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ 391 if( poFeature->IsFieldSet( iField ) ){ 392 tmp1=strdup(poFeature->GetFieldAsString( iField )); 393 hasRes=1; 457 if(zoo_ResultSet!=NULL){ 458 while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ 459 for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ 460 if( poFeature->IsFieldSet( iField ) ){ 461 tmp1=strdup(poFeature->GetFieldAsString( iField )); 462 hasRes=1; 463 } 464 else 465 tmp1=NULL; 466 } 467 OGRFeature::DestroyFeature( poFeature ); 394 468 } 395 else396 tmp1=NULL;397 }398 OGRFeature::DestroyFeature( poFeature );399 469 } 400 470 if(hasRes<0) 401 471 tmp1=NULL; 402 cleanUpResultSet(conf); 472 else 473 cleanUpResultSet(conf,zoo_ds_nb-1); 403 474 free(sqlQuery); 404 475 return (char*)tmp1; … … 412 483 */ 413 484 void removeService(maps* conf,char* pid){ 485 int zoo_ds_nb=getCurrentId(conf); 414 486 map *schema=getMapFromMaps(conf,"database","schema"); 415 487 char *sqlQuery=(char*) 416 488 malloc((strlen(pid)+strlen(schema->value)+38+1) 417 489 *sizeof(char)); 418 if( zoo_DS == NULL ) 490 if( zoo_ds_nb== 491 #ifdef META_DB 492 1 493 #else 494 0 495 #endif 496 ){ 419 497 init_sql(conf); 498 zoo_ds_nb++; 499 } 420 500 sprintf(sqlQuery, 421 501 "DELETE FROM %s.services where uuid=$$%s$$;", 422 502 schema->value,pid); 423 execSql(conf, sqlQuery);424 cleanUpResultSet(conf );425 close_sql(conf );503 execSql(conf,zoo_ds_nb-1,sqlQuery); 504 cleanUpResultSet(conf,zoo_ds_nb-1); 505 close_sql(conf,zoo_ds_nb-1); 426 506 free(sqlQuery); 427 507 end_sql(); … … 434 514 */ 435 515 void unhandleStatus(maps* conf){ 516 int zoo_ds_nb=getCurrentId(conf); 436 517 map *schema=getMapFromMaps(conf,"database","schema"); 437 518 map *sid=getMapFromMaps(conf,"lenv","usid"); … … 447 528 " where uuid=$$%s$$;", 448 529 schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value); 449 execSql(conf, sqlQuery);450 cleanUpResultSet(conf );451 close_sql(conf );530 execSql(conf,zoo_ds_nb-1,sqlQuery); 531 cleanUpResultSet(conf,zoo_ds_nb-1); 532 close_sql(conf,zoo_ds_nb-1); 452 533 free(sqlQuery); 453 534 end_sql(); … … 462 543 */ 463 544 char* getStatusId(maps* conf,char* pid){ 545 int zoo_ds_nb=getCurrentId(conf); 464 546 map *schema=getMapFromMaps(conf,"database","schema"); 465 547 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); … … 467 549 "select osid from %s.services where uuid=$$%s$$", 468 550 schema->value,pid); 469 if( zoo_ DS == NULL )551 if( zoo_ds_nb==0 ){ 470 552 init_sql(conf); 471 execSql(conf,sqlQuery); 553 zoo_ds_nb++; 554 } 555 execSql(conf,zoo_ds_nb-1,sqlQuery); 472 556 OGRFeature *poFeature = NULL; 473 557 const char *tmp1; … … 486 570 tmp1=NULL; 487 571 free(sqlQuery); 572 cleanUpResultSet(conf,zoo_ds_nb-1); 488 573 return (char*)tmp1; 489 574 } … … 496 581 */ 497 582 void readFinalRes(maps* conf,char* pid,map* statusInfo){ 583 int zoo_ds_nb=getCurrentId(conf); 498 584 map *schema=getMapFromMaps(conf,"database","schema"); 499 585 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); … … 503 589 if( zoo_DS == NULL ) 504 590 init_sql(conf); 505 execSql(conf, sqlQuery);591 execSql(conf,zoo_ds_nb-1,sqlQuery); 506 592 OGRFeature *poFeature = NULL; 507 593 int hasRes=-1; … … 531 617 int isRunning(maps* conf,char* pid){ 532 618 int res=0; 619 int zoo_ds_nb=getCurrentId(conf); 533 620 map *schema=getMapFromMaps(conf,"database","schema"); 534 621 char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char)); 535 622 sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid); 536 if( zoo_ DS == NULL )623 if( zoo_ds_nb == 0 ){ 537 624 init_sql(conf); 538 execSql(conf,sqlQuery); 625 zoo_ds_nb++; 626 } 627 execSql(conf,zoo_ds_nb-1,sqlQuery); 539 628 OGRFeature *poFeature = NULL; 540 629 const char *tmp1; … … 549 638 OGRFeature::DestroyFeature( poFeature ); 550 639 } 551 cleanUpResultSet(conf );640 cleanUpResultSet(conf,zoo_ds_nb-1); 552 641 free(sqlQuery); 553 642 return res;
Note: See TracChangeset
for help on using the changeset viewer.