Introduce utility functions zmDbDo and zmDbDoInsert.

This commit is contained in:
Isaac Connor 2021-02-18 16:01:30 -05:00
parent bc41cd944e
commit 63a45888a0
2 changed files with 33 additions and 0 deletions

View File

@ -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);

View File

@ -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 );