From 63a45888a07a0b550d1e6022cd308d249b92f9cc Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 18 Feb 2021 16:01:30 -0500 Subject: [PATCH] Introduce utility functions zmDbDo and zmDbDoInsert. --- src/zm_db.cpp | 31 +++++++++++++++++++++++++++++++ src/zm_db.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/zm_db.cpp b/src/zm_db.cpp index bd3988661..bb8da3a1f 100644 --- a/src/zm_db.cpp +++ b/src/zm_db.cpp @@ -175,6 +175,37 @@ MYSQL_RES *zmDbRow::fetch(const char *query) { return result_set; } +int zmDbDo(const char *query) { + db_mutex.lock(); + int rc; + while ( rc = mysql_query(&dbconn, query) ) { + db_mutex.unlock(); + Error("Can't run query %s: %s", query, mysql_error(&dbconn)); + if ( (mysql_errno(&dbconn) != ER_LOCK_WAIT_TIMEOUT) ) + return rc; + + db_mutex.lock(); + } + db_mutex.unlock(); + return 1; +} + +int zmDbDoInsert(const char *query) { + db_mutex.lock(); + int rc; + while ( rc = mysql_query(&dbconn, query) ) { + db_mutex.unlock(); + Error("Can't run query %s: %s", query, mysql_error(&dbconn)); + if ( (mysql_errno(&dbconn) != ER_LOCK_WAIT_TIMEOUT) ) + return 0; + + db_mutex.lock(); + } + int id = mysql_insert_id(&dbconn); + db_mutex.unlock(); + return id; +} + zmDbRow::~zmDbRow() { if ( result_set ) { mysql_free_result(result_set); diff --git a/src/zm_db.h b/src/zm_db.h index d4d41cc46..22e684c67 100644 --- a/src/zm_db.h +++ b/src/zm_db.h @@ -48,6 +48,8 @@ extern bool zmDbConnected; bool zmDbConnect(); void zmDbClose(); +int zmDbDo(const char *query); +int zmDbDoInsert(const char *query); MYSQL_RES * zmDbFetch( const char *query ); zmDbRow *zmDbFetchOne( const char *query );