| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Rubric::Entry::Query 0.157; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: construct and execute a complex query | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 7 | #pod | ||||||||||||||||||||||||||||
| 8 | #pod Rubric::Entry::Query builds a query based on a simple hash of parameters, | ||||||||||||||||||||||||||||
| 9 | #pod performs that query, and returns the rendered report on the results. | ||||||||||||||||||||||||||||
| 10 | #pod | ||||||||||||||||||||||||||||
| 11 | #pod =cut | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Date::Span; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Rubric::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #pod =head1 METHODS | ||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||
| 20 | #pod =head2 query(\%arg, \%context) | ||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||
| 22 | #pod This is the only interface to this module. Given a hashref of named arguments, | ||||||||||||||||||||||||||||
| 23 | #pod it returns the entries that match constraints built from the arguments. It | ||||||||||||||||||||||||||||
| 24 | #pod generates these constraints with C<get_constraint> and its helpers. If any | ||||||||||||||||||||||||||||
| 25 | #pod constraint is invalid, an empty set of results is returned. | ||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||
| 27 | #pod The second hashref passed to the method provides context for generating | ||||||||||||||||||||||||||||
| 28 | #pod implicit query parameters; for example, if the querying user is indicated in | ||||||||||||||||||||||||||||
| 29 | #pod the context, private entries for that user will be returned. | ||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||
| 31 | #pod =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 32 | ▶ | sub _private_constraint { | ||||||||||||||||||||||||||
|
Subroutine
_private_constraint: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | 32 | my ($self, $user) = @_; | |||||||||||||||||||||||||||
| 35 | 32 | my $priv_tag = Rubric::Config->private_tag; | |||||||||||||||||||||||||||
| 36 | 32 | $priv_tag = Rubric::Entry->db_Main->quote($priv_tag); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 32 | ▶ | return "id NOT IN (SELECT entry FROM entrytags WHERE tag=$priv_tag)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | unless $user; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 10 | $user = Rubric::Entry->db_Main->quote($user); | |||||||||||||||||||||||||||
| 42 | return | ||||||||||||||||||||||||||||
| 43 | 10 | "((username = $user) OR " . | |||||||||||||||||||||||||||
| 44 | "id NOT IN (SELECT entry FROM entrytags WHERE tag=$priv_tag))"; | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 47 | ▶ | sub _nolist_constraint { | ||||||||||||||||||||||||||
|
Subroutine
_nolist_constraint: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 47 | return q{id NOT IN (SELECT entry FROM entrytags WHERE tag='@nolist')}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 47 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 47 | my ($self, $arg, $context) = @_; | |||||||||||||||||||||||||||
| 53 | 47 | ▶ | $context ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$context ||= {}
MC/DC100%1/1
$context ||= {}
$context
Truth table100%2/2
A
A= $context
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 47 | my @constraints = map { $self->get_constraint($_, $arg->{$_}) } keys %$arg; | |||||||||||||||||||||||||||
| 56 | 47 | ▶ | @constraints = ("1 = 0") if grep { not defined } @constraints; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 47 | push @constraints, $self->_nolist_constraint; | |||||||||||||||||||||||||||
| 59 | push @constraints, $self->_private_constraint($context->{user}) | ||||||||||||||||||||||||||||
| 60 | 47 | ▶ | if exists $context->{user}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | ## no critic (ConditionalDeclarations) | ||||||||||||||||||||||||||||
| 63 | my $order_by = "$context->{order_by} DESC" | ||||||||||||||||||||||||||||
| 64 | 47 | ▶ | if $context->{order_by}||'' =~ /\A(?:created|modified)\Z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$context->{"order_by"} or "" =~ /\A(?:created|modified)\Z/
MC/DC0%0/2
$context->{"order_by"} or "" =~ /\A(?:created|modified)\Z/
$context->{"order_by"}
"" =~ /\A(?:created|modified)\Z/
Truth table33%1/3
A or B
A= $context->{"order_by"}B= "" =~ /\A(?:created|modified)\Z/
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 47 | $self->get_entries(\@constraints, $order_by); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #pod =head2 get_constraint($param => $value) | ||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||
| 71 | #pod Given a name/value pair describing a constraint, this method will attempt to | ||||||||||||||||||||||||||||
| 72 | #pod generate part of an SQL WHERE clause enforcing the constraint. To do this, it | ||||||||||||||||||||||||||||
| 73 | #pod looks for and calls a method called "constraint_for_NAME" where NAME is the | ||||||||||||||||||||||||||||
| 74 | #pod passed value of C<$param>. If no clause can be generated, it returns undef. | ||||||||||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||||||||||
| 76 | #pod =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 59 | ▶ | sub get_constraint { | ||||||||||||||||||||||||||
|
Subroutine
get_constraint: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 59 | my ($self, $param, $value) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | ## no critic (ReturnUndef) | ||||||||||||||||||||||||||||
| 82 | 59 | ▶ | return undef unless my $code = $self->can("constraint_for_$param"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 58 | $code->($self, $value); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | #pod =head2 get_entries(\@constraints) | ||||||||||||||||||||||||||||
| 87 | #pod | ||||||||||||||||||||||||||||
| 88 | #pod Given a set of SQL constraints, this method builds the WHERE and ORDER BY | ||||||||||||||||||||||||||||
| 89 | #pod clauses and performs a query with Class::DBI's C<retrieve_from_sql>. | ||||||||||||||||||||||||||||
| 90 | #pod | ||||||||||||||||||||||||||||
| 91 | #pod =cut | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 47 | ▶ | sub get_entries { | ||||||||||||||||||||||||||
|
Subroutine
get_entries: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 47 | my ($self, $constraints, $order_by) = @_; | |||||||||||||||||||||||||||
| 95 | 47 | ▶ | $order_by ||= 'created DESC'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$order_by ||= "created DESC"
MC/DC0%0/1
$order_by ||= "created DESC"
$order_by
Truth table50%1/2
A
A= $order_by
| |||||||||||||||||||||||||||||
| 96 | 47 | ▶ | return Rubric::Entry->retrieve_all unless @$constraints; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 47 | Rubric::Entry->retrieve_from_sql( | |||||||||||||||||||||||||||
| 98 | join(" AND ", @$constraints) | ||||||||||||||||||||||||||||
| 99 | . " ORDER BY $order_by" | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #pod =head2 constraint_for_NAME | ||||||||||||||||||||||||||||
| 104 | #pod | ||||||||||||||||||||||||||||
| 105 | #pod These methods are called to produce SQL for the named parameter, and are passed | ||||||||||||||||||||||||||||
| 106 | #pod a scalar argument. If the argument is not valid, they return undef, which will | ||||||||||||||||||||||||||||
| 107 | #pod cause C<query> to produce an empty set of records. | ||||||||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||||||||
| 109 | #pod =head3 constraint_for_user($user) | ||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||
| 111 | #pod Given a Rubric::User object, this returns SQL to limit results to entries by | ||||||||||||||||||||||||||||
| 112 | #pod the user. | ||||||||||||||||||||||||||||
| 113 | #pod | ||||||||||||||||||||||||||||
| 114 | #pod =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 13 | ▶ | sub constraint_for_user { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_user: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 13 | my ($self, $user) = @_; | |||||||||||||||||||||||||||
| 118 | ## no critic (ReturnUndef) | ||||||||||||||||||||||||||||
| 119 | 13 | ▶ | return undef unless $user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 12 | return "username = " . Rubric::Entry->db_Main->quote($user); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #pod =head3 constraint_for_tags($tags) | ||||||||||||||||||||||||||||
| 124 | #pod | ||||||||||||||||||||||||||||
| 125 | #pod =head3 constraint_for_exact_tags($tags) | ||||||||||||||||||||||||||||
| 126 | #pod | ||||||||||||||||||||||||||||
| 127 | #pod Given a set of tags, this returns SQL to limit results to entries marked | ||||||||||||||||||||||||||||
| 128 | #pod with the given tags. | ||||||||||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||||||||||
| 130 | #pod The C<exact> version of this constraint returns SQL for entries with only the | ||||||||||||||||||||||||||||
| 131 | #pod given tags. | ||||||||||||||||||||||||||||
| 132 | #pod | ||||||||||||||||||||||||||||
| 133 | #pod =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 20 | ▶ | sub constraint_for_tags { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_tags: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 20 | my ($self, $tags) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | ## no critic (ReturnUndef) | ||||||||||||||||||||||||||||
| 139 | 20 | ▶ | return undef unless $tags and ref $tags eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tags and ref $tags eq "HASH"
MC/DC100%2/2
$tags and ref $tags eq "HASH"
$tags
ref $tags eq "HASH"
Truth table100%3/3
A and B
A= $tagsB= ref $tags eq "HASH"
| |||||||||||||||||||||||||||||
| 140 | ## use critic | ||||||||||||||||||||||||||||
| 141 | 18 | ▶ | return unless %$tags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 16 | my @snippets; | |||||||||||||||||||||||||||
| 144 | 16 | ▶ | while (my ($tag, $tag_value) = each %$tags) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 16 | my $tn = Rubric::Entry->db_Main->quote($tag); | |||||||||||||||||||||||||||
| 146 | 16 | my $tv = Rubric::Entry->db_Main->quote($tag_value); | |||||||||||||||||||||||||||
| 147 | 16 | ▶ | push @snippets, defined $tag_value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | ? "id IN (SELECT entry FROM entrytags WHERE tag=$tn AND tag_value=$tv)" | ||||||||||||||||||||||||||||
| 149 | : "id IN (SELECT entry FROM entrytags WHERE tag=$tn)" | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 16 | return join ' AND ', @snippets; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | sub constraint_for_exact_tags { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_exact_tags: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 1 | my ($self, $tags) = @_; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | ## no critic (ReturnUndef) | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | return undef unless $tags and ref $tags eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tags and ref $tags eq "HASH"
MC/DC0%0/2
$tags and ref $tags eq "HASH"
$tags
ref $tags eq "HASH"
Truth table33%1/3
A and B
A= $tagsB= ref $tags eq "HASH"
| |||||||||||||||||||||||||||||
| 160 | ## use critic | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | my $count = keys %$tags; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # XXX determine which one is faster | ||||||||||||||||||||||||||||
| 165 | return | ||||||||||||||||||||||||||||
| 166 | 1 | "(SELECT COUNT(tag) FROM entrytags WHERE entry = entries.id) = $count", | |||||||||||||||||||||||||||
| 167 | # "id IN (SELECT entry FROM entrytags GROUP BY entry HAVING COUNT(tag) = $count)", | ||||||||||||||||||||||||||||
| 168 | $self->constraint_for_tags($tags); | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | #pod =head3 constraint_for_desc_like($value) | ||||||||||||||||||||||||||||
| 172 | #pod | ||||||||||||||||||||||||||||
| 173 | #pod =cut | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | ▶ | sub constraint_for_desc_like { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_desc_like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | 2 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 177 | 2 | my $like = substr Rubric::Entry->db_Main->quote($value), 1, -1; | |||||||||||||||||||||||||||
| 178 | 2 | "(description LIKE '\%$like\%' OR title LIKE '\%$like\%')" | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | #pod =head3 constraint_for_body_like($value) | ||||||||||||||||||||||||||||
| 182 | #pod | ||||||||||||||||||||||||||||
| 183 | #pod =cut | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | sub constraint_for_body_like { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_body_like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 2 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 187 | 2 | my $like = substr Rubric::Entry->db_Main->quote($value), 1, -1; | |||||||||||||||||||||||||||
| 188 | 2 | "(body LIKE '\%$like\%')" | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | #pod =head3 constraint_for_like($value) | ||||||||||||||||||||||||||||
| 192 | #pod | ||||||||||||||||||||||||||||
| 193 | #pod =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | sub constraint_for_like { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 196 | 1 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 197 | 1 | "(" . $self->constraint_for_desc_like($value) . | |||||||||||||||||||||||||||
| 198 | "OR" . $self->constraint_for_body_like($value) . ")" | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | #pod =head3 constraint_for_has_body($bool) | ||||||||||||||||||||||||||||
| 202 | #pod | ||||||||||||||||||||||||||||
| 203 | #pod This returns SQL to limit the results to entries with bodies. | ||||||||||||||||||||||||||||
| 204 | #pod | ||||||||||||||||||||||||||||
| 205 | #pod =cut | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 2 | ▶ | sub constraint_for_has_body { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_has_body: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 2 | my ($self, $bool) = @_; | |||||||||||||||||||||||||||
| 209 | 2 | ▶ | return $bool ? "body IS NOT NULL" : "body IS NULL"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | #pod =head3 constraint_for_has_link($bool) | ||||||||||||||||||||||||||||
| 213 | #pod | ||||||||||||||||||||||||||||
| 214 | #pod This returns SQL to limit the results to entries with links. | ||||||||||||||||||||||||||||
| 215 | #pod | ||||||||||||||||||||||||||||
| 216 | #pod =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 2 | ▶ | sub constraint_for_has_link { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_has_link: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | 2 | my ($self, $bool) = @_; | |||||||||||||||||||||||||||
| 220 | 2 | ▶ | return $bool ? "link IS NOT NULL" : "link IS NULL"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | #pod =head3 constraint_for_first_only($bool) | ||||||||||||||||||||||||||||
| 224 | #pod | ||||||||||||||||||||||||||||
| 225 | #pod This returns SQL to limit the results to the first entry posted for any given | ||||||||||||||||||||||||||||
| 226 | #pod link. | ||||||||||||||||||||||||||||
| 227 | #pod | ||||||||||||||||||||||||||||
| 228 | #pod =cut | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 10 | ▶ | sub constraint_for_first_only { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_first_only: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 10 | my ($self, $bool) = @_; | |||||||||||||||||||||||||||
| 232 | 10 | ▶ | return $bool | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | ? "(link is NULL OR id IN (SELECT MIN(id) FROM entries GROUP BY link))" | ||||||||||||||||||||||||||||
| 234 | : (); | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | #pod =head3 constraint_for_urimd5($md5) | ||||||||||||||||||||||||||||
| 238 | #pod | ||||||||||||||||||||||||||||
| 239 | #pod This returns SQL to limit the results to entries whose link has the given | ||||||||||||||||||||||||||||
| 240 | #pod md5sum. | ||||||||||||||||||||||||||||
| 241 | #pod | ||||||||||||||||||||||||||||
| 242 | #pod =cut | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 4 | ▶ | sub constraint_for_urimd5 { | ||||||||||||||||||||||||||
|
Subroutine
constraint_for_urimd5: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | 4 | my ($self, $md5) = @_; | |||||||||||||||||||||||||||
| 246 | ## no critic (ReturnUndef) | ||||||||||||||||||||||||||||
| 247 | 4 | ▶ | return undef unless my ($link) = Rubric::Link->search({ md5 => $md5 }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | ## use critic | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 3 | return "link = " . $link->id; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | #pod =head3 constraint_for_{timefield}_{preposition}($datetime) | ||||||||||||||||||||||||||||
| 254 | #pod | ||||||||||||||||||||||||||||
| 255 | #pod This set of six methods return SQL to limit the results based on its | ||||||||||||||||||||||||||||
| 256 | #pod timestamps. | ||||||||||||||||||||||||||||
| 257 | #pod | ||||||||||||||||||||||||||||
| 258 | #pod The passed value is a complete or partial datetime in the form: | ||||||||||||||||||||||||||||
| 259 | #pod | ||||||||||||||||||||||||||||
| 260 | #pod YYYY[-MM[-DD[ HH[:MM]]]] # space may be replaced with 'T' | ||||||||||||||||||||||||||||
| 261 | #pod | ||||||||||||||||||||||||||||
| 262 | #pod The timefield may be "created" or "modified". | ||||||||||||||||||||||||||||
| 263 | #pod | ||||||||||||||||||||||||||||
| 264 | #pod The prepositions are as follows: | ||||||||||||||||||||||||||||
| 265 | #pod | ||||||||||||||||||||||||||||
| 266 | #pod after - after the latest part of the given unit of time | ||||||||||||||||||||||||||||
| 267 | #pod before - before the earliest part of the given unit of time | ||||||||||||||||||||||||||||
| 268 | #pod on - after (or at) the earliest part and before (or at) the latest part | ||||||||||||||||||||||||||||
| 269 | #pod | ||||||||||||||||||||||||||||
| 270 | #pod =cut | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | ## here there be small lizards | ||||||||||||||||||||||||||||
| 273 | ## date parameter handling below... | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 4 | ▶ | sub _unit_from_string { | ||||||||||||||||||||||||||
|
Subroutine
_unit_from_string: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 276 | 4 | my ($datetime) = @_; | |||||||||||||||||||||||||||
| 277 | 4 | ▶ | return unless my @unit = $datetime =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | qr/^(\d{4})(?:-(\d{2})(?:-(\d{2})(?:(?:T|)(\d{2})(?::(\d{2}))?)?)?)?$/o; | ||||||||||||||||||||||||||||
| 279 | 3 | ▶ | $unit[1]-- if $unit[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 3 | return @unit; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | { | ||||||||||||||||||||||||||||
| 284 | ## no critic (NoStrict) | ||||||||||||||||||||||||||||
| 285 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 286 | 3 | for my $field (qw(created modified)) { | |||||||||||||||||||||||||||
| 287 | 6 | for my $prep (qw(after before on)) { | |||||||||||||||||||||||||||
| 288 | 18 | ▶ | *{"constraint_for_${field}_${prep}"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 289 | 4 | my ($self, $datetime) = @_; | |||||||||||||||||||||||||||
| 290 | ## no critic (ReturnUndef) | ||||||||||||||||||||||||||||
| 291 | 4 | ▶ | return undef unless my @time = _unit_from_string($datetime); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | ## use critic | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 3 | my ($start,$end) = range_from_unit(@time); | |||||||||||||||||||||||||||
| 295 | return | ||||||||||||||||||||||||||||
| 296 | 3 | ▶ | ( $prep eq 'after' ? "$field > $end" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 297 | : $prep eq 'before' ? "$field < $start" | ||||||||||||||||||||||||||||
| 298 | : "$field >= $start AND $field <= $end") | ||||||||||||||||||||||||||||
| 299 | # : $prep eq 'on' ? "$field >= $start AND $field <= $end" | ||||||||||||||||||||||||||||
| 300 | # : die "illegal preposition in temporal comparison" ) | ||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | 18 | } | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 3 | 1; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | __END__ | ||||||||||||||||||||||||||||