Changeset 666


Ignore:
Timestamp:
10/08/14 12:26:55 (10 years ago)
Author:
Kris Deugau
Message:

/trunk

Convert calling convention for getRecCount() to a hash so we can pass the
same arguments to getRecCount() and getRecList()

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r665 r666  
    40044004  my %args = @_;
    40054005
     4006  $args{revrec} = 'n' if !$args{revrec};
     4007  $args{defrec} = 'n' if !$args{defrec};
     4008
    40064009  # protection against bad or missing arguments
    40074010  $args{sortorder} = 'ASC' if !$args{sortorder} || !grep /^$args{sortorder}$/, ('ASC','DESC');
     
    40154018  my $perpage = ($args{nrecs} ? $args{nrecs} : $self->{perpage});
    40164019
    4017   # sort reverse zones on IP, correctly
    4018   # do other fiddling with $args{sortby} while we're at it.
    4019   # whee!  multisort means just passing comma-separated fields in sortby!
    4020   my $newsort = '';
    4021   foreach my $sf (split /,/, $args{sortby}) {
    4022     $sf = "r.$sf";
    4023     $sf =~ s/r\.val/inetlazy(r.val)/;
    4024 # hmm.  do we really need to limit this?
    4025 #       if $args{revrec} eq 'y' && $args{defrec} eq 'n';
    4026     $sf =~ s/r\.type/t.alphaorder/;
    4027     $newsort .= ",$sf";
    4028   }
    4029   $newsort =~ s/^,//;
    4030 
    4031   my @bindvars = ($args{id});
    4032   push @bindvars, ($args{filter},$args{filter}) if $args{filter};
    40334020
    40344021##fixme:  do we need a knob to twist to switch from unix epoch to postgres time string?
    4035   my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.ttl";
     4022  my @bindvars;
     4023  my $sql = "SELECT r.record_id,";
     4024  # only include the parent info if we don't already know which parent we're in
     4025  $sql .= "r.domain_id,r.rdns_id," unless $args{id};
     4026  $sql .= "r.host,r.type,r.val,r.ttl";
    40364027  $sql .= ",l.description AS locname,stamp,r.stamp < now() AS ispast,r.expires,r.stampactive"
    40374028        if $args{defrec} eq 'n';
     
    40404031  $sql .= "INNER JOIN rectypes t ON r.type=t.val ";     # for sorting by type alphabetically
    40414032  $sql .= "LEFT JOIN locations l ON r.location=l.location " if $args{defrec} eq 'n';
    4042   $sql .= "WHERE "._recparent($args{defrec},$args{revrec})." = ?";
    4043   $sql .= " AND NOT r.type=$reverse_typemap{SOA}";
     4033  $sql .= "WHERE NOT r.type=$reverse_typemap{SOA}";
     4034
     4035  # "normal" record list
     4036  if ($args{id}) {
     4037    $sql .= " AND "._recparent($args{defrec},$args{revrec})." = ?";
     4038    push @bindvars, $args{id};
     4039  }
     4040
     4041  # Filtering on host/val (mainly normal record list)
    40444042  if ($args{filter}) {
    40454043    $sql .= " AND (r.host ~* ? OR r.val ~* ? OR r.host ~* ? OR r.val ~* ?)";
    40464044    my $tmp = join('.',reverse(split(/\./,$args{filter})));
     4045    push @bindvars, ($args{filter},$args{filter});
    40474046    push @bindvars, ($tmp, $tmp);
    40484047  }
     4048
     4049  # Filtering on other fields
     4050  foreach (qw(type distance weight port ttl description)) {
     4051    if ($args{$_}) {
     4052      $sql .= " AND $_ ~* ?";
     4053      push @bindvars, $args{$_};
     4054    }
     4055  }
     4056
     4057  # whee!  multisort means just passing comma-separated fields in sortby!
     4058  my $newsort = '';
     4059  foreach my $sf (split /,/, $args{sortby}) {
     4060    $sf = "r.$sf";
     4061    # sort on IP, correctly
     4062    $sf =~ s/r\.val/inetlazy(r.val)/;
     4063# hmm.  do we really need to limit this?
     4064#       if $args{revrec} eq 'y' && $args{defrec} eq 'n';
     4065    $sf =~ s/r\.type/t.alphaorder/;  # subtly different from sorting on rectypes.name
     4066    $newsort .= ",$sf";
     4067  }
     4068  $newsort =~ s/^,//;
     4069##enhance: pass in ascending/descending sort per-field
    40494070  $sql .= " ORDER BY $newsort $args{sortorder}";
    40504071  # ensure consistent ordering by sorting on record_id too
    40514072  $sql .= ", record_id $args{sortorder}";
     4073
     4074  # Offset/pagination
    40524075  $sql .= ($args{offset} eq 'all' ? '' : " LIMIT $perpage OFFSET ".$args{offset}*$perpage);
    40534076
     
    40784101  my $self = shift;
    40794102  my $dbh = $self->{dbh};
    4080   my $defrec = shift;
    4081   my $revrec = shift;
    4082   my $id = shift;
    4083   my $filter = shift || '';
    4084 
    4085   # keep the nasties down, since we can't ?-sub this bit.  :/
    4086   # note this is chars allowed in DNS hostnames
    4087   $filter =~ s/[^a-zA-Z0-9_.:-]//g;
    4088 
    4089   my @bindvars = ($id);
    4090   push @bindvars, ($filter,$filter) if $filter;
     4103
     4104  my %args = @_;
     4105
     4106  $args{defrec} = 'n' if !$args{defrec};
     4107  $args{revrec} = 'n' if !$args{revrec};
     4108
     4109  my @bindvars;
    40914110  my $sql = "SELECT count(*) FROM ".
    4092         _rectable($defrec,$revrec).
    4093         " WHERE "._recparent($defrec,$revrec)."=? ".
    4094         "AND NOT type=$reverse_typemap{SOA}";
    4095   if ($filter) {
     4111        _rectable($args{defrec},$args{revrec}).
     4112        " WHERE NOT type=$reverse_typemap{SOA}";
     4113  if ($args{id}) {
     4114    $sql .= " AND "._recparent($args{defrec},$args{revrec})." = ?";
     4115    push @bindvars, $args{id};
     4116  }
     4117
     4118  # Filtering on host/val (mainly normal record list)
     4119  if ($args{filter}) {
    40964120    $sql .= " AND (r.host ~* ? OR r.val ~* ? OR r.host ~* ? OR r.val ~* ?)";
    4097     my $tmp = join('.',reverse(split(/\./,$filter)));
     4121    my $tmp = join('.',reverse(split(/\./,$args{filter})));
     4122    push @bindvars, ($args{filter},$args{filter});
    40984123    push @bindvars, ($tmp, $tmp);
    40994124  }
    4100   $sql .= " AND (host ~* ? OR val ~* ? OR host ~* ? OR val ~* ?)" if $filter;
    4101   my $tmp = join('.',reverse(split(/\./,$filter)));
    4102   push @bindvars, ($tmp, $tmp) if $filter;
     4125
     4126  # Filtering on other fields
     4127  foreach (qw(type distance weight port ttl description)) {
     4128    if ($args{$_}) {
     4129      $sql .= " AND $_ ~* ?";
     4130      push @bindvars, $args{$_};
     4131    }
     4132  }
    41034133
    41044134  my ($count) = $dbh->selectrow_array($sql, undef, (@bindvars) );
  • trunk/dns-rpc.cgi

    r659 r666  
    455455  $args{direction} = 'ASC' if !$args{direction};
    456456
    457   my $ret = $dnsdb->getRecCount($args{defrec}, $args{revrec}, $args{id}, $args{filter});
     457  my $ret = $dnsdb->getRecCount(defrec => $args{defrec}, revrec => $args{revrec},
     458        id => $args{id}, filter => $args{filter});
    458459
    459460  die "$dnsdb->errstr\n" if !$ret;
  • trunk/dns.cgi

    r641 r666  
    632632    $page->param(curpage => $webvar{page});
    633633
    634     my $count = $dnsdb->getRecCount($webvar{defrec}, $webvar{revrec}, $webvar{id}, $filter);
     634    my $count = $dnsdb->getRecCount(defrec => $webvar{defrec}, revrec => $webvar{revrec},
     635        id => $webvar{id}, filter => $filter);
    635636
    636637    $sortby = 'host';
Note: See TracChangeset for help on using the changeset viewer.