File Coverage

blib/lib/LINQ/Database/Table.pm
Criterion Covered Total %
statement 69 72 95.8
branch 22 24 91.6
condition 6 10 60.0
subroutine 22 23 95.6
pod 0 5 0.0
total 119 134 88.8


line stmt bran cond sub pod time code
1 1     1   21 use 5.008003;
  1         4  
2 1     1   5 use strict;
  1         2  
  1         35  
3 1     1   7 use warnings;
  1         1  
  1         75  
4              
5             package LINQ::Database::Table;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.000_002';
9              
10 1     1   7 use Class::Tiny qw( database name sql_select sql_where );
  1         1  
  1         19  
11              
12 1     1   642 use LINQ ();
  1         13  
  1         14  
13 1     1   480 use LINQ::Util::Internal ();
  1         469  
  1         23  
14 1     1   504 use LINQ::Database::Util ();
  1         2  
  1         23  
15 1     1   481 use Object::Adhoc ();
  1         7690  
  1         23  
16              
17 1     1   497 use Role::Tiny::With ();
  1         5085  
  1         707  
18             Role::Tiny::With::with 'LINQ::Collection';
19              
20             sub _clone {
21 5     5   12 my ( $self ) = ( shift );
22            
23 5         23 my %args = ( %$self, @_ );
24 5         11 delete $args{'_linq_iterator'};
25 5         27 ref( $self )->new( %args );
26             }
27              
28             sub select {
29 3     3 0 5288 my ( $self ) = ( shift );
30 3         10 my $selection = LINQ::Util::Internal::assert_code( @_ );
31            
32 3 50       295 if ( ! defined($self->sql_select) ) {
33 3         27 my $columns = LINQ::Database::Util::selection_to_sql( $selection );
34 3 50       13 return $self->_clone( sql_select => $selection ) if $columns;
35             }
36            
37 0         0 $self->LINQ::Collection::select( $selection );
38             }
39              
40             sub where {
41 4     4 0 7369 my ( $self ) = ( shift );
42 4         13 my $assertion = LINQ::Util::Internal::assert_code( @_ );
43            
44 4 100 66     347 if ( ! defined($self->sql_where) and ! defined($self->sql_select) ) {
45 3         86 my $filter = LINQ::Database::Util::assertion_to_sql( $assertion );
46 3 100       22 return $self->_clone( sql_where => $assertion ) if $filter;
47             }
48            
49 2         35 $self->LINQ::Collection::where( $assertion );
50             }
51              
52             sub to_iterator {
53 2     2 0 141 my ( $self ) = ( shift );
54 2         5 $self->_linq_iterator->to_iterator;
55             }
56              
57             sub to_list {
58 6     6 0 337 my ( $self ) = ( shift );
59 6         14 $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 8     8   12 my ( $self ) = ( shift );
69 8   66     29 $self->{_linq_iterator} ||= $self->_build_linq_iterator;
70             }
71              
72             sub _build_linq_iterator {
73 6     6   10 my ( $self ) = ( shift );
74            
75 6         11 my $sth = $self->_build_sth;
76             my $map = defined( $self->sql_select )
77             ? $self->sql_select
78 6 100   13   955 : sub { Object::Adhoc::object( $_ ) };
  13         37  
79 6         92 my $started = 0;
80            
81             LINQ::LINQ( sub {
82 32 100   32   11306 if ( not $started ) {
83 6         430 $sth->execute;
84 6         25 ++$started;
85             }
86 32 100       653 local $_ = $sth->fetchrow_hashref or return LINQ::END;
87 26         119 return $map->( $_ );
88 6         35 } );
89             }
90              
91             sub _build_sth {
92 6     6   9 my ( $self ) = ( shift );
93            
94 6 100       119 my $sql_select = defined($self->sql_select) ? $self->sql_select : '*';
95 6 100       88 if ( ref( $sql_select ) ) {
96             $sql_select = LINQ::Database::Util::selection_to_sql(
97             $sql_select,
98 4     4   62 sub { $self->database->quote_identifier( @_ ) },
99 3   50     18 ) || '*';
100             }
101            
102 6 100       105 my $sql_where = defined($self->sql_where) ? $self->sql_where : '';
103 6 100       71 if ( ref( $sql_where ) ) {
104             $sql_where = LINQ::Database::Util::assertion_to_sql(
105             $sql_where,
106 2     2   75 sub { $self->database->quote_identifier( @_ ) },
107 2     2   36 sub { $self->database->quote( @_ ) },
108 2   50     13 ) || '';
109             }
110            
111 6 100       109 $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;