deprecate zmdbFetchOne in favor of zmDbRow

This commit is contained in:
Isaac Connor 2016-04-20 11:56:58 -04:00
parent 2fcdee87d9
commit 44f32f46b7
6 changed files with 62 additions and 27 deletions

View File

@ -117,7 +117,8 @@ void zmLoadConfig()
Debug( 1, "Fetching ZM_SERVER_ID For Name = %s", staticConfig.SERVER_NAME.c_str() ); Debug( 1, "Fetching ZM_SERVER_ID For Name = %s", staticConfig.SERVER_NAME.c_str() );
std::string sql = stringtf("SELECT Id FROM Servers WHERE Name='%s'", staticConfig.SERVER_NAME.c_str() ); std::string sql = stringtf("SELECT Id FROM Servers WHERE Name='%s'", staticConfig.SERVER_NAME.c_str() );
if ( MYSQL_ROW dbrow = zmDbFetchOne( sql.c_str() ) ) { zmDbRow dbrow;
if ( dbrow.fetch( sql.c_str() ) ) {
staticConfig.SERVER_ID = atoi(dbrow[0]); staticConfig.SERVER_ID = atoi(dbrow[0]);
} else { } else {
Fatal("Can't get ServerId for Server %s", staticConfig.SERVER_NAME.c_str() ); Fatal("Can't get ServerId for Server %s", staticConfig.SERVER_NAME.c_str() );
@ -128,7 +129,8 @@ void zmLoadConfig()
Debug( 1, "Fetching ZM_SERVER_NAME For Id = %d", staticConfig.SERVER_ID ); Debug( 1, "Fetching ZM_SERVER_NAME For Id = %d", staticConfig.SERVER_ID );
std::string sql = stringtf("SELECT Name FROM Servers WHERE Id='%d'", staticConfig.SERVER_ID ); std::string sql = stringtf("SELECT Name FROM Servers WHERE Id='%d'", staticConfig.SERVER_ID );
if ( MYSQL_ROW dbrow = zmDbFetchOne( sql.c_str() ) ) { zmDbRow dbrow;
if ( dbrow.fetch( sql.c_str() ) ) {
staticConfig.SERVER_NAME = std::string(dbrow[0]); staticConfig.SERVER_NAME = std::string(dbrow[0]);
} else { } else {
Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID ); Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID );

View File

@ -95,20 +95,38 @@ MYSQL_RES * zmDbFetch( const char * query ) {
return result; return result;
} // end MYSQL_RES * zmDbFetch( const char * query ); } // end MYSQL_RES * zmDbFetch( const char * query );
MYSQL_ROW zmDbFetchOne( const char *query ) { zmDbRow *zmDbFetchOne( const char *query ) {
MYSQL_RES *result = zmDbFetch( query ); zmDbRow *row = new zmDbRow();
int n_rows = mysql_num_rows( result ); if ( row->fetch( query ) ) {
return row;
}
delete row;
return NULL;
}
MYSQL_RES *zmDbRow::fetch( const char *query ) {
result_set = zmDbFetch( query );
if ( ! result_set ) return result_set;
int n_rows = mysql_num_rows( result_set );
if ( n_rows != 1 ) { if ( n_rows != 1 ) {
Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query ); Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query );
mysql_free_result( result ); mysql_free_result( result_set );
return NULL; result_set = NULL;
return result_set;
} }
MYSQL_ROW dbrow = mysql_fetch_row( result ); row = mysql_fetch_row( result_set );
if ( ! dbrow ) { if ( ! row ) {
mysql_free_result( result ); mysql_free_result( result_set );
result_set = NULL;
Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) ); Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) );
return NULL; } else {
Debug(3, "Succes");
} }
return dbrow; return result_set;
}
zmDbRow::~zmDbRow() {
if ( result_set )
mysql_free_result( result_set );
} }

View File

@ -22,6 +22,21 @@
#include <mysql/mysql.h> #include <mysql/mysql.h>
class zmDbRow {
private:
MYSQL_RES *result_set;
MYSQL_ROW row;
public:
zmDbRow() { result_set = NULL; row = NULL; };
MYSQL_RES *fetch( const char *query );
zmDbRow( MYSQL_RES *, MYSQL_ROW *row );
~zmDbRow();
char *operator[](unsigned int index) const {
return row[index];
}
};
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -33,7 +48,7 @@ void zmDbConnect();
void zmDbClose(); void zmDbClose();
MYSQL_RES * zmDbFetch( const char *query ); MYSQL_RES * zmDbFetch( const char *query );
MYSQL_ROW zmDbFetchOne( const char *query ); zmDbRow *zmDbFetchOne( const char *query );
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

