chiark / gitweb /
69703a6745e1bcf7b7365ac50fd021dbe9f9580c
[ypp-sc-tools.main.git] / yarrg / db-idempotent-populate
1 #!/usr/bin/perl -w
2 #
3 # Normally run from
4 #  update-master-info
5 #
6 # usage: ./db-idempotent-populate <Oceanname>
7 #  creates or updates OCEAN-Oceanname.db
8 #  from master-master.txt
9
10 # This is part of ypp-sc-tools, a set of third-party tools for assisting
11 # players of Yohoho Puzzle Pirates.
12 #
13 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
14 #
15 # This program is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 #
28 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
29 # are used without permission.  This program is not endorsed or
30 # sponsored by Three Rings.
31
32 use strict (qw(vars));
33
34 use DBI;
35
36 use Commods;
37 use CommodsDatabase;
38
39 @ARGV==1 or die;
40 my ($oceanname) = @ARGV;
41
42 #---------- setup ----------
43
44 parse_info_serverside();
45 parse_info_serverside_ocean($oceanname);
46 our $ocean= $oceans{$oceanname};
47
48 db_setocean($oceanname);
49 db_writer();
50 db_connect();
51
52 #---------- schema ----------
53
54 foreach my $bs (qw(buy sell)) {
55     db_doall(<<END)
56  CREATE TABLE IF NOT EXISTS $bs (
57         commodid        INTEGER                 NOT NULL,
58         islandid        INTEGER                 NOT NULL,
59         stallid         INTEGER                 NOT NULL,
60         price           INTEGER                 NOT NULL,
61         qty             INTEGER                 NOT NULL,
62         PRIMARY KEY (commodid, islandid, stallid)
63  );
64  CREATE INDEX IF NOT EXISTS ${bs}_by_island ON $bs (commodid, islandid, price);
65  CREATE INDEX IF NOT EXISTS ${bs}_by_price  ON $bs (commodid, price, islandid);
66 END
67     ;
68 }
69
70 db_doall(<<END)
71  CREATE TABLE IF NOT EXISTS commods (
72         commodid        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
73         commodname      TEXT    UNIQUE          NOT NULL,
74         unitmass        INTEGER,
75         unitvolume      INTEGER
76  );
77  CREATE TABLE IF NOT EXISTS islands (
78         islandid        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
79         islandname      TEXT    UNIQUE          NOT NULL,
80         archipelago     TEXT                    NOT NULL
81  );
82  CREATE TABLE IF NOT EXISTS stalls (
83         stallid         INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
84         islandid        INTEGER                 NOT NULL,
85         stallname       TEXT                    NOT NULL,
86         UNIQUE (islandid, stallname)
87  );
88  CREATE TABLE IF NOT EXISTS uploads (
89         islandid        INTEGER PRIMARY KEY     NOT NULL,
90         timestamp       INTEGER                 NOT NULL,
91         message         TEXT                    NOT NULL,
92         clientspec      TEXT                    NOT NULL,
93         serverspec      TEXT                    NOT NULL
94  );
95  CREATE TABLE IF NOT EXISTS dists (
96         aiid            INTEGER                 NOT NULL,
97         biid            INTEGER                 NOT NULL,
98         dist            INTEGER                 NOT NULL,
99         PRIMARY KEY (aiid, biid)
100  );
101  CREATE TABLE IF NOT EXISTS routes (
102         aiid            INTEGER                 NOT NULL,
103         biid            INTEGER                 NOT NULL,
104         dist            INTEGER                 NOT NULL,
105         PRIMARY KEY (aiid, biid)
106  );
107 END
108     ;
109
110 $dbh->commit;
111
112 #---------- commodity list ----------
113
114 {
115     my $insert= $dbh->prepare(<<'END')
116  INSERT OR IGNORE INTO commods
117      (unitmass,
118       unitvolume,
119       commodname)
120      VALUES (?,?,?);
121 END
122     ;
123     my $update= $dbh->prepare(<<'END')
124  UPDATE commods
125      SET unitmass = ?,
126          unitvolume = ?
127      WHERE commodname = ?
128 END
129     ;
130     foreach my $commod (sort keys %commods) {
131         my $c= $commods{$commod};
132         die "no mass for $commod" unless defined $c->{Mass};
133         die "no colume for $commod" unless defined $c->{Volume};
134         my @qa= ($c->{Mass}, $c->{Volume}, $commod);
135         $insert->execute(@qa);
136         $update->execute(@qa);
137     }
138     $dbh->commit;
139 }
140
141 #---------- island list ----------
142 #---------- routes ----------
143 # now done by yppedia-chart-parser
144
145 __DATA__