Database-Join

 view release on metacpan or  search on metacpan

t/pushdown.t  view on Meta::CPAN

#!perl -w

# Key pushdown on the array backend: a secondary database with no criteria of
# its own is asked for only the primary's keys ({ -in => [...] }) instead of
# being read in full.
#
#   - results are the same with and without pushdown, on the slurp and the
#     SQL paths, and for TSV and JSON tables
#   - a left join still keeps primary rows that have no secondary row
#   - pushdown doesn't apply to outer joins, to a secondary the caller already
#     restricted by join column, past $MAX_PUSHDOWN_KEYS keys, or to an object
#     whose selectall_arrayref isn't Database::Abstraction's own
#   - the secondary's own criteria and base filters still apply
#   - a database that croaks on -in is read in full instead, and a genuine
#     error still reaches the caller

use strict;
use warnings;

use File::Spec;
use File::Temp qw(tempdir);
use FindBin qw($Bin);
use Test::Most;

use lib 't/lib';

BEGIN {
	eval { require Database::Abstraction; 1 }
		or plan(skip_all => 'Database::Abstraction needed');
}

use Database::Join;

# Two tables named after their packages, as Database::Abstraction expects
{
	package Database::pdservice;
	our @ISA = ('Database::Abstraction');
}
{
	package Database::pdperson;
	our @ISA = ('Database::Abstraction');
}
# Implements the interface itself, with equality matching only
{
	package PDEqualityDA;
	sub new { my ($class, %a) = @_; return bless { %a, calls => [] }, $class }
	sub columns { return [ 'entry', 'title' ] }
	sub updated { return 0 }
	sub selectall_arrayref {
		my ($self, $criteria) = @_;
		push @{ $self->{calls} }, $criteria;
		my @rows = @{ $self->{rows} };
		for my $col (keys %{ $criteria // {} }) {
			my $v = $criteria->{$col};
			@rows = grep { defined($_->{$col}) && $_->{$col} eq $v } @rows;
		}
		return \@rows;
	}
	sub DESTROY {}
}

# Write a !-separated CSV file
sub write_csv
{
	my ($dir, $table, @rows) = @_;

	my $file = File::Spec->catfile($dir, "$table.csv");
	open(my $out, '>', $file) or die "$file: $!";
	print $out join('!', @{$_}), "\n" foreach(@rows);
	close $out or die "$file: $!";
}

# Records every criteria hashref given to Database::Abstraction's
# selectall_arrayref.  Replacing the method in Database::Abstraction itself
# keeps it the object's own, so pushdown still applies.
my @calls;
my $real_select = \&Database::Abstraction::selectall_arrayref;
our $croak_on_in = 0;
{
	no warnings 'redefine';
	*Database::Abstraction::selectall_arrayref = sub {
		my ($self, @args) = @_;
		my $criteria = ref($args[0]) eq 'HASH' ? $args[0] : { @args };
		push @calls, { table => ref($self), criteria => $criteria };
		die "Unknown operator '-in'\n"
			if($croak_on_in && grep { ref($_) eq 'HASH' && exists $_->{'-in'} } values %{$criteria});
		return $real_select->($self, @args);
	};
}

# The criteria the person table was given in the last query
sub person_criteria
{
	return [ map { $_->{criteria} } grep { $_->{table} eq 'Database::pdperson' } @calls ];
}

# Query with and without pushdown, so the results can be compared
sub both_ways
{
	my ($join, @criteria) = @_;

	@calls = ();
	my $pushed = $join->selectall_arrayref(@criteria);
	my $pushed_calls = [ @calls ];

	no warnings 'redefine';
	local *Database::Join::_understands_in = sub { 0 };
	@calls = ();



( run in 0.661 second using v1.01-cache-2.11-cpan-0e0213f3164 )