Skip to content

Commit

Permalink
Merge in from NJH::Snippets::DB
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Jan 25, 2018
1 parent b830fbc commit d37c14f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/Geo/Coder/Free.pm
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ sub geocode {
} elsif(defined($state) && $admin2cache{$state} && !defined($county)) {
$region = $admin2cache{$state};
} else {
@admin2s = @{$self->{'admin2'}->selectall_hashref(asciiname => $county)};
@admin2s = $self->{'admin2'}->selectall_hash(asciiname => $county);
foreach my $admin2(@admin2s) {
if($admin2->{'concatenated_codes'} =~ $concatenated_codes) {
$region = $admin2->{'concatenated_codes'};
Expand All @@ -248,7 +248,7 @@ sub geocode {
$region = $state;
@regions = ();
} else {
@admin2s = @{$self->{'admin2'}->selectall_hashref(asciiname => $state)};
@admin2s = $self->{'admin2'}->selectall_hash(asciiname => $state);
foreach my $admin2(@admin2s) {
if($admin2->{'concatenated_codes'} =~ $concatenated_codes) {
$region = $admin2->{'concatenated_codes'};
Expand All @@ -262,7 +262,7 @@ sub geocode {

if((scalar(@regions) == 0) && !defined($region)) {
# e.g. Unitary authorities in the UK
@admin2s = @{$self->{'admin2'}->selectall_hashref(asciiname => $location)};
@admin2s = $self->{'admin2'}->selectall_hash(asciiname => $location);
if(scalar(@admin2s) && defined($admin2s[0]->{'concatenated_codes'})) {
foreach my $admin2(@admin2s) {
if($admin2->{'concatenated_codes'} =~ $concatenated_codes) {
Expand All @@ -272,7 +272,7 @@ sub geocode {
}
} else {
# e.g. states in the US
my @admin1s = @{$self->{'admin1'}->selectall_hashref(asciiname => $county)};
my @admin1s = $self->{'admin1'}->selectall_hash(asciiname => $county);
foreach my $admin1(@admin1s) {
if($admin1->{'concatenated_codes'} =~ /^$concatenated_codes\./i) {
$region = $admin1->{'concatenated_codes'};
Expand Down Expand Up @@ -305,7 +305,7 @@ sub geocode {

# This case nonsense is because DBD::CSV changes the columns to lowercase, wherease DBD::SQLite does not
if(wantarray) {
my @rc = @{$self->{'cities'}->selectall_hashref($options)};
my @rc = $self->{'cities'}->selectall_hash($options);
foreach my $city(@rc) {
if($city->{'Latitude'}) {
$city->{'latitude'} = delete $city->{'Latitude'};
Expand Down
52 changes: 35 additions & 17 deletions lib/Geo/Coder/Free/DB.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package Geo::Coder::Free::DB;

# Author Nigel Horne: njh@bandsman.co.uk
# Copyright (C) 2015-2018, Nigel Horne

# Usage is subject to licence terms.
# The licence terms of this software are as follows:
# Personal single user, single computer use: GPL2
# All other users (including Commercial, Charity, Educational, Government)
# must apply in writing for a licence for use from Nigel Horne at the
# above e-mail.

# Read-only access to databases

use warnings;
Expand All @@ -25,6 +35,11 @@ sub new {

my $class = ref($proto) || $proto;

if($class eq 'Geo::Coder::Free::DB') {
die "$class: abstract class";
}

die "$class: where are the files?" unless($directory || $args{'directory'});
# init(\%args);

return bless {
Expand Down Expand Up @@ -193,6 +208,12 @@ sub _open {
# Returns a reference to an array of hash references of all the data meeting
# the given criteria
sub selectall_hashref {
my @rc = selectall_hash(@_);
return \@rc;
}

# Returns an array of hash references
sub selectall_hash {
my $self = shift;
my %params = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

Expand All @@ -203,9 +224,9 @@ sub selectall_hashref {

if((scalar(keys %params) == 0) && $self->{'data'}) {
if($self->{'logger'}) {
$self->{'logger'}->trace("$table: selectall_hashref fast track return");
$self->{'logger'}->trace("$table: selectall_hash fast track return");
}
return $self->{'data'};
return @{$self->{'data'}};
}

my $query = "SELECT * FROM $table";
Expand All @@ -220,7 +241,7 @@ sub selectall_hashref {
push @args, $params{$c1};
}
if($self->{'logger'}) {
$self->{'logger'}->debug("selectall_hashref $query: " . join(', ', @args));
$self->{'logger'}->debug("selectall_hash $query: " . join(', ', @args));
}
my $sth = $self->{$table}->prepare($query);
$sth->execute(@args) || throw Error::Simple("$query: @args");
Expand All @@ -229,19 +250,19 @@ sub selectall_hashref {
my $c;
if($c = $self->{cache}) {
if(my $rc = $c->get($key)) {
return $rc;
return @{$rc};
}
}
my @rc;
while (my $href = $sth->fetchrow_hashref()) {
while(my $href = $sth->fetchrow_hashref()) {
push @rc, $href;
# last if(!wantarray);
last if(!wantarray);
}
if($c) {
if($c && wantarray) {
$c->set($key, \@rc, '1 hour');
}

return \@rc;
return @rc;
}

# Returns a hash reference for one row in a table
Expand All @@ -254,12 +275,7 @@ sub fetchrow_hashref {

$self->_open() if(!$self->{table});

my $query;
if(wantarray) {
$query = "SELECT * FROM $table";
} else {
$query = "SELECT DISTINCT * FROM $table";
}
my $query = "SELECT DISTINCT * FROM $table";
my @args;
foreach my $c1(keys(%params)) {
if(scalar(@args) == 0) {
Expand Down Expand Up @@ -296,7 +312,7 @@ sub execute {
my $sth = $self->{$table}->prepare($query);
$sth->execute() || throw Error::Simple($query);
my @rc;
while (my $href = $sth->fetchrow_hashref()) {
while(my $href = $sth->fetchrow_hashref()) {
push @rc, $href;
}

Expand All @@ -310,8 +326,10 @@ sub updated {
return $self->{'_updated'};
}

# Return the contents of an arbiratary column in the database which match the given criteria
# Returns an array of the matches, or just the first entry when called in scalar context
# Return the contents of an arbiratary column in the database which match the
# given criteria
# Returns an array of the matches, or just the first entry when called in
# scalar context

# Set distinct to 1 if you're after a uniq list
sub AUTOLOAD {
Expand Down

0 comments on commit d37c14f

Please sign in to comment.