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

Last change on this file since 477 was 477, checked in by kake, 13 years ago

Applied a modified version of tgj's patch (OpenGuides? ticket #23). Also fixed minor POD error while I was at it.

  • 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.04;
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=cut
56
57sub check_and_write_node {
58    my ($self, %args) = @_;
59    my ($node, $checksum) = @args{qw( node checksum )};
60    $self->_lock_node($node) or croak "Can't lock node";
61    my $ok = $self->verify_checksum($node, $checksum);
62    unless ($ok) {
63        $self->_unlock_node($node) or carp "Can't unlock node";
64        return 0;
65    }
66    $ok = $self->write_node_post_locking( %args );
67    $self->_unlock_node($node) or carp "Can't unlock node";
68    return $ok;
69}
70
71# Returns 1 if we can get a lock, 0 if we can't, croaks on error.
72sub _lock_node {
73    my ($self, $node) = @_;
74    my $dbh = $self->{_dbh};
75    $node = $dbh->quote($node);
76    my $sql = "SELECT GET_LOCK($node, 10)";
77    my $sth = $dbh->prepare($sql);
78    $sth->execute or croak $dbh->errstr;
79    my $locked = $sth->fetchrow_array;
80    $sth->finish;
81    return $locked;
82}
83
84# Returns 1 if we can unlock, 0 if we can't, croaks on error.
85sub _unlock_node {
86    my ($self, $node) = @_;
87    my $dbh = $self->{_dbh};
88    $node = $dbh->quote($node);
89    my $sql = "SELECT RELEASE_LOCK($node)";
90    my $sth = $dbh->prepare($sql);
91    $sth->execute or croak $dbh->errstr;
92    my $unlocked = $sth->fetchrow_array;
93    $sth->finish;
94    return $unlocked;
95}
96
97sub _get_casesensitive_compare_sql {
98    my ($self, $column) = @_;
99    return "BINARY $column = ?";
100}
101
1021;
Note: See TracBrowser for help on using the repository browser.