+# IMAP search, this is limited by callers to UID_SLICE size (50K)
+sub uid_range {
+ my ($self, $beg, $end, $sql) = @_;
+ my $dbh = $self->connect;
+ my $q = 'SELECT num FROM over WHERE num >= ? AND num <= ?';
+
+ # This is read-only, anyways; but caller should verify it's
+ # only sending \A[0-9]+\z for ds and ts column ranges
+ $q .= $$sql if $sql;
+ $q .= ' ORDER BY num ASC';
+ $dbh->selectcol_arrayref($q, undef, $beg, $end);
+}
+
+sub max {
+ my ($self) = @_;
+ my $sth = $self->connect->prepare_cached(<<'', undef, 1);
+SELECT MAX(num) FROM over WHERE num > 0
+
+ $sth->execute;
+ $sth->fetchrow_array // 0;
+}
+
+sub imap_exists {
+ my ($self, $uid_base, $uid_end) = @_;
+ my $sth = $self->connect->prepare_cached(<<'', undef, 1);
+SELECT COUNT(num) FROM over WHERE num > ? AND num <= ?
+
+ $sth->execute($uid_base, $uid_end);
+ $sth->fetchrow_array;
+}
+
+sub check_inodes {
+ my ($self) = @_;
+ my $dbh = $self->{dbh} or return;
+ my $f = $dbh->sqlite_db_filename;
+ if (my @st = stat($f)) { # did st_dev, st_ino change?
+ my $st = pack('dd', $st[0], $st[1]);
+
+ # don't actually reopen, just let {dbh} be recreated later
+ if ($st ne ($self->{st} // $st)) {
+ delete($self->{dbh});
+ $self->{filename} = $f;
+ }
+ } else {
+ warn "W: stat $f: $!\n";
+ }
+}
+