The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Hub::Query - Implementation of queries for Data::Hub::Courier

SYNOPSIS

EXPORTS

Nothing exported by default

DEPENDENCIES

This module requires these other modules and libraries:

    Data::Hub::Courier
    Data::Hub::Subset
    Error::Logical
    Perl::Module
    Data::Hub::Util
    Perl::Comparison

DESCRIPTION

EXAMPLES

This example will not abort:

  # This test case simply sets up the test data and subroutine for running
  # subsequent test queries.
  use Data::Hub::Util qw(:all);
  use Data::Format::Hash qw(hf_format hf_parse);
  use Data::OrderedHash;
  my $ttt_data = curry(hf_parse('
    array => @{
      a
      b
      c
      ab
      abc
    }
    hash => %{
      a => Alpha
      b => Beta
      c => Charlie
    }
    array_of_hashes => @{
      %{
        name => a
        text => Alpha
      }
      %{
        name => b
        text => Beta
      }
      %{
        name => c
        text => Charlie
      }
    }
    hash_of_hashes => %{
      a => %{
        text => Alpha
        num => 3
      }
      b => %{
        text => Beta
        num => 2
      }
      c => %{
        text => Charlie
        num => 1
      }
    }
  '));
  # The test data is curried to provide the get method of Data::Hub::Courier
  sub ttt_query {
    my $q = shift;
    my $r = $ttt_data->get($q);
    return unless defined $r;
    my $ref = ref($r);
    $ref ? hf_format({$ref => $r}) : $r;
  }

This example will not return a defined value:

  # Use an invalid index
  ttt_query('array/{?:fail}');

This example will not return a defined value:

  # Get the value whose value is ''
  ttt_query('array/{?:}');

This example:

  # Get the value whose key is eq 0
  ttt_query('array/{?:0}');

will return:

  a

This example:

  # Get the value whose key is == 0
  ttt_query('array/{?(==):0}');

will return:

  Data::Hub::Subset => %{
    0 => a
  }

This example:

  # Get all items whose key is >= 2
  ttt_query('array/{?(>=):2}');

will return:

  Data::Hub::Subset => %{
    2 => c
    3 => ab
    4 => abc
  }

This example:

  # Get the value whose value is 'a'
  ttt_query('array/{?*:a}');

will return:

  a

This example:

  # Get all items whose value is eq 'a'
  ttt_query('array/{?*(eq):a}');

will return:

  Data::Hub::Subset => %{
    0 => a
  }

This example:

  # Get all items whose value is =~ /a/
  ttt_query('array/{?*(=~):a}');

will return:

  Data::Hub::Subset => %{
    0 => a
    3 => ab
    4 => abc
  }

This example:

  # Get the value whose key is eq 'a'
  ttt_query('hash/{?:a}');

will return:

  Alpha

This example:

  # Get the value whose value is eq 'Alpha'
  ttt_query('hash/{?*:Alpha}');

will return:

  Alpha

This example:

  # Get all items whose value is eq 'Alpha'
  ttt_query('hash/{?*(eq):Alpha}');

will return:

  Data::Hub::Subset => %{
    a => Alpha
  }

This example:

  # Get all items whose value is =~ /a$/
  ttt_query('hash/{?*(=~):a$}');

will return:

  Data::Hub::Subset => %{
    a => Alpha
    b => Beta
  }

This example:

  # Get the value whose key is eq 0
  ttt_query('array_of_hashes/{?:0}');

will return:

  Data::OrderedHash => %{
    name => a
    text => Alpha
  }

This example:

  # Get the value whose name is eq 'a'
  ttt_query('array_of_hashes/{?name:a}');

will return:

  Data::OrderedHash => %{
    name => a
    text => Alpha
  }

This example:

  # Get all items whose name is eq 'a'
  ttt_query('array_of_hashes/{?name(eq):a}');

will return:

  Data::Hub::Subset => %{
    0 => %{
      name => a
      text => Alpha
    }
  }

This example:

  # Get all items whose name is =~ /a|b/
  ttt_query('array_of_hashes/{?name(=~):a|b}');

will return:

  Data::Hub::Subset => %{
    0 => %{
      name => a
      text => Alpha
    }
    1 => %{
      name => b
      text => Beta
    }
  }

This example:

  # Get the value whose key is eq 'a'
  ttt_query('hash_of_hashes/{?:a}');

will return:

  Data::OrderedHash => %{
    text => Alpha
    num => 3
  }

This example:

  # Get the value whose text is eq 'Alpha'
  ttt_query('hash_of_hashes/{?text:Alpha}');

will return:

  Data::OrderedHash => %{
    text => Alpha
    num => 3
  }

This example:

  # Get all items whose text is eq 'Alpha'
  ttt_query('hash_of_hashes/{?text(eq):Alpha}');

will return:

  Data::Hub::Subset => %{
    a => %{
      text => Alpha
      num => 3
    }
  }

This example:

  # Get all items whose num is > 1
  ttt_query('hash_of_hashes/{?num(>):1}');

will return:

  Data::Hub::Subset => %{
    a => %{
      text => Alpha
      num => 3
    }
    b => %{
      text => Beta
      num => 2
    }
  }

This example:

  ttt_query('hash_of_hashes/*|{?(ne):b}');

will return:

  Data::Hub::Subset => %{
    a => %{
      text => Alpha
      num => 3
    }
    c => %{
      text => Charlie
      num => 1
    }
  }

This example:

  ttt_query('hash_of_hashes/*|{?text(=~):B|C}');

will return:

  Data::Hub::Subset => %{
    b => %{
      text => Beta
      num => 2
    }
    c => %{
      text => Charlie
      num => 1
    }
  }

This example:

  ttt_query('hash_of_hashes/*|{?(=~):[ab]}|{?text(=~):B|C}');

will return:

  Data::Hub::Subset => %{
    b => %{
      text => Beta
      num => 2
    }
  }

This example:

  ttt_query('hash_of_hashes/{?(=~):[ab]}|{?text(=~):B|C}');

will return:

  Data::Hub::Subset => %{
    b => %{
      text => Beta
      num => 2
    }
  }

PACKAGE INTERNALS

_query_compare

Query a structure for values which match the given criteria

The general form is:

  {?[!]key(opr):val}

The leading ! negation symbol, when present, inverts the result of the comparson.

However both opr and key are optional.

  {?(opr):val}    # Without the key, all values are compared and a subset of
                  # matches is returned.
    
  {?key:val}      # Without the operator, a single value (first match) is 
                  # returned instead of a subset.
    
  {?:val}         # Without the key or operator, a single value (first match)
                  # is returned. All values are compared.
    
  {?key}          # With only the key, the result is items where the value
                  # at that key is logically true.

When the opr is omitted, the 'eq' operator is used in comparisons.

If key contains ( ) : those character must be escaped with a backslash.

If val contains } it must be escaped with a backslash.

The operators available for opr are those implemented by Perl::Comparison:

  Perl Operators (see L<perlop>)
    
    eq  =~  ==
    ne  !~  !=
    lt      <
    le      <=
    gt      >
    ge      >=
    
  Extended operators
    
    eqic    # Same as 'eq' however is case insensitive
    neic    # Same as 'ne' however is case insensitive
    mod     # Modulus, e.g., {?age(mod):30} is true for ages of 30,60,90,...

The key may contain spaces:

  /users/{?first name(=~):Ryan} # List all users with a first name of 'Ryan'

Random examples:

  {?(=~):[A-Z]}             # set of all values whose key has an upper-case letter
  {?id:1234}                # the first value whose id is 1234
  {?id(eq):1234}            # set of all values whose id is 1234
  {?id(==):1234}            # set of all values whose id is 1234 (numerically)
  {?first name(eq):Ryan}    # set of all users whose 'first name' is Ryan
  {?group(=~):aeiou}        # set of all values whose group contains a vowel
  {?!disabled}              # set of all values which are not 'disabled'

TODO - Query before expansion

  What we want is to say; /data.hf/**/{?schema(eq):product}
  and have the subset expansion stop at the point where an item with 
  a key of 'schema' is equal to 'product'. This however is code which
  slurps backward $c slashes from an expanded key:


  my $c = defined $key ? $key =~ tr'/'' : 0;
  my $i = rindex $_[0], '/';
  for (my $j = 0; $i > -1 && $j < $c; $j++) {
    $i = rindex $_[0], '/', $i - 1;
  }
  my $k = $i > -1 ? substr $_[0], $i + 1 : $_[0];

AUTHORS

    Ryan Gies <ryangies@cpan.org>

COPYRIGHT

    Copyright (C) 2014-2016 by Ryan Gies. All rights reserved.
    Copyright (C) 2006-2013 by Livesite Networks, LLC. All rights reserved.
    Copyright (C) 2000-2005 by Ryan Gies. All rights reserved.
    Redistribution and use in source and binary forms, with or without 
    modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
    * The origin of this software must not be misrepresented; you must not 
    claim that you wrote the original software. If you use this software in a 
    product, an acknowledgment in the product documentation would be 
    appreciated but is not required.
    * Altered source versions must be plainly marked as such, and must not be 
    misrepresented as being the original software.
    * The name of the author may not be used to endorse or promote products 
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
    EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
    OF SUCH DAMAGE.
    To the best of our knowledge, no patented algorithms have been used. However, we
    do not have the resources to carry out a patent search, and therefore cannot 
    give any guarantee of the above statement.