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