File Coverage

blib/lib/DBIx/RoboQuery/Util.pm
Criterion Covered Total %
statement 20 20 100.0
branch 11 12 91.6
condition 1 2 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 39 41 95.1


line stmt bran cond sub pod time code
1             # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2             #
3             # This file is part of DBIx-RoboQuery
4             #
5             # This software is copyright (c) 2010 by Randy Stauner.
6             #
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             #
10 9     9   29375 use strict;
  9         16  
  9         337  
11 9     9   45 use warnings;
  9         19  
  9         526  
12              
13             package DBIx::RoboQuery::Util;
14             {
15             $DBIx::RoboQuery::Util::VERSION = '0.032';
16             }
17             BEGIN {
18 9     9   2837 $DBIx::RoboQuery::Util::AUTHORITY = 'cpan:RWSTAUNER';
19             }
20             # ABSTRACT: Utility functions for DBIx::RoboQuery
21              
22             # convenience function used in both modules
23             # to convert specific hash items to arrayrefs
24              
25             sub _ensure_arrayrefs {
26 103     103   374 my ($hash, @keys) = @_;
27              
28             # if no keys were provided, use the defaults
29 103 50       455 @keys = $hash->_arrayref_args
30             if !@keys;
31              
32 103         236 foreach my $key ( @keys ){
33 412 100       1224 if( exists $hash->{$key} ){
34 209 100       639 $hash->{$key} = [$hash->{$key}]
35             unless ref($hash->{$key}) eq 'ARRAY';
36             }
37             }
38             }
39              
40             # flatten any arrayrefs and return a single list
41              
42             sub _flatten {
43 11 100   11   20 return map { ref $_ ? @$_ : $_ } @_;
  18         87  
44             }
45              
46              
47             sub order_from_sql {
48 13     13 1 8489 my ($sql, $opts) = @_;
49             # TODO: consider including /|LIMIT \d+/ in suffix unless 'no_limit' provided
50 13   50     37 $opts ||= {};
51              
52 13 100       158 my $suffix = $opts->{suffix}
53             # don't inherit /x from the parent re below
54             ? qr/(?-x:$opts->{suffix})?/
55             # nothing
56             : qr//;
57              
58             return
59 17         35 $sql =~ /\bORDER\s+BY\s+ # start order by clause
60             ( # start capture
61             (?:\w+) # first column
62             (?:\s+(?:ASC|DESC))? # direction
63             (?:\s*,\s* # comma, possibly spaced
64             (?:\w+) # next column
65             (?:\s+(?:ASC|DESC))? # direction
66             )* # repeat
67             )\s* # end capture
68             $suffix # possible query suffix
69             \s*;?\s*\Z # end of SQL
70             /isx
71             # ignore direction
72             ## no critic ProhibitMutatingListFunctions
73 13 100       650 ? map { s/\s+(ASC|DESC)$//; $_ } split(/\s*,\s*/, $1)
  17         113  
74             : ();
75             }
76              
77             1;
78              
79             __END__