diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/HPM.c | 2 | ||||
-rw-r--r-- | src/common/core.c | 4 | ||||
-rw-r--r-- | src/common/db.c | 46 | ||||
-rw-r--r-- | src/common/db.h | 88 | ||||
-rw-r--r-- | src/common/sql.c | 145 | ||||
-rw-r--r-- | src/common/sql.h | 192 | ||||
-rw-r--r-- | src/common/timer.h | 4 |
7 files changed, 254 insertions, 227 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c index ed6151c95..9283360dc 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -264,7 +264,7 @@ void hplugins_share_defaults(void) { HPM->share(&SERVER_TYPE,"SERVER_TYPE"); HPM->share((void*)get_svn_revision,"get_svn_revision"); HPM->share((void*)get_git_hash,"get_git_hash"); - HPM->share(iDB, "iDB"); + HPM->share(DB, "DB"); HPM->share(iMalloc, "iMalloc"); /* socket */ HPM->share(RFIFOSKIP,"RFIFOSKIP"); diff --git a/src/common/core.c b/src/common/core.c index d6cfff662..c53d2243b 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -322,7 +322,7 @@ int main (int argc, char **argv) { Sql_Init(); rathread_init(); mempool_init(); - iDB->init(); + DB->init(); signals_init(); #ifdef _WIN32 @@ -356,7 +356,7 @@ int main (int argc, char **argv) { #endif iTimer->final(); socket_final(); - iDB->final(); + DB->final(); mempool_final(); rathread_final(); #endif diff --git a/src/common/db.c b/src/common/db.c index b1fe22a4a..561371787 100644 --- a/src/common/db.c +++ b/src/common/db.c @@ -2349,7 +2349,7 @@ DBHasher db_default_hash(DBType type) DBReleaser db_default_release(DBType type, DBOptions options) { DB_COUNTSTAT(db_default_release); - options = iDB->fix_options(type, options); + options = DB->fix_options(type, options); if (options&DB_OPT_RELEASE_DATA) { // Release data, what about the key? if (options&(DB_OPT_DUP_KEY|DB_OPT_RELEASE_KEY)) return &db_release_both; // Release both key and data @@ -2416,7 +2416,7 @@ DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOpt #endif /* DB_ENABLE_STATS */ db = ers_alloc(db_alloc_ers, struct DBMap_impl); - options = iDB->fix_options(type, options); + options = DB->fix_options(type, options); /* Interface of the database */ db->vtable.iterator = db_obj_iterator; db->vtable.exists = db_obj_exists; @@ -2447,9 +2447,9 @@ DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOpt /* Other */ snprintf(ers_name, 50, "db_alloc:nodes:%s:%s:%d",func,file,line); db->nodes = ers_new(sizeof(struct dbn),ers_name,ERS_OPT_WAIT|ERS_OPT_FREE_NAME); - db->cmp = iDB->default_cmp(type); - db->hash = iDB->default_hash(type); - db->release = iDB->default_release(type, options); + db->cmp = DB->default_cmp(type); + db->hash = DB->default_hash(type); + db->release = DB->default_release(type, options); for (i = 0; i < HASH_SIZE; i++) db->ht[i] = NULL; db->cache = NULL; @@ -2830,22 +2830,22 @@ void linkdb_final( struct linkdb_node** head ) *head = NULL; } void db_defaults(void) { - iDB = &iDB_s; - iDB->alloc = db_alloc; - iDB->custom_release = db_custom_release; - iDB->data2i = db_data2i; - iDB->data2ptr = db_data2ptr; - iDB->data2ui = db_data2ui; - iDB->default_cmp = db_default_cmp; - iDB->default_hash = db_default_hash; - iDB->default_release = db_default_release; - iDB->final = db_final; - iDB->fix_options = db_fix_options; - iDB->i2data = db_i2data; - iDB->i2key = db_i2key; - iDB->init = db_init; - iDB->ptr2data = db_ptr2data; - iDB->str2key = db_str2key; - iDB->ui2data = db_ui2data; - iDB->ui2key = db_ui2key; + DB = &DB_s; + DB->alloc = db_alloc; + DB->custom_release = db_custom_release; + DB->data2i = db_data2i; + DB->data2ptr = db_data2ptr; + DB->data2ui = db_data2ui; + DB->default_cmp = db_default_cmp; + DB->default_hash = db_default_hash; + DB->default_release = db_default_release; + DB->final = db_final; + DB->fix_options = db_fix_options; + DB->i2data = db_i2data; + DB->i2key = db_i2key; + DB->init = db_init; + DB->ptr2data = db_ptr2data; + DB->str2key = db_str2key; + DB->ui2data = db_ui2data; + DB->ui2key = db_ui2key; } diff --git a/src/common/db.h b/src/common/db.h index 8ad033cce..5a555b2fa 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -599,73 +599,73 @@ struct DBMap { // For easy access to the common functions. #define db_exists(db,k) ( (db)->exists((db),(k)) ) -#define idb_exists(db,k) ( (db)->exists((db),iDB->i2key(k)) ) -#define uidb_exists(db,k) ( (db)->exists((db),iDB->ui2key(k)) ) -#define strdb_exists(db,k) ( (db)->exists((db),iDB->str2key(k)) ) +#define idb_exists(db,k) ( (db)->exists((db),DB->i2key(k)) ) +#define uidb_exists(db,k) ( (db)->exists((db),DB->ui2key(k)) ) +#define strdb_exists(db,k) ( (db)->exists((db),DB->str2key(k)) ) // Get pointer-type data from DBMaps of various key types -#define db_get(db,k) ( iDB->data2ptr((db)->get((db),(k))) ) -#define idb_get(db,k) ( iDB->data2ptr((db)->get((db),iDB->i2key(k))) ) -#define uidb_get(db,k) ( iDB->data2ptr((db)->get((db),iDB->ui2key(k))) ) -#define strdb_get(db,k) ( iDB->data2ptr((db)->get((db),iDB->str2key(k))) ) +#define db_get(db,k) ( DB->data2ptr((db)->get((db),(k))) ) +#define idb_get(db,k) ( DB->data2ptr((db)->get((db),DB->i2key(k))) ) +#define uidb_get(db,k) ( DB->data2ptr((db)->get((db),DB->ui2key(k))) ) +#define strdb_get(db,k) ( DB->data2ptr((db)->get((db),DB->str2key(k))) ) // Get int-type data from DBMaps of various key types -#define db_iget(db,k) ( iDB->data2i((db)->get((db),(k))) ) -#define idb_iget(db,k) ( iDB->data2i((db)->get((db),iDB->i2key(k))) ) -#define uidb_iget(db,k) ( iDB->data2i((db)->get((db),iDB->ui2key(k))) ) -#define strdb_iget(db,k) ( iDB->data2i((db)->get((db),iDB->str2key(k))) ) +#define db_iget(db,k) ( DB->data2i((db)->get((db),(k))) ) +#define idb_iget(db,k) ( DB->data2i((db)->get((db),DB->i2key(k))) ) +#define uidb_iget(db,k) ( DB->data2i((db)->get((db),DB->ui2key(k))) ) +#define strdb_iget(db,k) ( DB->data2i((db)->get((db),DB->str2key(k))) ) // Get uint-type data from DBMaps of various key types -#define db_uiget(db,k) ( iDB->data2ui((db)->get((db),(k))) ) -#define idb_uiget(db,k) ( iDB->data2ui((db)->get((db),iDB->i2key(k))) ) -#define uidb_uiget(db,k) ( iDB->data2ui((db)->get((db),iDB->ui2key(k))) ) -#define strdb_uiget(db,k) ( iDB->data2ui((db)->get((db),iDB->str2key(k))) ) +#define db_uiget(db,k) ( DB->data2ui((db)->get((db),(k))) ) +#define idb_uiget(db,k) ( DB->data2ui((db)->get((db),DB->i2key(k))) ) +#define uidb_uiget(db,k) ( DB->data2ui((db)->get((db),DB->ui2key(k))) ) +#define strdb_uiget(db,k) ( DB->data2ui((db)->get((db),DB->str2key(k))) ) // Put pointer-type data into DBMaps of various key types -#define db_put(db,k,d) ( (db)->put((db),(k),iDB->ptr2data(d),NULL) ) -#define idb_put(db,k,d) ( (db)->put((db),iDB->i2key(k),iDB->ptr2data(d),NULL) ) -#define uidb_put(db,k,d) ( (db)->put((db),iDB->ui2key(k),iDB->ptr2data(d),NULL) ) -#define strdb_put(db,k,d) ( (db)->put((db),iDB->str2key(k),iDB->ptr2data(d),NULL) ) +#define db_put(db,k,d) ( (db)->put((db),(k),DB->ptr2data(d),NULL) ) +#define idb_put(db,k,d) ( (db)->put((db),DB->i2key(k),DB->ptr2data(d),NULL) ) +#define uidb_put(db,k,d) ( (db)->put((db),DB->ui2key(k),DB->ptr2data(d),NULL) ) +#define strdb_put(db,k,d) ( (db)->put((db),DB->str2key(k),DB->ptr2data(d),NULL) ) // Put int-type data into DBMaps of various key types -#define db_iput(db,k,d) ( (db)->put((db),(k),iDB->i2data(d),NULL) ) -#define idb_iput(db,k,d) ( (db)->put((db),iDB->i2key(k),iDB->i2data(d),NULL) ) -#define uidb_iput(db,k,d) ( (db)->put((db),iDB->ui2key(k),iDB->i2data(d),NULL) ) -#define strdb_iput(db,k,d) ( (db)->put((db),iDB->str2key(k),iDB->i2data(d),NULL) ) +#define db_iput(db,k,d) ( (db)->put((db),(k),DB->i2data(d),NULL) ) +#define idb_iput(db,k,d) ( (db)->put((db),DB->i2key(k),DB->i2data(d),NULL) ) +#define uidb_iput(db,k,d) ( (db)->put((db),DB->ui2key(k),DB->i2data(d),NULL) ) +#define strdb_iput(db,k,d) ( (db)->put((db),DB->str2key(k),DB->i2data(d),NULL) ) // Put uint-type data into DBMaps of various key types -#define db_uiput(db,k,d) ( (db)->put((db),(k),iDB->ui2data(d),NULL) ) -#define idb_uiput(db,k,d) ( (db)->put((db),iDB->i2key(k),iDB->ui2data(d),NULL) ) -#define uidb_uiput(db,k,d) ( (db)->put((db),iDB->ui2key(k),iDB->ui2data(d),NULL) ) -#define strdb_uiput(db,k,d) ( (db)->put((db),iDB->str2key(k),iDB->ui2data(d),NULL) ) +#define db_uiput(db,k,d) ( (db)->put((db),(k),DB->ui2data(d),NULL) ) +#define idb_uiput(db,k,d) ( (db)->put((db),DB->i2key(k),DB->ui2data(d),NULL) ) +#define uidb_uiput(db,k,d) ( (db)->put((db),DB->ui2key(k),DB->ui2data(d),NULL) ) +#define strdb_uiput(db,k,d) ( (db)->put((db),DB->str2key(k),DB->ui2data(d),NULL) ) // Remove entry from DBMaps of various key types #define db_remove(db,k) ( (db)->remove((db),(k),NULL) ) -#define idb_remove(db,k) ( (db)->remove((db),iDB->i2key(k),NULL) ) -#define uidb_remove(db,k) ( (db)->remove((db),iDB->ui2key(k),NULL) ) -#define strdb_remove(db,k) ( (db)->remove((db),iDB->str2key(k),NULL) ) +#define idb_remove(db,k) ( (db)->remove((db),DB->i2key(k),NULL) ) +#define uidb_remove(db,k) ( (db)->remove((db),DB->ui2key(k),NULL) ) +#define strdb_remove(db,k) ( (db)->remove((db),DB->str2key(k),NULL) ) //These are discarding the possible vargs you could send to the function, so those //that require vargs must not use these defines. -#define db_ensure(db,k,f) ( iDB->data2ptr((db)->ensure((db),(k),(f))) ) -#define idb_ensure(db,k,f) ( iDB->data2ptr((db)->ensure((db),iDB->i2key(k),(f))) ) -#define uidb_ensure(db,k,f) ( iDB->data2ptr((db)->ensure((db),iDB->ui2key(k),(f))) ) -#define strdb_ensure(db,k,f) ( iDB->data2ptr((db)->ensure((db),iDB->str2key(k),(f))) ) +#define db_ensure(db,k,f) ( DB->data2ptr((db)->ensure((db),(k),(f))) ) +#define idb_ensure(db,k,f) ( DB->data2ptr((db)->ensure((db),DB->i2key(k),(f))) ) +#define uidb_ensure(db,k,f) ( DB->data2ptr((db)->ensure((db),DB->ui2key(k),(f))) ) +#define strdb_ensure(db,k,f) ( DB->data2ptr((db)->ensure((db),DB->str2key(k),(f))) ) // Database creation and destruction macros -#define idb_alloc(opt) iDB->alloc(__FILE__,__func__,__LINE__,DB_INT,(opt),sizeof(int)) -#define uidb_alloc(opt) iDB->alloc(__FILE__,__func__,__LINE__,DB_UINT,(opt),sizeof(unsigned int)) -#define strdb_alloc(opt,maxlen) iDB->alloc(__FILE__,__func__,__LINE__,DB_STRING,(opt),(maxlen)) -#define stridb_alloc(opt,maxlen) iDB->alloc(__FILE__,__func__,__LINE__,DB_ISTRING,(opt),(maxlen)) +#define idb_alloc(opt) DB->alloc(__FILE__,__func__,__LINE__,DB_INT,(opt),sizeof(int)) +#define uidb_alloc(opt) DB->alloc(__FILE__,__func__,__LINE__,DB_UINT,(opt),sizeof(unsigned int)) +#define strdb_alloc(opt,maxlen) DB->alloc(__FILE__,__func__,__LINE__,DB_STRING,(opt),(maxlen)) +#define stridb_alloc(opt,maxlen) DB->alloc(__FILE__,__func__,__LINE__,DB_ISTRING,(opt),(maxlen)) #define db_destroy(db) ( (db)->destroy((db),NULL) ) // Other macros #define db_clear(db) ( (db)->clear(db,NULL) ) #define db_size(db) ( (db)->size(db) ) #define db_iterator(db) ( (db)->iterator(db) ) -#define dbi_first(dbi) ( iDB->data2ptr((dbi)->first(dbi,NULL)) ) -#define dbi_last(dbi) ( iDB->data2ptr((dbi)->last(dbi,NULL)) ) -#define dbi_next(dbi) ( iDB->data2ptr((dbi)->next(dbi,NULL)) ) -#define dbi_prev(dbi) ( iDB->data2ptr((dbi)->prev(dbi,NULL)) ) +#define dbi_first(dbi) ( DB->data2ptr((dbi)->first(dbi,NULL)) ) +#define dbi_last(dbi) ( DB->data2ptr((dbi)->last(dbi,NULL)) ) +#define dbi_next(dbi) ( DB->data2ptr((dbi)->next(dbi,NULL)) ) +#define dbi_prev(dbi) ( DB->data2ptr((dbi)->prev(dbi,NULL)) ) #define dbi_remove(dbi) ( (dbi)->remove(dbi,NULL) ) #define dbi_exists(dbi) ( (dbi)->exists(dbi) ) #define dbi_destroy(dbi) ( (dbi)->destroy(dbi) ) @@ -867,9 +867,9 @@ void (*init) (void); * @see #db_init(void) */ void (*final) (void); -} iDB_s; +} DB_s; -struct db_interface *iDB; +struct db_interface *DB; void db_defaults(void); // Link DB System - From jAthena diff --git a/src/common/sql.c b/src/common/sql.c index d4bea7c12..441b860da 100644 --- a/src/common/sql.c +++ b/src/common/sql.c @@ -116,9 +116,9 @@ int Sql_GetTimeout(Sql* self, uint32* out_timeout) size_t len; if( SQL_SUCCESS == SQL->NextRow(self) && SQL_SUCCESS == SQL->GetData(self, 1, &data, &len) ) { - *out_timeout = (uint32)strtoul(data, NULL, 10); - SQL->FreeResult(self); - return SQL_SUCCESS; + *out_timeout = (uint32)strtoul(data, NULL, 10); + SQL->FreeResult(self); + return SQL_SUCCESS; } SQL->FreeResult(self); } @@ -447,7 +447,7 @@ static int Sql_P_BindSqlDataType(MYSQL_BIND* bind, enum SqlDataType buffer_type, case SQLDT_NULL: bind->buffer_type = MYSQL_TYPE_NULL; buffer_len = 0;// FIXME length = ? [FlavioJS] break; - // fixed size + // fixed size case SQLDT_UINT8: bind->is_unsigned = 1; case SQLDT_INT8: bind->buffer_type = MYSQL_TYPE_TINY; buffer_len = 1; @@ -464,7 +464,7 @@ static int Sql_P_BindSqlDataType(MYSQL_BIND* bind, enum SqlDataType buffer_type, case SQLDT_INT64: bind->buffer_type = MYSQL_TYPE_LONGLONG; buffer_len = 8; break; - // platform dependent size + // platform dependent size case SQLDT_UCHAR: bind->is_unsigned = 1; case SQLDT_CHAR: bind->buffer_type = Sql_P_SizeToMysqlIntType(sizeof(char)); buffer_len = sizeof(char); @@ -485,14 +485,14 @@ static int Sql_P_BindSqlDataType(MYSQL_BIND* bind, enum SqlDataType buffer_type, case SQLDT_LONGLONG: bind->buffer_type = Sql_P_SizeToMysqlIntType(sizeof(int64)); buffer_len = sizeof(int64); break; - // floating point + // floating point case SQLDT_FLOAT: bind->buffer_type = MYSQL_TYPE_FLOAT; buffer_len = 4; break; case SQLDT_DOUBLE: bind->buffer_type = MYSQL_TYPE_DOUBLE; buffer_len = 8; break; - // other + // other case SQLDT_STRING: case SQLDT_ENUM: bind->buffer_type = MYSQL_TYPE_STRING; break; @@ -524,25 +524,25 @@ static void Sql_P_ShowDebugMysqlFieldInfo(const char* prefix, enum enum_field_ty ShowDebug("%stype=%s%u, length=%d\n", prefix, sign, type, length); return; #define SHOW_DEBUG_OF(x) case x: type_string = #x; break - SHOW_DEBUG_OF(MYSQL_TYPE_TINY); - SHOW_DEBUG_OF(MYSQL_TYPE_SHORT); - SHOW_DEBUG_OF(MYSQL_TYPE_LONG); - SHOW_DEBUG_OF(MYSQL_TYPE_INT24); - SHOW_DEBUG_OF(MYSQL_TYPE_LONGLONG); - SHOW_DEBUG_OF(MYSQL_TYPE_DECIMAL); - SHOW_DEBUG_OF(MYSQL_TYPE_FLOAT); - SHOW_DEBUG_OF(MYSQL_TYPE_DOUBLE); - SHOW_DEBUG_OF(MYSQL_TYPE_TIMESTAMP); - SHOW_DEBUG_OF(MYSQL_TYPE_DATE); - SHOW_DEBUG_OF(MYSQL_TYPE_TIME); - SHOW_DEBUG_OF(MYSQL_TYPE_DATETIME); - SHOW_DEBUG_OF(MYSQL_TYPE_YEAR); - SHOW_DEBUG_OF(MYSQL_TYPE_STRING); - SHOW_DEBUG_OF(MYSQL_TYPE_VAR_STRING); - SHOW_DEBUG_OF(MYSQL_TYPE_BLOB); - SHOW_DEBUG_OF(MYSQL_TYPE_SET); - SHOW_DEBUG_OF(MYSQL_TYPE_ENUM); - SHOW_DEBUG_OF(MYSQL_TYPE_NULL); + SHOW_DEBUG_OF(MYSQL_TYPE_TINY); + SHOW_DEBUG_OF(MYSQL_TYPE_SHORT); + SHOW_DEBUG_OF(MYSQL_TYPE_LONG); + SHOW_DEBUG_OF(MYSQL_TYPE_INT24); + SHOW_DEBUG_OF(MYSQL_TYPE_LONGLONG); + SHOW_DEBUG_OF(MYSQL_TYPE_DECIMAL); + SHOW_DEBUG_OF(MYSQL_TYPE_FLOAT); + SHOW_DEBUG_OF(MYSQL_TYPE_DOUBLE); + SHOW_DEBUG_OF(MYSQL_TYPE_TIMESTAMP); + SHOW_DEBUG_OF(MYSQL_TYPE_DATE); + SHOW_DEBUG_OF(MYSQL_TYPE_TIME); + SHOW_DEBUG_OF(MYSQL_TYPE_DATETIME); + SHOW_DEBUG_OF(MYSQL_TYPE_YEAR); + SHOW_DEBUG_OF(MYSQL_TYPE_STRING); + SHOW_DEBUG_OF(MYSQL_TYPE_VAR_STRING); + SHOW_DEBUG_OF(MYSQL_TYPE_BLOB); + SHOW_DEBUG_OF(MYSQL_TYPE_SET); + SHOW_DEBUG_OF(MYSQL_TYPE_ENUM); + SHOW_DEBUG_OF(MYSQL_TYPE_NULL); #undef SHOW_DEBUG_TYPE_OF } ShowDebug("%stype=%s%s, length=%d%s\n", prefix, sign, type_string, length, length_postfix); @@ -610,7 +610,7 @@ int SqlStmt_Prepare(SqlStmt* self, const char* query, ...) va_list args; va_start(args, query); - res = SqlStmt_PrepareV(self, query, args); + res = SQL->StmtPrepareV(self, query, args); va_end(args); return res; @@ -624,7 +624,7 @@ int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args) if( self == NULL ) return SQL_ERROR; - SqlStmt_FreeResult(self); + SQL->StmtFreeResult(self); StrBuf->Clear(&self->buf); StrBuf->Vprintf(&self->buf, query, args); if( mysql_stmt_prepare(self->stmt, StrBuf->Value(&self->buf), (unsigned long)StrBuf->Length(&self->buf)) ) @@ -646,7 +646,7 @@ int SqlStmt_PrepareStr(SqlStmt* self, const char* query) if( self == NULL ) return SQL_ERROR; - SqlStmt_FreeResult(self); + SQL->StmtFreeResult(self); StrBuf->Clear(&self->buf); StrBuf->AppendStr(&self->buf, query); if( mysql_stmt_prepare(self->stmt, StrBuf->Value(&self->buf), (unsigned long)StrBuf->Length(&self->buf)) ) @@ -677,14 +677,14 @@ size_t SqlStmt_NumParams(SqlStmt* self) int SqlStmt_BindParam(SqlStmt* self, size_t idx, enum SqlDataType buffer_type, void* buffer, size_t buffer_len) { if( self == NULL ) - return SQL_ERROR; + return SQL_ERROR; if( !self->bind_params ) {// initialize the bindings size_t i; size_t count; - count = SqlStmt_NumParams(self); + count = SQL->StmtNumParams(self); if( self->max_params < count ) { self->max_params = count; @@ -709,7 +709,7 @@ int SqlStmt_Execute(SqlStmt* self) if( self == NULL ) return SQL_ERROR; - SqlStmt_FreeResult(self); + SQL->StmtFreeResult(self); if( (self->bind_params && mysql_stmt_bind_param(self->stmt, self->params)) || mysql_stmt_execute(self->stmt) ) { @@ -772,7 +772,7 @@ int SqlStmt_BindColumn(SqlStmt* self, size_t idx, enum SqlDataType buffer_type, size_t i; size_t cols; - cols = SqlStmt_NumColumns(self); + cols = SQL->StmtNumColumns(self); if( self->max_columns < cols ) { self->max_columns = cols; @@ -843,7 +843,7 @@ int SqlStmt_NextRow(SqlStmt* self) } // find truncated column - cols = SqlStmt_NumColumns(self); + cols = SQL->StmtNumColumns(self); for( i = 0; i < cols; ++i ) { column = &self->columns[i]; @@ -868,7 +868,7 @@ int SqlStmt_NextRow(SqlStmt* self) } // propagate column lengths and clear unused parts of string/enum/blob buffers - cols = SqlStmt_NumColumns(self); + cols = SQL->StmtNumColumns(self); for( i = 0; i < cols; ++i ) { length = self->column_lengths[i].length; @@ -946,21 +946,21 @@ void SqlStmt_Free(SqlStmt* self) void hercules_mysql_error_handler(unsigned int ecode) { static unsigned int retry = 1; switch( ecode ) { - case 2003:/* Can't connect to MySQL (this error only happens here when failing to reconnect) */ - if( mysql_reconnect_type == 1 ) { - if( ++retry > mysql_reconnect_count ) { - ShowFatalError("MySQL has been unreachable for too long, %d reconnects were attempted. Shutting Down\n", retry); - exit(EXIT_FAILURE); - } + case 2003:/* Can't connect to MySQL (this error only happens here when failing to reconnect) */ + if( mysql_reconnect_type == 1 ) { + if( ++retry > mysql_reconnect_count ) { + ShowFatalError("MySQL has been unreachable for too long, %d reconnects were attempted. Shutting Down\n", retry); + exit(EXIT_FAILURE); } - break; + } + break; } } void Sql_inter_server_read(const char* cfgName, bool first) { int i; char line[1024], w1[1024], w2[1024]; FILE* fp; - + fp = fopen(cfgName, "r"); if(fp == NULL) { if( first ) { @@ -970,22 +970,22 @@ void Sql_inter_server_read(const char* cfgName, bool first) { ShowError("File not found: %s\n", cfgName); return; } - + while(fgets(line, sizeof(line), fp)) { i = sscanf(line, "%[^:]: %[^\r\n]", w1, w2); if(i != 2) continue; - + if(!strcmpi(w1,"mysql_reconnect_type")) { mysql_reconnect_type = atoi(w2); switch( mysql_reconnect_type ) { - case 1: - case 2: - break; - default: - ShowError("%s::mysql_reconnect_type is set to %d which is not valid, defaulting to 1...\n", cfgName, mysql_reconnect_type); - mysql_reconnect_type = 1; - break; + case 1: + case 2: + break; + default: + ShowError("%s::mysql_reconnect_type is set to %d which is not valid, defaulting to 1...\n", cfgName, mysql_reconnect_type); + mysql_reconnect_type = 1; + break; } } else if(!strcmpi(w1,"mysql_reconnect_count")) { mysql_reconnect_count = atoi(w2); @@ -995,7 +995,7 @@ void Sql_inter_server_read(const char* cfgName, bool first) { Sql_inter_server_read(w2,false); } fclose(fp); - + return; } @@ -1003,7 +1003,7 @@ void Sql_HerculesUpdateCheck(Sql* self) { char line[22];// "yyyy-mm-dd--hh-mm" (17) + ".sql" (4) + 1 FILE* ifp;/* index fp */ unsigned int performed = 0; - + if( !( ifp = fopen("sql-files/upgrades/index.txt", "r") ) ) { ShowError("SQL upgrade index was not found!\n"); return; @@ -1013,22 +1013,22 @@ void Sql_HerculesUpdateCheck(Sql* self) { char path[41];// "sql-files/upgrades/" (19) + "yyyy-mm-dd--hh-mm" (17) + ".sql" (4) + 1 char timestamp[11];// "1360186680" (10) + 1 FILE* ufp;/* upgrade fp */ - + if( line[0] == '\n' || ( line[0] == '/' && line[1] == '/' ) )/* skip \n and "//" comments */ continue; - + sprintf(path,"sql-files/upgrades/%s",line); - + if( !( ufp = fopen(path, "r") ) ) { ShowError("SQL upgrade file %s was not found!\n",path); continue; } - + if( fgetc(ufp) != '#' ) continue; - + fseek (ufp,1,SEEK_SET);/* woo. skip the # */ - + if( fgets(timestamp,sizeof(timestamp),ufp) ) { unsigned int timestampui = atol(timestamp); if( SQL_ERROR == SQL->Query(self, "SELECT 1 FROM `sql_updates` WHERE `timestamp` = '%u' LIMIT 1", timestampui) ) @@ -1038,12 +1038,12 @@ void Sql_HerculesUpdateCheck(Sql* self) { performed++; } } - + fclose(ufp); } - + fclose(ifp); - + if( performed ) { ShowSQL("If you did apply these updates or would like to be skip, insert a new entry in your sql_updates table with the timestamp of each file\n"); } @@ -1054,7 +1054,7 @@ void Sql_Init(void) { } void sql_defaults(void) { SQL = &sql_s; - + SQL->Connect = Sql_Connect; SQL->GetTimeout = Sql_GetTimeout; SQL->GetColumnNames = Sql_GetColumnNames; @@ -1074,4 +1074,21 @@ void sql_defaults(void) { SQL->ShowDebug_ = Sql_ShowDebug_; SQL->Free = Sql_Free; SQL->Malloc = Sql_Malloc; + + /* SqlStmt defaults [Susu] */ + SQL->StmtBindColumn = SqlStmt_BindColumn; + SQL->StmtBindParam = SqlStmt_BindParam; + SQL->StmtExecute = SqlStmt_Execute; + SQL->StmtFree = SqlStmt_Free; + SQL->StmtFreeResult = SqlStmt_FreeResult; + SQL->StmtLastInsertId = SqlStmt_LastInsertId; + SQL->StmtMalloc = SqlStmt_Malloc; + SQL->StmtNextRow = SqlStmt_NextRow; + SQL->StmtNumColumns = SqlStmt_NumColumns; + SQL->StmtNumParams = SqlStmt_NumParams; + SQL->StmtNumRows = SqlStmt_NumRows; + SQL->StmtPrepare = SqlStmt_Prepare; + SQL->StmtPrepareStr = SqlStmt_PrepareStr; + SQL->StmtPrepareV = SqlStmt_PrepareV; + SQL->StmtShowDebug_ = SqlStmt_ShowDebug_; } diff --git a/src/common/sql.h b/src/common/sql.h index d5a0eda2c..535990649 100644 --- a/src/common/sql.h +++ b/src/common/sql.h @@ -142,145 +142,157 @@ struct sql_interface { void (*Free) (Sql* self); /// Allocates and initializes a new Sql handle. struct Sql *(*Malloc) (void); -} sql_s; -struct sql_interface *SQL; -void sql_defaults(void); -#if defined(SQL_REMOVE_SHOWDEBUG) - #define Sql_ShowDebug(self) (void)0 -#else - #define Sql_ShowDebug(self) SQL->ShowDebug_(self, __FILE__, __LINE__) -#endif + /////////////////////////////////////////////////////////////////////////////// + // Prepared Statements + /////////////////////////////////////////////////////////////////////////////// + // Parameters are placed in the statement by embedding question mark ('?') + // characters into the query at the appropriate positions. + // The markers are legal only in places where they represent data. + // The markers cannot be inside quotes. Quotes will be added automatically + // when they are required. + // + // example queries with parameters: + // 1) SELECT col FROM table WHERE id=? + // 2) INSERT INTO table(col1,col2) VALUES(?,?) -/////////////////////////////////////////////////////////////////////////////// -// Prepared Statements -/////////////////////////////////////////////////////////////////////////////// -// Parameters are placed in the statement by embedding question mark ('?') -// characters into the query at the appropriate positions. -// The markers are legal only in places where they represent data. -// The markers cannot be inside quotes. Quotes will be added automatically -// when they are required. -// -// example queries with parameters: -// 1) SELECT col FROM table WHERE id=? -// 2) INSERT INTO table(col1,col2) VALUES(?,?) -/// Allocates and initializes a new SqlStmt handle. -/// It uses the connection of the parent Sql handle. -/// Queries in Sql and SqlStmt are independent and don't affect each other. -/// -/// @return SqlStmt handle or NULL if an error occured -struct SqlStmt* SqlStmt_Malloc(Sql* sql); + /*===================================== + SQL Statement interface [Susu] + *-------------------------------------*/ + /// Allocates and initializes a new SqlStmt handle. + /// It uses the connection of the parent Sql handle. + /// Queries in Sql and SqlStmt are independent and don't affect each other. + /// + /// @return SqlStmt handle or NULL if an error occured + struct SqlStmt* (*StmtMalloc)(Sql* sql); -/// Prepares the statement. -/// Any previous result is freed and all parameter bindings are removed. -/// The query is constructed as if it was sprintf. -/// -/// @return SQL_SUCCESS or SQL_ERROR -int SqlStmt_Prepare(SqlStmt* self, const char* query, ...); -/// Prepares the statement. -/// Any previous result is freed and all parameter bindings are removed. -/// The query is constructed as if it was svprintf. -/// -/// @return SQL_SUCCESS or SQL_ERROR -int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args); + /// Prepares the statement. + /// Any previous result is freed and all parameter bindings are removed. + /// The query is constructed as if it was sprintf. + /// + /// @return SQL_SUCCESS or SQL_ERROR + int (*StmtPrepare)(SqlStmt* self, const char* query, ...); + /// Prepares the statement. + /// Any previous result is freed and all parameter bindings are removed. + /// The query is constructed as if it was svprintf. + /// + /// @return SQL_SUCCESS or SQL_ERROR + int (*StmtPrepareV)(SqlStmt* self, const char* query, va_list args); -/// Prepares the statement. -/// Any previous result is freed and all parameter bindings are removed. -/// The query is used directly. -/// -/// @return SQL_SUCCESS or SQL_ERROR -int SqlStmt_PrepareStr(SqlStmt* self, const char* query); + /// Prepares the statement. + /// Any previous result is freed and all parameter bindings are removed. + /// The query is used directly. + /// + /// @return SQL_SUCCESS or SQL_ERROR + int (*StmtPrepareStr)(SqlStmt* self, const char* query); -/// Returns the number of parameters in the prepared statement. -/// -/// @return Number or paramenters -size_t SqlStmt_NumParams(SqlStmt* self); + /// Returns the number of parameters in the prepared statement. + /// + /// @return Number or paramenters + size_t (*StmtNumParams)(SqlStmt* self); -/// Binds a parameter to a buffer. -/// The buffer data will be used when the statement is executed. -/// All parameters should have bindings. -/// -/// @return SQL_SUCCESS or SQL_ERROR -int SqlStmt_BindParam(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len); + /// Binds a parameter to a buffer. + /// The buffer data will be used when the statement is executed. + /// All parameters should have bindings. + /// + /// @return SQL_SUCCESS or SQL_ERROR + int (*StmtBindParam)(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len); -/// Executes the prepared statement. -/// Any previous result is freed and all column bindings are removed. -/// -/// @return SQL_SUCCESS or SQL_ERROR -int SqlStmt_Execute(SqlStmt* self); + /// Executes the prepared statement. + /// Any previous result is freed and all column bindings are removed. + /// + /// @return SQL_SUCCESS or SQL_ERROR + int (*StmtExecute)(SqlStmt* self); -/// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE statement. -/// -/// @return Value of the auto-increment column -uint64 SqlStmt_LastInsertId(SqlStmt* self); + /// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE statement. + /// + /// @return Value of the auto-increment column + uint64 (*StmtLastInsertId)(SqlStmt* self); -/// Returns the number of columns in each row of the result. -/// -/// @return Number of columns -size_t SqlStmt_NumColumns(SqlStmt* self); + /// Returns the number of columns in each row of the result. + /// + /// @return Number of columns + size_t (*StmtNumColumns)(SqlStmt* self); + + + + /// Binds the result of a column to a buffer. + /// The buffer will be filled with data when the next row is fetched. + /// For string/enum buffer types there has to be enough space for the data + /// and the nul-terminator (an extra byte). + /// + /// @return SQL_SUCCESS or SQL_ERROR + int (*StmtBindColumn)(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len, uint32* out_length, int8* out_is_null); + + + + /// Returns the number of rows in the result. + /// + /// @return Number of rows + uint64 (*StmtNumRows)(SqlStmt* self); -/// Binds the result of a column to a buffer. -/// The buffer will be filled with data when the next row is fetched. -/// For string/enum buffer types there has to be enough space for the data -/// and the nul-terminator (an extra byte). -/// -/// @return SQL_SUCCESS or SQL_ERROR -int SqlStmt_BindColumn(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len, uint32* out_length, int8* out_is_null); + /// Fetches the next row. + /// All column bindings will be filled with data. + /// + /// @return SQL_SUCCESS, SQL_ERROR or SQL_NO_DATA + int (*StmtNextRow)(SqlStmt* self); -/// Returns the number of rows in the result. -/// -/// @return Number of rows -uint64 SqlStmt_NumRows(SqlStmt* self); + /// Frees the result of the statement execution. + void (*StmtFreeResult)(SqlStmt* self); + /// Frees a SqlStmt returned by SqlStmt_Malloc. + void (*StmtFree)(SqlStmt* self); -/// Fetches the next row. -/// All column bindings will be filled with data. -/// -/// @return SQL_SUCCESS, SQL_ERROR or SQL_NO_DATA -int SqlStmt_NextRow(SqlStmt* self); + void (*StmtShowDebug_)(SqlStmt* self, const char* debug_file, const unsigned long debug_line); +} sql_s; + +struct sql_interface *SQL; +void sql_defaults(void); -/// Frees the result of the statement execution. -void SqlStmt_FreeResult(SqlStmt* self); +#if defined(SQL_REMOVE_SHOWDEBUG) +#define Sql_ShowDebug(self) (void)0 +#else +#define Sql_ShowDebug(self) SQL->ShowDebug_(self, __FILE__, __LINE__) +#endif void Sql_HerculesUpdateCheck(Sql* self); #if defined(SQL_REMOVE_SHOWDEBUG) - #define SqlStmt_ShowDebug(self) (void)0 +#define SqlStmt_ShowDebug(self) (void)0 #else - #define SqlStmt_ShowDebug(self) SqlStmt_ShowDebug_(self, __FILE__, __LINE__) +#define SqlStmt_ShowDebug(self) SQL->StmtShowDebug_(self, __FILE__, __LINE__) #endif /// Shows debug information (with statement). -void SqlStmt_ShowDebug_(SqlStmt* self, const char* debug_file, const unsigned long debug_line); -/// Frees a SqlStmt returned by SqlStmt_Malloc. -void SqlStmt_Free(SqlStmt* self); + + void Sql_Init(void); diff --git a/src/common/timer.h b/src/common/timer.h index d68b5ed0f..50f1c354b 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -1,10 +1,8 @@ // Copyright (c) Hercules Dev Team, licensed under GNU GPL. // See the LICENSE file // Portions Copyright (c) Athena Dev Teams - #ifndef _TIMER_H_ #define _TIMER_H_ - #include "../common/cbasetypes.h" #define DIFF_TICK(a,b) ((int)((a)-(b))) @@ -34,7 +32,7 @@ struct TimerData { intptr_t data; }; -/* Hercules Renewal Phase One */ + /*===================================== * Interface : timer.h * Generated by HerculesInterfaceMaker |