/* collect all crd data and write it to a syslog */ static int db_write_cdr( struct dlg_cell* dialog, struct sip_msg* message) { int m = 0; int i; db_func_t *df=NULL; db1_con_t *dh=NULL; void *vf=NULL; void *vh=NULL; if(acc_cdrs_table.len<=0) return 0; if(acc_get_db_handlers(&vf, &vh)<0) { LM_ERR("cannot get db handlers\n"); return -1; } df = (db_func_t*)vf; dh = (db1_con_t*)vh; /* get default values */ m = cdr_core2strar( dialog, cdr_value_array, cdr_int_array, cdr_type_array); for(i=0; i<m; i++) { db_cdr_keys[i] = &cdr_attrs[i]; VAL_TYPE(db_cdr_vals+i)=DB1_STR; VAL_NULL(db_cdr_vals+i)=0; VAL_STR(db_cdr_vals+i) = cdr_value_array[i]; } /* get extra values */ m += extra2strar( cdr_extra, message, cdr_value_array + m, cdr_int_array + m, cdr_type_array + m); for( ; i<m; i++) { db_cdr_keys[i] = &cdr_attrs[i]; VAL_TYPE(db_cdr_vals+i)=DB1_STR; VAL_NULL(db_cdr_vals+i)=0; VAL_STR(db_cdr_vals+i) = cdr_value_array[i]; } if (df->use_table(dh, &acc_cdrs_table /*table*/) < 0) { LM_ERR("error in use_table\n"); return -1; } if(acc_db_insert_mode==1 && df->insert_delayed!=NULL) { if (df->insert_delayed(dh, db_cdr_keys, db_cdr_vals, m) < 0) { LM_ERR("failed to insert delayed into database\n"); return -1; } } else { if (df->insert(dh, db_cdr_keys, db_cdr_vals, m) < 0) { LM_ERR("failed to insert into database\n"); return -1; } } return 0; }
/* collect all crd data and write it to a syslog */ static int db_write_cdr( struct dlg_cell* dialog, struct sip_msg* message) { int m = 0; int n = 0; int i; db_func_t *df=NULL; db1_con_t *dh=NULL; void *vf=NULL; void *vh=NULL; struct timeval timeval_val; long long_val; double double_val; char * end; if(acc_cdrs_table.len<=0) return 0; if(acc_get_db_handlers(&vf, &vh)<0) { LM_ERR("cannot get db handlers\n"); return -1; } df = (db_func_t*)vf; dh = (db1_con_t*)vh; /* get default values */ m = cdr_core2strar( dialog, cdr_value_array, cdr_int_array, cdr_type_array); for(i=0; i<m; i++) { db_cdr_keys[i] = &cdr_attrs[i]; /* reset errno, some strtoX don't reset it */ errno = 0; switch(cdr_type_array[i]) { case TYPE_NULL: VAL_NULL(db_cdr_vals+i)=1; break; case TYPE_INT: VAL_TYPE(db_cdr_vals+i)=DB1_INT; VAL_NULL(db_cdr_vals+i)=0; long_val = strtol(cdr_value_array[i].s, &end, 10); if(errno && (errno != EAGAIN)) { LM_ERR("failed to convert string to integer - %d.\n", errno); goto error; } VAL_INT(db_cdr_vals+i) = long_val; break; case TYPE_STR: VAL_TYPE(db_cdr_vals+i)=DB1_STR; VAL_NULL(db_cdr_vals+i)=0; VAL_STR(db_cdr_vals+i) = cdr_value_array[i]; break; case TYPE_DATE: VAL_TYPE(db_cdr_vals+i)=DB1_DATETIME; VAL_NULL(db_cdr_vals+i)=0; if(string2time(&cdr_value_array[i], &timeval_val) < 0) { LM_ERR("failed to convert string to timeval.\n"); goto error; } VAL_TIME(db_cdr_vals+i) = timeval_val.tv_sec; break; case TYPE_DOUBLE: VAL_TYPE(db_cdr_vals+i)=DB1_DOUBLE; VAL_NULL(db_cdr_vals+i)=0; double_val = strtod(cdr_value_array[i].s, &end); if(errno && (errno != EAGAIN)) { LM_ERR("failed to convert string to double - %d.\n", errno); goto error; } VAL_DOUBLE(db_cdr_vals+i) = double_val; break; } } /* get extra values */ if (message) { n += extra2strar( cdr_extra, message, cdr_value_array + m, cdr_int_array + m, cdr_type_array + m); m += n; } else if (cdr_expired_dlg_enable){ LM_WARN( "fallback to dlg_only search because of message doesn't exist.\n"); m += extra2strar_dlg_only( cdr_extra, dialog, cdr_value_array + m, cdr_int_array + m, cdr_type_array +m, &dlgb); } for( ; i<m; i++) { db_cdr_keys[i] = &cdr_attrs[i]; VAL_TYPE(db_cdr_vals+i)=DB1_STR; VAL_NULL(db_cdr_vals+i)=0; VAL_STR(db_cdr_vals+i) = cdr_value_array[i]; } if (df->use_table(dh, &acc_cdrs_table /*table*/) < 0) { LM_ERR("error in use_table\n"); goto error; } if(acc_db_insert_mode==1 && df->insert_delayed!=NULL) { if (df->insert_delayed(dh, db_cdr_keys, db_cdr_vals, m) < 0) { LM_ERR("failed to insert delayed into database\n"); goto error; } } else if(acc_db_insert_mode==2 && df->insert_async!=NULL) { if (df->insert_async(dh, db_cdr_keys, db_cdr_vals, m) < 0) { LM_ERR("failed to insert async into database\n"); goto error; } } else { if (df->insert(dh, db_cdr_keys, db_cdr_vals, m) < 0) { LM_ERR("failed to insert into database\n"); goto error; } } /* Free memory allocated by acc_extra.c/extra2strar */ free_strar_mem( &(cdr_type_array[m-n]), &(cdr_value_array[m-n]), n, m); return 0; error: /* Free memory allocated by acc_extra.c/extra2strar */ free_strar_mem( &(cdr_type_array[m-n]), &(cdr_value_array[m-n]), n, m); return -1; }