use zmDbDo to replace many lines of code

This commit is contained in:
Isaac Connor 2021-04-01 09:27:49 -04:00
parent 757ec2dbc1
commit 1fd9361e4e
1 changed files with 6 additions and 19 deletions

View File

@ -67,14 +67,8 @@ while (!$zm_terminate) {
my $row = $selectLogRowCountSth->fetchrow_hashref(); my $row = $selectLogRowCountSth->fetchrow_hashref();
my $logRows = $row->{Rows}; my $logRows = $row->{Rows};
if ( $logRows > $Config{ZM_LOG_DATABASE_LIMIT} ) { if ( $logRows > $Config{ZM_LOG_DATABASE_LIMIT} ) {
my $deleteLogByRowsSql = 'DELETE low_priority FROM `Logs` ORDER BY `TimeKey` ASC LIMIT ?'; my $rows = zmDbDo('DELETE low_priority FROM `Logs` ORDER BY `TimeKey` ASC LIMIT ?', $logRows - $Config{ZM_LOG_DATABASE_LIMIT});
my $deleteLogByRowsSth = $dbh->prepare_cached( $deleteLogByRowsSql ) Debug('Deleted '.$deleteLogByRowsSth->rows().' log table entries by count') if defined $rows;
or Fatal("Can't prepare '$deleteLogByRowsSql': ".$dbh->errstr());
$res = $deleteLogByRowsSth->execute( $logRows - $Config{ZM_LOG_DATABASE_LIMIT} )
or Fatal("Can't execute: ".$deleteLogByRowsSth->errstr());
if ( $deleteLogByRowsSth->rows() ) {
Debug('Deleted '.$deleteLogByRowsSth->rows().' log table entries by count');
}
} }
} else { } else {
# Time of record # Time of record
@ -83,18 +77,11 @@ while (!$zm_terminate) {
if ( $Config{ZM_LOG_DATABASE_LIMIT} =~ /^(.*)s$/ ) { if ( $Config{ZM_LOG_DATABASE_LIMIT} =~ /^(.*)s$/ ) {
$Config{ZM_LOG_DATABASE_LIMIT} = $1; $Config{ZM_LOG_DATABASE_LIMIT} = $1;
} }
my $deleted_rows; my $rows;
do { do {
my $deleteLogByTimeSql = my $rows = zmDbDo('DELETE FROM `Logs` WHERE `TimeKey` < unix_timestamp(now() - interval '.$Config{ZM_LOG_DATABASE_LIMIT}.') LIMIT 100');
'DELETE FROM `Logs` Debug("Deleted $rows log table entries by time") if defined $rows;
WHERE `TimeKey` < unix_timestamp(now() - interval '.$Config{ZM_LOG_DATABASE_LIMIT}.') LIMIT 100'; } while ($rows);
my $deleteLogByTimeSth = $dbh->prepare_cached( $deleteLogByTimeSql )
or Fatal("Can't prepare '$deleteLogByTimeSql': ".$dbh->errstr());
my $res = $deleteLogByTimeSth->execute()
or Fatal("Can't execute: ".$deleteLogByTimeSth->errstr());
$deleted_rows = $deleteLogByTimeSth->rows();
Debug("Deleted $deleted_rows log table entries by time");
} while ( $deleted_rows );
} }
} # end if ZM_LOG_DATABASE_LIMIT } # end if ZM_LOG_DATABASE_LIMIT