chiark / gitweb /
Correct link to test website source code
[ypp-sc-tools.db-live.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
48                       &dbr_filename &dbr_connect &db_connect_core);
49     %EXPORT_TAGS = ( );
50
51     @EXPORT_OK   = qw();
52 }
53
54 sub dbr_filename ($$) {
55     my ($datadir,$oceanname) = @_;
56     return "$datadir/OCEAN-$oceanname.db";
57 }
58 sub dbr_connect ($$) {
59     my ($datadir,$ocean) = @_;
60     return db_connect_core(dbr_filename($datadir,$ocean));
61 }
62
63 sub db_connect_core ($) {
64     my ($fn)= @_;
65     my $h= DBI->connect("dbi:SQLite:$fn",'','',
66                        { AutoCommit=>0,
67                          RaiseError=>1, ShowErrorStatement=>1,
68                          unicode=>1 })
69         or die "$fn $DBI::errstr ?";
70     return $h;
71     # default timeout is 30s which is plenty
72 }
73
74 our $dbfn;
75 our $dbh;
76
77 sub db_setocean ($) {
78     my ($oceanname) = @_;
79     $dbfn= dbr_filename('.',$oceanname);
80 }
81 sub db_filename () {
82     return $dbfn;
83 }
84
85 sub db_onconflict (&) {
86     my ($conflictproc) = @_;
87     $dbh->{HandleError}= sub {
88         my ($emsg,$dbh,$val1) = @_;
89         my $native_ecode= $dbh->err();
90         &$conflictproc($emsg) if grep { $_ == $native_ecode } qw(5 6);
91         # 5==SQLITE_BUSY, 6==SQLITE_LOCKED according to the SQLite3
92         # API documentation, .../capi3ref.html#extended-result-codes.
93         return 0; # RaiseError happens next.
94     };
95 }
96
97 our $writerlockh;
98
99 sub db_writer () {
100     my $lockfn= "Writer.lock";
101     $writerlockh= new IO::File "$lockfn", "w" or die "$lockfn $!";
102
103     my $flockall= pack 's!s!LLLLLL', F_WRLCK, SEEK_SET, 0,0,0,0,0,0;
104     # should work everywhere to lock the whole file, provided that
105     # l_type and l_whence are `short int' and come first in that order,
106     # and that start, len and pid are no more than 64 bits each.
107
108     my $r= fcntl($writerlockh, F_SETLKW, $flockall);
109     $r or die "$lockfn fcntl $!";
110 }
111
112 sub db_connect () {
113     $dbh= db_connect_core($dbfn);
114 }
115
116 sub db_doall ($) {
117     foreach my $cmd (split /\;/, $_[0]) {
118         $dbh->do("$cmd;") if $cmd =~ m/\S/;
119     }
120 }
121
122 1;