File Coverage

blib/lib/Memorator/Backend.pm
Criterion Covered Total %
statement 15 44 34.0
branch 0 2 0.0
condition n/a
subroutine 5 16 31.2
pod 10 10 100.0
total 30 72 41.6


line stmt bran cond sub pod time code
1             package Memorator::Backend::Mojo::SQLite;
2 1     1   1695 use strict;
  1         1  
  1         24  
3 1     1   4 use warnings;
  1         1  
  1         35  
4             { our $VERSION = '0.006'; }
5              
6 1     1   4 use Memorator::Util ();
  1         2  
  1         15  
7 1     1   4 use constant TABLE_NAME => 'eid2jid';
  1         1  
  1         46  
8              
9 1     1   5 use Mojo::Base -base;
  1         1  
  1         5  
10              
11             has mojodb => sub { die "missing mandatory parameter 'mojodb'" };
12             has name => sub { die "missing mandatory parameter 'name'" };
13              
14             sub add_mapping {
15 0     0 1   my ($self, $eid, $jid) = @_;
16 0           $self->_db->insert($self->table_name => {eid => $eid, jid => $jid});
17             }
18              
19 0     0     sub _db { return shift->mojodb->db }
20              
21             sub deactivate_mapping {
22 0     0 1   my ($self, $id) = @_;
23 0           $self->_db->query($self->deactivate_mapping_query($id));
24             }
25              
26             sub deactivate_mapping_query {
27 0     0 1   my ($self, $id) = @_;
28 0           my $table = $self->table_name;
29 0           return ("UPDATE $table SET active = 0 WHERE id = ?", $id);
30             }
31              
32             sub ensure_table {
33 0     0 1   my $self = shift;
34 0           $self->mojodb->migrations->name($self->name)
35             ->from_string($self->migration)->migrate;
36 0           return $self;
37             } ## end sub ensure_table
38              
39             sub mapping_between {
40 0     0 1   my ($self, $eid, $jid) = @_;
41 0           my $res = $self->_db->query($self->mapping_between_query($eid, $jid));
42 0           my $e2j = $res->hash;
43 0           $res->finish;
44 0           return $e2j;
45             } ## end sub mapping_between
46              
47             sub mapping_between_query {
48 0     0 1   my ($self, $eid, $jid) = @_;
49 0           my $table = $self->table_name;
50 0           my $query =
51             "SELECT * FROM $table "
52             . " WHERE jid = ? AND eid = ? AND active > 0 "
53             . " AND id IN (SELECT MAX(id) FROM $table WHERE eid = ?)";
54 0           return ($query, $jid, $eid, $eid);
55             } ## end sub mapping_between_query
56              
57             sub remove_mapping {
58 0     0 1   my ($self, $id) = @_;
59 0           $self->_db->delete($self->table_name, {id => $id});
60             }
61              
62             sub stale_mappings {
63 0     0 1   my $self = shift;
64 0 0         my $res = $self->_db->query($self->stale_mappings_query) or return;
65 0           return $res->hashes->each;
66             }
67              
68             sub stale_mappings_query {
69 0     0 1   my $self = shift;
70 0           my $table = $self->table_name;
71             return
72 0           "SELECT * FROM $table "
73             . " WHERE (id, eid) NOT IN "
74             . " (SELECT MAX(id), eid FROM $table GROUP BY eid)";
75             } ## end sub stale_mappings_query
76              
77 0     0 1   sub table_name { Memorator::Util::local_name(shift->name, TABLE_NAME) }
78              
79             1;