The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Metadata::DB::Search - search a metadata table

SYNOPSES

Example 1

This example returns all metadata record ids that match all of the requirements..

   use Metadata::DB::Search;
   
   my $s = Metadata::DB::Search->new({ DBH => $dbh });

   $s->search({
      'age:exact'       => 24,
      'first_name:like' => 'jo',   
      'speed:morethan'  => 40,
   });

   $s->ids_count or die('nothing found');

   for my $id (@$ids) {
      my $meta = $s->record_entries_hashref($id);
   }
   

Start a new search..

   $s->search_reset;

   $s->search( ... );

Example 2i

You want to search every record that has an attribute of name matching 'jo' in a value, and the attribute age which has a value over '24'. Here are ways you can do that:

   my $ids = $s->search({
      'age:morethan' => 24,
      'name' => 'jo',
   });

Or

   my $ids = $s->search(
      [ age  => '24', 'morethan'],
      [ name => 'jo'],   
   );

Or

   my $ids_olderthan = $s->search( age => 24, 'morethan' );

   my $ids_namelike  = $s->search( name => 'jo' );

   my $ids_matching_both_constraints = $s->ids;




   

DESCRIPTION

This module is for searching a Metadata::DB metadata table in a database. It returns matching metadata record identifiers.

METHODS

new()

Argument is hashref with database handle.

   my $s = Metadata::DB::Search->new({ DBH => $dbh });

search()

optional argument is a hash ref with search params these are key value pairs the value can be a string or an array ref

   $s->search({
      age => 25,
      'name:exact' => ['larry','joe']
   });

Returns array ref of ids matching.

Possible search types

Default is 'like'.

like
exact
lessthan
morethan

search_morethan()

First argument is attribute name, second argument is value.

   $s->search_morethan( age => 20 );

Returns all ids of records that match this search.

search_lessthan()

First argument is attribute name, second argument is value. Returns all ids of records that match this search.

search_like()

First argument is attribute name, second argument is value. Returns all ids of records that match this search.

search_exact()

First argument is attribute name, second argument is value. Returns all ids of records that match this search.

search_reset()

Resets the search. Returns all ids of records that match this search.

ids()

Returns array ref of matching ids, results, in metadata table that meet the criteria. If you run multiple searches before calling search_reset(), the ids returns are only the ones that were returned in every search result set.

ids_count()

Returns count of how many search results we have.

SEARCH TYPES

search_type_exists()

argument is search type label, such as 'like', 'exact', 'morethan', 'lessthan'

search_types_arrayref()

returns the names of the search types

search_types_hashref()

returns the search types names as keys and sql statements for each

   search( att => val , type )

   search( 
      att => val,
      att => val,
      att => val,
   );

record_entries_hashref()

Argument is id. Returns all entries in metadata with said id, in a hash ref. See also Metadata::DB.

SEARCH PARAMS

An alternative to providing arguments all at once to search() is to incrementally add the parameters.

   $s->search_params_add( name => 'jimmy' );
   $s->search_params_add( age  => 45, 'morethan' );
   $s->search_params_add( 'age:lessthan'  => 65 );

   $s->ids;

search_params() search_params_arrayref()

returns array ref, each element is an array ref with three elements, the attribute name, the value, and the search type.

search_params_add()

First argument is attribute name. Second argument is value. Optional third argument is search type, defaults to 'like'.

search_params_count()

returns how many search params we have

constriction_keys() DEPRECATED

returns array ref of what the search params were for the search, not the types

ADDING A SEARCH TYPE

This is in infancy.

   my ( $table_name, $col_id, $col_key, $col_val ) =
      $s->table_metadata_name,
      $s->table_metadata_column_name_id,
      $s->table_metadata_column_name_key,
      $s->table_metadata_column_name_value );

   $self->search_types_hashref->{custom1} =
      "SELECT $col_id FROM $table_name WHERE $col_key=? and $col_val LIKE ?";

All searches execute with two arguments, the attribute name, and the value specified. So the above search would work as..

   $s->search( age => 34 , 'custom1' );

The $col_key ? would receive 'age' and the $col_val would receive '34'.

This does not leave a lot of wiggle room, indeed. Feed free to code your own search method and contribute back to the project.

Any search methods you add will work with the other search methods to find ids that match all searches run. All search methods return an array ref with ids matching that cosntraint. All seeach methods should increment the searches run counter.

SEE ALSO

Metadata::DB Metadata::DB::Indexer Metadata::DB::WUI

CAVEATS

This is a work in progress.

BUGS

Please contact the AUTHOR of any bugs.

AUTHOR

Leo Charre leocharre at cpan dot org