Skip to content

Commit

Permalink
Added DISTINCT when only wanting one row
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Jan 16, 2018
1 parent c38dbf1 commit 06fa9f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Revision history for Geo-Coder-Free
Fix http://www.cpantesters.org/cpan/report/b10c956b-6bf9-1014-9a47-dc46d49c4260
Allow known peculiarities to be found
Improve handling of US lookups
Added DISTINCT when only wanting one row

0.04 Thu Oct 26 22:05:54 EDT 2017
Fix throw in http://www.cpantesters.org/cpan/report/ab86a142-b8d9-11e7-a1cf-bb670eaac09d
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ WriteMakefile(
'Module::Info' => 0,
'File::Spec' => 0,
'Error::Simple' => 0,
'CHI' => 0,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Geo-Coder-Free-*' },
Expand Down
9 changes: 7 additions & 2 deletions lib/Geo/Coder/Free.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Carp;
use Error::Simple;
use File::Spec;
use Locale::US;
use CHI;

our %admin1cache;
our %admin2cache;
Expand Down Expand Up @@ -76,7 +77,11 @@ sub new {

my $directory = Module::Info->new_from_loaded(__PACKAGE__)->file();
$directory =~ s/\.pm$//;
Geo::Coder::Free::DB::init(directory => File::Spec->catfile($directory, 'databases'));

Geo::Coder::Free::DB::init({
directory => File::Spec->catfile($directory, 'databases'),
cache => CHI->new(driver => 'Memory', datastore => { })
});

return bless { }, $class;
}
Expand Down Expand Up @@ -380,7 +385,7 @@ VWF, Maxmind and geonames.
=head1 LICENSE AND COPYRIGHT
Copyright 2017 Nigel Horne.
Copyright 2017-2018 Nigel Horne.
The program code is released under the following licence: GPL for personal use on a single computer.
All other users (including Commercial, Charity, Educational, Government)
Expand Down
28 changes: 25 additions & 3 deletions lib/Geo/Coder/Free/DB.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Geo::Coder::Free::DB;

# Read-only access to databases

use warnings;
use strict;

Expand All @@ -15,6 +17,7 @@ use Error::Simple;
our @databases;
our $directory;
our $logger;
our $cache;

sub new {
my $proto = shift;
Expand All @@ -24,7 +27,11 @@ sub new {

# init(\%args);

return bless { logger => $args{'logger'} || $logger, directory => $args{'directory'} || $directory }, $class;
return bless {
logger => $args{'logger'} || $logger,
directory => $args{'directory'} || $directory,
cache => $args{'cache'} || $cache
}, $class;
}

# Can also be run as a class level Geo::Coder::Free::DB::init(directory => '../databases')
Expand All @@ -33,6 +40,7 @@ sub init {

$directory ||= $args{'directory'};
$logger ||= $args{'logger'};
$cache ||= $args{'cache'};
if($args{'databases'}) {
@databases = $args{'databases'};
}
Expand Down Expand Up @@ -216,10 +224,21 @@ sub selectall_hashref {
}
my $sth = $self->{$table}->prepare($query);
$sth->execute(@args) || throw Error::Simple("$query: @args");

my $key = "$query " . join(', ', @args);
my $c;
if($c = $self->{cache}) {
if(my $rc = $c->get($key)) {
return $rc;
}
}
my @rc;
while (my $href = $sth->fetchrow_hashref()) {
push @rc, $href;
}
if($c) {
$c->set($key, \@rc, '1 hour');
}

return \@rc;
}
Expand All @@ -234,7 +253,7 @@ sub fetchrow_hashref {

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

my $query = "SELECT * FROM $table";
my $query = "SELECT DISTINCT * FROM $table";
my @args;
foreach my $c1(keys(%params)) {
if(scalar(@args) == 0) {
Expand Down Expand Up @@ -306,6 +325,9 @@ sub AUTOLOAD {
my $query = "SELECT DISTINCT $column FROM $table";
my @args;
foreach my $c1(keys(%params)) {
if(!defined($params{$c1})) {
$self->{'logger'}->debug("AUTOLOAD params $c1 isn't defined");
}
# $query .= " AND $c1 LIKE ?";
if(scalar(@args) == 0) {
$query .= ' WHERE';
Expand All @@ -317,7 +339,7 @@ sub AUTOLOAD {
}
$query .= " ORDER BY $column";
if($self->{'logger'}) {
if(scalar(@args)) {
if(scalar(@args) && $args[0]) {
$self->{'logger'}->debug("AUTOLOAD $query: " . join(', ', @args));
} else {
$self->{'logger'}->debug("AUTOLOAD $query");
Expand Down

0 comments on commit 06fa9f3

Please sign in to comment.