View File

@ -594,8 +594,8 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
//Keep the last keyframe so we can establish immediate video //Keep the last keyframe so we can establish immediate video
if(packet.flags & AV_PKT_FLAG_KEY) { if(packet.flags & AV_PKT_FLAG_KEY) {
Debug(4, "Have keyframe"); //Debug(4, "Have keyframe");
av_copy_packet(&lastKeyframePkt, &packet); //av_copy_packet(&lastKeyframePkt, &packet);
//TODO I think we need to store the key frame location for seeking as part of the event //TODO I think we need to store the key frame location for seeking as part of the event
} }

View File

@ -1119,8 +1119,8 @@ void Monitor::DumpZoneImage( const char *zone_string ) {
Debug(3, "Trying to load from event"); Debug(3, "Trying to load from event");
// Grab the most revent event image // Grab the most revent event image
std::string sql = stringtf( "SELECT MAX(Id) FROM Events WHERE MonitorId=%d AND Frames > 0", id ); std::string sql = stringtf( "SELECT MAX(Id) FROM Events WHERE MonitorId=%d AND Frames > 0", id );
MYSQL_ROW eventid_row = zmDbFetchOne(sql.c_str() ); zmDbRow eventid_row;
if ( eventid_row ) { if ( eventid_row.fetch( sql.c_str() ) ) {
int event_id = atoi( eventid_row[0] ); int event_id = atoi( eventid_row[0] );
Debug( 3, "Got event %d", event_id ); Debug( 3, "Got event %d", event_id );
@ -2770,8 +2770,8 @@ int Monitor::LoadFfmpegMonitors( const char *file, Monitor **&monitors, Purpose
Monitor *Monitor::Load( unsigned int p_id, bool load_zones, Purpose purpose ) { Monitor *Monitor::Load( unsigned int p_id, bool load_zones, Purpose purpose ) {
std::string sql = stringtf( "select Id, Name, ServerId, StorageId, Type, Function+0, Enabled, LinkedMonitors, Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, Protocol, Method, Host, Port, Path, Options, User, Pass, Width, Height, Colours, Palette, Orientation+0, Deinterlacing, RTSPDescribe, SaveJPEGs, VideoWriter, EncoderParameters, RecordAudio, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, LabelSize, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MotionFrameSkip, AnalysisFPS, AnalysisUpdateDelay, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, AlarmRefBlendPerc, TrackMotion, SignalCheckColour, Exif from Monitors where Id = %d", p_id ); std::string sql = stringtf( "select Id, Name, ServerId, StorageId, Type, Function+0, Enabled, LinkedMonitors, Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, Protocol, Method, Host, Port, Path, Options, User, Pass, Width, Height, Colours, Palette, Orientation+0, Deinterlacing, RTSPDescribe, SaveJPEGs, VideoWriter, EncoderParameters, RecordAudio, Brightness, Contrast, Hue, Colour, EventPrefix, LabelFormat, LabelX, LabelY, LabelSize, ImageBufferCount, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, SectionLength, FrameSkip, MotionFrameSkip, AnalysisFPS, AnalysisUpdateDelay, MaxFPS, AlarmMaxFPS, FPSReportInterval, RefBlendPerc, AlarmRefBlendPerc, TrackMotion, SignalCheckColour, Exif from Monitors where Id = %d", p_id );
MYSQL_ROW dbrow = zmDbFetchOne( sql.c_str() ); zmDbRow dbrow;
if ( ! dbrow ) { if ( ! dbrow.fetch( sql.c_str() ) ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) ); Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) ); exit( mysql_errno( &dbconn ) );
} }

View File

@ -50,8 +50,8 @@ Storage::Storage( unsigned int p_id ) {
char sql[ZM_SQL_SML_BUFSIZ]; char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "SELECT Id, Name, Path from Storage WHERE Id=%d", p_id ); snprintf( sql, sizeof(sql), "SELECT Id, Name, Path from Storage WHERE Id=%d", p_id );
Debug(1,"Loading Storage for %d using %s", p_id, sql ); Debug(1,"Loading Storage for %d using %s", p_id, sql );
MYSQL_ROW dbrow = zmDbFetchOne( sql ); zmDbRow dbrow;
if ( ! dbrow ) { if ( ! dbrow.fetch( sql ) ) {
Error( "Unable to load storage area for id %d: %s", p_id, mysql_error( &dbconn ) ); Error( "Unable to load storage area for id %d: %s", p_id, mysql_error( &dbconn ) );
} else { } else {
unsigned int index = 0; unsigned int index = 0;