Added SQL_GetInsertId()
This commit is contained in:
@ -148,7 +148,7 @@ static cell AMX_NATIVE_CALL SQL_Execute(AMX *amx, cell *params)
|
||||
|
||||
memset(&qInfo->info, 0, sizeof(QueryInfo));
|
||||
|
||||
if (!qInfo->pQuery->Execute(&qInfo->info, qInfo->error, 254))
|
||||
if (!qInfo->pQuery->Execute2(&qInfo->info, qInfo->error, 254))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -419,6 +419,18 @@ static cell AMX_NATIVE_CALL SQL_FieldNameToNum(AMX *amx, cell *params)
|
||||
return columnId;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL SQL_GetInsertId(AMX *amx, cell *params)
|
||||
{
|
||||
AmxQueryInfo *qInfo = (AmxQueryInfo *)GetHandle(params[1], Handle_Query);
|
||||
if (!qInfo)
|
||||
{
|
||||
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid query handle: %d", params[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return qInfo->info.insert_id;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL SQL_GetAffinity(AMX *amx, cell *params)
|
||||
{
|
||||
return MF_SetAmxString(amx, params[1], g_Sqlite.NameString(), params[2]);
|
||||
@ -471,6 +483,7 @@ AMX_NATIVE_INFO g_BaseSqlNatives[] =
|
||||
{"SQL_FieldNameToNum", SQL_FieldNameToNum},
|
||||
{"SQL_GetAffinity", SQL_GetAffinity},
|
||||
{"SQL_SetAffinity", SQL_SetAffinity},
|
||||
{"SQL_GetInsertId", SQL_GetInsertId},
|
||||
{"SQL_GetQueryString", SQL_GetQueryString},
|
||||
|
||||
{NULL, NULL},
|
||||
|
@ -107,7 +107,7 @@ static cell AMX_NATIVE_CALL dbi_query(AMX *amx, cell *params)
|
||||
old->error[0] = '\0';
|
||||
old->errcode = 0;
|
||||
|
||||
if (!pQuery->Execute(&info, old->error, 254))
|
||||
if (!pQuery->Execute2(&info, old->error, 254))
|
||||
{
|
||||
old->errcode = info.errorcode;
|
||||
return -1;
|
||||
@ -151,7 +151,7 @@ static cell AMX_NATIVE_CALL dbi_query2(AMX *amx, cell *params)
|
||||
old->error[0] = '\0';
|
||||
old->errcode = 0;
|
||||
|
||||
if (!pQuery->Execute(&info, old->error, 254))
|
||||
if (!pQuery->Execute2(&info, old->error, 254))
|
||||
{
|
||||
old->errcode = info.errorcode;
|
||||
return -1;
|
||||
|
@ -65,6 +65,7 @@ namespace SourceMod
|
||||
unsigned long long affected_rows;
|
||||
int errorcode;
|
||||
bool success;
|
||||
unsigned long long insert_id;
|
||||
};
|
||||
|
||||
class IQuery
|
||||
@ -94,6 +95,10 @@ namespace SourceMod
|
||||
* Returns the query string.
|
||||
*/
|
||||
virtual const char *GetQueryString() =0;
|
||||
/**
|
||||
* Same as execute, but supports insert_id
|
||||
*/
|
||||
virtual bool Execute2(QueryInfo *info, char *error, size_t maxlength) =0;
|
||||
};
|
||||
|
||||
class ISQLDriver;
|
||||
|
@ -47,6 +47,25 @@ bool SqliteQuery::Execute(QueryInfo *info, char *error, size_t maxlength)
|
||||
return res;
|
||||
}
|
||||
|
||||
bool SqliteQuery::Execute2(QueryInfo *info, char *error, size_t maxlength)
|
||||
{
|
||||
bool res = ExecuteR(info, error, maxlength);
|
||||
|
||||
if (m_LastRes)
|
||||
m_LastRes->FreeHandle();
|
||||
|
||||
m_LastRes = (SqliteResultSet *)info->rs;
|
||||
|
||||
if (info->success)
|
||||
{
|
||||
info->insert_id = sqlite3_last_insert_rowid(m_pDatabase->m_pSql);
|
||||
} else {
|
||||
info->insert_id = 0;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
const char *SqliteQuery::GetQueryString()
|
||||
{
|
||||
return m_QueryString;
|
||||
|
@ -24,6 +24,7 @@ namespace SourceMod
|
||||
void FreeHandle();
|
||||
bool Execute(QueryInfo *info, char *error, size_t maxlength);
|
||||
bool ExecuteR(QueryInfo *info, char *error, size_t maxlength);
|
||||
bool Execute2(QueryInfo *info, char *error, size_t maxlength);
|
||||
const char *GetQueryString();
|
||||
private:
|
||||
SqliteDatabase *m_pDatabase;
|
||||
|
@ -157,7 +157,7 @@ void MysqlThread::RunThread(IThreadHandle *pHandle)
|
||||
} else {
|
||||
m_qrInfo.connect_success = true;
|
||||
pQuery = pDatabase->PrepareQuery(m_query.c_str());
|
||||
if (!pQuery->Execute(&m_qrInfo.amxinfo.info, m_qrInfo.amxinfo.error, 254))
|
||||
if (!pQuery->Execute2(&m_qrInfo.amxinfo.info, m_qrInfo.amxinfo.error, 254))
|
||||
{
|
||||
m_qrInfo.query_success = false;
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user