X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FmysqlConn.c;h=45ab1931dee8766b7e13c9a4a15da6cfa41fd3fc;hp=ea3c2b89ab4d1042463438d4ce62d5ed97c2dd54;hb=f6bdc9d0fc8db22c265918f3325e11177fd001b9;hpb=55831bf424312a6908ca07a904f288fba0919a9a diff --git a/src/mysqlConn.c b/src/mysqlConn.c index ea3c2b8..45ab193 100644 --- a/src/mysqlConn.c +++ b/src/mysqlConn.c @@ -1,4 +1,4 @@ -/* mysqlConn.c - NeonServ v5.3 +/* mysqlConn.c - NeonServ v5.6 * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -16,11 +16,16 @@ */ #include "mysqlConn.h" -#define DATABASE_VERSION "14" +#include "ConfigParser.h" +#include "tools.h" +#include "log.h" +#define DATABASE_VERSION "20" + +static void show_mysql_error(); struct mysql_conn_struct { - unsigned long tid; - MYSQL *mysql_conn = NULL; + unsigned int tid; + MYSQL *mysql_conn; struct used_result *used_results; struct escaped_string *escaped_strings; struct mysql_conn_struct *next; @@ -46,7 +51,7 @@ static char *mysql_host, *mysql_user, *mysql_pass, *mysql_base; static pthread_mutex_t synchronized; #endif -void check_mysql() { +static void check_mysql() { MYSQL *mysql_conn = get_mysql_conn(); int errid; if((errid = mysql_ping(mysql_conn))) { @@ -94,14 +99,39 @@ void mysql_free() { mysql_conn->escaped_strings = NULL; } -void init_mysql(char *host, int port, char *user, char *pass, char *base) { - THREAD_MUTEX_INIT(synchronized); - mysql_host = strdup(host); - mysql_serverport = port; - mysql_user = strdup(user); - mysql_pass = strdup(pass); - mysql_base = strdup(base); +int reload_mysql() { + char *new_mysql_host = get_string_field("MySQL.host"); + char *new_mysql_user = get_string_field("MySQL.user"); + char *new_mysql_pass = get_string_field("MySQL.pass"); + char *new_mysql_base = get_string_field("MySQL.base"); + if(!(new_mysql_host && new_mysql_user && new_mysql_pass && new_mysql_base)) + return 0; + + //replace login data + if(mysql_host) + free(mysql_host); + mysql_host = strdup(new_mysql_host); + + if(mysql_user) + free(mysql_user); + mysql_user = strdup(new_mysql_user); + if(mysql_pass) + free(mysql_pass); + mysql_pass = strdup(new_mysql_pass); + + if(mysql_base) + free(mysql_base); + mysql_base = strdup(new_mysql_base); + + mysql_serverport = get_int_field("MySQL.port"); + if(!mysql_serverport) + mysql_serverport = 3306; + return 1; +} + +void init_mysql() { + THREAD_MUTEX_INIT(synchronized); MYSQL *mysql_conn = get_mysql_conn(); @@ -114,7 +144,6 @@ void init_mysql(char *host, int port, char *user, char *pass, char *base) { version = atoi(row[0]); } } - printf("%d \n", version); if(!version) { //CREATE DATABASE FILE *f = fopen("database.sql", "r"); @@ -132,7 +161,8 @@ void init_mysql(char *host, int port, char *user, char *pass, char *base) { } } fclose(f); - } + } else + printf_log("main", LOG_ERROR | LOG_MYSQL, "File not found: database.sql"); f = fopen("database.defaults.sql", "r"); if (f) { char line[4096]; @@ -147,7 +177,8 @@ void init_mysql(char *host, int port, char *user, char *pass, char *base) { } } fclose(f); - } + } else + printf_log("main", LOG_ERROR | LOG_MYSQL, "File not found: database.defaults.sql"); do { MYSQL_RES *res = mysql_store_result(mysql_conn); mysql_free_result(res); @@ -181,7 +212,7 @@ void init_mysql(char *host, int port, char *user, char *pass, char *base) { } fclose(f); } else - perror("database.sql missing!"); + printf_log("main", LOG_ERROR | LOG_MYSQL, "File not found: database.upgrade.sql"); do { MYSQL_RES *res = mysql_store_result(mysql_conn); mysql_free_result(res); @@ -201,10 +232,10 @@ void free_mysql() { mysql_conns = NULL; } -void show_mysql_error() { +static void show_mysql_error() { MYSQL *mysql_conn = get_mysql_conn(); //show mysql_error() - printf("MySQL Error: %s\n", mysql_error(mysql_conn)); + printf_log("main", LOG_ERROR | LOG_MYSQL, "Error: %s\n", mysql_error(mysql_conn)); } void printf_mysql_query(const char *text, ...) { @@ -218,7 +249,7 @@ void printf_mysql_query(const char *text, ...) { va_end(arg_list); if (pos < 0 || pos > (MYSQLMAXLEN - 2)) pos = MYSQLMAXLEN - 2; queryBuf[pos] = '\0'; - printf("MySQL: %s\n", queryBuf); + printf_log("main", LOG_MYSQL, "%s\n", queryBuf); if(mysql_query(mysql_conn, queryBuf)) { check_mysql(); if(mysql_query(mysql_conn, queryBuf)) { @@ -238,7 +269,7 @@ void printf_long_mysql_query(int len, const char *text, ...) { va_end(arg_list); if (pos < 0 || pos > (len - 2)) pos = len - 2; queryBuf[pos] = '\0'; - printf("MySQL: %s\n", queryBuf); + printf_log("main", LOG_MYSQL, "%s\n", queryBuf); if(mysql_query(mysql_conn, queryBuf)) { check_mysql(); if(mysql_query(mysql_conn, queryBuf)) { @@ -264,14 +295,19 @@ char* escape_string(const char *str) { struct mysql_conn_struct *get_mysql_conn_struct() { SYNCHRONIZE(synchronized); struct mysql_conn_struct *mysql_conn; - unsigned long tid = syscall(SYS_gettid); + unsigned int tid; + #ifdef HAVE_THREADS + tid = (unsigned int) pthread_self_tid(); + #else + tid = 1; + #endif for(mysql_conn = mysql_conns; mysql_conn; mysql_conn = mysql_conn->next) { if(mysql_conn->tid == tid) { DESYNCHRONIZE(synchronized); return mysql_conn; } } - mysql_conn = malloc(*mysql_conn); + mysql_conn = malloc(sizeof(*mysql_conn)); mysql_conn->mysql_conn = mysql_init(NULL); mysql_conn->tid = tid; mysql_conn->used_results = NULL;