| 1 | package Class::DBI::Plugin::AbstractCount; | ||||||||||||||||||||||||||||
| 2 | # vim:set tabstop=2 shiftwidth=2 expandtab: | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use base 'Class::DBI::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use SQL::Abstract; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub init | ||||||||||||||||||||||||||||
| 11 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 3 | $class->set_sql( count_search_where => qq{ | |||||||||||||||||||||||||||
| 14 | SELECT COUNT(*) | ||||||||||||||||||||||||||||
| 15 | FROM __TABLE__ | ||||||||||||||||||||||||||||
| 16 | %s | ||||||||||||||||||||||||||||
| 17 | } ); | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 10 | ▶ | sub count_search_where : Plugged | ||||||||||||||||||||||||||
|
Subroutine
count_search_where: called CC=17 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | 10 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 10 | my %where = (); | |||||||||||||||||||||||||||
| 24 | 10 | my $rh_attr = {}; | |||||||||||||||||||||||||||
| 25 | 10 | ▶ | if ( ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | $class->_croak( "where-clause must be a hashref it it's a reference" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | unless ref( $_[0] ) eq 'HASH'; | ||||||||||||||||||||||||||||
| 28 | 9 | %where = %{ $_[0] }; | |||||||||||||||||||||||||||
| 29 | 9 | $rh_attr = $_[1]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||
| 32 | 1 | ▶ | $rh_attr = pop if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | %where = @_; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 10 | delete $rh_attr->{order_by}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 10 | ▶ | $class->can( 'retrieve_from_sql' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | or $class->_croak( "$class should inherit from Class::DBI >= 0.95" ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | my ( %columns, %accessors ) = (); | |||||||||||||||||||||||||||
| 41 | 10 | for my $column ( $class->columns ) { | |||||||||||||||||||||||||||
| 42 | 40 | ++$columns{ $column }; | |||||||||||||||||||||||||||
| 43 | 40 | $accessors{ $column->accessor } = $column; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 10 | COLUMN: for my $column ( keys %where ) { | |||||||||||||||||||||||||||
| 47 | # Column names are (of course) OK | ||||||||||||||||||||||||||||
| 48 | 20 | ▶ | next COLUMN if exists $columns{ $column }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Accessor names are OK, but replace them with corresponding column name | ||||||||||||||||||||||||||||
| 51 | $where{ $accessors{ $column }} = delete $where{ $column }, next COLUMN | ||||||||||||||||||||||||||||
| 52 | 12 | ▶ | if exists $accessors{ $column }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # SQL::Abstract keywords are OK | ||||||||||||||||||||||||||||
| 55 | next COLUMN | ||||||||||||||||||||||||||||
| 56 | 7 | ▶ | if $column =~ /^-(?:and|or|nest|(?:(not_)?(?:like|between)))$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Check for functions | ||||||||||||||||||||||||||||
| 59 | 6 | ▶ | if ( index( $column, '(' ) > 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
index($column, "(") > 0 and index($column, ")") > 1
MC/DC0%0/2
index($column, "(") > 0 and index($column, ")") > 1
index($column, "(") > 0
index($column, ")") > 1
Truth table33%1/3
A and B
A= index($column, "(") > 0B= index($column, ")") > 1
| |||||||||||||||||||||||||||||
| 60 | && index( $column, ')' ) > 1 ) | ||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | 6 | my @tokens = ( $column =~ /(-?\w+(?:\s*\(\s*)?|\W+)/g ); | |||||||||||||||||||||||||||
| 63 | 6 | TOKEN: for my $token ( @tokens ) { | |||||||||||||||||||||||||||
| 64 | 30 | ▶ | if ( $token !~ /\W/ ) { # must be column or accessor name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 8 | ▶ | next TOKEN if exists $columns{ $token }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | $token = $accessors{ $token }, next TOKEN | ||||||||||||||||||||||||||||
| 67 | 3 | ▶ | if exists $accessors{ $token }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $class->_croak( | |||||||||||||||||||||||||||
| 69 | qq{"$token" is not a column/accessor of class "$class"} ); | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | my $normalized = join "", @tokens; | |||||||||||||||||||||||||||
| 74 | 6 | ▶ | $where{ $normalized } = delete $where{ $column } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | if $normalized ne $column; | ||||||||||||||||||||||||||||
| 76 | 6 | next COLUMN; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | $class->_croak( qq{"$column" is not a column/accessor of class "$class"} ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 10 | my ( $phrase, @bind ) = SQL::Abstract | |||||||||||||||||||||||||||
| 83 | -> new( %$rh_attr ) | ||||||||||||||||||||||||||||
| 84 | -> where( \%where ); | ||||||||||||||||||||||||||||
| 85 | 10 | $class | |||||||||||||||||||||||||||
| 86 | -> sql_count_search_where( $phrase ) | ||||||||||||||||||||||||||||
| 87 | -> select_val( @bind ); | ||||||||||||||||||||||||||||
| 88 | 3 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 3 | 1; | |||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||