piler/src/mysql.c

244 lines
5.8 KiB
C
Raw Normal View History

2013-01-31 22:20:23 +01:00
/*
* mysql.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <piler.h>
int open_database(struct session_data *sdata, struct config *cfg){
2013-04-28 14:18:09 +02:00
int rc=1;
char buf[BUFLEN];
2013-04-28 14:18:09 +02:00
mysql_init(&(sdata->mysql));
mysql_options(&(sdata->mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg->mysql_connect_timeout);
mysql_options(&(sdata->mysql), MYSQL_OPT_RECONNECT, (const char*)&rc);
if(mysql_real_connect(&(sdata->mysql), cfg->mysqlhost, cfg->mysqluser, cfg->mysqlpwd, cfg->mysqldb, cfg->mysqlport, cfg->mysqlsocket, 0) == 0){
printf("cant connect to mysql server\n");
return ERR;
}
snprintf(buf, sizeof(buf)-2, "SET NAMES %s", cfg->mysqlcharset);
mysql_real_query(&(sdata->mysql), buf, strlen(buf));
snprintf(buf, sizeof(buf)-2, "SET CHARACTER SET %s", cfg->mysqlcharset);
mysql_real_query(&(sdata->mysql), buf, strlen(buf));
2013-04-28 14:18:09 +02:00
return OK;
}
void close_database(struct session_data *sdata){
mysql_close(&(sdata->mysql));
}
void p_bind_init(struct data *data){
int i;
data->pos = 0;
for(i=0; i<MAX_SQL_VARS; i++){
data->sql[i] = NULL;
data->type[i] = TYPE_UNDEF;
data->len[i] = 0;
}
}
2016-01-02 08:16:38 +01:00
void p_query(struct session_data *sdata, char *s){
mysql_real_query(&(sdata->mysql), s, strlen(s));
}
int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct data *data){
MYSQL_BIND bind[MAX_SQL_VARS];
unsigned long length[MAX_SQL_VARS];
int i, ret=ERR;
2014-07-10 11:54:37 +02:00
sdata->sql_errno = 0;
memset(bind, 0, sizeof(bind));
for(i=0; i<MAX_SQL_VARS; i++){
if(data->type[i] > TYPE_UNDEF){
switch(data->type[i]) {
2013-05-15 11:52:41 +02:00
case TYPE_SHORT:
bind[i].buffer_type = MYSQL_TYPE_SHORT;
bind[i].length = 0;
break;
case TYPE_LONG:
bind[i].buffer_type = MYSQL_TYPE_LONG;
bind[i].length = 0;
break;
case TYPE_LONGLONG:
bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
bind[i].length = 0;
break;
case TYPE_STRING:
bind[i].buffer_type = MYSQL_TYPE_STRING;
length[i] = strlen(data->sql[i]);
bind[i].length = &length[i];
break;
default:
bind[i].buffer_type = 0;
bind[i].length = 0;
break;
};
bind[i].buffer = data->sql[i];
bind[i].is_null = 0;
}
else { break; }
}
if(mysql_stmt_bind_param(stmt, bind)){
2014-07-10 11:54:37 +02:00
sdata->sql_errno = mysql_stmt_errno(stmt);
syslog(LOG_PRIORITY, "%s: mysql_stmt_bind_param() error: %s (errno: %d)", sdata->ttmpfile, mysql_stmt_error(stmt), sdata->sql_errno);
2015-12-18 22:38:52 +01:00
return ret;
}
if(mysql_stmt_execute(stmt)){
2014-07-10 11:54:37 +02:00
sdata->sql_errno = mysql_stmt_errno(stmt);
syslog(LOG_PRIORITY, "%s: mysql_stmt_execute error: *%s* (errno: %d)", sdata->ttmpfile, mysql_error(&(sdata->mysql)), sdata->sql_errno);
2015-12-18 22:38:52 +01:00
return ret;
}
ret = OK;
return ret;
}
int p_store_results(MYSQL_STMT *stmt, struct data *data){
MYSQL_BIND bind[MAX_SQL_VARS];
int i, ret=ERR;
memset(bind, 0, sizeof(bind));
for(i=0; i<MAX_SQL_VARS; i++){
if(data->type[i] > TYPE_UNDEF){
switch(data->type[i]) {
2013-05-15 11:52:41 +02:00
case TYPE_SHORT: bind[i].buffer_type = MYSQL_TYPE_SHORT;
break;
case TYPE_LONG: bind[i].buffer_type = MYSQL_TYPE_LONG;
break;
case TYPE_LONGLONG:
bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
break;
case TYPE_STRING:
bind[i].buffer_type = MYSQL_TYPE_STRING;
bind[i].buffer_length = data->len[i];
break;
default:
bind[i].buffer_type = 0;
break;
};
bind[i].buffer = (char *)data->sql[i];
bind[i].is_null = &(data->is_null[i]);
bind[i].length = &(data->length[i]);
bind[i].error = &(data->error[i]);
}
else { break; }
}
if(mysql_stmt_bind_result(stmt, bind)){
2015-12-18 22:38:52 +01:00
return ret;
}
if(mysql_stmt_store_result(stmt)){
2015-12-18 22:38:52 +01:00
return ret;
}
ret = OK;
return ret;
}
int p_fetch_results(MYSQL_STMT *stmt){
if(mysql_stmt_fetch(stmt) == 0) return OK;
return ERR;
}
void p_free_results(MYSQL_STMT *stmt){
mysql_stmt_free_result(stmt);
}
uint64 p_get_insert_id(MYSQL_STMT *stmt){
return mysql_stmt_insert_id(stmt);
}
int p_get_affected_rows(MYSQL_STMT *stmt){
return mysql_stmt_affected_rows(stmt);
}
2016-04-05 21:10:09 +02:00
int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s){
2013-01-31 22:20:23 +01:00
*stmt = mysql_stmt_init(&(sdata->mysql));
if(!*stmt){
syslog(LOG_PRIORITY, "%s: mysql_stmt_init() error", sdata->ttmpfile);
return ERR;
}
if(mysql_stmt_prepare(*stmt, s, strlen(s))){
syslog(LOG_PRIORITY, "%s: mysql_stmt_prepare() error: %s => sql: %s", sdata->ttmpfile, mysql_stmt_error(*stmt), s);
return ERR;
}
return OK;
}
void insert_offset(struct session_data *sdata, int server_id){
char s[SMALLBUFSIZE];
uint64 id = server_id * 1000000000000ULL;
snprintf(s, sizeof(s)-1, "INSERT INTO %s (`id`) VALUES (%llu)", SQL_METADATA_TABLE, id);
mysql_real_query(&(sdata->mysql), s, strlen(s));
}
void close_prepared_statement(MYSQL_STMT *stmt){
if(stmt) mysql_stmt_close(stmt);
}