Changeset 3 for trunk/DNSDB.pm
- Timestamp:
- 08/18/09 18:04:14 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r2 r3 23 23 @ISA = qw(Exporter); 24 24 @EXPORT_OK = qw( 25 &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec 25 &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs 26 &addRec &delRec &domStatus 26 27 %typemap %reverse_typemap 27 28 ); … … 29 30 @EXPORT = (); # Export nothing by default. 30 31 %EXPORT_TAGS = ( ALL => [qw( 31 &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec 32 &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs 33 &addRec &delRec &domStatus 32 34 %typemap %reverse_typemap 33 35 )] … … 159 161 return ('FAIL',"Need domain status") if !defined($state); 160 162 163 my $dom_id; 164 161 165 # Allow transactions, and raise an exception on errors so we can catch it later. 162 166 # Use local to make sure these get "reset" properly on exiting this block … … 173 177 $sth = $dbh->prepare("select domain_id from domains where domain='$domain'"); 174 178 $sth->execute; 175 my($dom_id) = $sth->fetchrow_array();179 ($dom_id) = $sth->fetchrow_array(); 176 180 177 181 # ... and now we construct the standard records from the default set. NB: group should be variable. 178 $sth = $dbh->prepare("select host,type,val,distance, ttl from default_records where group_id=$group");179 my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance, ttl)".180 " values ($dom_id,?,?,?,?,? )");182 $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group"); 183 my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)". 184 " values ($dom_id,?,?,?,?,?,?,?)"); 181 185 $sth->execute; 182 while (my ($host,$type,$val,$dist,$ ttl) = $sth->fetchrow_array()) {186 while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) { 183 187 $host =~ s/DOMAIN/$domain/g; 184 $sth_in->execute($host,$type,$val,$dist,$ ttl);188 $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl); 185 189 } 190 191 # once we get here, we should have suceeded. 192 $dbh->commit; 193 }; # end eval 194 195 if ($@) { 196 my $msg = $@; 197 eval { $dbh->rollback; }; 198 return ('FAIL',$msg); 199 } else { 200 return ('OK',$dom_id); 201 } 202 } # end addDomain 203 204 205 ## DNSDB::delDomain() 206 # Delete a domain. 207 # for now, just delete the records, then the domain. 208 # later we may want to archive it in some way instead (status code 2, for example?) 209 sub delDomain { 210 my $dbh = shift; 211 my $domain = shift; 212 213 # Allow transactions, and raise an exception on errors so we can catch it later. 214 # Use local to make sure these get "reset" properly on exiting this block 215 local $dbh->{AutoCommit} = 0; 216 local $dbh->{RaiseError} = 1; 217 218 # Wrap all the SQL in a transaction 219 eval { 220 # brute force. 221 my $sth = $dbh->prepare("select domain_id from domains where domain=?"); 222 $sth->execute($domain); 223 die "Domain not found, can't delete\n" if $sth->rows < 1; 224 my ($id) = $sth->fetchrow_array; 225 226 $sth = $dbh->prepare("delete from records where domain_id=$id"); 227 $sth->execute; 228 $sth = $dbh->prepare("delete from domains where domain=?"); 229 $sth->execute($domain); 186 230 187 231 # once we get here, we should have suceeded. … … 196 240 return ('OK','OK'); 197 241 } 198 } # end addDomain 242 243 } # end delDomain() 199 244 200 245 … … 249 294 $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}"; 250 295 } 251 print "MDEBUG: $sql<br>\n";296 #print "getSOA DEBUG: $sql<br>\n"; 252 297 my $sth = $dbh->prepare($sql); 253 298 $sth->execute; … … 292 337 return undef; 293 338 } 294 my %ret;339 my %ret; 295 340 $ret{recid} = $recid; 296 341 $ret{host} = $host; … … 338 383 339 384 340 ## DN DB::addRec()385 ## DNSDB::addRec() 341 386 # Add a new record to a domain or a group's default records 342 387 # Takes a database handle, default/live flag, group/domain ID, … … 378 423 my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)"; 379 424 # something is bugging me about this... 425 print "DEBUG: $sql<br>\n"; 380 426 my $sth = $dbh->prepare($sql); 381 427 $sth->execute; … … 387 433 388 434 435 ## DNSDB::delRec() 436 # Delete a record. 437 sub delRec { 438 $errstr = ''; 439 my $dbh = shift; 440 my $defrec = shift; 441 my $id = shift; 442 443 my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?"); 444 $sth->execute($id); 445 446 return ('FAIL',$sth->errstr) if $sth->err; 447 448 return ('OK','OK'); 449 } # end delRec() 450 451 452 ## DNSDB::domStatus() 453 # Sets and/or returns a domain's status 454 # Takes a database handle, domain ID and optionally a status argument 455 # Returns undef on errors. 456 sub domStatus { 457 my $dbh = shift; 458 my $id = shift; 459 my $newstatus = shift; 460 461 return undef if $id !~ /^\d+$/; 462 463 my $sth; 464 465 # ooo, fun! let's see what we were passed for status 466 if ($newstatus) { 467 $sth = $dbh->prepare("update domains set status=? where domain_id=?"); 468 # ass-u-me caller knows what's going on in full 469 if ($newstatus =~ /^[01]$/) { # only two valid for now. 470 $sth->execute($newstatus,$id); 471 } elsif ($newstatus =~ /^domo(?:n|ff)$/) { 472 $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id); 473 } 474 } 475 476 $sth = $dbh->prepare("select status from domains where domain_id=?"); 477 $sth->execute($id); 478 my ($status) = $sth->fetchrow_array; 479 return $status; 480 } # end domStatus() 481 482 389 483 # shut Perl up 390 484 1;
Note:
See TracChangeset
for help on using the changeset viewer.