From 242e5a56d8eb1beaaf7ba2415d05f4c172894860 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 14 Jan 2017 16:55:28 -0500 Subject: [PATCH 1/6] rough in the ability to filter on RunState --- db/zm_create.sql.in | 1 + src/zm_event.cpp | 8 ++++++-- web/includes/functions.php | 1 + web/lang/en_gb.php | 1 + web/skins/classic/views/filter.php | 12 ++++++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index 50c4e9012..e9df807f7 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -203,6 +203,7 @@ CREATE TABLE `Events` ( `Messaged` tinyint(3) unsigned NOT NULL default '0', `Executed` tinyint(3) unsigned NOT NULL default '0', `Notes` text, + `StateId` int(10) unsigned NOT NULL, PRIMARY KEY (`Id`,`MonitorId`), KEY `MonitorId` (`MonitorId`), KEY `StartTime` (`StartTime`), diff --git a/src/zm_event.cpp b/src/zm_event.cpp index 9b6202966..00e4e38a0 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -77,10 +77,14 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string gettimeofday( &start_time, 0 ); } - static char sql[ZM_SQL_MED_BUFSIZ]; + unsigned int state_id = 0; + if ( MYSQL_ROW dbrow = zmDbFetchOne( "SELECT Id FROM States WHERE IsActive=1" ) ) { + state_id = atoi(dbrow[0]); + } + static char sql[ZM_SQL_MED_BUFSIZ]; struct tm *stime = localtime( &start_time.tv_sec ); - snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, Name, StartTime, Width, Height, Cause, Notes ) values ( %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s' )", monitor->Id(), start_time.tv_sec, monitor->Width(), monitor->Height(), cause.c_str(), notes.c_str() ); + snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, Name, StartTime, Width, Height, Cause, Notes, StateId ) values ( %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d )", monitor->Id(), start_time.tv_sec, monitor->Width(), monitor->Height(), cause.c_str(), notes.c_str(), state_id ); if ( mysql_query( &dbconn, sql ) ) { Error( "Can't insert event: %s", mysql_error( &dbconn ) ); diff --git a/web/includes/functions.php b/web/includes/functions.php index 36a80cf2e..bd1375b73 100644 --- a/web/includes/functions.php +++ b/web/includes/functions.php @@ -1243,6 +1243,7 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&' ) { case 'MaxScore': case 'Cause': case 'Notes': + case 'StateId': case 'Archived': $filter['sql'] .= 'E.'.$filter['terms'][$i]['attr']; break; diff --git a/web/lang/en_gb.php b/web/lang/en_gb.php index 4adb1fc7f..922ca1e21 100644 --- a/web/lang/en_gb.php +++ b/web/lang/en_gb.php @@ -127,6 +127,7 @@ $SLANG = array( 'AttrMonitorName' => 'Monitor Name', 'AttrServerId' => 'Server Id', 'AttrServerName' => 'Server Name', + 'AttrStateId' => 'Run State', 'AttrName' => 'Name', 'AttrNotes' => 'Notes', 'AttrSystemLoad' => 'System Load', diff --git a/web/skins/classic/views/filter.php b/web/skins/classic/views/filter.php index 390e94ee1..465777bd3 100644 --- a/web/skins/classic/views/filter.php +++ b/web/skins/classic/views/filter.php @@ -97,6 +97,7 @@ $attrTypes = array( 'DiskPercent' => translate('AttrDiskPercent'), 'DiskBlocks' => translate('AttrDiskBlocks'), 'SystemLoad' => translate('AttrSystemLoad'), + 'StateId' => translate('AttrStateId'), 'ServerId' => translate('AttrServerId'), 'ServerName' => translate('AttrServerName'), ); @@ -217,6 +218,17 @@ for ( $i = 0; isset($_REQUEST['filter']) && $i < count($_REQUEST['filter']['term ?> + + + Date: Sat, 14 Jan 2017 16:58:22 -0500 Subject: [PATCH 2/6] add an update script to add StateId to Events table --- db/zm_update-1.30.2.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 db/zm_update-1.30.2.sql diff --git a/db/zm_update-1.30.2.sql b/db/zm_update-1.30.2.sql new file mode 100644 index 000000000..48e9b3c52 --- /dev/null +++ b/db/zm_update-1.30.2.sql @@ -0,0 +1,20 @@ +-- +-- This updates a 1.30.1 database to 1.30.2 +-- +-- Add StateId Column to Events. +-- + +SET @s = (SELECT IF( + (SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = 'Events' + AND table_schema = DATABASE() + AND column_name = 'StateId' + ) > 0, +"SELECT 'Column StateId exists in Events'", +"ALTER TABLE Events ADD `StateId` int(10) unsigned default NULL AFTER `Notes`" +)); + +PREPARE stmt FROM @s; +EXECUTE stmt; + From d24d151f53e9db4cbe920c1dc2aeacb631a9231d Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 15 Jan 2017 10:54:23 -0500 Subject: [PATCH 3/6] Slurp db update file in and do() it instead of calling mysql --- scripts/zmupdate.pl.in | 50 ++++++++++-------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/scripts/zmupdate.pl.in b/scripts/zmupdate.pl.in index cb0781c2e..b0f98bbd6 100644 --- a/scripts/zmupdate.pl.in +++ b/scripts/zmupdate.pl.in @@ -116,7 +116,7 @@ GetOptions( 'dir:s' =>\$updateDir ) or pod2usage(-exitstatus => -1); -my $dbh = zmDbConnect(); +my $dbh = zmDbConnect(undef, { mysql_multi_statements=>1 } ); $Config{ZM_DB_USER} = $dbUser; $Config{ZM_DB_PASS} = $dbPass; @@ -449,47 +449,19 @@ if ( $version ) my $dbh = shift; my $version = shift; - my ( $host, $port ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ ); - my $command = "mysql -h".$host; - $command .= " -P".$port if defined($port); - if ( $dbUser ) - { - $command .= " -u".$dbUser; - $command .= ' -p"'.$dbPass.'"' if $dbPass; - } - $command .= " ".$Config{ZM_DB_NAME}." < "; - if ( $updateDir ) - { - $command .= $updateDir; - } - else - { - $command .= $Config{ZM_PATH_DATA}."/db"; - } - $command .= "/zm_update-".$version.".sql"; + my $file = ( $updateDir ? $updateDir : $Config{ZM_PATH_DATA}.'/db' ) . "/zm_update-$version.sql"; - print( "Executing '$command'\n" ) if ( logDebugging() ); - my $output = qx($command); - my $status = $? >> 8; - if ( $status || logDebugging() ) - { - chomp( $output ); - print( "Output: $output\n" ); - } - if ( $status ) - { - die( "Command '$command' exited with status: $status\n" ); - } - else - { - print( "\nDatabase successfully upgraded to version $version.\n" ); - my $sql = "update Config set Value = ? where Name = 'ZM_DYN_DB_VERSION'"; - my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res = $sth->execute( $version ) or die( "Can't execute: ".$sth->errstr() ); - } + open( my $fh, '<', $file ) or die "Unable to open $file $!"; + $/ = undef; + my $sql = <$fh>; + close $fh; + $dbh->do($sql) or die $dbh->errstr(); + print( "\nDatabase successfully upgraded to version $version.\n" ); + my $sql = "update Config set Value = ? where Name = 'ZM_DYN_DB_VERSION'"; + my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $version ) or die( "Can't execute: ".$sth->errstr() ); } - print( "\nUpgrading database to version ".ZM_VERSION."\n" ); # Update config first of all From 4973d8c74cb8a55ad972c336227fc84eb52ec849 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 15 Jan 2017 10:54:46 -0500 Subject: [PATCH 4/6] add support for connect options --- scripts/ZoneMinder/lib/ZoneMinder/Database.pm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm index 0ab2aee3b..b2c625071 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm @@ -75,6 +75,8 @@ sub zmDbConnect { if ( $force ) { zmDbDisconnect(); } + my $options = shift; + if ( ( ! defined( $dbh ) ) or ! $dbh->ping() ) { my ( $host, $portOrSocket ) = ( $ZoneMinder::Config::Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ ); my $socket; @@ -89,7 +91,7 @@ sub zmDbConnect { $socket = ";host=".$Config{ZM_DB_HOST}; } $dbh = DBI->connect( "DBI:mysql:database=".$Config{ZM_DB_NAME} - .$socket + .$socket . ($options?'.'.join(';', map { $_.'='.$$options{$_} } keys %{$options} )) , $Config{ZM_DB_USER} , $Config{ZM_DB_PASS} ); From c79dfb6a163364769d8ad56e313fe6e2106fef14 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 15 Jan 2017 10:56:57 -0500 Subject: [PATCH 5/6] fix typos --- scripts/ZoneMinder/lib/ZoneMinder/Database.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm index b2c625071..19374543e 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Database.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Database.pm @@ -91,7 +91,7 @@ sub zmDbConnect { $socket = ";host=".$Config{ZM_DB_HOST}; } $dbh = DBI->connect( "DBI:mysql:database=".$Config{ZM_DB_NAME} - .$socket . ($options?'.'.join(';', map { $_.'='.$$options{$_} } keys %{$options} )) + .$socket . ($options?';'.join(';', map { $_.'='.$$options{$_} } keys %{$options} ) : '' ) , $Config{ZM_DB_USER} , $Config{ZM_DB_PASS} ); From 052807aaf519c60cd580f07c42c0a58900de8912 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 15 Jan 2017 10:57:39 -0500 Subject: [PATCH 6/6] remove extra my --- scripts/zmupdate.pl.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/zmupdate.pl.in b/scripts/zmupdate.pl.in index b0f98bbd6..ee1829a99 100644 --- a/scripts/zmupdate.pl.in +++ b/scripts/zmupdate.pl.in @@ -457,7 +457,7 @@ if ( $version ) close $fh; $dbh->do($sql) or die $dbh->errstr(); print( "\nDatabase successfully upgraded to version $version.\n" ); - my $sql = "update Config set Value = ? where Name = 'ZM_DYN_DB_VERSION'"; + $sql = "update Config set Value = ? where Name = 'ZM_DYN_DB_VERSION'"; my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $res = $sth->execute( $version ) or die( "Can't execute: ".$sth->errstr() ); }