--- Zone.pm.ori Thu Feb 1 13:20:18 2007 +++ Zone.pm.new Thu Feb 1 13:43:19 2007 @@ -391,6 +391,21 @@ . " AND nt_zone.deleted='". ($data->{'search_deleted'}?'1':'0' ). "' " ; + $sql .= ( @$conditions ? ' AND (' . join(' ', @$conditions) . ') ' : '' ); + + my $sth = $dbh->prepare($sql); warn "$sql\n" if $self->debug_sql; + $sth->execute || return $self->error_response(508,$dbh->errstr); + $r_data->{'total'} = $sth->fetch->[0]; + $sth->finish; + + ## slink 2007-02-01 : + ## this query is highly ineffecient, due to massive left joining, result selt + ## becomes really big (with redundancies removed by COUNT(DISTINCT ) afterwards) + ## + ## rather than getting the count first and then getting the delegates, + ## we might as well get the delegates now + + if(0) { #add count of delegates/pseudo delegates, my $sql2 = "SELECT COUNT(DISTINCT nt_zone.nt_zone_id) FROM nt_group " . "LEFT JOIN nt_delegate as zdel ON zdel.nt_group_id= $data->{'nt_group_id'} AND zdel.nt_object_type='ZONE' " @@ -402,32 +417,15 @@ ; - - $sql .= ( @$conditions ? ' AND (' . join(' ', @$conditions) . ') ' : '' ); $sql2 .= ( @$conditions ? ' AND (' . join(' ', @$conditions) . ') ' : '' ); - - my $sth = $dbh->prepare($sql); warn "$sql\n" if $self->debug_sql; - $sth->execute || return $self->error_response(508,$dbh->errstr); - $r_data->{'total'} = $sth->fetch->[0]; - $sth->finish; - $sth = $dbh->prepare($sql2); warn "$sql2\n" if $self->debug_sql; $sth->execute || return $self->error_response(508,$dbh->errstr); $r_data->{'total'} += $sth->fetch->[0]; $sth->finish; - - $self->set_paging_vars($data, $r_data); - - my $sortby; - if( $r_data->{'total'} == 0 ) { - return $r_data; - } elsif( $r_data->{'total'} > 10000) { # if more than 10,000 zones, don't explicity ORDER BY -- mysql takes too long. --ai - $sortby = $self->format_sort_conditions($data, \%field_map, ""); - } else { - $sortby = $self->format_sort_conditions($data, \%field_map, "nt_zone.zone"); } + my %delegates; #get zones that are 'pseudo' delegates: some of their records are delegated. $sql = " SELECT nt_zone.nt_zone_id, " @@ -441,10 +439,10 @@ . " WHERE nt_delegate.nt_group_id=$data->{'nt_group_id'} AND nt_delegate.nt_object_type='ZONERECORD'" . " GROUP BY nt_zone.nt_zone_id" ; + $sql .= ( @$conditions ? ' AND (' . join(' ', @$conditions) . ') ' : '' ); + $sth = $dbh->prepare($sql); warn "$sql\n" if $self->debug_sql; $sth->execute || return $self->error_response(505,$sth->errstr); - my %delegates; - my @zones; while(my $z=$sth->fetchrow_hashref){ $delegates{$z->{'nt_zone_id'}}=$z; } @@ -463,13 +461,29 @@ . " INNER JOIN nt_zone ON nt_zone.nt_zone_id=d.nt_object_id" . " WHERE d.nt_group_id=$data->{'nt_group_id'} AND d.nt_object_type='ZONE'" ; + $sql .= ( @$conditions ? ' AND (' . join(' ', @$conditions) . ') ' : '' ); + $sth = $dbh->prepare($sql); warn "$sql\n" if $self->debug_sql; $sth->execute || return $self->error_response(505,$sth->errstr); while(my $z=$sth->fetchrow_hashref){ $delegates{$z->{'nt_zone_id'}}=$z; } - @zones= keys %delegates; + my @zones= keys %delegates; + $r_data->{'total'}+=scalar(@zones); + + $self->set_paging_vars($data, $r_data); + + my $sortby; + if( $r_data->{'total'} == 0 ) { + return $r_data; + } elsif( $r_data->{'total'} > 10000) { # if more than 10,000 zones, don't explicity ORDER BY -- mysql takes too long. --ai + $sortby = $self->format_sort_conditions($data, \%field_map, ""); + } else { + $sortby = $self->format_sort_conditions($data, \%field_map, "nt_zone.zone"); + } + + #get all zones in user's group (or subgroups) and the delegated zones $sql = "SELECT nt_zone.nt_zone_id, " . " nt_zone.zone, "