[Serusers] mysql API

.:: Francesco la Torre ::. francesco.latorre at studenti.ing.unipi.it
Tue Jul 1 13:29:35 CEST 2008


Hi all,
is there any reference to new *db* API ?

In developer's guide, ch 8 there are old api.

Reading ser code, in particular auth_db module, some functions seems to
use valid database api.

I need only to do a query but it's hard to build correctly a query and
execute this, referencing only to some _similar_ code in some module.

What I need, and hope to be usefull for anybody, is a
template/guide/tutorial/documentation/dummy_example for a correct query
execution.


There is a snippet of the code :

 471 static int db_example(char* db_url){
 472     str result;
 473     db_cmd_t *query = NULL;
 474     db_res_t *result_t;
 475     db_rec_t *row;
 476 
 477     str id_column;
 478     str value_column;
 479     str table;
 480     str id;
 481 
 482     table.s = "value_att";
 483     table.len  = 10;
 484     id_column.s = "id_value";
 485     id_column.len = 9;
 486     value_column.s = "value";
 487     value_column.len = 5;
 488     int len;
 489 
 490     module_db_handle = db_ctx("module_db");
 491     if (!module_db_handle) goto err;
 492     if (db_add_db(module_db_handle, db_url) < 0) goto err;
 493     if (db_connect(module_db_handle) < 0)
 494         goto err;
 495     else
 496         LOG(L_INFO,"DB connected.\n");
 497 
 498     /* Setup query */
 499 
 500     db_fld_t match_with[] = {
 501         { .name = id_column.s, .type = DB_INT },
 502         { .name = NULL }
 503     };
 504     db_fld_t *result_cols = NULL;
 505 
 506 
 507     len = sizeof(*result_cols);
 508     result_cols = pkg_malloc(len);
 509     if (!result_cols) {
 510         ERR("can't allocate pkg mem\n");
 511         return -1;
 512     }
 513     memset(result_cols, 0, len);
 514 
 515     result_cols->name = value_column.s;
 516     result_cols->type = DB_STR;
 517 
 518     query =  db_cmd(DB_GET, module_db_handle, table.s, result_cols,
match_with, NULL);
 519 
 520     id.s="2";
 521     id.len=2;
 522     query->match[0].v.lstr = id;

 if (db_exec(&result_t, query) < 0 ) {
 525                  ERR("Error while querying database\n");
 526                  return -1;
 527      }
 528 
 529     if (result_t) row = db_first(result_t);
 530     else row=NULL;
 531 
 532     while (row) {
 533         if (IS_NULL((row)->fld[0]) || IS_NULL((row)->fld[1])) {
 534             
 535         } else {
 536             if ((row)->fld[1].v.int4 & DB_DISABLED) {
 537             } else {
 538                 if ((row)->fld[1].v.int4 & DB_LOAD_SER) {
 539                     break;
 540                 }
 541             }
 542         }
 543         row = db_next(result_t);
 544     }
 545 
 546     if (!row) {
 547         DBG("Value not found\n");
 548         return 1;
 549     }               
 550 
 551     result.s = (row)->fld[0].v.cstr;
 552     result.len = strlen(result.s);
 553    
 554 
 555     return 0;
 556 
 557 
 558 
 559 
 560 err:
 561 
 562     if (module_db_handle) {
 563         module_db_handle = NULL;
 564         db_ctx_free(module_db_handle);
 565     }
 566 
 567     LOG(L_ERR,"Error while initializing database layer\n");
 568     return -1;
 569 
 570 }    

I really hope to solve this problem because I don't want to use my sql
library when ser has his own database support.

Thanks in advance to anybody,
Francesco la Torre

_______________________________________________
Serdev mailing list
Serdev at lists.iptel.org
http://lists.iptel.org/mailman/listinfo/serdev




More information about the sr-users mailing list