File Coverage

blib/lib/Sort/SQL.pm
Criterion Covered Total %
statement 25 25 100.0
branch 7 8 87.5
condition 1 4 25.0
subroutine 5 5 100.0
pod 2 2 100.0
total 40 44 90.9


line stmt bran cond sub pod time code
1             package Sort::SQL;
2              
3 3     3   52338 use strict;
  3         6  
  3         129  
4 3     3   88 use warnings;
  3         9  
  3         114  
5 3     3   25 use vars qw( $VERSION ); # for version 5.005...
  3         5  
  3         1388  
6              
7             $VERSION = '0.08';
8              
9             my $debug = $ENV{PERL_DEBUG} || 0;
10              
11             sub parse {
12 7     7 1 12 my $class = shift;
13 7   50     119 my $order = shift || ''; # will return empty array
14              
15 7         45 my @s = split( m/[\ ,]+/, $order );
16 7         10 my @pairs;
17              
18 7         33 while ( my ( $prop, $dir ) = splice( @s, 0, 2 ) ) {
19              
20 13 50 0     24 $debug and warn sprintf( "prop='%s' dir='%s'\n", $prop, $dir || '' );
21              
22 13 100       39 next if $prop =~ m/[^\.\w]/; # avoid sql injection
23              
24 11 100       66 if ( !defined $dir ) {
    100          
25 1         3 $dir = 'ASC';
26             }
27             elsif ( $dir !~ m/^(ASC|DESC)$/i ) {
28 2         5 unshift( @s, $dir );
29 2         2 $dir = 'ASC';
30             }
31              
32 11         60 push @pairs, [ $prop => uc($dir) ];
33             }
34              
35 7         16 return \@pairs;
36             }
37              
38             sub string2array {
39 7     7 1 828 my $pairs = shift->parse(@_);
40             return [
41 11         81 map {
42 7         14 { $_->[0] => $_->[1] }
43             } @$pairs
44             ];
45             }
46              
47             1;
48             __END__