chiark / gitweb /
4866c855d3baacccc7c29ae754a60415169672d6
[ypp-sc-tools.main.git] / pctb / 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 use strict;
26 use warnings;
27
28 use DBI;
29
30 use Commods;
31
32 BEGIN {
33     use Exporter ();
34     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
35     $VERSION     = 1.00;
36     @ISA         = qw(Exporter);
37     @EXPORT      = qw(&db_setocean &db_connect $dbh
38                       &db_filename &db_doall);
39     %EXPORT_TAGS = ( );
40
41     @EXPORT_OK   = qw();
42 }
43
44 our $dbfn;
45 our $dbh;
46
47 sub db_setocean ($) {
48     my ($oceanname) = @_;
49     $dbfn= "OCEAN-$oceanname.db";
50 }
51 sub db_filename () {
52     return $dbfn;
53 }
54
55 sub db_connect () {
56     $dbh= DBI->connect("dbi:SQLite:$dbfn",'','',
57                        { AutoCommit=>0,
58                          RaiseError=>1, ShowErrorStatement=>1,
59                          unicode=>1 })
60         or die "$dbfn $DBI::errstr ?";
61 }
62
63 sub db_doall ($) {
64     foreach my $cmd (split /\;/, $_[0]) {
65         $dbh->do("$cmd;") if $cmd =~ m/\S/;
66     }
67 }
68
69 1;