| 1 | package Query::Abstract; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use v5.10; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 3 | ▶ | use Class::Load qw/load_class/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Carp qw/croak/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 3 | our $VERSION = '0.01'; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||
| 14 | 2 | my $class = shift; | |||||||||||||
| 15 | 2 | my %args = @_; | |||||||||||||
| 16 | 2 | my $driver = $args{driver}; | |||||||||||||
| 17 | 2 | ▶ | croak "Wrong driver" unless ref $driver; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | my $self = bless {}, $class; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 2 | ▶ | if ( ref $driver eq 'ARRAY' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 22 | 2 | my $driver_class = 'Query::Abstract::Driver::' . $driver->[0]; | |||||||||||||
| 23 | 2 | load_class($driver_class); | |||||||||||||
| 24 | |||||||||||||||
| 25 | 2 | ▶ | $self->{driver} = $driver_class->new( @{ $driver->[1] || [] } ); | ||||||||||||
|
Condition100%2/2
$driver->[1] || []
MC/DC100%1/1
$driver->[1] || []
$driver->[1]
Truth table100%2/2
A
A= $driver->[1]
| |||||||||||||||
| 26 | } elsif ( $driver->isa('Query::Abstract::Driver::Base') ) { | ||||||||||||||
| 27 | 0 | $self->{driver} = $driver; | |||||||||||||
| 28 | } else { | ||||||||||||||
| 29 | 0 | croak "Wrong driver [$driver]"; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 2 | $self->init(); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 2 | return $self; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 2 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 38 | 2 | my $self = shift; | |||||||||||||
| 39 | 2 | $self->{driver}->init(@_); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | |||||||||||||||
| 43 | 19 | ▶ | sub convert_query { | ||||||||||||
|
Subroutine
convert_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 44 | 19 | my ($self, @args) = @_; | |||||||||||||
| 45 | 19 | my %query = $self->_normalize_query(@args); | |||||||||||||
| 46 | 19 | return $self->{driver}->convert_query(%query); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | ▶ | sub convert_filter { | ||||||||||||
|
Subroutine
convert_filter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 50 | 0 | my ($self, $filter) = @_; | |||||||||||||
| 51 | |||||||||||||||
| 52 | return $self->{driver}->convert_filter( | ||||||||||||||
| 53 | 0 | $self->_normalize_where($filter) | |||||||||||||
| 54 | ); | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | |||||||||||||||
| 58 | 0 | ▶ | sub convert_sort { | ||||||||||||
|
Subroutine
convert_sort: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 59 | 0 | my ($self, $sort_by) = @_; | |||||||||||||
| 60 | |||||||||||||||
| 61 | return $self->{driver}->convert_sort( | ||||||||||||||
| 62 | 0 | $self->_normalize_sort_by($sort_by) | |||||||||||||
| 63 | ); | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 19 | ▶ | sub _normalize_query { | ||||||||||||
|
Subroutine
_normalize_query: called CC=2 SCAR=6.9
| |||||||||||||||
| 67 | 19 | my $self = shift; | |||||||||||||
| 68 | 19 | my %query; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 19 | ▶ | if ( ref($_[0]) eq 'ARRAY' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 15 | $query{where} = $_[0]; | |||||||||||||
| 72 | } else { | ||||||||||||||
| 73 | 4 | %query = @_; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 19 | my $where = $self->_normalize_where($query{where}); | |||||||||||||
| 77 | 19 | my $sort_by = $self->_normalize_sort_by($query{sort_by}); | |||||||||||||
| 78 | |||||||||||||||
| 79 | return ( | ||||||||||||||
| 80 | 19 | where => $where, | |||||||||||||
| 81 | sort_by => $sort_by | ||||||||||||||
| 82 | ); | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 19 | ▶ | sub _normalize_where { | ||||||||||||
|
Subroutine
_normalize_where: called CC=6 SCAR=18.0
| |||||||||||||||
| 86 | 19 | my ($self, $where) = @_; | |||||||||||||
| 87 | 19 | ▶ | return [] unless $where; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | |||||||||||||||
| 89 | 19 | my @norm_where; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 19 | ▶ | for (my $i = 0; $i < @$where; $i+=2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 25 | my $field = $where->[$i]; | |||||||||||||
| 93 | 25 | my ($oper, $restriction); | |||||||||||||
| 94 | 25 | ▶ | if ( ref($where->[$i+1]) eq 'HASH' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 22 | my $condition = $where->[$i+1]; | |||||||||||||
| 96 | 22 | ($oper, $restriction) = %$condition; | |||||||||||||
| 97 | } else { | ||||||||||||||
| 98 | 3 | ▶ | $oper = ref($where->[$i+1]) eq 'ARRAY' ? 'in' : 'eq'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 3 | $restriction = $where->[$i+1]; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | die "UNSUPPORTED OPERATOR [$oper]" | ||||||||||||||
| 103 | 25 | ▶ | unless grep { $oper eq $_ } qw/eq in ne gt lt le gt ge like < > <= >=/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | |||||||||||||||
| 105 | 25 | push @norm_where, $field => {$oper => $restriction} ; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 19 | return \@norm_where; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 19 | ▶ | sub _normalize_sort_by { | ||||||||||||
|
Subroutine
_normalize_sort_by: called CC=3 SCAR=11.0
| |||||||||||||||
| 112 | 19 | my ($self, $sort_by) = @_; | |||||||||||||
| 113 | 19 | ▶ | return [] unless $sort_by; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 3 | ▶ | return $sort_by if ref $sort_by eq 'ARRAY'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | # TODO add validation | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 2 | return [ split(/\s*,\s*/, $sort_by, 2) ]; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 3 | 1; # End of Query::Abstract | |||||||||||||
| 121 | |||||||||||||||
| 122 | =head1 NAME | ||||||||||||||
| 123 | |||||||||||||||
| 124 | Query::Abstract - Create filters in Perlish way and transforms them into coderefs or SQL | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =head1 SYNOPSIS | ||||||||||||||
| 127 | |||||||||||||||
| 128 | # Pure Perl filtering | ||||||||||||||
| 129 | my $qa = Query::Abstract->new( driver => ['ArrayOfHashes'] ); | ||||||||||||||
| 130 | |||||||||||||||
| 131 | my $query_sub = $qa->convert_query( | ||||||||||||||
| 132 | where => [ | ||||||||||||||
| 133 | name => 'John', | ||||||||||||||
| 134 | age => { '>' => 25 }, | ||||||||||||||
| 135 | last_name => { like => 'ing' } | ||||||||||||||
| 136 | ], | ||||||||||||||
| 137 | sort_by => 'last_name DESC, login ASC' | ||||||||||||||
| 138 | ); | ||||||||||||||
| 139 | |||||||||||||||
| 140 | $filtered_and_sorted_users = $query_sub->(\@users); | ||||||||||||||
| 141 | |||||||||||||||
| 142 | # Preparing SQL statement | ||||||||||||||
| 143 | my $qa = Query::Abstract->new( driver => ['SQL' => [table => 'users']] ); | ||||||||||||||
| 144 | |||||||||||||||
| 145 | ## The same but explicilty creating driver object. | ||||||||||||||
| 146 | my $qa = Query::Abstract->new( driver => Query::Abstract::Driver::SQL->new(table => 'users') ); | ||||||||||||||
| 147 | |||||||||||||||
| 148 | my $sql_statement = $qa->convert_query( | ||||||||||||||
| 149 | where => [ | ||||||||||||||
| 150 | name => 'John', | ||||||||||||||
| 151 | age => { '>' => 25 }, | ||||||||||||||
| 152 | last_name => { like => 'ing' } | ||||||||||||||
| 153 | ], | ||||||||||||||
| 154 | sort_by => 'last_name DESC, login ASC' | ||||||||||||||
| 155 | ); | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =head1 WARNING | ||||||||||||||
| 158 | |||||||||||||||
| 159 | This software is under the heavy development and considered ALPHA quality. | ||||||||||||||
| 160 | Things might be broken, not all features have been implemented, and APIs will be likely to change. | ||||||||||||||
| 161 | YOU HAVE BEEN WARNED. | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =head1 DESCRIPTION | ||||||||||||||
| 164 | |||||||||||||||
| 165 | L<Query::Abstract> - allows you to write queries and then tranform them into another format(depends in driver). Queries are almost compatible with Rose::DB::Object queries. | ||||||||||||||
| 166 | This module apperared because I wanted to have pure Perl queries but with ability to convert them into SQL(or other format). | ||||||||||||||
| 167 | |||||||||||||||
| 168 | Currently this module has two standard drivers - ArrayOfHashes and SQL.(You can write your own) | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =head1 METHODS | ||||||||||||||
| 171 | |||||||||||||||
| 172 | =head2 C<convert_filter> | ||||||||||||||
| 173 | |||||||||||||||
| 174 | $self->convert_filter([ name => 'John', age => { '>' => 25 }, last_name => { like => 'ing' } ]); | ||||||||||||||
| 175 | |||||||||||||||
| 176 | "SQL" Driver will return 'WHERE' clause and bind values. | ||||||||||||||
| 177 | |||||||||||||||
| 178 | "ArrayOfHashes" will return a coderef which takes hashref and returns true or false depending on condition testing result. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | my $tester = $self->convert_filter([ name => 'John', age => { '>' => 25 }, last_name => { like => 'ing' } ]); | ||||||||||||||
| 181 | @filtered = grep { $tester->($_) } ( {name => 'Anton', age => 37, last_name => 'Corning'}, {name => 'John'} ... ) | ||||||||||||||
| 182 | |||||||||||||||
| 183 | =head2 C<convert_sort> | ||||||||||||||
| 184 | |||||||||||||||
| 185 | $self->convert_sort('name DESC, age ASC, last_name DESC'); | ||||||||||||||
| 186 | |||||||||||||||
| 187 | "SQL" Driver will return 'ORDER BY' clause. | ||||||||||||||
| 188 | |||||||||||||||
| 189 | "ArrayOfHashes" will return a coderef for "sort" function | ||||||||||||||
| 190 | |||||||||||||||
| 191 | my $sort_sub = $self->convert_sort(...); | ||||||||||||||
| 192 | @sorted = sort $sort_sub @data; | ||||||||||||||
| 193 | |||||||||||||||
| 194 | =head2 C<convert_query> | ||||||||||||||
| 195 | |||||||||||||||
| 196 | $self->convert_query( where => [name => 'John'], sort_by => 'last_name DESC' ); | ||||||||||||||
| 197 | |||||||||||||||
| 198 | "SQL" Driver will return 'SELECT' with 'WHERE' and 'ORDER BY' conditions. | ||||||||||||||
| 199 | |||||||||||||||
| 200 | "ArrayOfHashes" will return a coderef for quering data | ||||||||||||||
| 201 | |||||||||||||||
| 202 | my $query_sub = $self->convert_query(...); | ||||||||||||||
| 203 | $filtered_and_sorted = $query_sub->( \@data ); | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =head1 AUTHOR | ||||||||||||||
| 206 | |||||||||||||||
| 207 | Viktor Turskyi <koorchik@cpan.org> | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =head1 BUGS | ||||||||||||||
| 210 | |||||||||||||||
| 211 | Please report any bugs or feature requests to Github L<https://github.com/koorchik/Query-Abstract> | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =head1 SEE ALSO | ||||||||||||||
| 214 | |||||||||||||||
| 215 | L<Rose::DB::Object::QueryBuilder>, L<SQL::Abstract>, L<SQL::Maker> | ||||||||||||||
| 216 | |||||||||||||||
| 217 | =cut | ||||||||||||||