int sqlops_is_null(str *sres, int i, int j) { sql_result_t *res = NULL; res = sql_get_result(sres); if(res==NULL) { LM_ERR("invalid result [%.*s]\n", sres->len, sres->s); goto error; } if(i>=res->nrows) { LM_ERR("row index out of bounds [%d/%d]\n", i, res->nrows); goto error; } if(j>=res->ncols) { LM_ERR("column index out of bounds [%d/%d]\n", j, res->ncols); goto error; } if(res->vals[i][j].flags&PV_VAL_NULL) return 1; return 0; error: return -1; }
int sqlops_get_value(str *sres, int i, int j, sql_val_t **val) { sql_result_t *res = NULL; res = sql_get_result(sres); if(res==NULL) { LM_ERR("invalid result [%.*s]\n", sres->len, sres->s); goto error; } if(i>=res->nrows) { LM_ERR("row index out of bounds [%d/%d]\n", i, res->nrows); goto error; } if(j>=res->ncols) { LM_ERR("column index out of bounds [%d/%d]\n", j, res->ncols); goto error; } *val = &res->vals[i][j]; return 0; error: return -1; }
int pv_get_dbr(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { sql_pv_t *spv; int row; int col; spv = (sql_pv_t*)param->pvn.u.dname; if(spv->res==NULL) { spv->res = sql_get_result(&spv->resname); if(spv->res==NULL) return pv_get_null(msg, param, res); } switch(spv->type) { case 1: return pv_get_sintval(msg, param, res, spv->res->nrows); break; case 2: return pv_get_sintval(msg, param, res, spv->res->ncols); break; case 3: if(fixup_get_ivalue(msg, &spv->row, &row)!=0) return pv_get_null(msg, param, res); if(fixup_get_ivalue(msg, &spv->col, &col)!=0) return pv_get_null(msg, param, res); if(row>=spv->res->nrows) return pv_get_null(msg, param, res); if(col>=spv->res->ncols) return pv_get_null(msg, param, res); if(spv->res->vals[row][col].flags&PV_VAL_NULL) return pv_get_null(msg, param, res); if(spv->res->vals[row][col].flags&PV_VAL_INT) return pv_get_sintval(msg, param, res, spv->res->vals[row][col].value.n); return pv_get_strval(msg, param, res, &spv->res->vals[row][col].value.s); break; case 4: if(fixup_get_ivalue(msg, &spv->col, &col)!=0) return pv_get_null(msg, param, res); if(col>=spv->res->ncols) return pv_get_null(msg, param, res); return pv_get_strval(msg, param, res, &spv->res->cols[col].name); break; } return 0; }
void sqlops_reset_result(str *sres) { sql_result_t *res = NULL; res = sql_get_result(sres); if(res==NULL) { LM_ERR("invalid result [%.*s]\n", sres->len, sres->s); return; } sql_reset_result(res); return; }
int sqlops_num_rows(str *sres) { sql_result_t *res = NULL; res = sql_get_result(sres); if(res==NULL) { LM_ERR("invalid result [%.*s]\n", sres->len, sres->s); goto error; } return res->nrows; error: return -1; }
static int fixup_sql_rfree(void** param, int param_no) { sql_result_t *res = NULL; str s; s.s = (char*)(*param); s.len = strlen(s.s); if (param_no==1) { res = sql_get_result(&s); if(res==NULL) { LM_ERR("invalid result [%s]\n", s.s); return E_UNSPEC; } *param = (void*)res; } return 0; }
int sqlops_get_column(str *sres, int i, str *col) { sql_result_t *res = NULL; res = sql_get_result(sres); if(res==NULL) { LM_ERR("invalid result [%.*s]\n", sres->len, sres->s); goto error; } if(i>=res->ncols) { LM_ERR("column index out of bounds [%d/%d]\n", i, res->ncols); goto error; } *col = res->cols[i].name; return 0; error: return -1; }
int sqlops_do_query(str *scon, str *squery, str *sres) { sql_con_t *con = NULL; sql_result_t *res = NULL; con = sql_get_connection(scon); if(con==NULL) { LM_ERR("invalid connection [%.*s]\n", scon->len, scon->s); goto error; } res = sql_get_result(sres); if(res==NULL) { LM_ERR("invalid result [%.*s]\n", sres->len, sres->s); goto error; } if(sql_do_query(con, squery, res)<0) goto error; return 0; error: return -1; }
static int fixup_sql_query(void** param, int param_no) { sql_con_t *con = NULL; pv_elem_t *query = NULL; sql_result_t *res = NULL; str s; s.s = (char*)(*param); s.len = strlen(s.s); if (param_no==1) { con = sql_get_connection(&s); if(con==NULL) { LM_ERR("invalid connection [%s]\n", s.s); return E_UNSPEC; } *param = (void*)con; } else if (param_no==2) { if(pv_parse_format(&s, &query)<0) { LM_ERR("invalid query string [%s]\n", s.s); return E_UNSPEC; } *param = (void*)query; } else if (param_no==3) { res = sql_get_result(&s); if(res==NULL) { LM_ERR("invalid result [%s]\n", s.s); return E_UNSPEC; } *param = (void*)res; } return 0; }
/** * parse sqlres module parameter */ int sql_res_param(modparam_t type, void *val) { sql_result_t *res = NULL; str s; if(val==NULL) { LM_ERR("invalid parameter\n"); goto error; } s.s = (char*)val; s.len = strlen(s.s); res = sql_get_result(&s); if(res==NULL) { LM_ERR("invalid result [%s]\n", s.s); goto error; } return 0; error: return -1; }
int pv_parse_dbr_name(pv_spec_p sp, str *in) { sql_pv_t *spv=NULL; char *p; str pvs; str tok; spv = (sql_pv_t*)pkg_malloc(sizeof(sql_pv_t)); if(spv==NULL) return -1; memset(spv, 0, sizeof(sql_pv_t)); p = in->s; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0') goto error; spv->resname.s = p; while(p < in->s + in->len) { if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r') break; p++; } if(p>in->s+in->len || *p=='\0') goto error; spv->resname.len = p - spv->resname.s; spv->res = sql_get_result(&spv->resname); if(*p!='=') { while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0' || *p!='=') goto error; } p++; if(*p!='>') goto error; p++; pvs.len = in->len - (int)(p - in->s); pvs.s = p; p = pvs.s+pvs.len-1; while(p>pvs.s && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p--; if(p==pvs.s) { LM_ERR("invalid key in [%.*s]\n", in->len, in->s); goto error; } pvs.len = p - pvs.s + 1; LM_DBG("res [%.*s] - key [%.*s]\n", spv->resname.len, spv->resname.s, pvs.len, pvs.s); if(pvs.len==4 && strncmp(pvs.s, "rows", 4)==0) { spv->type = 1; } else if(pvs.len==4 && strncmp(pvs.s, "cols", 4)==0) { spv->type = 2; } else if(pvs.s[0]=='[') { spv->type = 3; p = pvs.s+1; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0') goto error_index; tok.s = p; while(p < in->s + in->len) { if(*p==',' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r') break; p++; } if(p>in->s+in->len || *p=='\0') goto error_index; tok.len = p - tok.s; if(sql_parse_index(&tok, &spv->row)!=0) goto error_index; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0' || *p!=',') goto error_index; p++; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0') goto error_index; tok.s = p; while(p < in->s + in->len) { if(*p==']' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r') break; p++; } if(p>in->s+in->len || *p=='\0') goto error_index; tok.len = p - tok.s; if(sql_parse_index(&tok, &spv->col)!=0) goto error_index; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0' || *p!=']') goto error_index; } else if(pvs.len>9 && strncmp(pvs.s, "colname", 7)==0) { spv->type = 4; p = pvs.s+7; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0' || *p!='[') goto error_index; p++; tok.s = p; while(p < in->s + in->len) { if(*p==']' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r') break; p++; } if(p>in->s+in->len || *p=='\0') goto error_index; tok.len = p - tok.s; if(sql_parse_index(&tok, &spv->col)!=0) goto error_index; while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r')) p++; if(p>in->s+in->len || *p=='\0' || *p!=']') goto error_index; } else { LM_ERR("unknow key [%.*s]\n", pvs.len, pvs.s); return -1; } sp->pvp.pvn.u.dname = (void*)spv; sp->pvp.pvn.type = PV_NAME_PVAR; return 0; error: LM_ERR("invalid pv name [%.*s]\n", in->len, in->s); if(spv!=NULL) pkg_free(spv); return -1; error_index: LM_ERR("invalid index in [%.*s]\n", pvs.len, pvs.s); if(spv!=NULL) pkg_free(spv); return -1; }