SQL_QuoteString can now be used without a DB handle (bug 3427, r=sawce).

This commit is contained in:
David Anderson 2009-01-08 20:02:09 +00:00
parent d97ab913cf
commit 0b4f28fecf
8 changed files with 127 additions and 46 deletions

View File

@ -519,28 +519,12 @@ static cell AMX_NATIVE_CALL SQL_NextResultSet(AMX *amx, cell *params)
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)
if (params[1] != 0)
{
IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database);
if (!pDb)
@ -549,11 +533,40 @@ static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params)
return 0;
}
if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0)
{
MF_SetAmxString(amx, params[2], buffer, params[3]);
return newsize;
} else {
return -1;
}
} else {
if (g_Mysql.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)
{
int len;
char *str = MF_FormatAmxString(amx, params, 4, &len);
size_t newsize;
static char buffer[8192];
if (params[1] != 0)
{
IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database);
if (!pDb)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]);
return 0;
}
if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0)
{
MF_SetAmxString(amx, params[2], buffer, params[3]);
@ -561,6 +574,15 @@ static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params)
} else {
return -1;
}
} else {
if (g_Mysql.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[] =

View File

@ -74,3 +74,23 @@ IDatabase *MysqlDriver::_Connect(DatabaseInfo *info, int *errcode, char *error,
return static_cast<IDatabase *>(pMysql);
}
int MysqlDriver::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize)
{
unsigned long size = static_cast<unsigned long>(strlen(str));
unsigned long needed = size*2 + 1;
if (maxlen < needed)
{
return (int)needed;
}
needed = mysql_escape_string(buffer, str, size);
if (newsize)
{
*newsize = static_cast<size_t>(needed);
}
return 0;
}

View File

@ -12,6 +12,7 @@ namespace SourceMod
IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength);
const char *NameString();
bool IsCompatDriver(const char *namestring);
int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize);
public:
IDatabase *_Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength, bool do_timeout);
};

View File

@ -519,28 +519,12 @@ static cell AMX_NATIVE_CALL SQL_NextResultSet(AMX *amx, cell *params)
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)
if (params[1] != 0)
{
IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database);
if (!pDb)
@ -549,11 +533,40 @@ static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params)
return 0;
}
if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0)
{
MF_SetAmxString(amx, params[2], buffer, params[3]);
return newsize;
} else {
return -1;
}
} else {
if (g_Sqlite.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)
{
int len;
char *str = MF_FormatAmxString(amx, params, 4, &len);
size_t newsize;
static char buffer[8192];
if (params[1] != 0)
{
IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database);
if (!pDb)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]);
return 0;
}
if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0)
{
MF_SetAmxString(amx, params[2], buffer, params[3]);
@ -561,6 +574,15 @@ static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params)
} else {
return -1;
}
} else {
if (g_Sqlite.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[] =

View File

@ -77,3 +77,4 @@ int SqliteDatabase::QuoteString(const char *str, char buffer[], size_t maxlen, s
return 0;
}

View File

@ -63,3 +63,15 @@ IDatabase *SqliteDriver::Connect(DatabaseInfo *info, int *errcode, char *error,
}
}
int SqliteDriver::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize)
{
char *res = sqlite3_snprintf(static_cast<int>(maxlen), buffer, "%q", str);
if (res != NULL && newsize != NULL)
{
*newsize = strlen(buffer);
}
return 0;
}

View File

@ -12,6 +12,7 @@ namespace SourceMod
IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength);
const char *NameString();
bool IsCompatDriver(const char *namestr);
int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize);
};
};

View File

@ -67,7 +67,8 @@ native Handle:SQL_PrepareQuery(Handle:db, const fmt[], any:...);
* Note: The buffer's maximum size should be 2*strlen(string) to catch
* all scenarios.
*
* @param db Database handle, for localization.
* @param db Database handle for localization, or Empty_Handle
* for when a handle is not available.
* @param buffer Buffer to copy to.
* @param buflen Maximum size of the buffer.
* @param string String to backquote (should not overlap buffer).
@ -80,7 +81,8 @@ native SQL_QuoteString(Handle:db, buffer[], buflen, const string[]);
* Note: The buffer's maximum size should be 2*strlen(string) to catch
* all scenarios.
*
* @param db Database handle, for localization.
* @param db Database handle for localization, or Empty_Handle
* for when a handle is not available.
* @param buffer Buffer to copy to.
* @param buflen Maximum size of the buffer.
* @param fmt Format of string to backquote (should not overlap buffer).