chiark / gitweb /
abolish $dbh global in Mason code to shorten db connection lifetime
[ypp-sc-tools.web-live.git] / yarrg / web / route
index 595976f8293a7cde6b6cbe56871ba5e90f47595b..9c7200c39480df0343ab70b7615e4e994316b3d2 100644 (file)
@@ -76,7 +76,7 @@ foreach my $var (@vars) {
                        my %qf= (%baseqf,%queryqf);
                        delete $qf{$lname};
                        $qf{$lname}= $value if $cvalix;
-                       print '<a href="',&$quri(%qf),'">';
+                       print '<a href="',$quri->(%qf),'">';
                        $after= '</a>';
                }
                print $html, $after;
@@ -87,11 +87,7 @@ foreach my $var (@vars) {
 
 #---------- initial checks, startup, main entry form ----------
 
-die "unknown ocean $a{Ocean} ?"
-       unless grep { $_ eq $a{Ocean} } ocean_list();
-
-db_setocean($a{Ocean});
-db_connect();
+dbw_connect($a{Ocean});
 
 </%perl>
 <%args>
@@ -100,7 +96,7 @@ $routestring => ''
 </%args>
 
 <h1>Specify route</h1>
-<form action="<% &$quri() %>" method="get">
+<form action="<% $quri->() %>" method="get">
 
 %#---------- textbox, user enters route as string ----------
 % if (!$a{Dropdowns}) {
@@ -176,6 +172,8 @@ my $optionlistmap= sub {
        return $out;
 };
 
+my $dbh= dbw_connect($a{Ocean});
+
 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
                            ORDER BY archipelago;");
 $sth->execute();
@@ -201,7 +199,7 @@ while ($row=$sth->fetchrow_arrayref) {
 my %resetislandlistdata;
 foreach my $arch (keys %islandlistdata) {
        $resetislandlistdata{$arch}=
-               &$optionlistmap($islandlistdata{$arch}, '');
+               $optionlistmap->($islandlistdata{$arch}, '');
 }
 
 </%perl>
@@ -229,7 +227,7 @@ function ms_Setarch(dd) {
 <td>
 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
 <option value="none">Whole ocean</option>
-<% &$optionlistmap(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
+<% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
 %      }
 </tr>
 
@@ -239,7 +237,7 @@ function ms_Setarch(dd) {
 %              $arch= 'none' if !defined $arch;
 <td>
 <select name="islandid<% $dd %>">
-<% &$optionlistmap($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
+<% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
 </select></td>
 %      }
 </tr>
@@ -251,14 +249,19 @@ function ms_Setarch(dd) {
 <input type=submit name=submit value="Go">
 </form>
 
-<h1>Results</h1>
-
 <%perl>
 #========== result computations ==========
 
+my $results_head;
+$results_head= sub {
+       print "<h1>Results</h1>\n";
+       $results_head= sub { };
+};
+
 #---------- result computation - textstring ----------
 if (!$a{Dropdowns}) {
   if (length $routestring) {
+       $results_head->();
        my $rsr= $m->comp('routetextstring',
                ocean => $a{Ocean},
                string => $routestring,
@@ -285,13 +288,14 @@ my $argorundef= sub {
 };
 
 for my $dd (0..$a{Dropdowns}-1) {
-       my $arch= &$argorundef($dd,'archipelago');
-       my $island= &$argorundef($dd,'islandid');
+       my $arch= $argorundef->($dd,'archipelago');
+       my $island= $argorundef->($dd,'islandid');
        next unless defined $arch or defined $island;
        if (defined $island and defined $arch) {
                my $ii= $islandid2{$island};
                my $iarch= $ii->{Arch};
                if ($iarch ne $arch) {
+                       $results_head->();
 </%perl>
  Specified archipelago <% $arch %> but
  island <% $ii->{Name} %>
@@ -308,6 +312,7 @@ for my $dd (0..$a{Dropdowns}-1) {
 </%perl>
 
 % if (@islandids) {
+%      $results_head->();
 
 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
 
@@ -335,5 +340,6 @@ function debug (m) {
 use CommodsWeb;
 use HTML::Entities;
 use URI::Escape;
+use JSON;
 
 </%init>