Skip to content

Commit 5303f0e

Browse files
committed
mysql: add support for not null columns
1 parent baaee9c commit 5303f0e

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/Thruk/Backend/Provider/Mysql.pm

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,6 @@ sub _get_filter {
823823
$filter = " WHERE ".$filter if $filter;
824824

825825
$filter =~ s/WHERE\ \(\((.*)\)\ AND\ \)/WHERE ($1)/gmx;
826-
$filter =~ s/\Qtype = ''\E/type IS NULL/gmx;
827826
$filter =~ s/\ AND\ \)/)/gmx;
828827
$filter =~ s/\(\ AND\ \(/((/gmx;
829828
$filter =~ s/AND\s+AND/AND/gmx;
@@ -912,6 +911,7 @@ sub _get_subfilter {
912911
my $v = [values %{$inp}]->[0];
913912
($k, $v) = $self->_replace_column_name($k, $v);
914913
if($k eq 'IS NULL') { return $v.' '.$k; }
914+
if($k eq 'IS NOT NULL') { return $v.' '.$k; }
915915
if($k eq '=') { return '= '._quote($v); }
916916
if($k eq '!=') { return '!= '._quote($v); }
917917
if($k eq '~') { return 'RLIKE '._quote_backslash(_quote(Thruk::Utils::clean_regex($v))); }
@@ -983,8 +983,22 @@ sub _replace_column_name {
983983
return("c.name", $val);
984984
}
985985

986-
if($col eq 'service_description' && !defined $val) {
987-
return("IS NULL", $col);
986+
if($col eq 'service_description' || $col eq 'type') {
987+
if(ref $val eq 'HASH') {
988+
my @keys = keys %{$val};
989+
if(scalar @keys == 1) {
990+
$op = $keys[0];
991+
$val = $val->{$op};
992+
}
993+
}
994+
if(!defined $val) {
995+
if($op eq '=') {
996+
return("IS NULL", $col);
997+
}
998+
if($op eq '!=') {
999+
return("IS NOT NULL", $col);
1000+
}
1001+
}
9881002
}
9891003

9901004
# using ids makes mysql prefer index

t/100-backend_mysql.t

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use warnings;
22
use strict;
33
use Data::Dumper;
4-
use Test::More tests => 72;
4+
use Test::More tests => 74;
55

66
$Data::Dumper::Sortkeys = 1;
77

@@ -183,7 +183,21 @@ test_filter(
183183
test_filter(
184184
'hash list',
185185
[{ '-or' => [ { 'service_description' => { '!=' => undef } }, { 'service_description' => undef } ] }, 'service_description', undef ],
186-
" WHERE ((service_description != '' OR service_description = '') AND service_description = '')"
186+
" WHERE ((service_description IS NOT NULL OR service_description IS NULL) AND service_description IS NULL)"
187+
);
188+
189+
#####################################################################
190+
test_filter(
191+
'scalar list',
192+
[ 'type', undef ],
193+
" WHERE type IS NULL",
194+
);
195+
196+
#####################################################################
197+
test_filter(
198+
'scalar list',
199+
[ { 'type' => { '!=' => undef } } ],
200+
" WHERE type IS NOT NULL",
187201
);
188202

189203
#####################################################################
@@ -204,7 +218,7 @@ test_filter(
204218
test_filter(
205219
'tripple filter',
206220
{ '-and' => [ { 'type' => 'SERVICE ALERT' }, { 'service_description' => { '!=' => undef }, 'state' => 1 } ] },
207-
" WHERE (type = 'SERVICE ALERT' AND (service_description != '' AND state = 1))"
221+
" WHERE (type = 'SERVICE ALERT' AND (service_description IS NOT NULL AND state = 1))"
208222
);
209223

210224
#####################################################################

0 commit comments

Comments
 (0)