File Coverage

blib/lib/App/Alice/History.pm
Criterion Covered Total %
statement 12 34 35.2
branch n/a
condition 0 2 0.0
subroutine 4 10 40.0
pod 0 3 0.0
total 16 49 32.6


line stmt bran cond sub pod time code
1             package App::Alice::History;
2              
3 4     4   23 use Any::Moose;
  4         8  
  4         28  
4 4     4   6320 use AnyEvent::DBI;
  4         120618  
  4         158  
5 4     4   45 use AnyEvent::IRC::Util qw/filter_colors/;
  4         16  
  4         282  
6 4     4   5549 use SQL::Abstract;
  4         41564  
  4         1999  
7              
8             has dbi => (
9             is => 'ro',
10             isa => 'AnyEvent::DBI',
11             lazy => 1,
12             default => sub {
13             my $self = shift;
14             AnyEvent::DBI->new("DBI:SQLite:dbname=".$self->dbfile,"","");
15             }
16             );
17              
18             has sql => (
19             is => 'ro',
20             isa => 'SQL::Abstract',
21             default => sub {SQL::Abstract->new(cmp => "like")},
22             );
23              
24             has dbfile => (
25             is => 'ro',
26             isa => 'Str',
27             #required => 1,
28             );
29              
30             sub store {
31 0     0 0   my ($self, %fields) = @_;
32 0           my ($stmt, @bind) = $self->sql->insert("messages", \%fields);
33 0     0     $self->dbi->exec($stmt, @bind, sub {});
  0            
34             }
35              
36             sub range {
37 0     0 0   my $cb = pop;
38 0           my ($self, $user, $channel, $id, $limit) = @_;
39 0   0       $limit ||=5;
40             $self->dbi->exec(
41             "SELECT * FROM messages WHERE id < ? AND channel=? AND user=? ORDER BY id DESC LIMIT ?",
42             $id, $channel, $user, $limit, sub {
43 0     0     my $before = [ reverse @{$_[1]} ];
  0            
44             $self->dbi->exec(
45             "SELECT * FROM messages WHERE id > ? AND channel=? AND user=? ORDER BY id ASC LIMIT ?",
46             $id, $channel, $user, $limit, sub {
47 0           my $after = $_[1];
48 0           $cb->($before, $after);
49             }
50 0           );
51             }
52 0           );
53             }
54              
55              
56             sub search {
57 0     0 0   my $cb = pop;
58 0           my ($self, %query) = @_;
59 0           %query = map {$_ => "%$query{$_}%"} grep {$query{$_}} qw/body channel nick user/;
  0            
  0            
60 0           my ($stmt, @bind) = $self->sql->select("messages", '*', \%query, {-desc => 'id'});
61             $self->dbi->exec($stmt, @bind, sub {
62 0     0     my ($db, $rows, $rv) = @_;
63 0           $cb->($rows);
64 0           });
65             }
66              
67             __PACKAGE__->meta->make_immutable;
68             1;