File Coverage

blib/lib/Teng/Schema/Table.pm
Criterion Covered Total %
statement 61 81 75.3
branch 8 16 50.0
condition 2 2 100.0
subroutine 12 16 75.0
pod 8 9 88.8
total 91 124 73.3


line stmt bran cond sub pod time code
1             package Teng::Schema::Table;
2 70     70   107285 use strict;
  70         137  
  70         2231  
3 70     70   378 use warnings;
  70         197  
  70         2952  
4             use Class::Accessor::Lite
5 70         701 rw => [ qw(
6             name
7             primary_keys
8             columns
9             escaped_columns
10             sql_types
11             row_class
12             base_row_class
13             ) ]
14 70     70   984 ;
  70         1519  
15 70     70   10471 use Carp ();
  70         174  
  70         1685  
16 70     70   1918 use Class::Load ();
  70         67882  
  70         8574  
17              
18             sub new {
19 248     248 1 4184 my ($class, %args) = @_;
20 248         2510 my $self = bless {
21             deflators => [],
22             inflators => [],
23             escaped_columns => {},
24             base_row_class => 'Teng::Row',
25             %args
26             }, $class;
27              
28             # load row class
29 248         1157 my $row_class = $self->row_class;
30 248 100       1890 Class::Load::load_optional_class($row_class) or do {
31             # make row class automatically
32 243         108946 Class::Load::load_class($self->base_row_class);
33 70     70   602 no strict 'refs'; @{"$row_class\::ISA"} = ($self->base_row_class);
  70         224  
  70         5267  
  243         19495  
  243         5863  
34             };
35 248         1334 for my $col (@{$self->columns}) {
  248         1023  
36 70     70   474 no strict 'refs';
  70         214  
  70         51754  
37 741 100       7030 unless ($row_class->can($col)) {
38 737         2306 *{"$row_class\::$col"} = $row_class->generate_column_accessor($col);
  737         2906  
39             }
40             }
41 248         1017 $self->row_class($row_class);
42              
43 248         2531 return $self;
44             }
45              
46             sub get_sql_type {
47 341     341 1 3758 my ($self, $column_name) = @_;
48 341         902 $self->sql_types->{ $column_name };
49             }
50              
51             sub add_deflator {
52 0     0 1 0 my ($self, $rule, $code) = @_;
53 0 0       0 if ( ref $rule ne 'Regexp' ) {
54 0         0 $rule = qr/^\Q$rule\E$/;
55             }
56 0 0       0 unless (ref($code) eq 'CODE') {
57 0         0 Carp::croak('deflate code must be coderef.');
58             }
59 0         0 push @{ $self->{deflators} }, ( $rule, $code );
  0         0  
60             }
61              
62             sub add_inflator {
63 0     0 1 0 my ($self, $rule, $code) = @_;
64 0 0       0 if ( ref $rule ne 'Regexp' ) {
65 0         0 $rule = qr/^\Q$rule\E$/;
66             }
67 0 0       0 unless (ref($code) eq 'CODE') {
68 0         0 Carp::croak('inflate code must be coderef.');
69             }
70 0         0 push @{ $self->{inflators} }, ( $rule, $code );
  0         0  
71             }
72              
73             sub call_deflate {
74 345     345 1 900 my ($self, $col_name, $col_value) = @_;
75 345         739 my $rules = $self->{deflators};
76 345         506 my $i = 0;
77 345         619 my $max = @$rules;
78 345         915 while ( $i < $max ) {
79 70         144 my ($rule, $code) = @$rules[ $i, $i + 1 ];
80 70 100       264 if ($col_name =~ /$rule/) {
81 25         87 return $code->($col_value);
82             }
83 45         92 $i += 2;
84             }
85 320         1093 return $col_value;
86             }
87              
88             sub call_inflate {
89 232     232 1 641 my ($self, $col_name, $col_value) = @_;
90 232         543 my $rules = $self->{inflators};
91 232         408 my $i = 0;
92 232         445 my $max = @$rules;
93 232         796 while ( $i < $max ) {
94 66         119 my ($rule, $code) = @$rules[ $i, $i + 1 ];
95 66 100       337 if ($col_name =~ /$rule/) {
96 33         116 return $code->($col_value);
97             }
98 33         63 $i += 2;
99             }
100 199         756 return $col_value;
101             }
102              
103             sub has_deflators {
104 0     0 1 0 my $self = shift;
105 0         0 return scalar @{ $self->{deflators} };
  0         0  
106             }
107              
108             sub has_inflators {
109 0     0 1 0 my $self = shift;
110 0         0 return scalar @{ $self->{inflators} };
  0         0  
111             }
112              
113             sub prepare_from_dbh {
114 472     472 0 7453 my ($self, $dbh) = @_;
115              
116             $self->escaped_columns->{$dbh->{Driver}->{Name}} ||= [
117 712         163460 map { \$dbh->quote_identifier($_) }
118 472   100     1421 @{$self->columns}
  234         5488  
119             ];
120             }
121              
122             1;
123              
124             __END__
125              
126             =head1 NAME
127              
128             Teng::Schema::Table - Teng table class.
129              
130             =head1 METHODS
131              
132             =over 4
133              
134             =item $table = Teng::Schema::Table->new
135              
136             create new Teng::Schema::Table's object.
137              
138             =item $table->get_sql_type
139              
140             get column SQL type.
141              
142             =item $table->add_deflator($column_rule, $code)
143              
144             add deflate code reference.
145              
146             =item $table->add_inflator($column_rule, $code)
147              
148             add inflate code reference.
149              
150             =item $table->call_deflate
151              
152             execute deflate.
153              
154             =item $table->call_inflate
155              
156             execute inflate.
157              
158             =item $table->has_deflators()
159              
160             Returns true if there are any deflators
161              
162             =item $table->has_inflators();
163              
164             Returns true if there are any inflators
165              
166             =back