]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Over.pm
imap: STATUS/EXAMINE: rely on SQLite overview
[public-inbox.git] / lib / PublicInbox / Over.pm
index 286fb7f6b4f9b85062f0e5f39ab0c956000f9228..e0f20ea6d3da2de5a1e92cdd84fee2e66e68e4bd 100644 (file)
@@ -179,11 +179,12 @@ SELECT COUNT(num) FROM over WHERE num > 0
 sub get_art {
        my ($self, $num) = @_;
        my $dbh = $self->connect;
-       my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
+       my $sth = $dbh->prepare_cached(<<'', undef, 1);
 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
 
-       return load_from_row($smsg) if $smsg;
-       undef;
+       $sth->execute($num);
+       my $smsg = $sth->fetchrow_hashref;
+       $smsg ? load_from_row($smsg) : undef;
 }
 
 sub next_by_mid {
@@ -215,4 +216,47 @@ SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
        load_from_row($smsg);
 }
 
+# IMAP search
+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 LIMIT ' . DEFAULT_LIMIT;
+       $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_status {
+       my ($self, $uid_base, $uid_end) = @_;
+       my $dbh = $self->connect;
+       my $sth = $dbh->prepare_cached(<<'', undef, 1);
+SELECT COUNT(num) FROM over WHERE num > ? AND num <= ?
+
+       $sth->execute($uid_base, $uid_end);
+       my $exists = $sth->fetchrow_array;
+
+       $sth = $dbh->prepare_cached(<<'', undef, 1);
+SELECT MAX(num) + 1 FROM over WHERE num <= ?
+
+       $sth->execute($uid_end);
+       my $uidnext = $sth->fetchrow_array;
+
+       $sth = $dbh->prepare_cached(<<'', undef, 1);
+SELECT MAX(num) FROM over WHERE num > 0
+
+       ($exists, $uidnext, $sth->fetchrow_array // 0);
+}
+
 1;