Handle PostSQL Conditions when generating SQL

This commit is contained in:
Isaac Connor 2020-09-29 15:02:46 -04:00
parent 3024c46dbb
commit f2e4d1dd5c
1 changed files with 97 additions and 90 deletions

View File

@ -205,6 +205,8 @@ sub Sql {
# #
} elsif ( $term->{attr} eq 'ExistsInFileSystem' ) { } elsif ( $term->{attr} eq 'ExistsInFileSystem' ) {
push @{$self->{PostSQLConditions}}, $term; push @{$self->{PostSQLConditions}}, $term;
$self->{Sql} .= 'TRUE /* ExistsInFileSystem */';
} elsif ( $term->{attr} eq 'DiskSpace' ) { } elsif ( $term->{attr} eq 'DiskSpace' ) {
$self->{Sql} .= 'E.DiskSpace'; $self->{Sql} .= 'E.DiskSpace';
} elsif ( $term->{attr} eq 'DiskPercent' ) { } elsif ( $term->{attr} eq 'DiskPercent' ) {
@ -223,109 +225,114 @@ sub Sql {
$self->{Sql} .= 'E.'.$term->{attr}; $self->{Sql} .= 'E.'.$term->{attr};
} }
( my $stripped_value = $value ) =~ s/^["\']+?(.+)["\']+?$/$1/; if ( $term->{attr} eq 'ExistsInFileSystem' ) {
foreach my $temp_value ( split( /["'\s]*?,["'\s]*?/, $stripped_value ) ) { # PostCondition, so no further SQL
} else {
if ( $term->{attr} eq 'AlarmedZoneId' ) { ( my $stripped_value = $value ) =~ s/^["\']+?(.+)["\']+?$/$1/;
$value = '(SELECT * FROM Stats WHERE EventId=E.Id AND ZoneId='.$value.')'; foreach my $temp_value ( split( /["'\s]*?,["'\s]*?/, $stripped_value ) ) {
} elsif ( $term->{attr} =~ /^MonitorName/ ) {
$value = "'$temp_value'"; if ( $term->{attr} eq 'AlarmedZoneId' ) {
} elsif ( $term->{attr} =~ /ServerId/) { $value = '(SELECT * FROM Stats WHERE EventId=E.Id AND ZoneId='.$value.')';
Debug("ServerId, temp_value is ($temp_value) ($ZoneMinder::Config::Config{ZM_SERVER_ID})"); } elsif ( $term->{attr} =~ /^MonitorName/ ) {
if ( $temp_value eq 'ZM_SERVER_ID' ) {
$value = "'$ZoneMinder::Config::Config{ZM_SERVER_ID}'";
# This gets used later, I forget for what
$$self{Server} = new ZoneMinder::Server($ZoneMinder::Config::Config{ZM_SERVER_ID});
} elsif ( $temp_value eq 'NULL' ) {
$value = $temp_value;
} else {
$value = "'$temp_value'"; $value = "'$temp_value'";
# This gets used later, I forget for what } elsif ( $term->{attr} =~ /ServerId/) {
$$self{Server} = new ZoneMinder::Server($temp_value); Debug("ServerId, temp_value is ($temp_value) ($ZoneMinder::Config::Config{ZM_SERVER_ID})");
} if ( $temp_value eq 'ZM_SERVER_ID' ) {
} elsif ( $term->{attr} eq 'StorageId' ) { $value = "'$ZoneMinder::Config::Config{ZM_SERVER_ID}'";
$value = "'$temp_value'"; # This gets used later, I forget for what
$$self{Storage} = new ZoneMinder::Storage($temp_value); $$self{Server} = new ZoneMinder::Server($ZoneMinder::Config::Config{ZM_SERVER_ID});
} elsif ( $term->{attr} eq 'Name' } elsif ( $temp_value eq 'NULL' ) {
$value = $temp_value;
} else {
$value = "'$temp_value'";
# This gets used later, I forget for what
$$self{Server} = new ZoneMinder::Server($temp_value);
}
} elsif ( $term->{attr} eq 'StorageId' ) {
$value = "'$temp_value'";
$$self{Storage} = new ZoneMinder::Storage($temp_value);
} elsif ( $term->{attr} eq 'Name'
|| $term->{attr} eq 'Cause' || $term->{attr} eq 'Cause'
|| $term->{attr} eq 'Notes' || $term->{attr} eq 'Notes'
) { ) {
if ( $term->{op} eq 'LIKE' if ( $term->{op} eq 'LIKE'
|| $term->{op} eq 'NOT LIKE' || $term->{op} eq 'NOT LIKE'
) { ) {
$temp_value = '%'.$temp_value.'%' if $temp_value !~ /%/; $temp_value = '%'.$temp_value.'%' if $temp_value !~ /%/;
}
$value = "'$temp_value'";
} elsif ( $term->{attr} eq 'DateTime' or $term->{attr} eq 'StartDateTime' or $term->{attr} eq 'EndDateTime' ) {
if ( $temp_value eq 'NULL' ) {
$value = $temp_value;
} else {
$value = DateTimeToSQL($temp_value);
if ( !$value ) {
Error("Error parsing date/time '$temp_value', skipping filter '$self->{Name}'");
return;
}
$value = "'$value'";
}
} elsif ( $term->{attr} eq 'Date' or $term->{attr} eq 'StartDate' or $term->{attr} eq 'EndDate' ) {
if ( $temp_value eq 'NULL' ) {
$value = $temp_value;
} elsif ( $temp_value eq 'CURDATE()' or $temp_value eq 'NOW()' ) {
$value = 'to_days('.$temp_value.')';
} else {
$value = DateTimeToSQL($temp_value);
if ( !$value ) {
Error("Error parsing date/time '$temp_value', skipping filter '$self->{Name}'");
return;
}
$value = "to_days( '$value' )";
}
} elsif ( $term->{attr} eq 'Time' or $term->{attr} eq 'StartTime' or $term->{attr} eq 'EndTime' ) {
if ( $temp_value eq 'NULL' ) {
$value = $temp_value;
} else {
$value = DateTimeToSQL($temp_value);
if ( !$value ) {
Error("Error parsing date/time '$temp_value', skipping filter '$self->{Name}'");
return;
}
$value = "extract( hour_second from '$value' )";
} }
$value = "'$temp_value'";
} elsif ( $term->{attr} eq 'DateTime' or $term->{attr} eq 'StartDateTime' or $term->{attr} eq 'EndDateTime' ) {
if ( $temp_value eq 'NULL' ) {
$value = $temp_value;
} else { } else {
$value = DateTimeToSQL($temp_value);
if ( !$value ) {
Error("Error parsing date/time '$temp_value', skipping filter '$self->{Name}'");
return;
}
$value = "'$value'";
}
} elsif ( $term->{attr} eq 'Date' or $term->{attr} eq 'StartDate' or $term->{attr} eq 'EndDate' ) {
if ( $temp_value eq 'NULL' ) {
$value = $temp_value; $value = $temp_value;
} elsif ( $temp_value eq 'CURDATE()' or $temp_value eq 'NOW()' ) {
$value = 'to_days('.$temp_value.')';
} else {
$value = DateTimeToSQL($temp_value);
if ( !$value ) {
Error("Error parsing date/time '$temp_value', skipping filter '$self->{Name}'");
return;
}
$value = "to_days( '$value' )";
} }
} elsif ( $term->{attr} eq 'Time' or $term->{attr} eq 'StartTime' or $term->{attr} eq 'EndTime' ) { push @value_list, $value;
if ( $temp_value eq 'NULL' ) { } # end foreach temp_value
$value = $temp_value; } # end if has an attr
if ( $term->{op} ) {
if ( $term->{op} eq '=~' ) {
$self->{Sql} .= ' REGEXP '.$value;
} elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= ' NOT REGEXP '.$value;
} elsif ( $term->{op} eq 'IS' ) {
if ( $value eq 'Odd' ) {
$self->{Sql} .= ' % 2 = 1';
} elsif ( $value eq 'Even' ) {
$self->{Sql} .= ' % 2 = 0';
} else { } else {
$value = DateTimeToSQL($temp_value); $self->{Sql} .= " IS $value";
if ( !$value ) {
Error("Error parsing date/time '$temp_value', skipping filter '$self->{Name}'");
return;
}
$value = "extract( hour_second from '$value' )";
} }
} elsif ( $term->{op} eq 'EXISTS' ) {
$self->{Sql} .= ' EXISTS '.$value;
} elsif ( $term->{op} eq 'IS NOT' ) {
$self->{Sql} .= ' IS NOT '.$value;
} elsif ( $term->{op} eq '=[]' ) {
$self->{Sql} .= ' IN ('.join(',', @value_list).')';
} elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= ' NOT IN ('.join(',', @value_list).')';
} elsif ( $term->{op} eq 'LIKE' ) {
$self->{Sql} .= ' LIKE '.$value;
} elsif ( $term->{op} eq 'NOT LIKE' ) {
$self->{Sql} .= ' NOT LIKE '.$value;
} else { } else {
$value = $temp_value; $self->{Sql} .= ' '.$term->{op}.' '.$value;
} }
push @value_list, $value; } # end if has an operator
} # end foreach temp_value } # end if Pre/Post or SQL
} # end if has an attr
if ( $term->{op} ) {
if ( $term->{op} eq '=~' ) {
$self->{Sql} .= " regexp $value";
} elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= " not regexp $value";
} elsif ( $term->{op} eq 'IS' ) {
if ( $value eq 'Odd' ) {
$self->{Sql} .= ' % 2 = 1';
} elsif ( $value eq 'Even' ) {
$self->{Sql} .= ' % 2 = 0';
} else {
$self->{Sql} .= " IS $value";
}
} elsif ( $term->{op} eq 'EXISTS' ) {
$self->{Sql} .= " EXISTS $value";
} elsif ( $term->{op} eq 'IS NOT' ) {
$self->{Sql} .= " IS NOT $value";
} elsif ( $term->{op} eq '=[]' ) {
$self->{Sql} .= ' IN ('.join(',', @value_list).')';
} elsif ( $term->{op} eq '!~' ) {
$self->{Sql} .= ' NOT IN ('.join(',', @value_list).')';
} elsif ( $term->{op} eq 'LIKE' ) {
$self->{Sql} .= " LIKE $value";
} elsif ( $term->{op} eq 'NOT LIKE' ) {
$self->{Sql} .= " NOT LIKE $value";
} else {
$self->{Sql} .= ' '.$term->{op}.' '.$value;
}
} # end if has an operator
if ( exists($term->{cbr}) ) { if ( exists($term->{cbr}) ) {
$self->{Sql} .= ' '.str_repeat(')', $term->{cbr}).' '; $self->{Sql} .= ' '.str_repeat(')', $term->{cbr}).' ';
} }