source: wiki-toolkit/trunk/lib/Wiki/Toolkit/Store/Pg.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.2 KB
Line 
1package Wiki::Toolkit::Store::Pg;
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.06;
12
13=head1 NAME
14
15Wiki::Toolkit::Store::Pg - Postgres 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:Pg:dbname=$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
51=cut
52
53sub check_and_write_node {
54    my ($self, %args) = @_;
55    my ($node, $checksum) = @args{qw( node checksum )};
56
57    my $dbh = $self->{_dbh};
58    $dbh->{AutoCommit} = 0;
59
60    my $ok = eval {
61        $dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
62        $self->verify_checksum($node, $checksum) or return 0;
63        $self->write_node_post_locking( %args );
64    };
65    if ($@) {
66        my $error = $@;
67        $dbh->rollback;
68        $dbh->{AutoCommit} = 1;
69        if ( $error =~ /can't serialize access due to concurrent update/i
70            or $error =~ /could not serialize access due to concurrent update/i
71           ) {
72            return 0;
73        } else {
74            croak $error;
75        }
76    } else {
77        $dbh->commit;
78        $dbh->{AutoCommit} = 1;
79        return $ok;
80    }
81}
82
83sub _get_comparison_sql {
84    my ($self, %args) = @_;
85    if ( $args{ignore_case} ) {
86        return "lower($args{thing1}) = lower($args{thing2})";
87    } else {
88        return "$args{thing1} = $args{thing2}";
89    }
90}
91
92sub _get_node_exists_ignore_case_sql {
93    return "SELECT name FROM node WHERE lower(name) = lower(?) ";
94}
95
961;
Note: See TracBrowser for help on using the repository browser.