File Coverage

blib/lib/Bot/BasicBot/Pluggable/Module/SimpleBlog/Store/SQLite.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 14 57.1
condition n/a
subroutine 9 9 100.0
pod 3 4 75.0
total 58 65 89.2


line stmt bran cond sub pod time code
1             package Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite;
2              
3 1     1   1098 use strict;
  1         2  
  1         27  
4 1     1   5 use vars qw( $VERSION );
  1         2  
  1         46  
5             $VERSION = '0.03';
6              
7 1     1   5 use base qw(Bot::BasicBot::Pluggable::Module::Base);
  1         1  
  1         103  
8              
9 1     1   7 use Carp;
  1         2  
  1         64  
10 1     1   620 use DBD::SQLite;
  1         25667  
  1         333  
11              
12             =head1 NAME
13              
14             Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite - SQLite storage for Bot::BasicBot::Pluggable::Module::SimpleBlog.
15              
16             =head1 SYNOPSIS
17              
18             use Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite;
19              
20             my $blog_store =
21             Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite->new(
22             "/home/bot/brane.db" );
23              
24             =head1 DESCRIPTION
25              
26             Store URLs in a sqlite database for
27             L<Bot::BasicBot::Pluggable::Module::SimpleBot>.
28              
29             =head1 IMPORTANT NOTE WHEN UPGRADING FROM PRE-0.02 VERSIONS
30              
31             I'd made a thinko in version 0.01 in one of the column names in the
32             table used to store the URLs in the database, so you'll have to delete
33             your store file and start again. It didn't seem worth automatically
34             detecting and fixing this since I only released 0.01 yesterday and I
35             don't expect anyone to have installed it yet.
36              
37             =head1 METHODS
38              
39             =over 4
40              
41             =item B<new>
42              
43             my $blog_store =
44             Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite->new(
45             "/home/bot/brane.db" );
46              
47             You must supply a filename writeable by the user the bot runs as. The
48             file need not already exist; it will be created and the correct
49             database schema set up as necessary.
50              
51             Croaks if L<DBD::SQLite> fails to connect to the file.
52              
53             =cut
54              
55             sub new {
56 3     3 1 1796 my ($class, $filename) = @_;
57              
58 3 50       17 my $dbh = DBI->connect("dbi:SQLite:dbname=$filename", "", "")
59             or croak "ERROR: Can't connect to sqlite database: " . DBI->errstr;
60              
61 2         1018 my $self = { };
62 2         5 bless $self, $class;
63              
64 2         8 $self->{dbh} = $dbh;
65 2 50       9 $self->ensure_db_schema_correct or return;
66 2         11 return $self;
67             }
68              
69             =item B<dbh>
70              
71             my $dbh = $store->dbh;
72              
73             Returns the store's database handle.
74              
75             =cut
76              
77             sub dbh {
78 1     1 1 360 my $self = shift;
79 1         3 return $self->{dbh};
80             }
81              
82             sub ensure_db_schema_correct {
83 2     2 0 3 my $self = shift;
84 2         5 my $dbh = $self->{dbh};
85              
86 2         4 my $sql = "SELECT name FROM sqlite_master WHERE type='table'
87             AND name='blogged'";
88 2 50       11 my $sth = $dbh->prepare($sql)
89             or croak "ERROR: " . $dbh->errstr;
90 2         548 $sth->execute;
91 2         24 my ($ok) = $sth->fetchrow_array;
92 2 100       17 return 1 if $ok;
93              
94 1 50       5 $dbh->do("CREATE TABLE blogged
95             ( timestamp text, name text, channel text, url text, comment text )" )
96             or croak "ERROR: " . $dbh->errstr;
97 1         7220 return 1;
98             }
99              
100             =item B<store>
101              
102             $store->store( timestamp => $timestamp,
103             name => $who,
104             channel => $channel,
105             url => $url,
106             comment => $comment );
107              
108             Stores the given information in the database. Croaks on error.
109              
110             =cut
111              
112             sub store {
113 1     1 1 28 my ($self, %args) = @_;
114 1         3 my $dbh = $self->{dbh};
115              
116 1 50       7 my $sth = $dbh->prepare( qq{
117             INSERT INTO blogged (timestamp, name, channel, url, comment)
118             VALUES (?, ?, ?, ?, ?)
119             }) or return "Error: can't insert into database: " . $dbh->errstr;
120              
121 1 50       9072 $sth->execute( @args{ qw( timestamp name channel url comment ) } )
122             or croak "Error: can't insert into database: " . $dbh->errstr;
123              
124 1         24 return 1;
125             }
126              
127             =head1 BUGS
128              
129             No retrieval methods yet.
130              
131             =head1 SEE ALSO
132              
133             =over 4
134              
135             =item * L<Bot::BasicBot::Pluggable::Module::SimpleBlog>
136              
137             =back
138              
139             =head1 AUTHOR
140              
141             Kake Pugh (kake@earth.li).
142              
143             =head1 COPYRIGHT
144              
145             Copyright (C) 2003 Kake Pugh. All Rights Reserved.
146              
147             This module is free software; you can redistribute it and/or modify it
148             under the same terms as Perl itself.
149              
150             =cut
151              
152             1;