Changeset 154


Ignore:
Timestamp:
02/04/05 17:29:30 (19 years ago)
Author:
Kris Deugau
Message:

/branches/sql-cleanup

main.cgi:

  • Removed commented legacy initialization code
  • Tweaked all SQL except for one section that needs serious redesigning to tighten any further. Adjusted processing on data returned via fetchrow_array() to make sure it's correct.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sql-cleanup/cgi-bin/main.cgi

    r149 r154  
    5656cleanInput(\%webvar);
    5757
    58 # Stuff that gets loaded from the database
    59 #my @citylist;
    60 #my @poplist;
    61 #my %allocated; # Count for allocated blocks in a master block
    62 #my %free;      # Count for free blocks (routed and unrouted) in a master block
    63 #my %bigfree;   # Tracking largest free block in a master block
    64 #my %routed;    # Number of routed blocks in a master block
    65 
    66 # Slurp up the master block list - we need this several places
    67 # While we're at it, initialize the related hashes.
    68 #$sth = $ip_dbh->prepare("select * from masterblocks order by cidr");
    69 #$sth->execute;
    70 #for (my $i=0; my @data = $sth->fetchrow_array(); $i++) {
    71 #  $masterblocks[$i] = new NetAddr::IP $data[0];
    72 #  $allocated{"$masterblocks[$i]"} = 0;
    73 #  $free{"$masterblocks[$i]"} = 0;
    74 #  $bigfree{"$masterblocks[$i]"} = 128; # Larger number means smaller block.
    75 #                                       # Set to 128 to prepare for IPv6
    76 #  $routed{"$masterblocks[$i]"} = 0;
    77 #}
    78 
    79 
    80 
    8158
    8259#main()
     
    10986# Thus the "routed" flag.
    11087
    111     $sth = $ip_dbh->prepare("insert into freeblocks values ('$webvar{cidr}',".
    112         $cidr->masklen.",'<NULL>','n')");
     88    $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
     89        " values ('$webvar{cidr}',".$cidr->masklen.",'<NULL>','n')");
    11390    $sth->execute;
    11491
     
    493470
    494471  # Fetch only the blocks relevant to this master
    495   $sth = $ip_dbh->prepare("select * from routed where cidr <<= '$master' order by cidr");
     472  $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr");
    496473  $sth->execute();
    497474
     
    504481    $bigfree{"$cidr"} = 128;
    505482    # Retain the routing destination
    506     $routed{"$cidr"} = $data[2];
     483    $routed{"$cidr"} = $data[1];
    507484  }
    508485
     
    596573  my $master = new NetAddr::IP $webvar{block};
    597574
    598   $sth = $ip_dbh->prepare("select * from routed where cidr='$master'");
     575  $sth = $ip_dbh->prepare("select city from routed where cidr='$master'");
    599576  $sth->execute;
    600577  my @data = $sth->fetchrow_array;
    601578
    602579  print qq(<center><div class="heading">Summarizing allocated blocks for ).
    603         qq($master ($data[2]):</div></center><br>\n);
     580        qq($master ($data[0]):</div></center><br>\n);
    604581
    605582  startTable('CIDR allocation','Customer Location','Type','CustID','Description/Name');
    606583
    607584  # Snag the allocations for this block
    608   $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$master' order by cidr");
     585  $sth = $ip_dbh->prepare("select cidr,city,type,custid,description".
     586        " from allocations where cidr <<= '$master' order by cidr");
    609587  $sth->execute();
    610588
    611589  my $count=0;
    612590  while (my @data = $sth->fetchrow_array()) {
    613     # cidr,custid,type,city,description,notes,maskbits,circuitid
     591    # cidr,city,type,custid,description, as per the SELECT
    614592    my $cidr = new NetAddr::IP $data[0];
    615593
    616594    # Clean up extra spaces that are borking things.
    617     $data[2] =~ s/\s+//g;
     595#    $data[2] =~ s/\s+//g;
    618596
    619597    my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=edit&block=$data[0]\">$data[0]</a>",
    620         $data[3], $disp_alloctypes{$data[2]}, $data[1], $data[4]);
     598        $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
    621599    # If the allocation is a pool, allow listing of the IPs in the pool.
    622600    if ($data[2] =~ /^.[pd]$/) {
     
    653631  # unrouted free blocks, but it's better to let the database do the work if we can.
    654632  $count = 0;
    655   $sth = $ip_dbh->prepare("select * from freeblocks where routed='y' and cidr <<= '$master' order by cidr");
     633  $sth = $ip_dbh->prepare("select cidr from freeblocks where routed='y' and cidr <<= '$master' order by cidr");
    656634  $sth->execute();
    657635  while (my @data = $sth->fetchrow_array()) {
     
    701679
    702680  startTable('IP','Customer ID','Available?','Description','');
    703   $sth = $ip_dbh->prepare("select * from poolips where pool='$webvar{pool}' order by ip");
     681  $sth = $ip_dbh->prepare("select ip,custid,available,description,type".
     682        " from poolips where pool='$webvar{pool}' order by ip");
    704683  $sth->execute;
    705684  my $count = 0;
    706685  while (my @data = $sth->fetchrow_array) {
    707686    # pool,ip,custid,city,ptype,available,notes,description,circuitid
    708     # If desc is null, make it not null.  <g>
    709     if ($data[7] eq '') {
    710       $data[7] = '&nbsp;';
     687    # ip,custid,available,description,type
     688    # If desc is "null", make it not null.  <g>
     689    if ($data[3] eq '') {
     690      $data[3] = '&nbsp;';
    711691    }
    712692    # Some nice hairy Perl to decide whether to allow unassigning each IP
    713     #   -> if $data[5] (aka poolips.available) == 'n' then we print the unassign link
     693    #   -> if $data[2] (aka poolips.available) == 'n' then we print the unassign link
    714694    #      else we print a blank space
    715     my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[1]">$data[1]</a>),
    716         $data[2],$data[5],$data[7],
    717         ( ($data[5] eq 'n') ?
    718           ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[1]&".
    719            "alloctype=$data[4]i\">Unassign this IP</a>") :
     695    my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
     696        $data[1],$data[2],$data[3],
     697        ( ($data[2] eq 'n') ?
     698          ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&".
     699           "alloctype=$data[4]\">Unassign this IP</a>") :
    720700          ("&nbsp;") )
    721701        );
     
    814794    # ... aside from #^%#$%#@#^%^^!!!! legacy data.  GRRR.
    815795    # Note that we want to retain the requested city to relate to customer info.
     796##fixme This needs thought.
     797##SELECT DISTINCT pool, Count(*) FROM poolips where available='y' GROUP BY pool;
    816798    if ($base =~ /^[ds]$/) {
    817799      $sql = "select * from poolips where available='y' and".
     
    883865        }
    884866        if ($webvar{allocfrom} ne '-') {
    885           $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
     867          $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
    886868                " and cidr <<= '$webvar{allocfrom}' and routed='y' order by cidr,maskbits desc";
    887869        } else {
    888           $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
     870          $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
    889871                " and routed='y' order by cidr,maskbits desc";
    890872        }
     
    11431125        "type='$webvar{alloctype}',circuitid='$webvar{circid}' where cidr='$webvar{block}'";
    11441126    }
    1145 syslog "debug", $sql;
     1127    # Log the details of the change.
     1128    syslog "debug", $sql;
    11461129    $sth = $ip_dbh->prepare($sql);
    11471130    $sth->execute;
Note: See TracChangeset for help on using the changeset viewer.