SimpleBlog/Store/SQLite.pm

filter: Statementstmt 100% 40 / 40 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 75.0% 3 / 4 totaltotal 89.5% 60 / 67 CC 8 SCAR 9.3
CC
8
Cov
89%
ensure_db_schema_correct
4.0
store
3.1
new
3.1
CRAP 2.5
« SimpleBlog.pm ↑ Summary
1 package Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = '0.02';
6  
7 1 ▶use base qw(Bot::BasicBot::Pluggable::Module::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ my ($class, $filename) = @_;
Subroutine new: called CC=3 SCAR=11.2
Pod documented
57  
58 3 ▶ my $dbh = DBI->connect("dbi:SQLite:dbname=$filename", "", "")
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("dbi:SQLite:dbname=$filename", "", "") or croak(q[ERROR: Can't connect to sqlite database: ] . "DBI"->errstr) 0 2 2
59 or croak "ERROR: Can't connect to sqlite database: " . DBI->errstr;
60  
61 2 my $self = { };
62 2 bless $self, $class;
63  
64 2 $self->{dbh} = $dbh;
65 2 ▶ $self->ensure_db_schema_correct or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->ensure_db_schema_correct or return 0 2 2
66 2 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 ▶ my $self = shift;
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
79 1 return $self->{dbh};
80 }
81  
82 sub ensure_db_schema_correct {
83 2 ▶ my $self = shift;
Subroutine ensure_db_schema_correct: called CC=4 SCAR=14.0
Pod undocumented
84 2 my $dbh = $self->{dbh};
85  
86 2 my $sql = "SELECT name FROM sqlite_master WHERE type='table'
87 AND name='blogged'";
88 2 ▶ my $sth = $dbh->prepare($sql)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare($sql) or croak('ERROR: ' . $dbh->errstr) 0 2 2
89 or croak "ERROR: " . $dbh->errstr;
90 2 $sth->execute;
91 2 my ($ok) = $sth->fetchrow_array;
92 2 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 1 2
93  
94 1 ▶ $dbh->do("CREATE TABLE blogged
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE TABLE blogged\n ( timestamp text, name text, channel text, url text, comment text )") or croak('ERROR: ' . $dbh->errstr) 0 1 1
95 ( timestamp text, name text, channel text, url text, comment text )" )
96 or croak "ERROR: " . $dbh->errstr;
97 1 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 ▶ my ($self, %args) = @_;
Subroutine store: called CC=3 SCAR=11.3
Pod documented
114 1 my $dbh = $self->{dbh};
115  
116 1 ▶ my $sth = $dbh->prepare( qq{
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("\n INSERT INTO blogged (timestamp, name, channel, url, comment)\n VALUES (?, ?, ?, ?, ?)\n ") or return q[Error: can't insert into database: ] . $dbh->errstr 0 1 1
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 ▶ $sth->execute( @args{ qw( timestamp name channel url comment ) } )
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@args{"timestamp", "name", "channel", "url", "comment"}) or croak(q[Error: can't insert into database: ] . $dbh->errstr) 0 1 1
122 or croak "Error: can't insert into database: " . $dbh->errstr;
123  
124 1 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 1;
« SimpleBlog.pm ↑ Summary