Changeset 254 for trunk/DNSDB.pm


Ignore:
Timestamp:
02/29/12 17:38:46 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Update parentID() (and all calls to it) for reverse records,
move to DNSDB.pm. See #26.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r252 r254  
    3636        &addRec &updateRec &delRec
    3737        &getTypelist
    38         &getParents
     38        &parentID
    3939        &isParent
    4040        &domStatus &importAXFR
     
    5858                &addRec &updateRec &delRec
    5959                &getTypelist
    60                 &getParents
     60                &parentID
    6161                &isParent
    6262                &domStatus &importAXFR
     
    22352235
    22362236
    2237 ## DNSDB::getParents()
    2238 # Find out which entities are parent to the requested id
    2239 # Returns arrayref containing hash pairs of id/type
    2240 sub getParents {
    2241   my $dbh = shift;
    2242   my $id = shift;
    2243   my $type = shift;
    2244   my $depth = shift || 'all';   # valid values:  'all', 'immed', <int> (stop at this group ID)
    2245 
    2246   my @parlist;
    2247 
    2248   while (1) {
    2249     my $result = $dbh->selectrow_hashref("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
    2250         undef, ($id) );
    2251     my %tmp = ($result->{$par_col{$type}} => $par_type{$type});
    2252     unshift @parlist, \%tmp;
    2253     last if $result->{$par_col{$type}} == 1;    # group 1 is its own parent
    2254     $id = $result->{$par_col{$type}};
    2255     $type = $par_type{$type};
    2256   }
    2257 
    2258   return \@parlist;
    2259 
    2260 } # end getParents()
     2237## DNSDB::parentID()
     2238# Get ID of entity that is nearest parent to requested id
     2239# Takes a database handle and a hash of entity ID, entity type, optional parent type flag
     2240# (domain/reverse zone or group), and optional default/live and forward/reverse flags
     2241# Returns the ID or undef on failure
     2242sub parentID {
     2243  my $dbh = shift;
     2244
     2245  my %args = @_;
     2246
     2247  # clean up the parent-type.  Set it to group if not set;  coerce revzone to domain for simpler logic
     2248  $args{partype} = 'group' if !$args{partype};
     2249  $args{partype} = 'domain' if $args{partype} eq 'revzone';
     2250
     2251  # clean up defrec and revrec.  default to live record, forward zone
     2252  $args{defrec} = 'n' if !$args{defrec};
     2253  $args{revrec} = 'n' if !$args{revrec};
     2254
     2255  if ($par_type{$args{partype}} eq 'domain') {
     2256    # only live records can have a domain/zone parent
     2257    return unless ($args{type} eq 'record' && $args{defrec} eq 'n');
     2258    my $result = $dbh->selectrow_hashref("SELECT ".($args{revrec} eq 'n' ? 'domain_id' : 'rdns_id').
     2259        " FROM records WHERE record_id = ?",
     2260        undef, ($args{id}) ) or return;
     2261    return $result;
     2262  } else {
     2263    # snag some arguments that will either fall through or be overwritten to save some code duplication
     2264    my $tmpid = $args{id};
     2265    my $type = $args{type};
     2266    if ($type eq 'record' && $args{defrec} eq 'n') {
     2267      # Live records go through the records table first.
     2268      ($tmpid) = $dbh->selectrow_array("SELECT ".($args{revrec} eq 'n' ? 'domain_id' : 'rdns_id').
     2269        " FROM records WHERE record_id = ?",
     2270        undef, ($args{id}) ) or return;
     2271      $type = ($args{revrec} eq 'n' ? 'domain' : 'revzone');
     2272    }
     2273    my ($result) = $dbh->selectrow_array("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
     2274        undef, ($tmpid) );
     2275    return $result;
     2276  }
     2277# should be impossible to get here with even remotely sane arguments
     2278  return;
     2279} # end parentID()
    22612280
    22622281
Note: See TracChangeset for help on using the changeset viewer.