Added SQL_GetInsertId()
This commit is contained in:
parent
baf406cb6a
commit
9bd22661ff
@ -143,7 +143,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;
|
||||
}
|
||||
@ -414,6 +414,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_Mysql.NameString(), params[2]);
|
||||
@ -466,6 +478,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},
|
||||
|
@ -65,9 +65,10 @@ namespace SourceMod
|
||||
unsigned long long affected_rows;
|
||||
int errorcode;
|
||||
bool success;
|
||||
unsigned long long insert_id;
|
||||
};
|
||||
|
||||
class IQuery
|
||||
class IQuery
|
||||
{
|
||||
public:
|
||||
virtual ~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;
|
||||
|
@ -46,6 +46,25 @@ bool MysqlQuery::Execute(QueryInfo *info, char *error, size_t maxlength)
|
||||
return res;
|
||||
}
|
||||
|
||||
bool MysqlQuery::Execute2(QueryInfo *info, char *error, size_t maxlength)
|
||||
{
|
||||
bool res = ExecuteR(info, error, maxlength);
|
||||
|
||||
if (m_LastRes)
|
||||
m_LastRes->FreeHandle();
|
||||
|
||||
m_LastRes = (MysqlResultSet *)info->rs;
|
||||
|
||||
if (info->success)
|
||||
{
|
||||
info->insert_id = mysql_insert_id(m_pDatabase->m_pMysql);
|
||||
} else {
|
||||
info->insert_id = 0;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
const char *MysqlQuery::GetQueryString()
|
||||
{
|
||||
return m_QueryString;
|
||||
@ -91,10 +110,5 @@ bool MysqlQuery::ExecuteR(QueryInfo *info, char *error, size_t maxlength)
|
||||
}
|
||||
}
|
||||
|
||||
if (info->success && error && maxlength)
|
||||
{
|
||||
*error = '\0';
|
||||
}
|
||||
|
||||
return info->success;
|
||||
}
|
||||
|
@ -17,6 +17,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:
|
||||
MysqlDatabase *m_pDatabase;
|
||||
|
@ -105,7 +105,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;
|
||||
@ -149,7 +149,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;
|
||||
|
@ -161,7 +161,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 {
|
||||
|
@ -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 {
|
||||
|
@ -170,6 +170,14 @@ native SQL_FieldNumToName(Handle:query, num, name[], maxlength);
|
||||
*/
|
||||
native SQL_FieldNameToNum(Handle:query, const name[]);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the insert id of the last INSERT query.
|
||||
* Returns 0 otherwise.
|
||||
*/
|
||||
native SQL_GetInsertId(Handle:query);
|
||||
|
||||
|
||||
/**
|
||||
* Returns which driver this plugin is currently bound to.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user