Index: trunk/DNSDB.pm
===================================================================
--- trunk/DNSDB.pm	(revision 2)
+++ trunk/DNSDB.pm	(revision 3)
@@ -23,5 +23,6 @@
 @ISA		= qw(Exporter);
 @EXPORT_OK	= qw(
-	&initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec
+	&initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs
+	&addRec &delRec &domStatus
 	%typemap %reverse_typemap
 	);
@@ -29,5 +30,6 @@
 @EXPORT		= (); # Export nothing by default.
 %EXPORT_TAGS	= ( ALL => [qw(
-		&initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec
+		&initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs
+		&addRec &delRec &domStatus
 		%typemap %reverse_typemap
 		)]
@@ -159,4 +161,6 @@
   return ('FAIL',"Need domain status") if !defined($state);
 
+  my $dom_id;
+
   # Allow transactions, and raise an exception on errors so we can catch it later.
   # Use local to make sure these get "reset" properly on exiting this block
@@ -173,15 +177,55 @@
     $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
     $sth->execute;
-    my ($dom_id) = $sth->fetchrow_array();
+    ($dom_id) = $sth->fetchrow_array();
 
     # ... and now we construct the standard records from the default set.  NB:  group should be variable.
-    $sth = $dbh->prepare("select host,type,val,distance,ttl from default_records where group_id=$group");
-    my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,ttl)".
-	" values ($dom_id,?,?,?,?,?)");
+    $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
+    my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
+	" values ($dom_id,?,?,?,?,?,?,?)");
     $sth->execute;
-    while (my ($host,$type,$val,$dist,$ttl) = $sth->fetchrow_array()) {
+    while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
       $host =~ s/DOMAIN/$domain/g;
-      $sth_in->execute($host,$type,$val,$dist,$ttl);
+      $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
     }
+
+    # once we get here, we should have suceeded.
+    $dbh->commit;
+  }; # end eval
+
+  if ($@) {
+    my $msg = $@;
+    eval { $dbh->rollback; };
+    return ('FAIL',$msg);
+  } else {
+    return ('OK',$dom_id);
+  }
+} # end addDomain
+
+
+## DNSDB::delDomain()
+# Delete a domain.
+# for now, just delete the records, then the domain.
+# later we may want to archive it in some way instead (status code 2, for example?)
+sub delDomain {
+  my $dbh = shift;
+  my $domain = shift;
+
+  # Allow transactions, and raise an exception on errors so we can catch it later.
+  # Use local to make sure these get "reset" properly on exiting this block
+  local $dbh->{AutoCommit} = 0;
+  local $dbh->{RaiseError} = 1;
+
+  # Wrap all the SQL in a transaction
+  eval {
+    # brute force.
+    my $sth = $dbh->prepare("select domain_id from domains where domain=?");
+    $sth->execute($domain);
+    die "Domain not found, can't delete\n" if $sth->rows < 1;
+    my ($id) = $sth->fetchrow_array;
+
+    $sth = $dbh->prepare("delete from records where domain_id=$id");
+    $sth->execute;
+    $sth = $dbh->prepare("delete from domains where domain=?");
+    $sth->execute($domain);
 
     # once we get here, we should have suceeded.
@@ -196,5 +240,6 @@
     return ('OK','OK');
   }
-} # end addDomain
+
+} # end delDomain()
 
 
@@ -249,5 +294,5 @@
     $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}";
   }
-print "MDEBUG: $sql<br>\n";
+#print "getSOA DEBUG: $sql<br>\n";
   my $sth = $dbh->prepare($sql);
   $sth->execute;
@@ -292,5 +337,5 @@
     return undef;
   }
-my %ret;
+  my %ret;
   $ret{recid}	= $recid;
   $ret{host}	= $host;
@@ -338,5 +383,5 @@
 
 
-## DNDB::addRec()
+## DNSDB::addRec()
 # Add a new record to a domain or a group's default records
 # Takes a database handle, default/live flag, group/domain ID,
@@ -378,4 +423,5 @@
   my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
 # something is bugging me about this...
+print "DEBUG: $sql<br>\n";
   my $sth = $dbh->prepare($sql);
   $sth->execute;
@@ -387,4 +433,52 @@
 
 
+## DNSDB::delRec()
+# Delete a record.  
+sub delRec {
+  $errstr = '';
+  my $dbh = shift;
+  my $defrec = shift;
+  my $id = shift;
+
+  my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?");
+  $sth->execute($id);
+
+  return ('FAIL',$sth->errstr) if $sth->err;
+
+  return ('OK','OK');
+} # end delRec()
+
+
+## DNSDB::domStatus()
+# Sets and/or returns a domain's status
+# Takes a database handle, domain ID and optionally a status argument
+# Returns undef on errors.
+sub domStatus {
+  my $dbh = shift;
+  my $id = shift;
+  my $newstatus = shift;
+
+  return undef if $id !~ /^\d+$/;
+
+  my $sth;
+
+# ooo, fun!  let's see what we were passed for status
+  if ($newstatus) {
+    $sth = $dbh->prepare("update domains set status=? where domain_id=?");
+    # ass-u-me caller knows what's going on in full
+    if ($newstatus =~ /^[01]$/) {	# only two valid for now.
+      $sth->execute($newstatus,$id);
+    } elsif ($newstatus =~ /^domo(?:n|ff)$/) {
+      $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id);
+    }
+  }
+
+  $sth = $dbh->prepare("select status from domains where domain_id=?");
+  $sth->execute($id);
+  my ($status) = $sth->fetchrow_array;
+  return $status;
+} # end domStatus()
+
+
 # shut Perl up
 1;
