source: wiki-toolkit/trunk/lib/Wiki/Toolkit/Store/MySQL.pm

Last change on this file was 551, checked in by Dominic Hargreaves, 9 years ago

update copyright year/versions

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