Changeset 908 for trunk


Ignore:
Timestamp:
08/16/17 15:23:11 (7 years ago)
Author:
Kris Deugau
Message:

/trunk

Further refine general RPC search with variations on alloctype matching
and sort ordering

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/search-rpc.cgi

    r907 r908  
    151151  my @matchtypes;
    152152
    153   my %mt = (EXACT => '=',
     153  my %mt = (
     154        EXACT => '=',
    154155        EQUAL => '=',
    155         NOT => '!~',
     156        NOT => '!~',    # text only?
    156157        # CIDR options
    157158        MASK => 'MASK',
     
    161162
    162163  if ($args{type}) {
     164    my $notflag = '';
     165    if ($args{type} =~ /^NOT:/) {
     166      $args{type} =~ s/^NOT://;
     167      $notflag = 'NOT ';
     168    }
     169    if ($args{type} =~ /\./) {
     170      $args{type} =~ s/\./_/;
     171      push @matchtypes, $notflag.'LIKE';
     172    } else {
     173      push @matchtypes, ($notflag ? '<>' : '=');
     174    }
    163175    push @fields, 's.type';
    164176    push @vals, $args{type};
    165     push @matchtypes, '=';
    166177  }
    167178
     
    224235  }
    225236
    226   my $sql = q(SELECT s.cidr,s.custid,s.type,s.description,s.city,a.dispname FROM searchme s JOIN alloctypes a ON s.type = a.type);
     237  my $cols = "s.cidr, s.custid, s.type, s.city, s.description, s.id, s.parent_id, s.available, s.vrf, a.dispname";
     238  my $sql = qq(SELECT $cols FROM searchme s JOIN alloctypes a ON s.type = a.type);
    227239  my @sqlcriteria;
    228240  for (my $i = 0; $i <= $#fields; $i++) {
     
    231243  $sql .= " WHERE ".join(' AND ', @sqlcriteria) if @sqlcriteria;
    232244
     245  # multifield sorting!
     246  if ($args{order}) {
     247    my @ordfields = split /,/, $args{order};
     248    # there are probably better ways to do this
     249    my %omap = (cidr => 's.cidr', net => 's.cidr', network => 's.cidr', ip => 's.cidr',
     250      custid => 's.custid', type => 's.type', city => 's.city',
     251      desc => 's.description', description => 's.description');
     252    my @ordlist;
     253    # only pass sort field values from the list of acceptable field names or aliases as per %omap
     254    foreach my $ord (@ordfields) {
     255      push @ordlist, $omap{$ord}
     256        if grep /^$ord$/, (keys %omap);
     257    }
     258    if (@ordlist) {
     259      $sql .= " ORDER BY ". join(',', @ordlist);
     260    }
     261  }
     262
    233263  my $result = $ip_dbh->selectall_arrayref($sql, {Slice=>{}}, @vals);
    234264  die $ip_dbh->errstr if !$result;
Note: See TracChangeset for help on using the changeset viewer.