static int exec_transl(str *group_name, db_cmd_t **cmd, db_res_t **res) { db_fld_t cols[5]; db_fld_t params[2]; memset(cols, 0, sizeof(cols)); cols[0].name = transl_cfg_table_fld; cols[0].type = DB_CSTR; cols[1].name = transl_cfg_table_group_name_field_fld; cols[1].type = DB_CSTR; cols[2].name = transl_cfg_table_name_field_fld; cols[2].type = DB_CSTR; cols[3].name = transl_cfg_table_value_field_fld; cols[3].type = DB_CSTR; memset(params, 0, sizeof(params)); params[0].name = transl_group_name_fld; params[0].type = DB_STR; params[0].op = DB_EQ; DBG(MODULE_NAME": exec_transl('%.*s', ...)\n", group_name->len, group_name->s); *cmd = db_cmd(DB_GET, db_cntx, transl_tbl, cols, params, NULL); if (!*cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", transl_tbl); return -1; } (*cmd)->match[0].flags &= ~DB_NULL; (*cmd)->match[0].v.lstr = *group_name; // FIXME: proprietary code! db_setopt(*cmd, "key", "pKey"); db_setopt(*cmd, "key_omit", 1); if (db_exec(res, *cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", transl_tbl); db_cmd_free(*cmd); return -1; } return 0; }
static int init_db(struct dbops_action* p) { db_fld_t* matches = NULL, *result = NULL, *values = NULL; int type, i; DEBUG(MODULE_NAME": init_db: query: %s(%d)\n", p->query_name, p->query_no); if (p->db_url == NULL) { ERR(MODULE_NAME": No database URL specified\n"); return -1; } p->ctx = db_ctx(MODULE_NAME); if (p->ctx == NULL) { ERR(MODULE_NAME": Error while initializing database layer\n"); return -1; } if (db_add_db(p->ctx, p->db_url) < 0) return -1; if (db_connect(p->ctx) < 0) return -1; if (p->is_raw_query) { type = DB_SQL; if (build_params(&matches, p) < 0) return -1; } else { switch(p->operation) { case INSERT_OPS: case REPLACE_OPS: type = DB_PUT; if (build_params(&values, p) < 0) return -1; break; case UPDATE_OPS: type = DB_UPD; if (build_match(&matches, p) < 0) return -1; if (build_params(&values, p) < 0) { if (matches) pkg_free(matches); return -1; } break; case DELETE_OPS: type = DB_DEL; if (build_match(&matches, p) < 0) return -1; break; case OPEN_QUERY_OPS: type = DB_GET; if (build_match(&matches, p) < 0) return -1; if (build_result(&result, p) < 0) { if (matches) pkg_free(matches); return -1; } break; default: BUG("Unknown operation %d\n", p->operation); return -1; } } p->cmd = db_cmd(type, p->ctx, p->table.s, result, matches, values); if (p->cmd == NULL) { ERR(MODULE_NAME": init_db: query: %s(%d), error while compiling database query\n", p->query_name, p->query_no); if (values) pkg_free(values); if (matches) pkg_free(matches); if (result) pkg_free(result); db_disconnect(p->ctx); db_ctx_free(p->ctx); return -1; } if (values) pkg_free(values); if (matches) pkg_free(matches); if (result) pkg_free(result); for (i=0; i<p->extra_ops_count; i++) { char *end; DEBUG(MODULE_NAME": init_db: query_no: %s(%d), setopt('%s', %i, '%s'\n", p->query_name, p->query_no, p->extra_ops[i].name, p->extra_ops[i].type, p->extra_ops[i].value); switch (p->extra_ops[i].type) { case DB_NONE: /* set null ?? */ break; case DB_DATETIME: { time_t v; v = strtol(p->extra_ops[i].value, &end, 10); if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1; break; } case DB_INT: { int v; v = strtol(p->extra_ops[i].value, &end, 10); if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1; break; } case DB_FLOAT: { float v; #ifdef __USE_ISOC99 v = strtof(p->extra_ops[i].value, &end); #else v = strtod(p->extra_ops[i].value, &end); #endif if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1; break; } case DB_DOUBLE: { double v; v = strtod(p->extra_ops[i].value, &end); if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1; break; } case DB_CSTR: if (db_setopt(p->cmd, p->extra_ops[i].name, p->extra_ops[i].value) < 0) return -1; break; default: BUG("Unknown extra_op type: %d\n", p->extra_ops[i].type); return -1; } } return 0; }
/* translate name using translation table, returns 0..not found, 1..success, -1..error */ static int find_cfg_var(str *group_name, char *def_name, db_res_t *transl_res) { db_rec_t *transl_rec; int ret = -1; DBG(MODULE_NAME": find_cfg_var('%.*s', '%s', ...)\n", group_name->len, group_name->s, def_name); transl_rec = db_first(transl_res); /* iterate through each candidate where cfg def may be found */ while (transl_rec) { static db_cmd_t* cmd; db_rec_t *rec; db_res_t *res; db_fld_t params[3], cols[2]; memset(cols, 0, sizeof(cols)); cols[0].name = GETCSTR(transl_rec->fld[3], def_cfg_table_value_field); cols[0].type = DB_NONE; memset(params, 0, sizeof(params)); params[0].name = GETCSTR(transl_rec->fld[1], def_cfg_table_group_name_field); params[0].type = DB_STR; params[0].op = DB_EQ; params[1].name = GETCSTR(transl_rec->fld[2], def_cfg_table_name_field); params[1].type = DB_CSTR; params[1].op = DB_EQ; DBG(MODULE_NAME": exec_transl: looking in '%s'\n", GETCSTR(transl_rec->fld[0], def_cfg_table)); cmd = db_cmd(DB_GET, db_cntx, GETCSTR(transl_rec->fld[0], def_cfg_table), cols, params, NULL); if (!cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", transl_tbl); return -1; } cmd->match[0].flags &= ~DB_NULL; cmd->match[0].v.lstr = *group_name; cmd->match[1].flags &= ~DB_NULL; cmd->match[1].v.cstr = def_name; // FIXME: proprietary code! db_setopt(cmd, "key", "bySerGroup"); db_setopt(cmd, "key_omit", 0); if (db_exec(&res, cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", transl_tbl); db_cmd_free(cmd); return -1; } rec = db_first(res); if (rec) { /* var found in config table */ str def_name_s; def_name_s.s = def_name; def_name_s.len = strlen(def_name); DBG(MODULE_NAME": exec_transl: found record, type:%d\n", rec->fld[0].type); /* read and set cfg var */ switch (rec->fld[0].type) { case DB_STR: if (cfg_set_now(cfg_ctx, group_name, NULL /* group id */, &def_name_s, &rec->fld[0].v.lstr, CFG_VAR_STR) < 0) goto err; break; case DB_CSTR: if (cfg_set_now_string(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.cstr) < 0) goto err; break; case DB_INT: if (cfg_set_now_int(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.int4) < 0) goto err; break; default: ERR(MODULE_NAME": unexpected field type (%d), table:'%s', field:'%s'\n", rec->fld[0].type, GETCSTR(transl_rec->fld[0], def_cfg_table), GETCSTR(transl_rec->fld[3], def_cfg_table_value_field) ); goto err; } ret = 1; err: db_res_free(res); db_cmd_free(cmd); return ret; } db_res_free(res); db_cmd_free(cmd); transl_rec = db_next(transl_res); } return 0; }