File Coverage

blib/lib/SQL/Statement/Term.pm
Criterion Covered Total %
statement 62 65 95.3
branch 12 16 75.0
condition 0 2 0.0
subroutine 15 16 93.7
pod 2 2 100.0
total 91 101 90.1


line stmt bran cond sub pod time code
1             package SQL::Statement::Term;
2              
3             ######################################################################
4             #
5             # This module is copyright (c), 2009-2017 by Jens Rehsack.
6             # All rights reserved.
7             #
8             # It may be freely distributed under the same terms as Perl itself.
9             # See below for help and copyright information (search for SYNOPSIS).
10             #
11             ######################################################################
12              
13 16     16   56 use strict;
  16         20  
  16         400  
14 16     16   52 use warnings FATAL => "all";
  16         15  
  16         612  
15              
16             our $VERSION = '1.412';
17              
18 16     16   58 use Scalar::Util qw(weaken);
  16         17  
  16         611  
19 16     16   55 use Carp ();
  16         15  
  16         2127  
20              
21             =pod
22              
23             =head1 NAME
24              
25             SQL::Statement::Term - base class for all terms
26              
27             =head1 SYNOPSIS
28              
29             # create a term with an SQL::Statement object as owner
30             my $term = SQL::Statement::Term->new( $owner );
31             # access the value of that term
32             $term->value( $eval );
33              
34             =head1 DESCRIPTION
35              
36             SQL::Statement::Term is an abstract base class providing the interface
37             for all terms.
38              
39             =head1 INHERITANCE
40              
41             SQL::Statement::Term
42              
43             =head1 METHODS
44              
45             =head2 new
46              
47             Instantiates new term and stores a weak reference to the owner.
48              
49             =head2 value
50              
51             I method which will return the value of the term. Must be
52             overridden by derived classes.
53              
54             =head2 DESTROY
55              
56             Destroys the term and undefines the weak reference to the owner.
57              
58             =cut
59              
60             sub new
61             {
62 2926     2926 1 2422 my $class = $_[0];
63 2926         1973 my $owner = $_[1];
64              
65 2926         4470 my $self = bless( { OWNER => $owner }, $class );
66 2926         4859 weaken( $self->{OWNER} );
67              
68 2926         3357 return $self;
69             }
70              
71             sub DESTROY
72             {
73 2925     2925   1955 my $self = $_[0];
74 2925         6988 undef $self->{OWNER};
75             }
76              
77             sub value($)
78             {
79 0   0 0 1 0 Carp::confess( sprintf( q{pure virtual function '%s->value' called}, ref( $_[0] ) || __PACKAGE__ ) );
80             }
81              
82             package SQL::Statement::ConstantTerm;
83              
84 16     16   59 use vars qw(@ISA);
  16         30  
  16         1871  
85             @ISA = qw(SQL::Statement::Term);
86              
87             =pod
88              
89             =head1 NAME
90              
91             SQL::Statement::ConstantTerm - term for constant values
92              
93             =head1 SYNOPSIS
94              
95             # create a term with an SQL::Statement object as owner
96             my $term = SQL::Statement::ConstantTerm->new( $owner, 'foo' );
97             # access the value of that term - returns 'foo'
98             $term->value( $eval );
99              
100             =head1 DESCRIPTION
101              
102             SQL::Statement::ConstantTerm implements a term which will always return the
103             same constant value.
104              
105             =head1 INHERITANCE
106              
107             SQL::Statement::ConstantTerm
108             ISA SQL::Statement::Term
109              
110             =head1 METHODS
111              
112             =head2 new
113              
114             Instantiates new term and stores the constant to deliver and a weak
115             reference to the owner.
116              
117             =head2 value
118              
119             Returns the specified constant.
120              
121             =cut
122              
123             sub new
124             {
125 1173     1173   1180 my ( $class, $owner, $value ) = @_;
126              
127 1173         1596 my $self = $class->SUPER::new($owner);
128 1173         1341 $self->{VALUE} = $value;
129              
130 1173         1715 return $self;
131             }
132              
133 1765     1765   3229 sub value($$) { return $_[0]->{VALUE}; }
134              
135             package SQL::Statement::ColumnValue;
136              
137 16     16   68 use vars qw(@ISA);
  16         16  
  16         592  
138             @ISA = qw(SQL::Statement::Term);
139              
140 16     16   53 use Carp qw(croak);
  16         25  
  16         659  
141 16     16   60 use Params::Util qw(_INSTANCE _ARRAY0 _SCALAR);
  16         15  
  16         639  
