⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sql_common.c

📁 打魔兽战网的都知道他是什么
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2002 TheUndying * Copyright (C) 2002 zap-zero * Copyright (C) 2002,2003,2005 Dizzy * Copyright (C) 2002 Zzzoom * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */#include "common/setup_before.h"#ifdef WITH_SQL#include <stdio.h>#ifdef STDC_HEADERS# include <stdlib.h>#else# ifdef HAVE_MALLOC_H#  include <malloc.h># endif#endif#ifdef HAVE_STRING_H# include <string.h>#else# ifdef HAVE_STRINGS_H#  include <strings.h># endif#endif#include "compat/strdup.h"#include "compat/strcasecmp.h"#include "compat/strncasecmp.h"#include "compat/strtoul.h"#include "compat/snprintf.h"#ifdef TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# ifdef HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#include "common/eventlog.h"#include "prefs.h"#include "common/util.h"#define CLAN_INTERNAL_ACCESS#define TEAM_INTERNAL_ACCESS#include "team.h"#include "account.h"#include "connection.h"#include "clan.h"#undef TEAM_INTERNAL_ACCESS#undef CLAN_INTERNAL_ACCESS#include "common/tag.h"#include "common/xalloc.h"#include "common/flags.h"#include "sql_dbcreator.h"#define SQL_INTERNAL# include "sql_common.h"#undef SQL_INTERNAL#ifdef WITH_SQL_MYSQL#include "sql_mysql.h"#endif#ifdef WITH_SQL_PGSQL#include "sql_pgsql.h"#endif#ifdef WITH_SQL_SQLITE3#include "sql_sqlite3.h"#endif#ifdef WITH_SQL_ODBC#include "sql_odbc.h"#endif#include "common/elist.h"#include "common/setup_after.h"unsigned int sql_defacct;t_sql_engine *sql = NULL;#ifndef SQL_ON_DEMANDchar *sql_tables[] = { "BNET", "Record", "profile", "friend", "Team", NULL };#endif	/* SQL_ON_DEMAND */const char* tab_prefix = SQL_DEFAULT_PREFIX;static char query[512];extern int sql_init(const char *dbpath){    char *tok, *path, *tmp, *p;    const char *dbhost = NULL;    const char *dbname = NULL;    const char *dbuser = NULL;    const char *dbpass = NULL;    const char *driver = NULL;    const char *dbport = NULL;    const char *dbsocket = NULL;    const char *def = NULL;    const char *pref = NULL;    path = xstrdup(dbpath);    tmp = path;    while ((tok = strtok(tmp, ";")) != NULL)    {	tmp = NULL;	if ((p = strchr(tok, '=')) == NULL)	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid storage_path, no '=' present in token");	    xfree((void *) path);	    return -1;	}	*p = '\0';	if (strcasecmp(tok, "host") == 0)	    dbhost = p + 1;	else if (strcasecmp(tok, "mode") == 0)	    driver = p + 1;	else if (strcasecmp(tok, "name") == 0)	    dbname = p + 1;	else if (strcasecmp(tok, "port") == 0)	    dbport = p + 1;	else if (strcasecmp(tok, "socket") == 0)	    dbsocket = p + 1;	else if (strcasecmp(tok, "user") == 0)	    dbuser = p + 1;	else if (strcasecmp(tok, "pass") == 0)	    dbpass = p + 1;	else if (strcasecmp(tok, "default") == 0)	    def = p + 1;	else if (strcasecmp(tok, "prefix") == 0)	    pref = p + 1;	else	    eventlog(eventlog_level_warn, __FUNCTION__, "unknown token in storage_path : '%s'", tok);    }    if (driver == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "no mode specified");	xfree((void *) path);	return -1;    }    if (def == NULL)	sql_defacct = STORAGE_SQL_DEFAULT_UID;    else	sql_defacct = atoi(def);    if (pref == NULL)    	tab_prefix = SQL_DEFAULT_PREFIX;    else    	tab_prefix = xstrdup(pref);    do    {#ifdef WITH_SQL_MYSQL	if (strcasecmp(driver, "mysql") == 0)	{	    sql = &sql_mysql;	    if (sql->init(dbhost, dbport, dbsocket, dbname, dbuser, dbpass))	    {		eventlog(eventlog_level_error, __FUNCTION__, "got error init db");		sql = NULL;		xfree((void *) path);		return -1;	    }	    break;	}#endif				/* WITH_SQL_MYSQL */#ifdef WITH_SQL_PGSQL	if (strcasecmp(driver, "pgsql") == 0)	{	    sql = &sql_pgsql;	    if (sql->init(dbhost, dbport, dbsocket, dbname, dbuser, dbpass))	    {		eventlog(eventlog_level_error, __FUNCTION__, "got error init db");		sql = NULL;		xfree((void *) path);		return -1;	    }	    break;	}#endif				/* WITH_SQL_PGSQL */#ifdef WITH_SQL_SQLITE3	if (strcasecmp(driver, "sqlite3") == 0)	{	    sql = &sql_sqlite3;	    if (sql->init(NULL, 0, NULL, dbname, NULL, NULL))	    {		eventlog(eventlog_level_error, __FUNCTION__, "got error init db");		sql = NULL;		xfree((void *) path);		return -1;	    }	    break;	}#endif				/* WITH_SQL_SQLITE3 */#ifdef WITH_SQL_ODBC	if (strcasecmp(driver, "odbc") == 0)	{	    sql = &sql_odbc;	    if (sql->init(dbhost, dbport, dbsocket, dbname, dbuser, dbpass))	    {		eventlog(eventlog_level_error, __FUNCTION__, "got error init db");		sql = NULL;		free((void *) path);		return -1;	    }	    break;	}#endif				/* WITH_SQL_ODBC */	eventlog(eventlog_level_error, __FUNCTION__, "no driver found for '%s'", driver);	xfree((void *) path);	return -1;    }    while (0);    xfree((void *) path);    sql_dbcreator(sql);    return 0;}extern int sql_close(void){    if (sql == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "sql not initilized");	return -1;    }    sql->close();    sql = NULL;    if (tab_prefix != SQL_DEFAULT_PREFIX) {    	xfree((void*)tab_prefix);    	tab_prefix = NULL;    }    return 0;}extern unsigned sql_read_maxuserid(void){    t_sql_res *result;    t_sql_row *row;    long maxuid;    if (sql == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "sql not initilized");	return 0;    }    snprintf(query, sizeof(query), "SELECT max("SQL_UID_FIELD") FROM %sBNET", tab_prefix);    if ((result = sql->query_res(query)) == NULL) {	eventlog(eventlog_level_error, __FUNCTION__, "error trying query: \"SELECT max("SQL_UID_FIELD") FROM %sBNET\"", tab_prefix);	return 0;    }    row = sql->fetch_row(result);    if (row == NULL || row[0] == NULL)    {	sql->free_result(result);	eventlog(eventlog_level_error, __FUNCTION__, "got NULL max");	return 0;    }    maxuid = atol(row[0]);    sql->free_result(result);    if (maxuid < 0)    {	eventlog(eventlog_level_error, __FUNCTION__, "got invalid maxuserid");	return 0;    }    return maxuid;}extern int sql_read_accounts(int flag,t_read_accounts_func cb, void *data){    t_sql_res *result = NULL;    t_sql_row *row;    t_storage_info *info;    if (!sql)    {	eventlog(eventlog_level_error, __FUNCTION__, "sql layer not initilized");	return -1;    }    if (cb == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "get NULL callback");	return -1;    }    /* don't actually load anything here if ST_FORCE is not set as SQL is indexed */    if (!FLAG_ISSET(flag,ST_FORCE)) return 1;    snprintf(query, sizeof(query), "SELECT "SQL_UID_FIELD" FROM %sBNET", tab_prefix);    if ((result = sql->query_res(query)) != NULL)    {	if (sql->num_rows(result) <= 1)	{	    sql->free_result(result);	    return 0;		/* empty user list */	}	while ((row = sql->fetch_row(result)) != NULL)	{	    if (row[0] == NULL)	    {		eventlog(eventlog_level_error, __FUNCTION__, "got NULL uid from db");		continue;	    }	    if ((unsigned int) atoi(row[0]) == sql_defacct)		continue;	/* skip default account */	    info = xmalloc(sizeof(t_sql_info));	    *((unsigned int *) info) = atoi(row[0]);	    cb(info, data);	}	sql->free_result(result);    } else    {	eventlog(eventlog_level_error, __FUNCTION__, "error query db (query:\"%s\")", query);	return -1;    }    return 0;}extern int sql_cmp_info(t_storage_info * info1, t_storage_info * info2){    return *((unsigned int *) info1) != *((unsigned int *) info2);}extern int sql_free_info(t_storage_info * info){    if (info)	xfree((void *) info);    return 0;}extern t_storage_info *sql_get_defacct(void){    t_storage_info *info;    info = xmalloc(sizeof(t_sql_info));    *((unsigned int *) info) = sql_defacct;    return info;}extern int sql_load_clans(t_load_clans_func cb){    t_sql_res *result;    t_sql_res *result2;    t_sql_row *row;    t_sql_row *row2;    t_clan *clan;    int member_uid;    t_clanmember *member;    if (!sql)    {	eventlog(eventlog_level_error, __FUNCTION__, "sql layer not initilized");	return -1;    }    if (cb == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "get NULL callback");	return -1;    }    snprintf(query, sizeof(query), "SELECT cid, short, name, motd, creation_time FROM %sclan WHERE cid > 0", tab_prefix);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -