From 9bd22661ffb22ae26996ad17b489c2364cb849a9 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 28 Aug 2006 19:17:26 +0000 Subject: [PATCH] Added SQL_GetInsertId() --- dlls/mysqlx/basic_sql.cpp | 15 ++++++++++++++- dlls/mysqlx/mysql/ISQLDriver.h | 7 ++++++- dlls/mysqlx/mysql/MysqlQuery.cpp | 24 +++++++++++++++++++----- dlls/mysqlx/mysql/MysqlQuery.h | 1 + dlls/mysqlx/oldcompat_sql.cpp | 4 ++-- dlls/mysqlx/threading.cpp | 2 +- dlls/sqlite/basic_sql.cpp | 15 ++++++++++++++- dlls/sqlite/oldcompat_sql.cpp | 4 ++-- dlls/sqlite/sqlitepp/ISQLDriver.h | 5 +++++ dlls/sqlite/sqlitepp/SqliteQuery.cpp | 19 +++++++++++++++++++ dlls/sqlite/sqlitepp/SqliteQuery.h | 1 + dlls/sqlite/threading.cpp | 2 +- plugins/include/sqlx.inc | 8 ++++++++ 13 files changed, 93 insertions(+), 14 deletions(-) diff --git a/dlls/mysqlx/basic_sql.cpp b/dlls/mysqlx/basic_sql.cpp index f5d41010..cff77756 100644 --- a/dlls/mysqlx/basic_sql.cpp +++ b/dlls/mysqlx/basic_sql.cpp @@ -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}, diff --git a/dlls/mysqlx/mysql/ISQLDriver.h b/dlls/mysqlx/mysql/ISQLDriver.h index dfb83ac9..2772da7b 100644 --- a/dlls/mysqlx/mysql/ISQLDriver.h +++ b/dlls/mysqlx/mysql/ISQLDriver.h @@ -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; diff --git a/dlls/mysqlx/mysql/MysqlQuery.cpp b/dlls/mysqlx/mysql/MysqlQuery.cpp index 0057e725..9c3b9edd 100644 --- a/dlls/mysqlx/mysql/MysqlQuery.cpp +++ b/dlls/mysqlx/mysql/MysqlQuery.cpp @@ -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; } diff --git a/dlls/mysqlx/mysql/MysqlQuery.h b/dlls/mysqlx/mysql/MysqlQuery.h index ed60f602..0549c108 100644 --- a/dlls/mysqlx/mysql/MysqlQuery.h +++ b/dlls/mysqlx/mysql/MysqlQuery.h @@ -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; diff --git a/dlls/mysqlx/oldcompat_sql.cpp b/dlls/mysqlx/oldcompat_sql.cpp index 178be0ee..8967c9fb 100644 --- a/dlls/mysqlx/oldcompat_sql.cpp +++ b/dlls/mysqlx/oldcompat_sql.cpp @@ -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; diff --git a/dlls/mysqlx/threading.cpp b/dlls/mysqlx/threading.cpp index e381f2be..5375c6bf 100644 --- a/dlls/mysqlx/threading.cpp +++ b/dlls/mysqlx/threading.cpp @@ -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 { diff --git a/dlls/sqlite/basic_sql.cpp b/dlls/sqlite/basic_sql.cpp index e6940a82..d169128b 100644 --- a/dlls/sqlite/basic_sql.cpp +++ b/dlls/sqlite/basic_sql.cpp @@ -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}, diff --git a/dlls/sqlite/oldcompat_sql.cpp b/dlls/sqlite/oldcompat_sql.cpp index 7021f059..0dbbb77c 100644 --- a/dlls/sqlite/oldcompat_sql.cpp +++ b/dlls/sqlite/oldcompat_sql.cpp @@ -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; diff --git a/dlls/sqlite/sqlitepp/ISQLDriver.h b/dlls/sqlite/sqlitepp/ISQLDriver.h index dfb83ac9..c3d6afb3 100644 --- a/dlls/sqlite/sqlitepp/ISQLDriver.h +++ b/dlls/sqlite/sqlitepp/ISQLDriver.h @@ -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; diff --git a/dlls/sqlite/sqlitepp/SqliteQuery.cpp b/dlls/sqlite/sqlitepp/SqliteQuery.cpp index 092540b1..0a7439f7 100644 --- a/dlls/sqlite/sqlitepp/SqliteQuery.cpp +++ b/dlls/sqlite/sqlitepp/SqliteQuery.cpp @@ -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; diff --git a/dlls/sqlite/sqlitepp/SqliteQuery.h b/dlls/sqlite/sqlitepp/SqliteQuery.h index 0585c203..c15bcfc9 100644 --- a/dlls/sqlite/sqlitepp/SqliteQuery.h +++ b/dlls/sqlite/sqlitepp/SqliteQuery.h @@ -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; diff --git a/dlls/sqlite/threading.cpp b/dlls/sqlite/threading.cpp index 913575c7..633ea750 100644 --- a/dlls/sqlite/threading.cpp +++ b/dlls/sqlite/threading.cpp @@ -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 { diff --git a/plugins/include/sqlx.inc b/plugins/include/sqlx.inc index 9655ff62..1cac5cfd 100644 --- a/plugins/include/sqlx.inc +++ b/plugins/include/sqlx.inc @@ -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. */