Added SQL_SetCharset native to (re)set charset even after reconnect (bug 5786, r=psychonic).
authorPeace-Maker <januk@freenet.de>
Thu Jul 18 10:27:12 2013 -0400 (2013-07-18)
changeset 3947999d29e113db
parent 3946 0fe6ce0499d1
child 3948 530b0c5c8a33
Added SQL_SetCharset native to (re)set charset even after reconnect (bug 5786, r=psychonic).
core/smn_database.cpp
extensions/mysql/mysql/MyDatabase.cpp
extensions/mysql/mysql/MyDatabase.h
extensions/sqlite/driver/SqDatabase.cpp
extensions/sqlite/driver/SqDatabase.h
plugins/include/dbi.inc
public/IDBDriver.h
     1.1 --- a/core/smn_database.cpp	Wed Jul 17 08:05:42 2013 -0400
     1.2 +++ b/core/smn_database.cpp	Thu Jul 18 10:27:12 2013 -0400
     1.3 @@ -1407,6 +1407,23 @@
     1.4  	return hndl;
     1.5  }
     1.6  
     1.7 +static cell_t SQL_SetCharset(IPluginContext *pContext, const cell_t *params)
     1.8 +{
     1.9 +	IDatabase *db = NULL;
    1.10 +	HandleError err;
    1.11 +
    1.12 +	if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
    1.13 +		!= HandleError_None)
    1.14 +	{
    1.15 +		return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
    1.16 +	}
    1.17 +
    1.18 +	char *characterset;
    1.19 +	pContext->LocalToString(params[2], &characterset);
    1.20 +
    1.21 +	return db->SetCharacterSet(characterset);
    1.22 +}
    1.23 +
    1.24  REGISTER_NATIVES(dbNatives)
    1.25  {
    1.26  	{"SQL_BindParamInt",		SQL_BindParamInt},
    1.27 @@ -1448,6 +1465,7 @@
    1.28  	{"SQL_TQuery",				SQL_TQuery},
    1.29  	{"SQL_UnlockDatabase",		SQL_UnlockDatabase},
    1.30  	{"SQL_ConnectCustom",		SQL_ConnectCustom},
    1.31 +	{"SQL_SetCharset",		SQL_SetCharset},
    1.32  	{NULL,						NULL},
    1.33  };
    1.34  
     2.1 --- a/extensions/mysql/mysql/MyDatabase.cpp	Wed Jul 17 08:05:42 2013 -0400
     2.2 +++ b/extensions/mysql/mysql/MyDatabase.cpp	Thu Jul 18 10:27:12 2013 -0400
     2.3 @@ -326,3 +326,8 @@
     2.4  {
     2.5  	return &g_MyDriver;
     2.6  }
     2.7 +
     2.8 +bool MyDatabase::SetCharacterSet(const char *characterset)
     2.9 +{
    2.10 +	return mysql_set_character_set(m_mysql, characterset) == 0 ? true : false;
    2.11 +}
    2.12 \ No newline at end of file
     3.1 --- a/extensions/mysql/mysql/MyDatabase.h	Wed Jul 17 08:05:42 2013 -0400
     3.2 +++ b/extensions/mysql/mysql/MyDatabase.h	Thu Jul 18 10:27:12 2013 -0400
     3.3 @@ -62,6 +62,7 @@
     3.4  	IQuery *DoQueryEx(const char *query, size_t len);
     3.5  	unsigned int GetAffectedRowsForQuery(IQuery *query);
     3.6  	unsigned int GetInsertIDForQuery(IQuery *query);
     3.7 +	bool SetCharacterSet(const char *characterset);
     3.8  public:
     3.9  	const DatabaseInfo &GetInfo();
    3.10  private:
     4.1 --- a/extensions/sqlite/driver/SqDatabase.cpp	Wed Jul 17 08:05:42 2013 -0400
     4.2 +++ b/extensions/sqlite/driver/SqDatabase.cpp	Thu Jul 18 10:27:12 2013 -0400
     4.3 @@ -254,3 +254,9 @@
     4.4  {
     4.5  	return m_sq3;
     4.6  }
     4.7 +
     4.8 +bool SqDatabase::SetCharacterSet(const char *characterset)
     4.9 +{
    4.10 +	// sqlite only supports utf8 and utf16 - by the time the database is created. It's too late here.
    4.11 +	return false;
    4.12 +}
    4.13 \ No newline at end of file
     5.1 --- a/extensions/sqlite/driver/SqDatabase.h	Wed Jul 17 08:05:42 2013 -0400
     5.2 +++ b/extensions/sqlite/driver/SqDatabase.h	Thu Jul 18 10:27:12 2013 -0400
     5.3 @@ -58,6 +58,7 @@
     5.4  	IQuery *DoQueryEx(const char *query, size_t len);
     5.5  	unsigned int GetAffectedRowsForQuery(IQuery *query);
     5.6  	unsigned int GetInsertIDForQuery(IQuery *query);
     5.7 +	bool SetCharacterSet(const char *characterset);
     5.8  public:
     5.9  	sqlite3 *GetDb();
    5.10  private:
     6.1 --- a/plugins/include/dbi.inc	Wed Jul 17 08:05:42 2013 -0400
     6.2 +++ b/plugins/include/dbi.inc	Thu Jul 18 10:27:12 2013 -0400
     6.3 @@ -250,6 +250,18 @@
     6.4  native SQL_GetDriverProduct(Handle:driver, String:product[], maxlength);
     6.5  
     6.6  /**
     6.7 + * Sets the character set of the current connection. 
     6.8 + * Like SET NAMES .. in mysql, but stays after connection problems.
     6.9 + * 
    6.10 + * Example: "utf8", "latin1"
    6.11 + *
    6.12 + * @param database      Database Handle.
    6.13 + * @param characterset  The character set string to change to.
    6.14 + * @return              True, if character set was changed, false otherwise.
    6.15 + */
    6.16 +native bool:SQL_SetCharset(Handle:database, const String:charset[]);
    6.17 +
    6.18 +/**
    6.19   * Returns the number of affected rows from the last query.
    6.20   *
    6.21   * @param hndl			A database OR statement Handle.
     7.1 --- a/public/IDBDriver.h	Wed Jul 17 08:05:42 2013 -0400
     7.2 +++ b/public/IDBDriver.h	Thu Jul 18 10:27:12 2013 -0400
     7.3 @@ -42,7 +42,7 @@
     7.4   */
     7.5  
     7.6  #define SMINTERFACE_DBI_NAME		"IDBI"
     7.7 -#define SMINTERFACE_DBI_VERSION		8
     7.8 +#define SMINTERFACE_DBI_VERSION		9
     7.9  
    7.10  namespace SourceMod
    7.11  {
    7.12 @@ -600,6 +600,13 @@
    7.13  		 *						if applicable.
    7.14  		 */
    7.15  		virtual unsigned int GetInsertIDForQuery(IQuery *query) =0;
    7.16 +
    7.17 +		/**
    7.18 +		 * @brief Sets the character set of the current connection
    7.19 +		 *
    7.20 +		 * @param characterset  The characterset to switch to. e.g. "utf8".
    7.21 +		 */
    7.22 +		virtual bool SetCharacterSet(const char *characterset) =0;
    7.23  	};
    7.24  
    7.25  	/**