diff options
Diffstat (limited to 'src/common/sql.c')
-rw-r--r-- | src/common/sql.c | 105 |
1 files changed, 8 insertions, 97 deletions
diff --git a/src/common/sql.c b/src/common/sql.c index 4ca14d43b..ee759eb61 100644 --- a/src/common/sql.c +++ b/src/common/sql.c @@ -6,19 +6,17 @@ #include "sql.h" -#include <stdlib.h> // strtoul -#include <string.h> // strlen/strnlen/memcpy/memset - -#include "../common/cbasetypes.h" -#include "../common/malloc.h" -#include "../common/showmsg.h" -#include "../common/strlib.h" -#include "../common/timer.h" +#include "common/cbasetypes.h" +#include "common/malloc.h" +#include "common/showmsg.h" +#include "common/strlib.h" +#include "common/timer.h" #ifdef WIN32 -# include "../common/winapi.h" // Needed before mysql.h +# include "common/winapi.h" // Needed before mysql.h #endif #include <mysql.h> +#include <stdlib.h> // strtoul void hercules_mysql_error_handler(unsigned int ecode); @@ -26,6 +24,7 @@ int mysql_reconnect_type; unsigned int mysql_reconnect_count; struct sql_interface sql_s; +struct sql_interface *SQL; /// Sql handle struct Sql { @@ -37,8 +36,6 @@ struct Sql { int keepalive; }; - - // Column length receiver. // Takes care of the possible size mismatch between uint32 and unsigned long. struct s_column_length { @@ -47,8 +44,6 @@ struct s_column_length { }; typedef struct s_column_length s_column_length; - - /// Sql statement struct SqlStmt { StringBuf buf; @@ -62,14 +57,10 @@ struct SqlStmt { bool bind_columns; }; - - /////////////////////////////////////////////////////////////////////////////// // Sql Handle /////////////////////////////////////////////////////////////////////////////// - - /// Allocates and initializes a new Sql handle. Sql* Sql_Malloc(void) { @@ -85,8 +76,6 @@ Sql* Sql_Malloc(void) return self; } - - static int Sql_P_Keepalive(Sql* self); /// Establishes a connection. @@ -112,8 +101,6 @@ int Sql_Connect(Sql* self, const char* user, const char* passwd, const char* hos return SQL_SUCCESS; } - - /// Retrieves the timeout of the connection. int Sql_GetTimeout(Sql* self, uint32* out_timeout) { @@ -131,8 +118,6 @@ int Sql_GetTimeout(Sql* self, uint32* out_timeout) return SQL_ERROR; } - - /// Retrieves the name of the columns of a table into out_buf, with the separator after each name. int Sql_GetColumnNames(Sql* self, const char* table, char* out_buf, size_t buf_len, char sep) { @@ -161,8 +146,6 @@ int Sql_GetColumnNames(Sql* self, const char* table, char* out_buf, size_t buf_l return SQL_SUCCESS; } - - /// Changes the encoding of the connection. int Sql_SetEncoding(Sql* self, const char* encoding) { @@ -171,8 +154,6 @@ int Sql_SetEncoding(Sql* self, const char* encoding) return SQL_ERROR; } - - /// Pings the connection. int Sql_Ping(Sql* self) { @@ -181,8 +162,6 @@ int Sql_Ping(Sql* self) return SQL_ERROR; } - - /// Wrapper function for Sql_Ping. /// /// @private @@ -194,8 +173,6 @@ static int Sql_P_KeepaliveTimer(int tid, int64 tick, int id, intptr_t data) return 0; } - - /// Establishes keepalive (periodic ping) on the connection. /// /// @return the keepalive timer id, or INVALID_TIMER @@ -219,8 +196,6 @@ static int Sql_P_Keepalive(Sql* self) return timer->add_interval(timer->gettick() + ping_interval*1000, Sql_P_KeepaliveTimer, 0, (intptr_t)self, ping_interval*1000); } - - /// Escapes a string. size_t Sql_EscapeString(Sql* self, char *out_to, const char *from) { @@ -230,8 +205,6 @@ size_t Sql_EscapeString(Sql* self, char *out_to, const char *from) return (size_t)mysql_escape_string(out_to, from, (unsigned long)strlen(from)); } - - /// Escapes a string. size_t Sql_EscapeStringLen(Sql* self, char *out_to, const char *from, size_t from_len) { @@ -241,8 +214,6 @@ size_t Sql_EscapeStringLen(Sql* self, char *out_to, const char *from, size_t fro return (size_t)mysql_escape_string(out_to, from, (unsigned long)from_len); } - - /// Executes a query. int Sql_Query(Sql *self, const char *query, ...) __attribute__((format(printf, 2, 3))); int Sql_Query(Sql *self, const char *query, ...) { @@ -256,8 +227,6 @@ int Sql_Query(Sql *self, const char *query, ...) { return res; } - - /// Executes a query. int Sql_QueryV(Sql* self, const char* query, va_list args) { @@ -283,8 +252,6 @@ int Sql_QueryV(Sql* self, const char* query, va_list args) return SQL_SUCCESS; } - - /// Executes a query. int Sql_QueryStr(Sql* self, const char* query) { @@ -310,8 +277,6 @@ int Sql_QueryStr(Sql* self, const char* query) return SQL_SUCCESS; } - - /// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE query. uint64 Sql_LastInsertId(Sql* self) { @@ -321,8 +286,6 @@ uint64 Sql_LastInsertId(Sql* self) return 0; } - - /// Returns the number of columns in each row of the result. uint32 Sql_NumColumns(Sql* self) { @@ -331,8 +294,6 @@ uint32 Sql_NumColumns(Sql* self) return 0; } - - /// Returns the number of rows in the result. uint64 Sql_NumRows(Sql* self) { @@ -341,8 +302,6 @@ uint64 Sql_NumRows(Sql* self) return 0; } - - /// Fetches the next row. int Sql_NextRow(Sql* self) { if( self && self->result ) { @@ -358,8 +317,6 @@ int Sql_NextRow(Sql* self) { return SQL_ERROR; } - - /// Gets the data of a column. int Sql_GetData(Sql* self, size_t col, char** out_buf, size_t* out_len) { @@ -376,8 +333,6 @@ int Sql_GetData(Sql* self, size_t col, char** out_buf, size_t* out_len) return SQL_ERROR; } - - /// Frees the result of the query. void Sql_FreeResult(Sql* self) { if( self && self->result ) { @@ -388,8 +343,6 @@ void Sql_FreeResult(Sql* self) { } } - - /// Shows debug information (last query). void Sql_ShowDebug_(Sql* self, const char* debug_file, const unsigned long debug_line) { @@ -401,8 +354,6 @@ void Sql_ShowDebug_(Sql* self, const char* debug_file, const unsigned long debug ShowDebug("at %s:%lu\n", debug_file, debug_line); } - - /// Frees a Sql handle returned by Sql_Malloc. void Sql_Free(Sql* self) { if( self ) @@ -415,14 +366,10 @@ void Sql_Free(Sql* self) { } } - - /////////////////////////////////////////////////////////////////////////////// // Prepared Statements /////////////////////////////////////////////////////////////////////////////// - - /// Returns the mysql integer type for the target size. /// /// @private @@ -440,8 +387,6 @@ static enum enum_field_types Sql_P_SizeToMysqlIntType(int sz) } } - - /// Binds a parameter/result. /// /// @private @@ -515,8 +460,6 @@ static int Sql_P_BindSqlDataType(MYSQL_BIND* bind, enum SqlDataType buffer_type, return SQL_SUCCESS; } - - /// Prints debug information about a field (type and length). /// /// @private @@ -552,8 +495,6 @@ static void Sql_P_ShowDebugMysqlFieldInfo(const char* prefix, enum enum_field_ty ShowDebug("%stype=%s%s, length=%lu%s\n", prefix, sign, type_string, length, length_postfix); } - - /// Reports debug information about a truncated column. /// /// @private @@ -576,8 +517,6 @@ static void SqlStmt_P_ShowDebugTruncatedColumn(SqlStmt* self, size_t i) mysql_free_result(meta); } - - /// Allocates and initializes a new SqlStmt handle. SqlStmt* SqlStmt_Malloc(Sql* sql) { SqlStmt* self; @@ -605,8 +544,6 @@ SqlStmt* SqlStmt_Malloc(Sql* sql) { return self; } - - /// Prepares the statement. int SqlStmt_Prepare(SqlStmt *self, const char *query, ...) __attribute__((format(printf, 2, 3))); int SqlStmt_Prepare(SqlStmt *self, const char *query, ...) { @@ -620,8 +557,6 @@ int SqlStmt_Prepare(SqlStmt *self, const char *query, ...) { return res; } - - /// Prepares the statement. int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args) { @@ -642,8 +577,6 @@ int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args) return SQL_SUCCESS; } - - /// Prepares the statement. int SqlStmt_PrepareStr(SqlStmt* self, const char* query) { @@ -664,8 +597,6 @@ int SqlStmt_PrepareStr(SqlStmt* self, const char* query) return SQL_SUCCESS; } - - /// Returns the number of parameters in the prepared statement. size_t SqlStmt_NumParams(SqlStmt* self) { @@ -675,8 +606,6 @@ size_t SqlStmt_NumParams(SqlStmt* self) return 0; } - - /// Binds a parameter to a buffer. int SqlStmt_BindParam(SqlStmt* self, size_t idx, enum SqlDataType buffer_type, void* buffer, size_t buffer_len) { @@ -705,8 +634,6 @@ int SqlStmt_BindParam(SqlStmt* self, size_t idx, enum SqlDataType buffer_type, v return SQL_SUCCESS;// out of range - ignore } - - /// Executes the prepared statement. int SqlStmt_Execute(SqlStmt* self) { @@ -732,8 +659,6 @@ int SqlStmt_Execute(SqlStmt* self) return SQL_SUCCESS; } - - /// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE statement. uint64 SqlStmt_LastInsertId(SqlStmt* self) { @@ -743,8 +668,6 @@ uint64 SqlStmt_LastInsertId(SqlStmt* self) return 0; } - - /// Returns the number of columns in each row of the result. size_t SqlStmt_NumColumns(SqlStmt* self) { @@ -754,8 +677,6 @@ size_t SqlStmt_NumColumns(SqlStmt* self) return 0; } - - /// Binds the result of a column to a buffer. int SqlStmt_BindColumn(SqlStmt *self, size_t idx, enum SqlDataType buffer_type, void *buffer, size_t buffer_len, uint32 *out_length, int8 *out_is_null) { if (self == NULL) @@ -797,8 +718,6 @@ int SqlStmt_BindColumn(SqlStmt *self, size_t idx, enum SqlDataType buffer_type, } } - - /// Returns the number of rows in the result. uint64 SqlStmt_NumRows(SqlStmt* self) { @@ -808,8 +727,6 @@ uint64 SqlStmt_NumRows(SqlStmt* self) return 0; } - - /// Fetches the next row. int SqlStmt_NextRow(SqlStmt* self) { @@ -893,8 +810,6 @@ int SqlStmt_NextRow(SqlStmt* self) return SQL_SUCCESS; } - - /// Frees the result of the statement execution. void SqlStmt_FreeResult(SqlStmt* self) { @@ -902,8 +817,6 @@ void SqlStmt_FreeResult(SqlStmt* self) mysql_stmt_free_result(self->stmt); } - - /// Shows debug information (with statement). void SqlStmt_ShowDebug_(SqlStmt* self, const char* debug_file, const unsigned long debug_line) { @@ -915,8 +828,6 @@ void SqlStmt_ShowDebug_(SqlStmt* self, const char* debug_file, const unsigned lo ShowDebug("at %s:%lu\n", debug_file, debug_line); } - - /// Frees a SqlStmt returned by SqlStmt_Malloc. void SqlStmt_Free(SqlStmt* self) { |