Changeset 723 for trunk/DNSDB.pm


Ignore:
Timestamp:
06/17/16 18:01:13 (8 years ago)
Author:
Kris Deugau
Message:

/trunk

Record search is substantially complete, including scope limits for users
in subgroups, sorting, and various links into zones or direct record
editing (records attached to both a forward and reverse zone.
See #57.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r722 r723  
    48844884
    48854885
    4886 ## DNSDB::recSearchCount()
    4887 # Get a total count for a global record search
    4888 ## DNSDB::recSearch()
    4889 # Find records matching the search string
    4890 sub recSearch {
    4891   my $self = shift;
    4892   my $dbh = $self->{dbh};
    4893 
    4894   my %args = @_;
    4895 
    4896 my $sql = q(
    4897 SELECT r.domain_id,d.domain,g1.group_name AS domgroup,r.rdns_id,z.revnet AS revzone,g2.group_name AS
    4898 revgroup,r.host,t.name AS rectype,r.val,l.description AS location
     4886# a collection of joins for the record search
     4887our $recsearchsqlbase = q(
    48994888FROM records r
    49004889LEFT JOIN domains d ON r.domain_id = d.domain_id
     
    49074896);
    49084897
    4909 # Limit scope based on group
    4910 if ($args{group} != 1) {
    4911   my @grouplist;
    4912   $self->getChildren($args{group}, \@grouplist);
    4913   my $groupset = join(',', $args{group}, @grouplist);
    4914   # oh my aching HEAD.  there has to be a better way to do conditions on joined tables...
    4915   $sql .= "AND (
    4916   (g1.group_id IN ($groupset) AND g2.group_id IN ($groupset)) OR
    4917   (g1.group_id IN ($groupset) AND g2.group_id IS NULL) OR
    4918   (g1.group_id IS NULL AND g2.group_id IN ($groupset))
    4919   )
     4898
     4899## DNSDB::recSearchCount()
     4900# Get a total count for a global record search
     4901# Takes a hash with the search string and the login group of the user
     4902sub recSearchCount {
     4903  my $self = shift;
     4904  my $dbh = $self->{dbh};
     4905  my %args = @_;
     4906
     4907  my $sql = "SELECT count(*)".$recsearchsqlbase;
     4908
     4909  # Limit scope based on group
     4910  if ($args{group} != 1) {
     4911    my @grouplist;
     4912    $self->getChildren($args{group}, \@grouplist);
     4913    my $groupset = join(',', $args{group}, @grouplist);
     4914    # oh my aching HEAD.  there has to be a better way to do conditions on joined tables...
     4915    $sql .= "AND (
     4916    (g1.group_id IN ($groupset) AND g2.group_id IN ($groupset)) OR
     4917    (g1.group_id IN ($groupset) AND g2.group_id IS NULL) OR
     4918    (g1.group_id IS NULL AND g2.group_id IN ($groupset))
     4919    )
    49204920";
    4921 }
    4922 
    4923 my $foo = q(
    4924 SELECT r.domain_id,d.domain,g1.group_name AS domgroup,r.rdns_id,z.revnet AS revzone,g2.group_name AS
    4925 revgroup,r.host,t.name AS rectype,r.val,l.description AS location
    4926 FROM records r
    4927 LEFT JOIN domains d on r.domain_id = d.domain_id
    4928   LEFT JOIN groups g1 on d.group_id = g1.group_id
    4929 LEFT JOIN revzones z on r.rdns_id = z.rdns_id
    4930   LEFT JOIN groups g2 on z.group_id = g2.group_id
    4931 JOIN rectypes t ON r.type = t.val
    4932 LEFT JOIN locations l ON r.location = l.location
    4933 WHERE r.type <> 6 AND (r.host ~* 'kings' OR r.val ~* 'kings');
    4934 );
    4935 #ORDER BY record_id
    4936 
    4937 #warn "$sql\n";
    4938 # safety valve.  probably need a way to flag this back to the caller.
    4939 if (!$args{searchfor} || $args{searchfor} =~ /^.{0,2}$/) { $sql .= "LIMIT 10"; }
    4940 
    4941 #$sql .= "LIMIT 20" if $sql !~ /LIMIT/;
    4942 
    4943   return $dbh->selectall_arrayref($sql, { Slice => {} }, $args{searchfor}, $args{searchfor}) or warn $dbh->errstr;
     4921  }
     4922
     4923  my $count = $dbh->selectrow_array($sql, undef, $args{searchfor}, $args{searchfor});
     4924  $errstr = $dbh->errstr if !$count;
     4925  return $count;
     4926
     4927} # end recSearchCount()
     4928
     4929
     4930## DNSDB::recSearch()
     4931# Find records matching the search string
     4932# Takes a hash with the search string, login group of the user, pagination offset, sort field,
     4933# and sort direction
     4934# Returns a reference to a list of hashrefs
     4935sub recSearch {
     4936  my $self = shift;
     4937  my $dbh = $self->{dbh};
     4938  my %args = @_;
     4939
     4940  my $sql = q(SELECT
     4941    r.domain_id, d.domain, g1.group_name AS domgroup,
     4942    r.rdns_id, z.revnet AS revzone, g2.group_name AS revgroup,
     4943    r.host, t.name AS rectype, r.val, l.description AS location, r.record_id).
     4944    $recsearchsqlbase;
     4945
     4946  # Limit scope based on group
     4947  if ($args{group} != 1) {
     4948    my @grouplist;
     4949    $self->getChildren($args{group}, \@grouplist);
     4950    my $groupset = join(',', $args{group}, @grouplist);
     4951    # oh my aching HEAD.  there has to be a better way to do conditions on joined tables...
     4952    $sql .= "AND (
     4953    (g1.group_id IN ($groupset) AND g2.group_id IN ($groupset)) OR
     4954    (g1.group_id IN ($groupset) AND g2.group_id IS NULL) OR
     4955    (g1.group_id IS NULL AND g2.group_id IN ($groupset))
     4956    )
     4957";
     4958  }
     4959
     4960  # mixed tables means this isn't a simple prefix like the regular record list filter.  :/
     4961  my %sortmap = (
     4962    domain => 'd.domain',
     4963    revzone => 'z.revnet',
     4964    host => 'r.host',
     4965    type => 't.name',
     4966    val => 'inetlazy(r.val)',
     4967    location => 'r.location',
     4968    );
     4969
     4970  # Sorting defaults
     4971  $args{sortorder} = 'ASC' if !$args{sortorder} || !grep /^$args{sortorder}$/, ('ASC','DESC');
     4972  $args{sortby} = 'r.host' if !$args{sortby} || $args{sortby} !~ /^[\w_.]+$/;
     4973  $args{offset} = 0 if !$args{offset} || $args{offset} !~ /^(?:all|\d+)$/;
     4974
     4975  $args{sortby} = $sortmap{$args{sortby}} if $args{sortby} !~ /\./;
     4976
     4977  # Add sort and offset to SQL
     4978  $sql .= "ORDER BY $args{sortby} $args{sortorder},record_id ASC\n";
     4979  $sql .= ($args{offset} eq 'all' ? '' : "LIMIT $self->{perpage} OFFSET ".$args{offset}*$self->{perpage});
     4980
     4981##fixme: should probably sent the warning somewhere else
     4982  return $dbh->selectall_arrayref($sql, { Slice => {} }, $args{searchfor}, $args{searchfor})
     4983    or warn $dbh->errstr;
     4984
    49444985} # end recSearch()
    49454986
Note: See TracChangeset for help on using the changeset viewer.