/*load rules from DB*/ int dp_load_db(dp_connection_list_p dp_conn) { int i, nr_rows; db_res_t * res = 0; db_val_t * values; db_row_t * rows; db_key_t query_cols[DP_TABLE_COL_NO] = { &dpid_column, &pr_column, &match_op_column, &match_exp_column, &match_flags_column, &subst_exp_column, &repl_exp_column, &attrs_column, &timerec_column }; db_key_t order = &pr_column; /* disabled condition */ db_key_t cond_cols[1] = { &disabled_column }; db_val_t cond_val[1]; dpl_node_t *rule; int no_rows = 10; lock_start_write( dp_conn->ref_lock ); if( dp_conn->crt_index != dp_conn->next_index){ LM_WARN("a load command already generated, aborting reload...\n"); lock_stop_write( dp_conn->ref_lock ); return 0; } dp_conn->next_index = dp_conn->crt_index == 0 ? 1 : 0; lock_stop_write( dp_conn->ref_lock ); if (dp_conn->dp_dbf.use_table(*dp_conn->dp_db_handle, &dp_conn->table_name) < 0){ LM_ERR("error in use_table\n"); goto err1; } VAL_TYPE(cond_val) = DB_INT; VAL_NULL(cond_val) = 0; VAL_INT(cond_val) = 0; if (DB_CAPABILITY(dp_conn->dp_dbf, DB_CAP_FETCH)) { if(dp_conn->dp_dbf.query(*dp_conn->dp_db_handle,cond_cols, 0,cond_val,query_cols,1, DP_TABLE_COL_NO, order, 0) < 0){ LM_ERR("failed to query database!\n"); goto err1; } no_rows = estimate_available_rows( 4+4+4+64+4+64+64+128, DP_TABLE_COL_NO); if (no_rows==0) no_rows = 10; if(dp_conn->dp_dbf.fetch_result(*dp_conn->dp_db_handle, &res, no_rows)<0) { LM_ERR("failed to fetch\n"); if (res) dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res); goto err1; } } else { /*select the whole table and all the columns*/ if(dp_conn->dp_dbf.query(*dp_conn->dp_db_handle, cond_cols,0,cond_val,query_cols,1, DP_TABLE_COL_NO, order, &res) < 0){ LM_ERR("failed to query database\n"); goto err1; } } nr_rows = RES_ROW_N(res); if(nr_rows == 0){ LM_WARN("no data in the db\n"); goto end; } do { for(i=0; i<RES_ROW_N(res); i++){ rows = RES_ROWS(res); values = ROW_VALUES(rows+i); if ((rule = build_rule(values)) == NULL) { LM_WARN(" failed to build rule -> skipping\n"); continue; } rule->table_id = i; if(add_rule2hash(rule , dp_conn, dp_conn->next_index) != 0) { LM_ERR("add_rule2hash failed\n"); goto err2; } } if (DB_CAPABILITY(dp_conn->dp_dbf, DB_CAP_FETCH)) { if(dp_conn->dp_dbf.fetch_result(*dp_conn->dp_db_handle, &res, no_rows)<0) { LM_ERR("failure while fetching!\n"); if (res) dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res); goto err1; } } else { break; } } while(RES_ROW_N(res)>0); end: /*update data*/ lock_start_write( dp_conn->ref_lock ); destroy_hash(&dp_conn->hash[dp_conn->crt_index]); dp_conn->crt_index = dp_conn->next_index; lock_stop_write( dp_conn->ref_lock ); list_hash(dp_conn->hash[dp_conn->crt_index], dp_conn->ref_lock); dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res); return 0; err1: lock_start_write( dp_conn->ref_lock ); dp_conn->next_index = dp_conn->crt_index; lock_stop_write( dp_conn->ref_lock ); return -1; err2: if(rule) destroy_rule(rule); destroy_hash(&dp_conn->hash[dp_conn->next_index]); dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res); lock_start_write( dp_conn->ref_lock ); dp_conn->next_index = dp_conn->crt_index; /* if lock defined - release the exclusive writing access */ lock_stop_write( dp_conn->ref_lock ); return -1; }
/*load rules from DB*/ int dp_load_db(void) { int i, nr_rows; db1_res_t * res = 0; db_val_t * values; db_row_t * rows; db_key_t query_cols[DP_TABLE_COL_NO] = { &dpid_column, &pr_column, &match_op_column, &match_exp_column, &match_len_column, &subst_exp_column, &repl_exp_column, &attrs_column }; db_key_t order = &pr_column; dpl_node_t *rule; LM_DBG("init\n"); if( (*crt_idx) != (*next_idx)){ LM_WARN("a load command already generated, aborting reload...\n"); return 0; } if (dp_dbf.use_table(dp_db_handle, &dp_table_name) < 0){ LM_ERR("error in use_table %.*s\n", dp_table_name.len, dp_table_name.s); return -1; } if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) { if(dp_dbf.query(dp_db_handle,0,0,0,query_cols, 0, DP_TABLE_COL_NO, order, 0) < 0){ LM_ERR("failed to query database!\n"); return -1; } if(dp_dbf.fetch_result(dp_db_handle, &res, dp_fetch_rows)<0) { LM_ERR("failed to fetch\n"); if (res) dp_dbf.free_result(dp_db_handle, res); return -1; } } else { /*select the whole table and all the columns*/ if(dp_dbf.query(dp_db_handle,0,0,0,query_cols, 0, DP_TABLE_COL_NO, order, &res) < 0){ LM_ERR("failed to query database\n"); return -1; } } nr_rows = RES_ROW_N(res); *next_idx = ((*crt_idx) == 0)? 1:0; destroy_hash(*next_idx); if(nr_rows == 0){ LM_WARN("no data in the db\n"); goto end; } do { for(i=0; i<RES_ROW_N(res); i++){ rows = RES_ROWS(res); values = ROW_VALUES(rows+i); if((rule = build_rule(values)) ==0 ) goto err2; if(add_rule2hash(rule , *next_idx) != 0) goto err2; } if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) { if(dp_dbf.fetch_result(dp_db_handle, &res, dp_fetch_rows)<0) { LM_ERR("failure while fetching!\n"); if (res) dp_dbf.free_result(dp_db_handle, res); return -1; } } else { break; } } while(RES_ROW_N(res)>0); end: /*update data*/ *crt_idx = *next_idx; list_hash(*crt_idx); dp_dbf.free_result(dp_db_handle, res); return 0; err2: if(rule) destroy_rule(rule); destroy_hash(*next_idx); dp_dbf.free_result(dp_db_handle, res); *next_idx = *crt_idx; return -1; }
/*load rules from DB*/ int dp_load_db(void) { int i, nr_rows; db_res_t * res = 0; db_val_t * values; db_row_t * rows; db_key_t query_cols[DP_TABLE_COL_NO] = { &dpid_column, &pr_column, &match_op_column, &match_exp_column, &match_len_column, &subst_exp_column, &repl_exp_column, &attrs_column }; db_key_t order = &pr_column; dpl_node_t *rule; int no_rows = 10; if( (*crt_idx) != (*next_idx)){ LM_WARN("a load command already generated, aborting reload...\n"); return 0; } if (dp_dbf.use_table(dp_db_handle, &dp_table_name) < 0){ LM_ERR("error in use_table\n"); return -1; } if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) { if(dp_dbf.query(dp_db_handle,0,0,0,query_cols, 0, DP_TABLE_COL_NO, order, 0) < 0){ LM_ERR("failed to query database!\n"); return -1; } no_rows = estimate_available_rows( 4+4+4+64+4+64+64+128, DP_TABLE_COL_NO); if (no_rows==0) no_rows = 10; if(dp_dbf.fetch_result(dp_db_handle, &res, no_rows)<0) { LM_ERR("failed to fetch\n"); if (res) dp_dbf.free_result(dp_db_handle, res); return -1; } } else { /*select the whole table and all the columns*/ if(dp_dbf.query(dp_db_handle,0,0,0,query_cols, 0, DP_TABLE_COL_NO, order, &res) < 0){ LM_ERR("failed to query database\n"); return -1; } } nr_rows = RES_ROW_N(res); lock_start_write( ref_lock ); *next_idx = ((*crt_idx) == 0)? 1:0; if(nr_rows == 0){ LM_WARN("no data in the db\n"); goto end; } do { for(i=0; i<RES_ROW_N(res); i++){ rows = RES_ROWS(res); values = ROW_VALUES(rows+i); if ((rule = build_rule(values)) == NULL ) { LM_WARN(" failed to build rule -> skipping\n"); continue; } if(add_rule2hash(rule , *next_idx) != 0) { LM_ERR("add_rule2hash failed\n"); goto err2; } } if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) { if(dp_dbf.fetch_result(dp_db_handle, &res, no_rows)<0) { LM_ERR("failure while fetching!\n"); if (res) dp_dbf.free_result(dp_db_handle, res); lock_stop_write( ref_lock ); return -1; } } else { break; } } while(RES_ROW_N(res)>0); end: destroy_hash(*crt_idx); /*update data*/ *crt_idx = *next_idx; /* release the exclusive writing access */ lock_stop_write( ref_lock ); list_hash(*crt_idx); dp_dbf.free_result(dp_db_handle, res); return 0; err2: if(rule) destroy_rule(rule); destroy_hash(*next_idx); dp_dbf.free_result(dp_db_handle, res); *next_idx = *crt_idx; /* if lock defined - release the exclusive writing access */ if(ref_lock) /* release the readers */ lock_stop_write( ref_lock ); return -1; }