File Coverage

blib/lib/LINQ/Database/Table.pm
Criterion Covered Total %
statement 63 72 87.5
branch 19 24 79.1
condition 3 7 42.8
subroutine 19 23 82.6
pod 0 5 0.0
total 104 131 79.3


line stmt bran cond sub pod time code
1 1     1   20 use 5.008003;
  1         4  
2 1     1   6 use strict;
  1         10  
  1         28  
3 1     1   6 use warnings;
  1         2  
  1         71  
4              
5             package LINQ::Database::Table;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.000_001';
9              
10 1     1   7 use Class::Tiny qw( database name sql_select sql_where );
  1         2  
  1         6  
11              
12 1     1   646 use LINQ ();
  1         12  
  1         15  
13 1     1   514 use LINQ::Util::Internal ();
  1         472  
  1         23  
14 1     1   467 use LINQ::Database::Util ();
  1         2  
  1         22  
15 1     1   488 use Object::Adhoc ();
  1         7739  
  1         22  
16              
17 1     1   486 use Role::Tiny::With ();
  1         5272  
  1         698  
18             Role::Tiny::With::with 'LINQ::Collection';
19              
20             sub _clone {
21 2     2   5 my ( $self ) = ( shift );
22            
23 2         11 my %args = ( %$self, @_ );
24 2         4 delete $args{'_linq_iterator'};
25 2         14 ref( $self )->new( %args );
26             }
27              
28             sub select {
29 1     1 0 4892 my ( $self ) = ( shift );
30 1         4 my $selection = LINQ::Util::Internal::assert_code( @_ );
31            
32 1 50       106 if ( !$self->sql_select ) {
33 1         12 my $columns = LINQ::Database::Util::selection_to_sql( $selection );
34 1 50       5 return $self->_clone( sql_select => $selection ) if $columns;
35             }
36            
37 0         0 $self->LINQ::Collection::select( $selection );
38             }
39              
40             sub where {
41 1     1 0 7199 my ( $self ) = ( shift );
42 1         4 my $assertion = LINQ::Util::Internal::assert_code( @_ );
43            
44 1 50       84 if ( !$self->sql_where ) {
45 1         9 my $filter = LINQ::Database::Util::assertion_to_sql( $assertion );
46 1 50       6 return $self->_clone( sql_where => $filter ) if $filter;
47             }
48            
49 0         0 $self->LINQ::Collection::where( $assertion );
50             }
51              
52             sub to_iterator {
53 0     0 0 0 my ( $self ) = ( shift );
54 0         0 $self->_linq_iterator->to_iterator;
55             }
56              
57             sub to_list {
58 4     4 0 232 my ( $self ) = ( shift );
59 4         10 $self->_linq_iterator->to_list;
60             }
61              
62             sub to_array {
63 0     0 0 0 my ( $self ) = ( shift );
64 0         0 $self->_linq_iterator->to_array;
65             }
66              
67             sub _linq_iterator {
68 4     4   6 my ( $self ) = ( shift );
69 4   66     15 $self->{_linq_iterator} ||= $self->_build_linq_iterator;
70             }
71              
72             sub _build_linq_iterator {
73 3     3   5 my ( $self ) = ( shift );
74            
75 3         6 my $sth = $self->_build_sth;
76             my $map = defined( $self->sql_select )
77             ? $self->sql_select
78 3 100   7   619 : sub { Object::Adhoc::object( $_ ) };
  7         19  
79 3         43 my $started = 0;
80            
81             LINQ::LINQ( sub {
82 16 100   16   5215 if ( not $started ) {
83 3         207 $sth->execute;
84 3         13 ++$started;
85             }
86 16 100       306 local $_ = $sth->fetchrow_hashref or return LINQ::END;
87 13         60 return $map->( $_ );
88 3         17 } );
89             }
90              
91             sub _build_sth {
92 3     3   6 my ( $self ) = ( shift );
93            
94 3 100       66 my $sql_select = defined($self->sql_select) ? $self->sql_select : '*';
95 3 100       39 if ( ref( $sql_select ) ) {
96             $sql_select = LINQ::Database::Util::selection_to_sql(
97             $sql_select,
98 2     2   34 sub { $self->database->quote_identifier( @_ ) },
99 1   50     7 ) || '*';
100             }
101            
102 3 100       58 my $sql_where = defined($self->sql_where) ? $self->sql_where : '';
103 3 50       39 if ( ref( $sql_where ) ) {
104             $sql_where = LINQ::Database::Util::selection_to_sql(
105             $sql_where,
106 0     0     sub { $self->database->quote_identifier( @_ ) },
107 0     0     sub { $self->database->quote( @_ ) },
108 0   0     0 ) || '';
109             }
110            
111 3 100       47 $self->database->prepare( sprintf(
112             'SELECT %s FROM %s%s',
113             $sql_select,
114             $self->name,
115             ( $sql_where ? " WHERE $sql_where" : $sql_where ),
116             ) );
117             }
118              
119             1;