source: wiki-toolkit/trunk/lib/Wiki/Toolkit/Store/SQLite.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.4 KB
Line 
1package Wiki::Toolkit::Store::SQLite;
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::SQLite - SQLite storage backend for Wiki::Toolkit
16
17=head1 SYNOPSIS
18
19See Wiki::Toolkit::Store::Database
20
21=cut
22
23# Internal method to return the data source string required by DBI.
24sub _dsn {
25    my ($self, $dbname) = @_;
26    return "dbi:SQLite:dbname=$dbname";
27}
28
29=head1 METHODS
30
31=over 4
32
33=item B<new>
34
35  my $store = Wiki::Toolkit::Store::SQLite->new( dbname => "wiki" );
36
37The dbname parameter is mandatory.
38
39=cut
40
41sub new {
42    my ($class, %args) = @_;
43    my $self = {};
44    bless $self, $class;
45    @args{qw(dbuser dbpass)} = ("", "");  # for the parent class _init
46    return $self->_init(%args);
47}
48
49=over 4
50
51=item B<check_and_write_node>
52
53  $store->check_and_write_node( node     => $node,
54                checksum => $checksum,
55                                %other_args );
56
57Locks the node, verifies the checksum, calls
58C<write_node_post_locking> with all supplied arguments, unlocks the
59node. Returns the version of the updated node on successful writing, 0 if
60checksum doesn't match, -1 if the change was not applied, croaks on error.
61
62=cut
63
64sub check_and_write_node {
65    my ($self, %args) = @_;
66    my ($node, $checksum) = @args{qw( node checksum )};
67
68    my $dbh = $self->{_dbh};
69    $dbh->{AutoCommit} = 0;
70
71    my $ok = eval {
72        $dbh->do("END TRANSACTION");
73        $dbh->do("BEGIN TRANSACTION");
74        $self->verify_checksum($node, $checksum) or return 0;
75        $self->write_node_post_locking( %args );
76    };
77    if ($@) {
78        my $error = $@;
79        $dbh->rollback;
80        $dbh->{AutoCommit} = 1;
81        if ( $error =~ /database is locked/
82            or $error =~ /DBI connect.+failed/ ) {
83            return 0;
84        } else {
85            croak "Unhandled error: [$error]";
86        }
87    } else {
88        $dbh->commit;
89        $dbh->{AutoCommit} = 1;
90        return $ok;
91    }
92}
93
94sub _get_lowercase_compare_sql {
95    my ($self, $column) = @_;
96    return "$column LIKE ?";
97}
98
99sub _get_comparison_sql {
100    my ($self, %args) = @_;
101    if ( $args{ignore_case} ) {
102        return "$args{thing1} LIKE $args{thing2}";
103    } else {
104        return "$args{thing1} = $args{thing2}";
105    }
106}
107
108sub _get_node_exists_ignore_case_sql {
109    return "SELECT name FROM node WHERE name LIKE ? ";
110}
111
1121;
Note: See TracBrowser for help on using the repository browser.