Data::Hub::Query - Implementation of queries for Data::Hub::Courier
Nothing exported by default
This module requires these other modules and libraries:
Data::Hub::Courier Data::Hub::Subset Error::Logical Perl::Module Data::Hub::Util Perl::Compare
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}');
# 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
# Get the value whose key is == 0 ttt_query('array/{?(==):0}');
Data::Hub::Subset => %{ 0 => a }
# Get all items whose key is >= 2 ttt_query('array/{?(>=):2}');
Data::Hub::Subset => %{ 2 => c 3 => ab 4 => abc }
# Get the value whose value is 'a' ttt_query('array/{?*:a}');
# Get all items whose value is eq 'a' ttt_query('array/{?*(eq):a}');
# Get all items whose value is =~ /a/ ttt_query('array/{?*(=~):a}');
Data::Hub::Subset => %{ 0 => a 3 => ab 4 => abc }
# Get the value whose key is eq 'a' ttt_query('hash/{?:a}');
Alpha
# Get the value whose value is eq 'Alpha' ttt_query('hash/{?*:Alpha}');
# Get all items whose value is eq 'Alpha' ttt_query('hash/{?*(eq):Alpha}');
Data::Hub::Subset => %{ a => Alpha }
# Get all items whose value is =~ /a$/ ttt_query('hash/{?*(=~):a$}');
Data::Hub::Subset => %{ a => Alpha b => Beta }
# Get the value whose key is eq 0 ttt_query('array_of_hashes/{?:0}');
Data::OrderedHash => %{ name => a text => Alpha }
# Get the value whose name is eq 'a' ttt_query('array_of_hashes/{?name:a}');
# Get all items whose name is eq 'a' ttt_query('array_of_hashes/{?name(eq):a}');
Data::Hub::Subset => %{ 0 => %{ name => a text => Alpha } }
# Get all items whose name is =~ /a|b/ ttt_query('array_of_hashes/{?name(=~):a|b}');
Data::Hub::Subset => %{ 0 => %{ name => a text => Alpha } 1 => %{ name => b text => Beta } }
# Get the value whose key is eq 'a' ttt_query('hash_of_hashes/{?:a}');
Data::OrderedHash => %{ text => Alpha num => 3 }
# Get the value whose text is eq 'Alpha' ttt_query('hash_of_hashes/{?text:Alpha}');
# Get all items whose text is eq 'Alpha' ttt_query('hash_of_hashes/{?text(eq):Alpha}');
Data::Hub::Subset => %{ a => %{ text => Alpha num => 3 } }
# Get all items whose num is > 1 ttt_query('hash_of_hashes/{?num(>):1}');
Data::Hub::Subset => %{ a => %{ text => Alpha num => 3 } b => %{ text => Beta num => 2 } }
ttt_query('hash_of_hashes/*|{?(ne):b}');
Data::Hub::Subset => %{ a => %{ text => Alpha num => 3 } c => %{ text => Charlie num => 1 } }
ttt_query('hash_of_hashes/*|{?text(=~):B|C}');
Data::Hub::Subset => %{ b => %{ text => Beta num => 2 } c => %{ text => Charlie num => 1 } }
ttt_query('hash_of_hashes/*|{?(=~):[ab]}|{?text(=~):B|C}');
Data::Hub::Subset => %{ b => %{ text => Beta num => 2 } }
ttt_query('hash_of_hashes/{?(=~):[ab]}|{?text(=~):B|C}');
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
key
{?(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.
val
}
The operators available for opr are those implemented by Perl::Compare:
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];
Ryan Gies <ryangies@cpan.org>
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.
To install Data::Hub, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Hub
CPAN shell
perl -MCPAN -e shell install Data::Hub
For more information on module installation, please visit the detailed CPAN module installation guide.