Skip to content

Commit

Permalink
mx_mysql: Add mx_mysql_do_statement()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed May 27, 2015
1 parent 425f813 commit 123f69c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
43 changes: 43 additions & 0 deletions mx_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,49 @@ int mx_mysql_bind_init(struct mx_mysql_bind *bind, unsigned long count, enum mx_
return 0;
}

int mx_mysql_do_statement(struct mx_mysql *mysql, char *query, struct mx_mysql_bind *param, struct mx_mysql_bind *result, void *from, void **to, size_t size)
{
struct mx_mysql_stmt *stmt = NULL;
unsigned long long num_rows = 0;
int res;
int cnt = 0;
char *tmpdata;

assert(mysql);

stmt = mx_mysql_statement_prepare_with_bindings(mysql, query, param, result);
if (!stmt) {
mx_log_err("mx_mysql_statement_prepare(): %m");
return -errno;
}

res = mx_mysql_statement_execute(stmt, &num_rows);
if (res < 0) {
mx_log_err("mx_mysql_statement_execute(): %m");
mx_mysql_statement_close(&stmt);
return res;
}

if (result && result->count && num_rows) {
tmpdata = mx_calloc_forever(num_rows, size);

for (cnt = 0; cnt < num_rows; cnt++) {
res = mx_mysql_statement_fetch(stmt);
if (res < 0) {
mx_log_err("mx_mysql_statement_fetch(): %m");
mx_free_null(tmpdata);
mx_mysql_statement_close(&stmt);
return res;
}
memcpy(tmpdata+(cnt*size), from, size);
}
*to = tmpdata;
}
mx_mysql_statement_close(&stmt);

return cnt;
}

struct mx_mysql_stmt *mx_mysql_statement_prepare_with_bindings(struct mx_mysql *mysql, char *statement, struct mx_mysql_bind *param, struct mx_mysql_bind *result)
{
int res;
Expand Down
4 changes: 4 additions & 0 deletions mx_mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ int mx_mysql_end(void);

int mx_mysql_finish(struct mx_mysql **mysql);

#define mx_mysql_do_statement_noresult(m, q, p) \
mx_mysql_do_statement(m, q, p, NULL, NULL, NULL, 0)
int mx_mysql_do_statement(struct mx_mysql *mysql, char *query, struct mx_mysql_bind *param, struct mx_mysql_bind *result, void *from, void **to, size_t size);

int mx_mysql_statement_init(struct mx_mysql *mysql, struct mx_mysql_stmt **stmt);
struct mx_mysql_stmt *mx_mysql_statement_prepare(struct mx_mysql *mysql, char *statement);
struct mx_mysql_stmt *mx_mysql_statement_prepare_with_bindings(struct mx_mysql *mysql, char *statement, struct mx_mysql_bind *param, struct mx_mysql_bind *result);
Expand Down

0 comments on commit 123f69c

Please sign in to comment.