2003-03-26 20:03:37 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder MySQL Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-03-26 20:03:37 +08:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2003-03-26 20:03:37 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-09-07 23:39:44 +08:00
|
|
|
#include <string.h>
|
2003-03-26 20:03:37 +08:00
|
|
|
|
2003-05-16 18:27:41 +08:00
|
|
|
#include "zm.h"
|
2003-03-26 20:03:37 +08:00
|
|
|
#include "zm_db.h"
|
|
|
|
|
2018-03-02 11:20:29 +08:00
|
|
|
// From what I read, we need one of these per thread
|
2003-03-26 20:03:37 +08:00
|
|
|
MYSQL dbconn;
|
2018-03-02 11:20:29 +08:00
|
|
|
Mutex db_mutex;
|
2003-03-26 20:03:37 +08:00
|
|
|
|
2018-03-02 11:20:29 +08:00
|
|
|
bool zmDbConnected = false;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-03-02 11:20:29 +08:00
|
|
|
bool zmDbConnect() {
|
2017-12-04 01:50:57 +08:00
|
|
|
// For some reason having these lines causes memory corruption and crashing on newer debian/ubuntu
|
|
|
|
//if ( zmDbConnected )
|
|
|
|
//return;
|
2017-07-07 05:45:23 +08:00
|
|
|
|
2018-03-09 20:22:59 +08:00
|
|
|
if ( !mysql_init(&dbconn) ) {
|
|
|
|
Error("Can't initialise database connection: %s", mysql_error(&dbconn));
|
2018-03-02 11:20:29 +08:00
|
|
|
return false;
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
|
|
|
my_bool reconnect = 1;
|
2018-03-22 22:25:47 +08:00
|
|
|
if ( mysql_options(&dbconn, MYSQL_OPT_RECONNECT, &reconnect) )
|
|
|
|
Error("Can't set database auto reconnect option: %s", mysql_error(&dbconn));
|
2017-08-14 22:30:42 +08:00
|
|
|
if ( !staticConfig.DB_SSL_CA_CERT.empty() )
|
2018-03-22 22:25:47 +08:00
|
|
|
mysql_ssl_set(&dbconn,
|
2018-03-02 11:20:29 +08:00
|
|
|
staticConfig.DB_SSL_CLIENT_KEY.c_str(),
|
|
|
|
staticConfig.DB_SSL_CLIENT_CERT.c_str(),
|
|
|
|
staticConfig.DB_SSL_CA_CERT.c_str(),
|
2018-03-22 22:25:47 +08:00
|
|
|
NULL, NULL);
|
|
|
|
std::string::size_type colonIndex = staticConfig.DB_HOST.find(":");
|
2017-07-07 05:45:23 +08:00
|
|
|
if ( colonIndex == std::string::npos ) {
|
2018-03-22 22:25:47 +08:00
|
|
|
if ( !mysql_real_connect(&dbconn, staticConfig.DB_HOST.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, NULL, 0) ) {
|
|
|
|
Error( "Can't connect to server: %s", mysql_error(&dbconn));
|
2018-03-02 11:20:29 +08:00
|
|
|
return false;
|
2007-09-07 23:39:44 +08:00
|
|
|
}
|
2017-07-07 05:45:23 +08:00
|
|
|
} else {
|
2016-05-22 23:08:12 +08:00
|
|
|
std::string dbHost = staticConfig.DB_HOST.substr( 0, colonIndex );
|
|
|
|
std::string dbPortOrSocket = staticConfig.DB_HOST.substr( colonIndex+1 );
|
2017-07-07 05:45:23 +08:00
|
|
|
if ( dbPortOrSocket[0] == '/' ) {
|
2018-03-22 22:25:47 +08:00
|
|
|
if ( !mysql_real_connect(&dbconn, NULL, staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, dbPortOrSocket.c_str(), 0) ) {
|
|
|
|
Error("Can't connect to server: %s", mysql_error(&dbconn));
|
2018-03-02 11:20:29 +08:00
|
|
|
return false;
|
2016-05-22 23:08:12 +08:00
|
|
|
}
|
2017-07-07 05:45:23 +08:00
|
|
|
} else {
|
|
|
|
if ( !mysql_real_connect( &dbconn, dbHost.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, atoi(dbPortOrSocket.c_str()), NULL, 0 ) ) {
|
2016-05-22 23:08:12 +08:00
|
|
|
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
2018-03-02 11:20:29 +08:00
|
|
|
return false;
|
2016-05-22 23:08:12 +08:00
|
|
|
}
|
2007-09-07 23:39:44 +08:00
|
|
|
}
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
2017-07-07 05:45:23 +08:00
|
|
|
if ( mysql_select_db( &dbconn, staticConfig.DB_NAME.c_str() ) ) {
|
2016-04-29 21:11:14 +08:00
|
|
|
Error( "Can't select database: %s", mysql_error( &dbconn ) );
|
2018-03-22 22:25:47 +08:00
|
|
|
return false;
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
|
|
|
zmDbConnected = true;
|
2018-03-02 11:20:29 +08:00
|
|
|
return zmDbConnected;
|
2003-03-26 20:03:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 05:45:23 +08:00
|
|
|
void zmDbClose() {
|
|
|
|
if ( zmDbConnected ) {
|
2018-03-22 22:25:47 +08:00
|
|
|
db_mutex.lock();
|
2016-04-29 21:11:14 +08:00
|
|
|
mysql_close( &dbconn );
|
|
|
|
// mysql_init() call implicitly mysql_library_init() but
|
|
|
|
// mysql_close() does not call mysql_library_end()
|
|
|
|
mysql_library_end();
|
|
|
|
zmDbConnected = false;
|
2018-03-22 22:25:47 +08:00
|
|
|
db_mutex.unlock();
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
2014-11-15 05:17:44 +08:00
|
|
|
}
|
2015-07-17 01:04:28 +08:00
|
|
|
|
2018-03-22 22:25:47 +08:00
|
|
|
MYSQL_RES * zmDbFetch(const char * query) {
|
2016-04-29 21:11:14 +08:00
|
|
|
if ( ! zmDbConnected ) {
|
2018-03-22 22:25:47 +08:00
|
|
|
Error("Not connected.");
|
2016-04-29 21:11:14 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-02 11:20:29 +08:00
|
|
|
db_mutex.lock();
|
2015-07-17 01:04:28 +08:00
|
|
|
|
2018-03-22 22:25:47 +08:00
|
|
|
if ( mysql_query(&dbconn, query) ) {
|
|
|
|
Error("Can't run query: %s", mysql_error(&dbconn));
|
|
|
|
db_mutex.unlock();
|
2016-04-29 21:11:14 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-22 22:25:47 +08:00
|
|
|
Debug(4, "Success running query: %s", query);
|
|
|
|
MYSQL_RES *result = mysql_store_result(&dbconn);
|
2016-04-29 21:11:14 +08:00
|
|
|
if ( !result ) {
|
2018-03-22 22:25:47 +08:00
|
|
|
Error("Can't use query result: %s for query %s", mysql_error(&dbconn), query);
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
2018-03-02 11:20:29 +08:00
|
|
|
db_mutex.unlock();
|
2016-04-29 21:11:14 +08:00
|
|
|
return result;
|
2015-07-17 01:04:28 +08:00
|
|
|
} // end MYSQL_RES * zmDbFetch( const char * query );
|
|
|
|
|
2016-04-20 23:56:58 +08:00
|
|
|
zmDbRow *zmDbFetchOne( const char *query ) {
|
2016-04-29 21:11:14 +08:00
|
|
|
zmDbRow *row = new zmDbRow();
|
|
|
|
if ( row->fetch( query ) ) {
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
delete row;
|
|
|
|
return NULL;
|
2016-04-20 23:56:58 +08:00
|
|
|
}
|
2015-07-17 01:04:28 +08:00
|
|
|
|
2016-04-20 23:56:58 +08:00
|
|
|
MYSQL_RES *zmDbRow::fetch( const char *query ) {
|
2016-04-29 21:11:14 +08:00
|
|
|
result_set = zmDbFetch( query );
|
|
|
|
if ( ! result_set ) return result_set;
|
2016-04-20 23:56:58 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
int n_rows = mysql_num_rows( result_set );
|
|
|
|
if ( n_rows != 1 ) {
|
|
|
|
Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query );
|
|
|
|
mysql_free_result( result_set );
|
|
|
|
result_set = NULL;
|
2016-04-20 23:56:58 +08:00
|
|
|
return result_set;
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
row = mysql_fetch_row( result_set );
|
|
|
|
if ( ! row ) {
|
|
|
|
mysql_free_result( result_set );
|
|
|
|
result_set = NULL;
|
|
|
|
Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) );
|
|
|
|
} else {
|
2017-09-26 04:21:46 +08:00
|
|
|
Debug(5, "Success");
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
|
|
|
return result_set;
|
2016-04-20 23:56:58 +08:00
|
|
|
}
|
|
|
|
zmDbRow::~zmDbRow() {
|
2018-03-22 22:25:47 +08:00
|
|
|
if ( result_set ) {
|
|
|
|
mysql_free_result(result_set);
|
|
|
|
result_set = NULL;
|
|
|
|
}
|
2015-07-17 01:04:28 +08:00
|
|
|
}
|