Merge pull request #2522 from f0o/alert_expansion_fix

Fix Glue-Expansion on alerts
This commit is contained in:
Neil Lathwood
2015-12-04 09:26:37 +00:00
+4 -8
View File
@@ -59,12 +59,6 @@ function GenSQL($rule) {
$join = "";
while( $i < $x ) {
if( isset($tables[$i+1]) ) {
if( dbFetchCell('SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_NAME = ? && COLUMN_NAME = ?',array($tables[$i+1],'device_id')) == 1 ) {
//Found valid glue in definition.
$join .= $tables[$i].".device_id = ".$tables[$i+1].".device_id && ";
}
else {
//No valid glue, Resolve a glue-chain.
$gtmp = ResolveGlues(array($tables[$i+1]),'device_id');
if( $gtmp === false ) {
//Cannot resolve glue-chain. Rule is invalid.
@@ -88,7 +82,6 @@ function GenSQL($rule) {
}
$join .= "( ".$qry.$tables[0].".device_id ) && ";
}
}
$i++;
}
$sql = "SELECT * FROM ".implode(",",$tables)." WHERE (".$join."".str_replace("(","",$tables[0]).".device_id = ?) && (".str_replace(array("%","@","!~","~"),array("",".*","NOT REGEXP","REGEXP"),$rule).")";
@@ -123,8 +116,11 @@ function ResolveGlues($tables,$target,$x=0,$hist=array(),$last=array()) {
$glues = dbFetchRows('SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ? && COLUMN_NAME LIKE "%\_id"',array($table));
if( sizeof($glues) == 1 && $glues[0]['COLUMN_NAME'] != $target ) {
//Search for new candidates to expand
$tmp = dbFetchRows('SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE "'.substr($table,0,-1).'_%" && TABLE_NAME != "'.$table.'"');
$ntables = array();
list($tmp) = explode('_',$glues[0]['COLUMN_NAME'],2);
$ntables[] = $tmp;
$ntables[] = $tmp.'s';
$tmp = dbFetchRows('SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE "'.substr($table,0,-1).'_%" && TABLE_NAME != "'.$table.'"');
foreach( $tmp as $expand ) {
$ntables[] = $expand['TABLE_NAME'];
}