From 82c3807bd50d204f097ec2e7dbb0c2956bff7833 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 25 Apr 2007 13:55:56 +0000 Subject: [PATCH] implemented SQL_QuoteString and SQL_QuoteStringFmt fixed sqlx test script not working on first load --- dlls/mysqlx/basic_sql.cpp | 48 +++++++++++++++++++++++++ dlls/mysqlx/mysql/MysqlDatabase.cpp | 6 +++- dlls/sqlite/basic_sql.cpp | 47 ++++++++++++++++++++++++ dlls/sqlite/sqlitepp/SqliteDatabase.cpp | 11 +++--- plugins/testsuite/sqlxtest.sma | 9 ++--- 5 files changed, 110 insertions(+), 11 deletions(-) diff --git a/dlls/mysqlx/basic_sql.cpp b/dlls/mysqlx/basic_sql.cpp index c41808a1..073a2ae0 100644 --- a/dlls/mysqlx/basic_sql.cpp +++ b/dlls/mysqlx/basic_sql.cpp @@ -489,6 +489,52 @@ static cell AMX_NATIVE_CALL SQL_Rewind(AMX *amx, cell *params) return 1; } +static cell AMX_NATIVE_CALL SQL_QuoteString(AMX *amx, cell *params) +{ + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + int len; + char *str = MF_GetAmxString(amx, params[4], 0, &len); + size_t newsize; + static char buffer[8192]; + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } +} + +static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params) +{ + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + int len; + char *str = MF_FormatAmxString(amx, params, 4, &len); + size_t newsize; + static char buffer[8192]; + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } +} + AMX_NATIVE_INFO g_BaseSqlNatives[] = { {"SQL_MakeDbTuple", SQL_MakeDbTuple}, @@ -511,6 +557,8 @@ AMX_NATIVE_INFO g_BaseSqlNatives[] = {"SQL_GetInsertId", SQL_GetInsertId}, {"SQL_GetQueryString", SQL_GetQueryString}, {"SQL_Rewind", SQL_Rewind}, + {"SQL_QuoteString", SQL_QuoteString}, + {"SQL_QuoteStringFmt", SQL_QuoteStringFmt}, {NULL, NULL}, }; diff --git a/dlls/mysqlx/mysql/MysqlDatabase.cpp b/dlls/mysqlx/mysql/MysqlDatabase.cpp index a787a6dd..d4c0e509 100644 --- a/dlls/mysqlx/mysql/MysqlDatabase.cpp +++ b/dlls/mysqlx/mysql/MysqlDatabase.cpp @@ -69,12 +69,16 @@ int MysqlDatabase::QuoteString(const char *str, char buffer[], size_t maxlen, si unsigned long size = static_cast(strlen(str)); unsigned long needed = size*2 + 1; - if (size < needed) + if (maxlen < needed) + { return (int)needed; + } needed = mysql_real_escape_string(m_pMysql, buffer, str, size); if (newsize) + { *newsize = static_cast(needed); + } return 0; } diff --git a/dlls/sqlite/basic_sql.cpp b/dlls/sqlite/basic_sql.cpp index c09f8bb8..54dfb961 100644 --- a/dlls/sqlite/basic_sql.cpp +++ b/dlls/sqlite/basic_sql.cpp @@ -489,6 +489,51 @@ static cell AMX_NATIVE_CALL SQL_Rewind(AMX *amx, cell *params) return 1; } +static cell AMX_NATIVE_CALL SQL_QuoteString(AMX *amx, cell *params) +{ + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + int len; + char *str = MF_GetAmxString(amx, params[4], 0, &len); + size_t newsize; + static char buffer[8192]; + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } +} + +static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params) +{ + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + int len; + char *str = MF_FormatAmxString(amx, params, 4, &len); + size_t newsize; + static char buffer[8192]; + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } +} AMX_NATIVE_INFO g_BaseSqlNatives[] = { @@ -512,6 +557,8 @@ AMX_NATIVE_INFO g_BaseSqlNatives[] = {"SQL_GetInsertId", SQL_GetInsertId}, {"SQL_GetQueryString", SQL_GetQueryString}, {"SQL_Rewind", SQL_Rewind}, + {"SQL_QuoteString", SQL_QuoteString}, + {"SQL_QuoteStringFmt", SQL_QuoteStringFmt}, {NULL, NULL}, }; diff --git a/dlls/sqlite/sqlitepp/SqliteDatabase.cpp b/dlls/sqlite/sqlitepp/SqliteDatabase.cpp index 39cb02f1..e1886f77 100644 --- a/dlls/sqlite/sqlitepp/SqliteDatabase.cpp +++ b/dlls/sqlite/sqlitepp/SqliteDatabase.cpp @@ -68,13 +68,12 @@ IQuery *SqliteDatabase::PrepareQueryFmt(const char *fmt, ...) int SqliteDatabase::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize) { - unsigned long size = static_cast(strlen(str)); - unsigned long needed = size*2 + 1; + char *res = sqlite3_snprintf(static_cast(maxlen), buffer, "%q", str); - if (size < needed) - return (int)needed; - - sqlite3_snprintf(static_cast(maxlen), buffer, "%q", str); + if (res != NULL && newsize != NULL) + { + *newsize = strlen(buffer); + } return 0; } diff --git a/plugins/testsuite/sqlxtest.sma b/plugins/testsuite/sqlxtest.sma index a8833183..8c5241db 100644 --- a/plugins/testsuite/sqlxtest.sma +++ b/plugins/testsuite/sqlxtest.sma @@ -22,7 +22,8 @@ public plugin_init() get_configsdir(configsDir, 63) server_cmd("exec %s/sql.cfg", configsDir) - server_exec() + + set_task(2.0, "start_map") } DoBasicInfo(affinities=0) @@ -55,12 +56,12 @@ DoBasicInfo(affinities=0) wanted_type, res ? "Success" : "Failed") SQL_GetAffinity(affinity, 11) - plugin_cfg() + start_map() server_print("Verification: %s", affinity) } } -public plugin_cfg() +public start_map() { new host[64] new user[64] @@ -181,7 +182,7 @@ public SqlxTest_Quote() } new buffer[500], num - num = SQL_QuoteString(buffer, sizeof(buffer)-1, "Hi y'all! C\lam") + num = SQL_QuoteString(db, buffer, sizeof(buffer)-1, "Hi y'all! C\lam") server_print("num: %d str: %s", num, buffer)