142 16     16   56 use Scalar::Util qw(looks_like_number);
  16         13  
  16         6082  
143              
144             =pod
145              
146             =head1 NAME
147              
148             SQL::Statement::ColumnValue - term for column values
149              
150             =head1 SYNOPSIS
151              
152             # create a term with an SQL::Statement object as owner
153             my $term = SQL::Statement::ColumnValue->new( $owner, 'id' );
154             # access the value of that term - returns the value of the column 'id'
155             # of the currently active row in $eval
156             $term->value( $eval );
157              
158             =head1 DESCRIPTION
159              
160             SQL::Statement::ColumnValue implements a term which will return the specified
161             column of the active row.
162              
163             =head1 INHERITANCE
164              
165             SQL::Statement::ColumnValue
166             ISA SQL::Statement::Term
167              
168             =head1 METHODS
169              
170             =head2 new
171              
172             Instantiates new term and stores the column name to deliver and a weak
173             reference to the owner.
174              
175             =head2 value
176              
177             Returns the specified column value.
178              
179             =cut
180              
181             sub new
182             {
183 1096     1096   1110 my ( $class, $owner, $value ) = @_;
184              
185 1096         1694 my $self = $class->SUPER::new($owner);
186 1096         1219 $self->{VALUE} = $value;
187              
188 1096         2130 return $self;
189             }
190              
191             sub value($)
192             {
193 18140     18140   12034 my ( $self, $eval ) = @_;
194 18140 100       21993 unless ( defined( $self->{TMPVAL} ) )
195             {
196 336         713 my ( $tbl, $col ) = $self->{OWNER}->full_qualified_column_name( $self->{VALUE} );
197 336 50       546 defined($tbl) or croak("Can't find table containing column named '$self->{VALUE}'");
198 336 50       463 defined($col) or croak("Unknown column: '$self->{VALUE}'");
199 336         629 $self->{TMPVAL} = $tbl . $self->{OWNER}->{dlm} . $col;
200 336         344 $self->{TABLE_NAME} = $tbl;
201 336         406 $self->{COLUMN_NAME} = $col;
202             }
203              
204             # XXX - can TMPVAL being defined without TABLE_NAME?
205 18140 50       19840 unless ( defined( $self->{TABLE_NAME} ) )
206             {
207 0         0 croak( "No table specified: '" . $self->{OWNER}->{original_string} . "'" );
208             }
209              
210             # with TempEval: return $eval->column($self->{TABLE_NAME}, $self->{COLUMN_NAME});
211 18140         9948 my $fp;
212             defined( $fp = $self->{fastpath}->{ "${eval}." . $self->{TABLE_NAME} } )
213 18140 100       48046 and return &$fp( $self->{COLUMN_NAME} );
214              
215             defined( $fp = $self->{fastpath}->{ "${eval}." . $self->{TMPVAL} } )
216 1413 100       3726 and return &$fp( $self->{TMPVAL} );
217              
218 394 100       2131 if ( defined( _INSTANCE( $eval, 'SQL::Eval' ) ) )
    50          
219             {
220             $self->{fastpath}->{ "${eval}." . $self->{TABLE_NAME} } =
221 224         490 $eval->_gen_access_fastpath( $self->{TABLE_NAME} );
222 224         237 return &{ $self->{fastpath}->{ "${eval}." . $self->{TABLE_NAME} } }( $self->{COLUMN_NAME} );
  224         580  
223             }
224             elsif ( defined( _INSTANCE( $eval, 'SQL::Eval::Table' ) ) )
225             {
226             $self->{fastpath}->{ "${eval}." . $self->{TMPVAL} } =
227 170         422 $eval->_gen_access_fastpath( $self->{TMPVAL} );
228 170         173 return &{ $self->{fastpath}->{ "${eval}." . $self->{TMPVAL} } }( $self->{TMPVAL} );
  170         464  
229             # return $eval->column( $self->{TMPVAL} );
230             }
231             else
232             {
233 0           croak( "Unsupported table storage: '" . ref($eval) . "'" );
234             }
235             }
236              
237             =head1 AUTHOR AND COPYRIGHT
238              
239             Copyright (c) 2009-2017 by Jens Rehsack: rehsackATcpan.org
240              
241             All rights reserved.
242              
243             You may distribute this module under the terms of either the GNU
244             General Public License or the Artistic License, as specified in
245             the Perl README file.
246              
247             =cut
248              
249             1;