Skip to content

Commit

Permalink
add support for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ido50 committed Jun 17, 2015
1 parent 500e84a commit a2e2c56
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for MQUL

{{$NEXT}}
- Feature: added support for the $and keyword in queries

2.000001 2015-02-19 00:36:41+02:00 Asia/Jerusalem
- Bugfix: functions generating dynamic attributes are only called if
Expand Down Expand Up @@ -38,4 +39,4 @@ Revision history for MQUL
- Minor documentation fixes and improvements

0.001000 2011-06-07 22:52:06 Asia/Jerusalem
- Initial commit
- Initial commit
6 changes: 5 additions & 1 deletion lib/MQUL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use DateTime::Format::W3CDTF;
use Scalar::Util qw/blessed/;
use Try::Tiny;

our $VERSION = "2.000001";
our $VERSION = "2.001000";
$VERSION = eval $VERSION;

=head1 NAME
Expand Down Expand Up @@ -273,6 +273,10 @@ sub doc_matches {
}
}
return unless $found;
} elsif ($key eq '$and' && ref $value eq 'ARRAY') {
foreach (@$value) {
return unless &doc_matches($doc, $_, $defs);
}
} else {
return unless &_attribute_matches($doc, $key, $value);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/MQUL/Reference.pod
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ can be used for more complex constraints, such as this:
],
}

=head2 AND QUERIES

Sometimes you need to run more complex queries that are difficult or even impossible
to run with a basic query hash-ref. Or you might just want to be more verbose when
creating your queries. Much like the C<$or> keyword, the C<$and> keyword can be used
with an array-ref of queries that must all match:

$query = {
'$and' => [
{ title => 'Freaks and Geeks' },
{ imdb_score => { '$gt' => 7 }
]
}

A good usage for the C<$and> keyword is to match several C<$or> queries:

$query = {
'$and' => [
{ '$or' => [ ... ] },
{ '$or' => [ ... ] }
]
}

=head2 THE DOT NOTATION

Since version 1.0.0, C<MQUL> supports the dot notation for querying against
Expand Down
24 changes: 23 additions & 1 deletion t/01-querying.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env perl

use Test::More tests => 100;
use Test::More tests => 103;
use MQUL qw/doc_matches/;
use Try::Tiny;

Expand Down Expand Up @@ -190,6 +190,28 @@ ok(doc_matches({
],
}), 'or #9 works');

# let's try some $and queries
ok(doc_matches({ a => 5, b => 5 }, {
'$and' => [
{ '$or' => [{ a => 5 }, { b => 10 }] },
{ '$or' => [{ a => 1 }, { b => 5 }] }
]
}), 'checking $and query #1');

ok(doc_matches({ a => 1, b => 2 }, {
'$and' => [
{ a => { '$lt' => 2 } },
{ b => { '$gt' => 1 } }
]
}), 'checking $and query #2');

ok(!doc_matches({ a => 1, b => 2 }, {
'$and' => [
{ a => 1 },
{ b => 1 }
]
}), 'checking $and query #3');

# let's try some functions
ok(
doc_matches(
Expand Down

0 comments on commit a2e2c56

Please sign in to comment.