Added SQL_SetCharset native to (re)set charset even after reconnect (bug 5786, r=psychonic).

This commit is contained in:
Peace-Maker 2013-07-18 10:27:12 -04:00
parent 5a5d8d2d07
commit 69d8b1c30d
7 changed files with 51 additions and 1 deletions

View File

@ -1407,6 +1407,23 @@ static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params)
return hndl; return hndl;
} }
static cell_t SQL_SetCharset(IPluginContext *pContext, const cell_t *params)
{
IDatabase *db = NULL;
HandleError err;
if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
}
char *characterset;
pContext->LocalToString(params[2], &characterset);
return db->SetCharacterSet(characterset);
}
REGISTER_NATIVES(dbNatives) REGISTER_NATIVES(dbNatives)
{ {
{"SQL_BindParamInt", SQL_BindParamInt}, {"SQL_BindParamInt", SQL_BindParamInt},
@ -1448,6 +1465,7 @@ REGISTER_NATIVES(dbNatives)
{"SQL_TQuery", SQL_TQuery}, {"SQL_TQuery", SQL_TQuery},
{"SQL_UnlockDatabase", SQL_UnlockDatabase}, {"SQL_UnlockDatabase", SQL_UnlockDatabase},
{"SQL_ConnectCustom", SQL_ConnectCustom}, {"SQL_ConnectCustom", SQL_ConnectCustom},
{"SQL_SetCharset", SQL_SetCharset},
{NULL, NULL}, {NULL, NULL},
}; };

View File

@ -326,3 +326,8 @@ IDBDriver *MyDatabase::GetDriver()
{ {
return &g_MyDriver; return &g_MyDriver;
} }
bool MyDatabase::SetCharacterSet(const char *characterset)
{
return mysql_set_character_set(m_mysql, characterset) == 0 ? true : false;
}

View File

@ -62,6 +62,7 @@ public: //IDatabase
IQuery *DoQueryEx(const char *query, size_t len); IQuery *DoQueryEx(const char *query, size_t len);
unsigned int GetAffectedRowsForQuery(IQuery *query); unsigned int GetAffectedRowsForQuery(IQuery *query);
unsigned int GetInsertIDForQuery(IQuery *query); unsigned int GetInsertIDForQuery(IQuery *query);
bool SetCharacterSet(const char *characterset);
public: public:
const DatabaseInfo &GetInfo(); const DatabaseInfo &GetInfo();
private: private:

View File

@ -254,3 +254,9 @@ sqlite3 *SqDatabase::GetDb()
{ {
return m_sq3; return m_sq3;
} }
bool SqDatabase::SetCharacterSet(const char *characterset)
{
// sqlite only supports utf8 and utf16 - by the time the database is created. It's too late here.
return false;
}

View File

@ -58,6 +58,7 @@ public:
IQuery *DoQueryEx(const char *query, size_t len); IQuery *DoQueryEx(const char *query, size_t len);
unsigned int GetAffectedRowsForQuery(IQuery *query); unsigned int GetAffectedRowsForQuery(IQuery *query);
unsigned int GetInsertIDForQuery(IQuery *query); unsigned int GetInsertIDForQuery(IQuery *query);
bool SetCharacterSet(const char *characterset);
public: public:
sqlite3 *GetDb(); sqlite3 *GetDb();
private: private:

View File

@ -249,6 +249,18 @@ native SQL_GetDriverIdent(Handle:driver, String:ident[], maxlength);
*/ */
native SQL_GetDriverProduct(Handle:driver, String:product[], maxlength); native SQL_GetDriverProduct(Handle:driver, String:product[], maxlength);
/**
* Sets the character set of the current connection.
* Like SET NAMES .. in mysql, but stays after connection problems.
*
* Example: "utf8", "latin1"
*
* @param database Database Handle.
* @param characterset The character set string to change to.
* @return True, if character set was changed, false otherwise.
*/
native bool:SQL_SetCharset(Handle:database, const String:charset[]);
/** /**
* Returns the number of affected rows from the last query. * Returns the number of affected rows from the last query.
* *

View File

@ -42,7 +42,7 @@
*/ */
#define SMINTERFACE_DBI_NAME "IDBI" #define SMINTERFACE_DBI_NAME "IDBI"
#define SMINTERFACE_DBI_VERSION 8 #define SMINTERFACE_DBI_VERSION 9
namespace SourceMod namespace SourceMod
{ {
@ -600,6 +600,13 @@ namespace SourceMod
* if applicable. * if applicable.
*/ */
virtual unsigned int GetInsertIDForQuery(IQuery *query) =0; virtual unsigned int GetInsertIDForQuery(IQuery *query) =0;
/**
* @brief Sets the character set of the current connection
*
* @param characterset The characterset to switch to. e.g. "utf8".
*/
virtual bool SetCharacterSet(const char *characterset) =0;
}; };
/** /**