From a64fad72bcb78337b3c04efe07fe2a687bb7abe0 Mon Sep 17 00:00:00 2001 From: Jash Date: Sat, 14 Nov 2026 19:06:43 +0530 Subject: [PATCH] fix: put GROUP BY before ORDER BY in Object::find_sql refs #5846 --- scripts/ZoneMinder/lib/ZoneMinder/Object.pm | 2 +- tests/perl/test_object_find_group_by.pl | 79 +++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/perl/test_object_find_group_by.pl diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Object.pm b/scripts/ZoneMinder/lib/ZoneMinder/Object.pm index 1b540b97d8..69a8cb5643 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Object.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Object.pm @@ -831,8 +831,8 @@ sub find_sql { ( 'SELECT', ( $sql{distinct} ? ('DISTINCT') : () ) ), ( $sql{columns}, 'FROM', $sql{table} ), ( @{$sql{where}} ? ('WHERE', join(' AND ', @{$sql{where}})) : () ), - ( $sql{order} ? ( 'ORDER BY', $sql{order} ) : () ), ( $sql{'group by'} ? ( 'GROUP BY', $sql{'group by'} ) : () ), + ( $sql{order} ? ( 'ORDER BY', $sql{order} ) : () ), ( $sql{limit} ? ( 'LIMIT', $sql{limit}) : () ), ( $sql{offset} ? ( 'OFFSET', $sql{offset} ) : () ), ); diff --git a/tests/perl/test_object_find_group_by.pl b/tests/perl/test_object_find_group_by.pl new file mode 100644 index 0000000000..560cc3f830 --- /dev/null +++ b/tests/perl/test_object_find_group_by.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin/../../scripts/ZoneMinder/lib"; + +BEGIN { + $INC{'ZoneMinder/Base.pm'} = 1; + $INC{'ZoneMinder/Config.pm'} = 1; + $INC{'ZoneMinder/Logger.pm'} = 1; + package ZoneMinder::Base; + our $VERSION = '0'; + package ZoneMinder::Config; + our %Config; + sub import { no strict 'refs'; *{caller().'::Config'} = \%Config; } + package ZoneMinder::Logger; + our $logger = bless {}, 'ZoneMinder::Logger::Null'; + sub INFO { return 1; } + sub logLevel { return 0; } + sub Debug { return 1; } + sub Info { return 1; } + sub Warning { return 1; } + sub Error { return 1; } + sub Fatal { return 1; } + sub import { + no strict 'refs'; + my $caller = caller(); + *{$caller.'::'.$_} = \&{$_} for qw(Debug Info Warning Error Fatal INFO); + *{$caller.'::logger'} = \$logger; + } + package ZoneMinder::Logger::Null; + sub AUTOLOAD { return 1; } + sub DESTROY { } +} + +use ZoneMinder::Object; + +package ZoneMinder::Thing; +our @ISA = ('ZoneMinder::Object'); +our ($table, $primary_key, %fields, $default_sort); +$table = 'Things'; +$primary_key = 'Id'; +$default_sort = 'Id'; +%fields = map { $_ => $_ } qw(Id MonitorId Name); + +package main; + +my $failures = 0; +my $passes = 0; + +sub check { + my ($name, $got, $want) = @_; + if ((defined $got ? $got : '') eq (defined $want ? $want : '')) { + $passes++; + print "ok - $name\n"; + } else { + $failures++; + print "FAIL - $name\n"; + print ' got: '.(defined $got ? $got : '(undef)')."\n"; + print ' want: '.(defined $want ? $want : '(undef)')."\n"; + } +} + +sub sql { + my $sql = ZoneMinder::Thing->find_sql(@_); + return $$sql{sql}; +} + +check('a plain search', + sql(Name => 'a'), 'SELECT * FROM Things WHERE `Name`=? ORDER BY Id'); +check('GROUP BY comes before ORDER BY', + sql(columns => 'MonitorId', 'group by' => 'MonitorId', order => 'MonitorId', limit => 2), + 'SELECT MonitorId FROM Things GROUP BY MonitorId ORDER BY MonitorId LIMIT 2'); +check('GROUP BY with the default sort', + sql(columns => 'MonitorId', 'group by' => 'MonitorId'), + 'SELECT MonitorId FROM Things GROUP BY MonitorId ORDER BY Id'); + +print "\n$passes passed, $failures failed\n"; +exit($failures ? 1 : 0);