my $st_delete_set = $DB->prepare("DELETE FROM dvd_set WHERE id = ?");
my $st_get_discs = $DB->prepare
- ("SELECT disc, path FROM dvd_disc
+ ("SELECT disc, path, box FROM dvd_disc
WHERE set_id = ? AND ? <= disc AND disc < ?
ORDER BY disc");
my $st_add_disc = $DB->prepare
- ("INSERT INTO dvd_disc (set_id, disc, path) VALUES (?, ?, ?)");
+ ("INSERT INTO dvd_disc (set_id, disc, path, box) VALUES (?, ?, ?, ?)");
my $st_update_disc = $DB->prepare
- ("UPDATE dvd_disc SET path = ?, disc_id = NULL
- WHERE set_id = ? AND disc = ?");
+ ("UPDATE dvd_disc SET path = ?, box = ? WHERE set_id = ? AND disc = ?");
my $st_delete_disc_range = $DB->prepare
("DELETE FROM dvd_disc WHERE set_id = ? AND ? <= disc AND disc < ?");
my $st_delete_discs = $DB->prepare
my ($id, $name, $ndisc) = (undef, undef, -1);
my @path;
+my %box;
sub flush_set () {
defined $id or return;
my $i = 0;
DISC: for (;;) {
my @r = $st_get_discs->fetchrow_array; last DISC unless @r;
- my ($disc, $old_path) = @r;
+ my ($disc, $old_path, $old_box) = @r; $old_box //= "#nil";
$disc == $i or die "unexpected disc number";
- my $path = $path[$i++];
- if (defined $path && (!defined $old_path || $path ne $old_path))
- { $st_update_disc->execute($path, $id, $disc); }
+ my $path = $path[$i++]; my $box = $box{$path};
+ if (defined $path &&
+ (!defined $old_path || $path ne $old_path || $old_box ne $box)) {
+ $st_update_disc->execute($path, $box eq "#nil" ? undef : $box,
+ $id, $disc);
+ }
}
$i == $min_ndisc or die "missing disc records";
$st_delete_disc_range->execute($id, $min_ndisc, $old_ndisc);
}
- for (my $i = $min_ndisc; $i < $ndisc; $i++)
- { $st_add_disc->execute($id, $i, $path[$i]); }
+ for (my $i = $min_ndisc; $i < $ndisc; $i++) {
+ my $box = $box{$path[$i]};
+ $st_add_disc->execute($id, $i, $path[$i],
+ $box eq "#nil" ? undef : $box);
+ }
- $id = undef; @path = ();
+ $id = undef; @path = (); %box = ();
}
+my $curbox = "#nil";
LINE: while (<>) {
chomp;
- if (/^ \[ \# (\d+ | NEW | UNK) \s* : \s* (\d+ | \* | DEL) ]
- \s* (\S .*)? $/x) {
+ if (/^ \s* (\; .*)? $/x) {
+ next LINE;
+ } elsif (/^ \[ \# (\d+ | NEW | UNK) \s* : \s* (\d+ | \* | DEL) ]
+ \s* (\S .*)? $/x) {
flush_set;
($id, $ndisc, $name) = ($1, $2, $3);
} else {
defined $name or die "missing name";
}
+ } elsif (/^ \s* \! \s* box \s+ (\S .*) $/x) {
+ $curbox = $1;
} elsif (/^ \s+ (?: !! \s*)? (\S .*) $/x) {
my $path = $1;
defined $id or die "no active set";
- push @path, $path;
- } elsif (/^ .* \S .* $/) {
+ push @path, $path; $box{$path} = $curbox;
+ } else {
die "unrecognized line `$_'";
}
}