mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
fix: put GROUP BY before ORDER BY in Object::find_sql refs #5846
This commit is contained in:
1 parent
985180b79f
commit
a64fad72bc
2 files changed
+80
-1
No files matched your search
@@ -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} ) : () ),
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
Reference in new issue
Block a user