chiark / gitweb /
Export for people's convenience
[ypp-sc-tools.main.git] / yarrg / CommodsDatabase.pm
1 # This is part of ypp-sc-tools, a set of third-party tools for assisting
2 # players of Yohoho Puzzle Pirates.
3 #
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
20 # are used without permission.  This program is not endorsed or
21 # sponsored by Three Rings.
22
23 package CommodsDatabase;
24
25 # Valid calling sequences:
26 #    db_setocean('Midnight')
27 #  [ db_filename() => 'OCEAN-Midnight.db'  also OK at any later time ]
28 #  [ db_writer() ]                         helpful but not essential
29 #    db_connect()
30 #  [ db_onconflict(sub { .... }) ]         essential if just dieing is not OK
31 #    $dbh->do(...), $dbh->prepare(...), db_doall("stmt;stmt;"), etc.
32
33 use strict;
34 use warnings;
35
36 use DBI;
37 use POSIX;
38
39 use Commods;
40
41 BEGIN {
42     use Exporter ();
43     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
44     $VERSION     = 1.00;
45     @ISA         = qw(Exporter);
46     @EXPORT      = qw(&db_setocean &db_writer &db_connect $dbh
47                       &db_filename &db_doall &db_onconflict &db_setdatadir);
48     %EXPORT_TAGS = ( );
49
50     @EXPORT_OK   = qw();
51 }
52
53 our $dbfn;
54 our $dbh;
55 our $datadir= '.';
56
57 sub db_setdatadir ($) {
58     $datadir= $_[0];
59 }
60 sub db_setocean ($) {
61     my ($oceanname) = @_;
62     $dbfn= "$datadir/OCEAN-$oceanname.db";
63 }
64 sub db_filename () {
65     return $dbfn;
66 }
67
68 sub db_onconflict (&) {
69     my ($conflictproc) = @_;
70     $dbh->{HandleError}= sub {
71         my ($emsg,$dbh,$val1) = @_;
72         my $native_ecode= $dbh->err();
73         &$conflictproc($emsg) if grep { $_ == $native_ecode } qw(5 6);
74         # 5==SQLITE_BUSY, 6==SQLITE_LOCKED according to the SQLite3
75         # API documentation, .../capi3ref.html#extended-result-codes.
76         return 0; # RaiseError happens next.
77     };
78 }
79
80 our $writerlockh;
81
82 sub db_writer () {
83     my $lockfn= "Writer.lock";
84     $writerlockh= new IO::File "$lockfn", "w" or die "$lockfn $!";
85
86     my $flockall= pack 's!s!LLLLLL', F_WRLCK, SEEK_SET, 0,0,0,0,0,0;
87     # should work everywhere to lock the whole file, provided that
88     # l_type and l_whence are `short int' and come first in that order,
89     # and that start, len and pid are no more than 64 bits each.
90
91     my $r= fcntl($writerlockh, F_SETLKW, $flockall);
92     $r or die "$lockfn fcntl $!";
93 }
94
95 sub db_connect () {
96     return if $dbh;
97     $dbh= DBI->connect("dbi:SQLite:$dbfn",'','',
98                        { AutoCommit=>0,
99                          RaiseError=>1, ShowErrorStatement=>1,
100                          unicode=>1 })
101         or die "$dbfn $DBI::errstr ?";
102     # default timeout is 30s which is plenty
103 }
104
105 sub db_doall ($) {
106     foreach my $cmd (split /\;/, $_[0]) {
107         $dbh->do("$cmd;") if $cmd =~ m/\S/;
108     }
109 }
110
111 1;