implemented SQL_QuoteString and SQL_QuoteStringFmt

fixed sqlx test script not working on first load
This commit is contained in:
David Anderson
2007-04-25 13:55:56 +00:00
parent 58ad23186b
commit 82c3807bd5
5 changed files with 110 additions and 11 deletions

View File

@ -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},
};

View File

@ -69,12 +69,16 @@ int MysqlDatabase::QuoteString(const char *str, char buffer[], size_t maxlen, si
unsigned long size = static_cast<unsigned long>(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<size_t>(needed);
}
return 0;
}