File Coverage

blib/lib/Wiki/Toolkit/Store/MySQL.pm
Criterion Covered Total %
statement 12 47 25.5
branch 0 16 0.0
condition n/a
subroutine 4 9 44.4
pod 1 1 100.0
total 17 73 23.2


line stmt bran cond sub pod time code
1             package Wiki::Toolkit::Store::MySQL;
2              
3 2     2   1280 use strict;
  2         2  
  2         57  
4              
5 2     2   7 use vars qw( @ISA $VERSION );
  2         2  
  2         93  
6              
7 2     2   587 use Wiki::Toolkit::Store::Database;
  2         4  
  2         56  
8 2     2   12 use Carp qw/carp croak/;
  2         2  
  2         764  
9              
10             @ISA = qw( Wiki::Toolkit::Store::Database );
11             $VERSION = 0.05;
12              
13             =head1 NAME
14              
15             Wiki::Toolkit::Store::MySQL - MySQL storage backend for Wiki::Toolkit
16              
17             =head1 REQUIRES
18              
19             Subclasses Wiki::Toolkit::Store::Database.
20              
21             =head1 SYNOPSIS
22              
23             See Wiki::Toolkit::Store::Database
24              
25             =cut
26              
27             # Internal method to return the data source string required by DBI.
28             sub _dsn {
29 0     0     my ($self, $dbname, $dbhost, $dbport) = @_;
30 0           my $dsn = "dbi:mysql:$dbname";
31 0 0         $dsn .= ";host=$dbhost" if $dbhost;
32 0 0         $dsn .= ";port=$dbport" if $dbport;
33 0           return $dsn;
34             }
35              
36             =head1 METHODS
37              
38             =over 4
39              
40             =item B
41              
42             $store->check_and_write_node( node => $node,
43             checksum => $checksum,
44             %other_args );
45              
46             Locks the node, verifies the checksum, calls
47             C with all supplied arguments, unlocks the
48             node. Returns the version of the updated node on successful writing, 0 if
49             checksum doesn't match, -1 if the change was not applied, croaks on error.
50              
51             Note: Uses MySQL's user level locking, so any locks are released when
52             the database handle disconnects. Doing it like this because I can't seem
53             to get it to work properly with transactions.
54              
55             =back
56              
57             =cut
58              
59             sub check_and_write_node {
60 0     0 1   my ($self, %args) = @_;
61 0           my ($node, $checksum) = @args{qw( node checksum )};
62 0 0         $self->_lock_node($node) or croak "Can't lock node";
63 0           my $ok = $self->verify_checksum($node, $checksum);
64 0 0         unless ($ok) {
65 0 0         $self->_unlock_node($node) or carp "Can't unlock node";
66 0           return 0;
67             }
68 0           $ok = $self->write_node_post_locking( %args );
69 0 0         $self->_unlock_node($node) or carp "Can't unlock node";
70 0           return $ok;
71             }
72              
73             # Returns 1 if we can get a lock, 0 if we can't, croaks on error.
74             sub _lock_node {
75 0     0     my ($self, $node) = @_;
76 0           my $dbh = $self->{_dbh};
77 0           $node = $dbh->quote($node);
78 0           my $sql = "SELECT GET_LOCK($node, 10)";
79 0           my $sth = $dbh->prepare($sql);
80 0 0         $sth->execute or croak $dbh->errstr;
81 0           my $locked = $sth->fetchrow_array;
82 0           $sth->finish;
83 0           return $locked;
84             }
85              
86             # Returns 1 if we can unlock, 0 if we can't, croaks on error.
87             sub _unlock_node {
88 0     0     my ($self, $node) = @_;
89 0           my $dbh = $self->{_dbh};
90 0           $node = $dbh->quote($node);
91 0           my $sql = "SELECT RELEASE_LOCK($node)";
92 0           my $sth = $dbh->prepare($sql);
93 0 0         $sth->execute or croak $dbh->errstr;
94 0           my $unlocked = $sth->fetchrow_array;
95 0           $sth->finish;
96 0           return $unlocked;
97             }
98              
99             sub _get_casesensitive_compare_sql {
100 0     0     my ($self, $column) = @_;
101 0           return "BINARY $column = ?";
102             }
103              
104             1;