Skip to content

Commit

Permalink
* Added support for EXact searching Compound fields, based on an id-…
Browse files Browse the repository at this point in the history
…encoded search value

git-svn-id: https://svn.eprints.org/eprints/trunk/system@5956 9491667e-5006-0410-a446-efbe8990b998
  • Loading branch information
Tim Brody committed Nov 22, 2010
1 parent 30fd2be commit f357938
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
53 changes: 52 additions & 1 deletion perl_lib/EPrints/MetaField/Compound.pm
Expand Up @@ -579,11 +579,21 @@ sub render_xml_schema_type
return $type;
}

sub get_search_conditions_not_ex
sub get_search_conditions
{
my( $self, $session, $dataset, $search_value, $match, $merge,
$search_mode ) = @_;

if( $match eq "EX" )
{
return EPrints::Search::Condition->new(
'=',
$dataset,
$self,
$self->get_value_from_id( $session, $search_value )
);
}

EPrints::abort( "Attempt to search compound field. Repository ID=".$session->get_repository->get_id.", dataset=". $self->{dataset}->confid . ", field=" . $self->get_name );
}

Expand All @@ -595,6 +605,47 @@ sub ordervalue_single
return "";
}

sub get_value_from_id
{
my( $self, $session, $id ) = @_;

return {} if $id eq "NULL";

my $value = {};

my @parts =
map { URI::Escape::uri_unescape($_) }
split /:/, $id, scalar(@{$self->property( "fields_cache" )});

foreach my $field (@{$self->property( "fields_cache" )})
{
my $v = $field->get_value_from_id( $session, shift @parts );
$value->{$field->property( "sub_name" )} = $v;
}

return $value;
}

sub get_id_from_value
{
my( $self, $session, $value ) = @_;

return "NULL" if !defined $value;

my @parts;
foreach my $field (@{$self->property( "fields_cache" )})
{
push @parts, $field->get_id_from_value(
$session,
$value->{$field->property( "sub_name" )}
);
}

return join(":",
map { URI::Escape::uri_escape($_, ":%") }
@parts);
}

######################################################################

######################################################################
Expand Down
53 changes: 53 additions & 0 deletions perl_lib/EPrints/Search/Condition/Comparison.pm
Expand Up @@ -115,6 +115,21 @@ sub joins
{
return ();
}
elsif( $self->{field}->isa( "EPrints::MetaField::Compound" ) )
{
my @joins;
foreach my $f (@{$self->{field}->property( "fields_cache" )})
{
my $table = $f->{dataset}->get_sql_sub_table_name( $f );
push @joins, {
type => "inner",
table => $table,
alias => "$prefix$table",
key => $self->dataset->get_key_field->get_sql_name,
};
}
return @joins;
}
else
{
my $table = $self->table;
Expand Down Expand Up @@ -181,6 +196,44 @@ sub logic
}
return "(".join(") AND (", @logic).")";
}
elsif( $field->isa( "EPrints::MetaField::Compound" ) )
{
my @logic;
my $prev_table;
foreach my $f (@{$self->{field}->property( "fields_cache" )})
{
local $self->{field} = $f;
my $table = $prefix . $self->table;
if( $f->property( "multiple" ) )
{
if( $prev_table )
{
push @logic, sprintf("%s %s %s",
$db->quote_identifier( $prev_table, "pos" ),
"=",
$db->quote_identifier( $table, "pos" ) );
}
$prev_table = $table;
}
push @logic, sprintf("%s %s %s",
$db->quote_identifier( $table, $f->get_sql_name ),
$self->{op},
$db->quote_value( $self->{params}->[0]->{$f->property( "sub_name" )} ) );
}
return "(".join(") AND (", @logic).")";
}
elsif( $field->isa( "EPrints::MetaField::Multipart" ) )
{
my @logic;
for($field->parts)
{
push @logic, sprintf("%s %s %s",
$db->quote_identifier( $table, "$sql_name\_$_" ),
$self->{op},
$db->quote_value( $self->{params}->[0]->{$_} ) );
}
return "(".join(") AND (", @logic).")";
}
elsif( $field->isa( "EPrints::MetaField::Date" ) )
{
return $self->_logic_time( %opts, table => $table );
Expand Down
15 changes: 14 additions & 1 deletion tests/30_search.pl
@@ -1,5 +1,5 @@
use strict;
use Test::More tests => 25;
use Test::More tests => 26;

BEGIN { use_ok( "EPrints" ); }
BEGIN { use_ok( "EPrints::Test" ); }
Expand Down Expand Up @@ -302,4 +302,17 @@
ok($list->count > 0, "documents.file.mime_type/satisfy_all => 0");
};

$searchexp = EPrints::Search->new(
session => $session,
dataset => $sample_doc->dataset,
satisfy_all => 0 );

$searchexp->add_field( $sample_doc->dataset->field( "relation" ), "http%3A//eprints.org/relation/islightboxThumbnailVersionOf:/id/document/1", "EX" );

#print STDERR $searchexp->get_conditions->sql( dataset => $sample_doc->dataset, session => $session );

$list = $searchexp->perform_search;

ok($list->count > 0, "compound type field query");

$session->terminate;

0 comments on commit f357938

Please sign in to comment.