Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r129 r130 1170 1170 my $id = shift; 1171 1171 1172 my $sql = "SELECT r .record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata".1173 (($def eq 'def' or $def eq 'y') ? ', r.group_id FROM default_' : ',r.domain_id FROM ').1174 "records r LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_idWHERE record_id=?";1172 my $sql = "SELECT record_id,host,type,val,distance,weight,port,ttl". 1173 (($def eq 'def' or $def eq 'y') ? ',group_id FROM default_' : ',domain_id FROM '). 1174 "records WHERE record_id=?"; 1175 1175 my $ret = $dbh->selectrow_hashref($sql, undef, ($id) ); 1176 1176 … … 1185 1185 } 1186 1186 1187 $ret->{val} = $ret->{recdata} if $ret->{longrec_id}; # put the long data in the real value space1188 delete $ret->{longrec_id}; # remove these since they shouldn't be exposed - the caller1189 delete $ret->{recdata}; # should not care about "long records" vs normal ones.1190 1187 $ret->{parid} = (($def eq 'def' or $def eq 'y') ? $ret->{group_id} : $ret->{domain_id}); 1191 1188 … … 1214 1211 $type = 'y' if $type eq 'def'; 1215 1212 1216 my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl ,r.longrec_id,l.recdataFROM ";1213 my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl FROM "; 1217 1214 $sql .= "default_" if $type eq 'y'; 1218 1215 $sql .= "records r "; 1219 1216 $sql .= "INNER JOIN rectypes t ON r.type=t.val "; # for sorting by type alphabetically 1220 $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id ";1221 1217 if ($type eq 'y') { 1222 1218 $sql .= "WHERE r.group_id=?"; … … 1234 1230 my @retbase; 1235 1231 while (my $ref = $sth->fetchrow_hashref()) { 1236 $ref->{val} = $ref->{recdata} if $ref->{longrec_id}; # put the long data in the real value space1237 delete $ref->{longrec_id}; # remove these since they shouldn't be exposed - the caller1238 delete $ref->{recdata}; # should not care about "long records" vs normal ones.1239 1232 push @retbase, $ref; 1240 1233 } … … 1321 1314 1322 1315 eval { 1323 if (length($val) > 100 ) {1324 # extralong records get an entry in a separate table.1325 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) );1326 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($val) );1327 $fields .= ",longrec_id";1328 $vallen .= ",?";1329 push @vallist, $longid;1330 $vallist[3] = ''; # so we don't barf when we insert the main record1331 }1332 1316 $dbh->do("INSERT INTO ".($defrec eq 'y' ? 'default_' : '')."records ($fields) VALUES ($vallen)", 1333 1317 undef, @vallist); … … 1378 1362 } 1379 1363 1380 # my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM ";1381 # $sql .= "default_" if $type eq 'y';1382 # $sql .= "records r ";1383 # $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id ";1384 1385 # get the long record ID, if any1386 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM ".($defrec eq 'y' ? 'default_' : '')."records ".1387 "WHERE record_id=?", undef, ($id) );1388 1389 1364 local $dbh->{AutoCommit} = 0; 1390 1365 local $dbh->{RaiseError} = 1; 1391 1366 1392 1367 eval { 1393 # there's really no tidy way to squash this down. :/1394 if (length($val) > 100) {1395 if ($longid) {1396 $dbh->do("UPDATE longrecs SET recdata=? WHERE longrec_id=?", undef, ($val, $longid) );1397 } else {1398 ##fixme: has to be a better way to be sure we get the right recid back once inserted...1399 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) );1400 my ($newlongid) = $dbh->selectrow_array("SELECT currval('longrecs_longrec_id_seq')");1401 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=? ".1402 "WHERE record_id=?", undef, ('', $newlongid, $id) );1403 }1404 } else {1405 if ($longid) {1406 $dbh->do("DELETE FROM longrecs WHERE longrec_id=?", undef, ($longid) );1407 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=NULL ".1408 "WHERE record_id=?", undef, ($val, $id) );1409 } else {1410 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=? ".1411 "WHERE record_id=?", undef, ($val, $id) );1412 }1413 }1414 1415 1368 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ". 1416 "SET host=?, type=?,ttl=?,distance=?,weight=?,port=? ".1417 "WHERE record_id=?", undef, ($host, $ type, $ttl, $dist, $weight, $port, $id) );1418 1369 "SET host=?,val=?,type=?,ttl=?,distance=?,weight=?,port=? ". 1370 "WHERE record_id=?", undef, ($host, $val, $type, $ttl, $dist, $weight, $port, $id) ); 1371 $dbh->commit; 1419 1372 }; 1420 1373 if ($@) { … … 1423 1376 return ('FAIL', $msg); 1424 1377 } 1425 # return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err;1426 1378 1427 1379 return ('OK','OK'); … … 1691 1643 ## but don't really seem enthusiastic about it. 1692 1644 my $rrdata = $rr->txtdata; 1693 if (length($rrdata) > 100 ) { 1694 # extralong records get an entry in a separate table. 1695 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($rrdata) ); 1696 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($rrdata) ); 1697 $sql .= ",longrec_id"; 1698 $vallen .= ",?"; 1699 push @vallist, ''; 1700 push @vallist, $longid; 1701 } else { 1702 push @vallist, $rrdata; 1703 } 1645 push @vallist, $rrdata; 1704 1646 } elsif ($type eq 'SPF') { 1705 1647 ##fixme: and the same caveat here, since it is apparently a clone of ::TXT 1706 1648 my $rrdata = $rr->txtdata; 1707 if (length($rrdata) > 100 ) { 1708 # extralong records get an entry in a separate table. 1709 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($rrdata) ); 1710 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($rrdata) ); 1711 $sql .= ",longrec_id"; 1712 $vallen .= ",?"; 1713 push @vallist, ''; 1714 push @vallist, $longid; 1715 } else { 1716 push @vallist, $rrdata; 1717 } 1649 push @vallist, $rrdata; 1718 1650 } elsif ($type eq 'AAAA') { 1719 1651 push @vallist, $rr->address; … … 1730 1662 } else { 1731 1663 my $rrdata = $rr->rdatastr; 1732 if (length($rrdata) > 100 ) { 1733 # extralong records get an entry in a separate table. 1734 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($rrdata) ); 1735 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($rrdata) ); 1736 $sql .= ",longrec_id"; 1737 $vallen .= ",?"; 1738 push @vallist, ''; 1739 push @vallist, $longid; 1740 } else { 1741 push @vallist, $rrdata; 1742 } 1664 push @vallist, $rrdata; 1743 1665 # Finding a different record type is not fatal.... just problematic. 1744 1666 # We may not be able to export it correctly. … … 1858 1780 1859 1781 my $domsth = $dbh->prepare("SELECT domain_id,domain,status FROM domains WHERE status=1"); 1860 my $recsth = $dbh->prepare("SELECT r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,l.recdata".1861 "FROM records r LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_idWHERE domain_id=?");1782 my $recsth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl ". 1783 "FROM records WHERE domain_id=?"); 1862 1784 $domsth->execute(); 1863 1785 while (my ($domid,$dom,$domstat) = $domsth->fetchrow_array) { 1864 1786 $recsth->execute($domid); 1865 while (my ($host,$type,$val,$dist,$weight,$port,$ttl ,$lval) = $recsth->fetchrow_array) {1787 while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $recsth->fetchrow_array) { 1866 1788 ##fixme: need to store location in the db, and retrieve it here. 1867 1789 # temporarily hardcoded to empty so we can include it further down. … … 1873 1795 # ~~ 2^62 + time() 1874 1796 my $stamp = ''; 1875 1876 $val = $lval if $lval;1877 1797 1878 1798 # raw packet in unknown format: first byte indicates length -
trunk/dns.sql
r102 r130 8 8 CREATE TABLE default_records ( 9 9 record_id serial NOT NULL, 10 longrec_id integer,11 10 group_id integer DEFAULT 1 NOT NULL, 12 host character varying(100) DEFAULT ''::character varyingNOT NULL,11 host text DEFAULT '' NOT NULL, 13 12 "type" integer DEFAULT 1 NOT NULL, 14 val character varying(100) DEFAULT ''::character varyingNOT NULL,13 val text DEFAULT '' NOT NULL, 15 14 distance integer DEFAULT 0 NOT NULL, 16 15 weight integer DEFAULT 0 NOT NULL, 17 16 port integer DEFAULT 0 NOT NULL, 18 17 ttl integer DEFAULT 86400 NOT NULL, 19 description character varying(255)18 description text 20 19 ); 21 20 … … 97 96 domain_id integer NOT NULL, 98 97 record_id serial NOT NULL, 99 longrec_id integer, 100 host character varying(100) DEFAULT ''::character varying NOT NULL, 98 host text DEFAULT '' NOT NULL, 101 99 "type" integer DEFAULT 1 NOT NULL, 102 val character varying(100) DEFAULT ''::character varyingNOT NULL,100 val text DEFAULT '' NOT NULL, 103 101 distance integer DEFAULT 0 NOT NULL, 104 102 weight integer DEFAULT 0 NOT NULL, 105 103 port integer DEFAULT 0 NOT NULL, 106 104 ttl integer DEFAULT 7200 NOT NULL, 107 description character varying(255) 108 ); 109 110 CREATE TABLE longrecs ( 111 longrec_id serial NOT NULL, 112 recdata text 105 description text 113 106 ); 114 107
Note:
See TracChangeset
for help on using the changeset viewer.