From: Ian Jackson Date: Mon, 2 Nov 2009 17:55:38 +0000 (+0000) Subject: Can compact ids and remove obsolete commodities X-Git-Tag: 6.0~15 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=commitdiff_plain;h=ee1e57fa0fab5d840206d73a3968fd59d7fa7127 Can compact ids and remove obsolete commodities --- diff --git a/yarrg/CommodsDatabase.pm b/yarrg/CommodsDatabase.pm index 6ed0b74..d978358 100644 --- a/yarrg/CommodsDatabase.pm +++ b/yarrg/CommodsDatabase.pm @@ -188,7 +188,7 @@ END nooutput(<prepare(<<'END') + my $insert= $dbh->prepare(<<'END'); INSERT OR IGNORE INTO commods (unitmass, unitvolume, commodname) VALUES (?,?,?); END - ; - my $setsizes= $dbh->prepare(<<'END') + my $setsizes= $dbh->prepare(<<'END'); UPDATE commods SET unitmass = ?, unitvolume = ? WHERE commodname = ? END - ; - my $setordval= $dbh->prepare(<<'END') + my $setordval= $dbh->prepare(<<'END'); UPDATE commods SET ordval = ? WHERE commodname = ? END - ; - my $setclass= $dbh->prepare(<<'END') + my $setclass= $dbh->prepare(<<'END'); UPDATE commods SET commodclass = ? WHERE commodname = ? END - ; - my $setinclass= $dbh->prepare(<<'END') + my $setinclass= $dbh->prepare(<<'END'); UPDATE commods SET inclass = ? WHERE commodname = ? END - ; my %incl; foreach my $commod (sort { commodsortkey($a) cmp commodsortkey($b) @@ -331,15 +326,60 @@ END db_doall(<prepare(<<'END') + my $addclass= $dbh->prepare(<<'END'); INSERT INTO commodclasses (commodclass, size) VALUES (?,?) END - ; foreach my $cl (sort keys %incl) { $addclass->execute($cl, $incl{$cl}); } + + my $search= $dbh->prepare(<<'END'); + SELECT commodname,commodid FROM commods; +END + my %check; + foreach my $bs (qw(buy sell)) { + $check{$bs}= $dbh->prepare(<prepare(<<'END'); + DELETE FROM commods WHERE commodid = ? +END + $search->execute(); + my $any=0; + while (my $row= $search->fetchrow_hashref()) { + next if defined $commods{$row->{'commodname'}}; + print $any++ ? '; ' : " Dropping old commodities: ", + $row->{'commodname'}; + foreach my $bs (qw(buy sell)) { + $check{$bs}->execute($row->{'commodid'}); + my $problem= $check{$bs}->fetchrow_hashref(); + if ($problem) { + print "\n"; + die <{'commodid'} + $row->{'commodname'} + but + $bs + $problem->{'islandname'} + $problem->{'stallname'} + $problem->{'qty'} at $problem->{'price'} +END + } + } + $delete->execute($row->{'commodid'}); + } + print ".\n" if $any; + db_check_referential_integrity(); } @@ -361,10 +401,69 @@ END } +#---------- compact IDs ---------- + +sub getminmax ($$$) { + my ($tab,$minmax,$f) = @_; + my $sth= $dbh->prepare("SELECT $minmax($f) FROM $tab"); + $sth->execute(); + my ($val)= $sth->fetchrow_array(); + return defined($val) ? $val : '?'; +} + +foreach my $cp (@need_compact) { + print " Compacting $cp->{Table}"; + my $tab= $cp->{Table}; + my $id= $cp->{Id}; + my $tmp_field_specs= $cp->{FieldSpecs}; + my $fields= join ',', @{$cp->{Fields}}; + $tmp_field_specs =~ s/\bprimary key\b/UNIQUE/i or + die "$tab $tmp_field_specs ?"; + db_doall(<1..%d:", + $cp->{Id}, + getminmax($tab,'min',$id), + $oldmax, + getminmax("aside_$tab",'max',"new_$id")); + my @updates= @{ $cp->{Updates} }; + while (@updates) { + my $utabs= shift @updates; + my $ufields= shift @updates; + foreach my $utab (@$utabs) { + printf(" %s",$utab); + my $fh= '.'; + foreach my $ufield (@$ufields) { + printf("%s%s",$fh,$ufield); $fh=','; + db_doall(<{AutoCommit} = 1; - print " Vacuuming.\n"; $dbh->do('VACUUM'); } diff --git a/yarrg/devel-notes b/yarrg/devel-notes index f4f6066..430523a 100644 --- a/yarrg/devel-notes +++ b/yarrg/devel-notes @@ -1,21 +1,4 @@ -removing an obsolete commodity: - - select * from (select * from sell union select * from buy) left outer join commods using (commodid) where commods.commodname = 'Black dye' limit 10; - -if that produces no output then: - - begin; - delete from commods where commodname like 'Black dye'; - select * from (select * from sell union select * from buy) left outer join commods using (commodid) where commods.commodname is null limit 10; - -and if that produces no output then: - commit; -otherwise - rollback; - -======================================= - ceb's example route: alpha,byrne,papaya,turtle,jorvik,luthien