Skip to content

Commit

Permalink
db_text: port MI commands to jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu committed Jan 10, 2019
1 parent 7bcf10b commit 3b67f80
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions modules/db_text/dbtext.c
Expand Up @@ -41,8 +41,14 @@
static int mod_init(void);
static void destroy(void);

static struct mi_root* mi_dbt_dump(struct mi_root* cmd, void* param);
static struct mi_root* mi_dbt_reload(struct mi_root* cmd, void* param);
static mi_response_t *mi_dbt_dump(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *mi_dbt_reload(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *mi_dbt_reload_1(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *mi_dbt_reload_2(const mi_params_t *params,
struct mi_handler *async_hdl);

/*
* Module parameter variables
Expand Down Expand Up @@ -71,9 +77,17 @@ static param_export_t params[] = {

/** MI commands */
static mi_export_t mi_cmds[] = {
{"dbt_dump", 0, mi_dbt_dump, 0, 0, 0},
{"dbt_reload", 0, mi_dbt_reload, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
{"dbt_dump", 0, 0, 0, {
{mi_dbt_dump, {0}},
{EMPTY_MI_RECIPE}}
},
{"dbt_reload", 0, 0, 0, {
{mi_dbt_reload, {0}},
{mi_dbt_reload_1, {"db_name", 0}},
{mi_dbt_reload_2, {"db_name", "table_name", 0}},
{EMPTY_MI_RECIPE}}
},
{EMPTY_MI_EXPORT}
};

struct module_exports exports = {
Expand Down Expand Up @@ -134,42 +148,63 @@ int dbt_bind_api(const str* mod, db_func_t *dbb)
return 0;
}

static struct mi_root* mi_dbt_dump(struct mi_root* cmd, void* param)
static mi_response_t *mi_dbt_dump(const mi_params_t *params,
struct mi_handler *async_hdl)
{
struct mi_root *rpl_tree;

rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
if (rpl_tree==NULL) return NULL;
if (dbt_cache_print(0)!=0) {
free_mi_tree(rpl_tree);
if (dbt_cache_print(0)!=0)
return NULL;
}
return rpl_tree;

return init_mi_result_ok();
}

static struct mi_root* mi_dbt_reload(struct mi_root* cmd, void* param)
static mi_response_t *mi_dbt_reload(const mi_params_t *params,
struct mi_handler *async_hdl)
{
struct mi_node *node;
str *dbname, *name;
int res;

dbname = name = NULL;
if( (node = cmd->node.kids) ) {
dbname = &(node->value);
if( (res = dbt_cache_reload(NULL, NULL)) >= 0 ) {
return init_mi_result_ok();
} else if( res == -1 ) {
return init_mi_error(400, MI_SSTR("Bad parameter value"));
} else {
return init_mi_error(500, MI_SSTR("Internal error"));
}
}

if( (node = node->next) ) {
name = &(node->value);
static mi_response_t *mi_dbt_reload_1(const mi_params_t *params,
struct mi_handler *async_hdl)
{
str dbname;
int res;

if( node->next )
return init_mi_tree(400, MI_SSTR(MI_MISSING_PARM_S));
}
if (get_mi_string_param(params, "db_name", &dbname.s, &dbname.len) < 0)
return init_mi_param_error();

if( (res = dbt_cache_reload(&dbname, NULL)) >= 0 ) {
return init_mi_result_ok();
} else if( res == -1 ) {
return init_mi_error(400, MI_SSTR("Bad parameter value"));
} else {
return init_mi_error(500, MI_SSTR("Internal error"));
}
}

static mi_response_t *mi_dbt_reload_2(const mi_params_t *params,
struct mi_handler *async_hdl)
{
str dbname, name;
int res;

if (get_mi_string_param(params, "db_name", &dbname.s, &dbname.len) < 0)
return init_mi_param_error();
if (get_mi_string_param(params, "table_name", &name.s, &name.len) < 0)
return init_mi_param_error();

if( (res = dbt_cache_reload(dbname, name)) >= 0 ) {
return init_mi_tree(200, MI_SSTR(MI_OK_S));
if ( (res = dbt_cache_reload(&dbname, &name)) >= 0 ) {
return init_mi_result_ok();
} else if( res == -1 ) {
return init_mi_tree(400, MI_SSTR(MI_BAD_PARM_S));
return init_mi_error(400, MI_SSTR("Bad parameter value"));
} else {
return init_mi_tree(500, MI_SSTR(MI_INTERNAL_ERR_S));
return init_mi_error(500, MI_SSTR("Internal error"));
}
}

0 comments on commit 3b67f80

Please sign in to comment.