Skip to content

Commit

Permalink
db_text: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed May 18, 2023
1 parent 3d7a376 commit 2a66061
Show file tree
Hide file tree
Showing 17 changed files with 1,302 additions and 1,486 deletions.
226 changes: 110 additions & 116 deletions src/modules/db_text/db_text.c
Expand Up @@ -43,11 +43,11 @@ static void destroy(void);
/*
* Module parameter variables
*/
int db_mode = 0; /* Database usage mode: 0 = cache, 1 = no cache */
int empty_string = 0; /* Treat empty string as "" = 0, 1 = NULL */
int db_mode = 0; /* Database usage mode: 0 = cache, 1 = no cache */
int empty_string = 0; /* Treat empty string as "" = 0, 1 = NULL */
int _db_text_read_buffer_size = DEFAULT_DB_TEXT_READ_BUFFER_SIZE;
int _db_text_max_result_rows = DEFAULT_MAX_RESULT_ROWS;
int _dbt_delim = ':'; /* ':' is the default delim */
int _dbt_delim = ':'; /* ':' is the default delim */
str _dbt_delim_str = str_init(":"); /* ':' is the default delim */
str dbt_default_connection = str_init("");

Expand All @@ -57,54 +57,50 @@ int dbt_bind_api(db_func_t *dbb);
* Exported functions
*/
static cmd_export_t cmds[] = {
{"db_bind_api", (cmd_function)dbt_bind_api, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
{"db_bind_api", (cmd_function)dbt_bind_api, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}};


/*
* Exported parameters
*/
static param_export_t params[] = {
{"db_mode", INT_PARAM, &db_mode},
{"emptystring", INT_PARAM, &empty_string},
{"file_buffer_size", INT_PARAM, &_db_text_read_buffer_size},
{"max_result_rows", INT_PARAM, &_db_text_max_result_rows},
{"default_connection", PARAM_STR, &dbt_default_connection},
{"db_delim", PARAM_STR, &_dbt_delim_str},
{0, 0, 0}
};
static param_export_t params[] = {{"db_mode", INT_PARAM, &db_mode},
{"emptystring", INT_PARAM, &empty_string},
{"file_buffer_size", INT_PARAM, &_db_text_read_buffer_size},
{"max_result_rows", INT_PARAM, &_db_text_max_result_rows},
{"default_connection", PARAM_STR, &dbt_default_connection},
{"db_delim", PARAM_STR, &_dbt_delim_str}, {0, 0, 0}};

static rpc_export_t rpc_methods[];

struct module_exports exports = {
"db_text", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* cmd (cfg function) exports */
params, /* param exports */
0, /* RPC method exports */
0, /* pseudo-variables exports */
0, /* response handling function */
mod_init, /* module init function */
0, /* per-child init function */
destroy /* module destroy function */
"db_text", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* cmd (cfg function) exports */
params, /* param exports */
0, /* RPC method exports */
0, /* pseudo-variables exports */
0, /* response handling function */
mod_init, /* module init function */
0, /* per-child init function */
destroy /* module destroy function */
};

int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
if (rpc_register_array(rpc_methods)!=0) {
if(rpc_register_array(rpc_methods) != 0) {
LM_ERR("failed to register RPC commands\n");
return -1;
}

if(db_api_init()<0)
if(db_api_init() < 0)
return -1;
return 0;
}

static int mod_init(void)
{
if (_dbt_delim_str.len != 1) {
if(_dbt_delim_str.len != 1) {
LM_ERR("db_delim must be a character, defaulting to \":\"\n");
pkg_free(_dbt_delim_str.s);
_dbt_delim_str.s = ":";
Expand All @@ -127,122 +123,120 @@ static void destroy(void)
}



int dbt_bind_api(db_func_t *dbb)
{
if(dbb==NULL)
if(dbb == NULL)
return -1;

memset(dbb, 0, sizeof(db_func_t));

dbb->use_table = dbt_use_table;
dbb->init = dbt_init;
dbb->close = dbt_close;
dbb->query = (db_query_f)dbt_query;
dbb->fetch_result = (db_fetch_result_f) dbt_fetch_result;
dbb->use_table = dbt_use_table;
dbb->init = dbt_init;
dbb->close = dbt_close;
dbb->query = (db_query_f)dbt_query;
dbb->fetch_result = (db_fetch_result_f)dbt_fetch_result;
dbb->free_result = dbt_free_result;
dbb->insert = (db_insert_f)dbt_insert;
dbb->delete = (db_delete_f)dbt_delete;
dbb->update = (db_update_f)dbt_update;
dbb->replace = (db_replace_f)dbt_replace;
dbb->affected_rows = (db_affected_rows_f) dbt_affected_rows;
dbb->raw_query = (db_raw_query_f) dbt_raw_query;
dbb->cap = DB_CAP_ALL | DB_CAP_AFFECTED_ROWS | DB_CAP_RAW_QUERY | DB_CAP_REPLACE | DB_CAP_FETCH;
dbb->insert = (db_insert_f)dbt_insert;
dbb->delete = (db_delete_f)dbt_delete;
dbb->update = (db_update_f)dbt_update;
dbb->replace = (db_replace_f)dbt_replace;
dbb->affected_rows = (db_affected_rows_f)dbt_affected_rows;
dbb->raw_query = (db_raw_query_f)dbt_raw_query;
dbb->cap = DB_CAP_ALL | DB_CAP_AFFECTED_ROWS | DB_CAP_RAW_QUERY
| DB_CAP_REPLACE | DB_CAP_FETCH;

return 0;
}

/* rpc function documentation */
static const char *rpc_dump_doc[2] = {
"Write back to disk modified tables", 0
};
static const char *rpc_dump_doc[2] = {"Write back to disk modified tables", 0};

/* rpc function implementations */
static void rpc_dump(rpc_t *rpc, void *c) {
if (0!=dbt_cache_print(0))
static void rpc_dump(rpc_t *rpc, void *c)
{
if(0 != dbt_cache_print(0))
rpc->rpl_printf(c, "Dump failed");
else
rpc->rpl_printf(c, "Dump OK");

return;
}

static const char *rpc_query_doc[2] = {
"Perform Live Query", 0
};
static const char *rpc_query_doc[2] = {"Perform Live Query", 0};

/* rpc function implementations */
static void rpc_query(rpc_t *rpc, void *ctx) {
static void rpc_query(rpc_t *rpc, void *ctx)
{
#ifdef __OS_linux
str sql;
db1_con_t *con;
db1_res_t* _r;
int res;
int n;
char *buf;
size_t len;
FILE *stream;
dbt_table_p tab;
dbt_row_p rowp;

rpc->scan(ctx, "S", &sql);

con = dbt_init(&dbt_default_connection);
if(con == NULL) {
rpc->rpl_printf(ctx, "invalid connection : %s", dbt_default_connection.s);
return;
}

res = dbt_raw_query(con, &sql, &_r);
if(res != 0) {
rpc->rpl_printf(ctx, "error executing sql statement");
goto end;
}


if(_r) {
tab = (dbt_table_p)_r->ptr;
if(_r->n == 0) {
rpc->rpl_printf(ctx, "statement returned 0 rows");
} else {
stream = open_memstream (&buf, &len);
if (stream == NULL) {
rpc->rpl_printf(ctx, "error opening stream");
goto end;
}
dbt_print_table_header(tab, stream);
fflush (stream);
buf[len] = '\0';
rpc->rpl_printf(ctx, "%s", buf);
rowp = tab->rows;
for(n=0; n < _r->n; n++) {
fseeko (stream, 0, SEEK_SET);
dbt_print_table_row_ex(tab, rowp, stream, 0);
fflush (stream);
buf[len] = '\0';
rpc->rpl_printf(ctx, "%s", buf);
rowp = rowp->next;
}
fclose (stream);
free (buf);
rpc->rpl_printf(ctx, "\ntotal rows %d / %d", _r->n, tab->nrrows);
}
} else {
rpc->rpl_printf(ctx, "%d affected rows", ((dbt_con_p)con->tail)->affected);
}

if(_r)
dbt_free_result(con, _r);
str sql;
db1_con_t *con;
db1_res_t *_r;
int res;
int n;
char *buf;
size_t len;
FILE *stream;
dbt_table_p tab;
dbt_row_p rowp;

rpc->scan(ctx, "S", &sql);

con = dbt_init(&dbt_default_connection);
if(con == NULL) {
rpc->rpl_printf(
ctx, "invalid connection : %s", dbt_default_connection.s);
return;
}

res = dbt_raw_query(con, &sql, &_r);
if(res != 0) {
rpc->rpl_printf(ctx, "error executing sql statement");
goto end;
}


if(_r) {
tab = (dbt_table_p)_r->ptr;
if(_r->n == 0) {
rpc->rpl_printf(ctx, "statement returned 0 rows");
} else {
stream = open_memstream(&buf, &len);
if(stream == NULL) {
rpc->rpl_printf(ctx, "error opening stream");
goto end;
}
dbt_print_table_header(tab, stream);
fflush(stream);
buf[len] = '\0';
rpc->rpl_printf(ctx, "%s", buf);
rowp = tab->rows;
for(n = 0; n < _r->n; n++) {
fseeko(stream, 0, SEEK_SET);
dbt_print_table_row_ex(tab, rowp, stream, 0);
fflush(stream);
buf[len] = '\0';
rpc->rpl_printf(ctx, "%s", buf);
rowp = rowp->next;
}
fclose(stream);
free(buf);
rpc->rpl_printf(ctx, "\ntotal rows %d / %d", _r->n, tab->nrrows);
}
} else {
rpc->rpl_printf(
ctx, "%d affected rows", ((dbt_con_p)con->tail)->affected);
}

if(_r)
dbt_free_result(con, _r);

end:
dbt_close(con);
dbt_close(con);
#else
rpc->fault(ctx, 500, "Command available on Linux only");
rpc->fault(ctx, 500, "Command available on Linux only");
#endif
}

static rpc_export_t rpc_methods[] = {
{"db_text.dump", rpc_dump, rpc_dump_doc, 0},
{"db_text.query", rpc_query, rpc_query_doc, 0},
{0, 0, 0, 0}
};
{"db_text.dump", rpc_dump, rpc_dump_doc, 0},
{"db_text.query", rpc_query, rpc_query_doc, 0}, {0, 0, 0, 0}};
29 changes: 14 additions & 15 deletions src/modules/db_text/db_text.h
Expand Up @@ -35,66 +35,65 @@
/*
* Initialize database connection
*/
db1_con_t* dbt_init(const str* _sqlurl);
db1_con_t *dbt_init(const str *_sqlurl);


/*
* Close a database connection
*/
void dbt_close(db1_con_t* _h);
void dbt_close(db1_con_t *_h);


/*
* Free all memory allocated by get_result
*/
int dbt_free_result(db1_con_t* _h, db1_res_t* _r);
int dbt_free_result(db1_con_t *_h, db1_res_t *_r);


/*
* Do a query
*/
int dbt_query(db1_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v,
db_key_t* _c, int _n, int _nc, db_key_t _o, db1_res_t** _r);
int dbt_query(db1_con_t *_h, db_key_t *_k, db_op_t *_op, db_val_t *_v,
db_key_t *_c, int _n, int _nc, db_key_t _o, db1_res_t **_r);

/*
* fetch result
*/
int dbt_fetch_result(db1_con_t* _h, db1_res_t** _r, const int nrows);
int dbt_fetch_result(db1_con_t *_h, db1_res_t **_r, const int nrows);

/*
* Raw SQL query
*/
int dbt_raw_query(db1_con_t* _h, str* _s, db1_res_t** _r);
int dbt_raw_query(db1_con_t *_h, str *_s, db1_res_t **_r);


/*
* Insert a row into table
*/
int dbt_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n);
int dbt_insert(db1_con_t *_h, db_key_t *_k, db_val_t *_v, int _n);


/*
* Delete a row from table
*/
int dbt_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n);
int dbt_delete(db1_con_t *_h, db_key_t *_k, db_op_t *_o, db_val_t *_v, int _n);


/*
* Update a row in table
*/
int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
db_key_t* _uk, db_val_t* _uv, int _n, int _un);
int dbt_update(db1_con_t *_h, db_key_t *_k, db_op_t *_o, db_val_t *_v,
db_key_t *_uk, db_val_t *_uv, int _n, int _un);

/*
* replace a row in table
*/
int dbt_replace(db1_con_t* _h, db_key_t* _k, db_val_t* _v,
int _n, int _nk, int _m);
int dbt_replace(
db1_con_t *_h, db_key_t *_k, db_val_t *_v, int _n, int _nk, int _m);

/*
* Affected rows
*/
int dbt_affected_rows(db1_con_t* _h);
int dbt_affected_rows(db1_con_t *_h);

#endif

0 comments on commit 2a66061

Please sign in to comment.