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-2020 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   103 use strict;
  16         33  
  16         482  
14 16     16   72 use warnings FATAL => "all";
  16         30  
  16         687  
15              
16             our $VERSION = '1.414';
17              
18 16     16   88 use Scalar::Util qw(weaken);
  16         28  
  16         678  
19 16     16   79 use Carp ();
  16         28  
  16         2622  
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 4090 my $class = $_[0];
63 2926         3926 my $owner = $_[1];
64              
65 2926         6511 my $self = bless( { OWNER => $owner }, $class );
66 2926         7701 weaken( $self->{OWNER} );
67              
68 2926         4890 return $self;
69             }
70              
71             sub DESTROY
72             {
73 2925     2925   4153 my $self = $_[0];
74 2925         9060 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   106 use vars qw(@ISA);
  16         32  
  16         2332  
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   2307 my ( $class, $owner, $value ) = @_;
126              
127 1173         2319 my $self = $class->SUPER::new($owner);
128 1173         2121 $self->{VALUE} = $value;
129              
130 1173         2401 return $self;
131             }
132              
133 1765     1765   4284 sub value($$) { return $_[0]->{VALUE}; }
134              
135             package SQL::Statement::ColumnValue;
136              
137 16     16   106 use vars qw(@ISA);
  16         34  
  16         764  
138             @ISA = qw(SQL::Statement::Term);
139              
140 16     16   96 use Carp qw(croak);
  16         28  
  16         744  
141 16     16   99 use Params::Util qw(_INSTANCE _ARRAY0 _SCALAR);
  16         31  
  16         761  
142 16     16   99 use Scalar::Util qw(looks_like_number);
  16         35  
  16         7032  
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   2136 my ( $class, $owner, $value ) = @_;
184              
185 1096         2290 my $self = $class->SUPER::new($owner);
186 1096         1949 $self->{VALUE} = $value;
187              
188 1096         2713 return $self;
189             }
190              
191             sub value($)
192             {
193 18140     18140   26455 my ( $self, $eval ) = @_;
194 18140 100       33015 unless ( defined( $self->{TMPVAL} ) )
195             {
196 336         903 my ( $tbl, $col ) = $self->{OWNER}->full_qualified_column_name( $self->{VALUE} );
197 336 50       757 defined($tbl) or croak("Can't find table containing column named '$self->{VALUE}'");
198 336 50       629 defined($col) or croak("Unknown column: '$self->{VALUE}'");
199 336         883 $self->{TMPVAL} = $tbl . $self->{OWNER}->{dlm} . $col;
200 336         942 $self->{TABLE_NAME} = $tbl;
201 336         659 $self->{COLUMN_NAME} = $col;
202             }
203              
204             # XXX - can TMPVAL being defined without TABLE_NAME?
205 18140 50       29420 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         22681 my $fp;
212             defined( $fp = $self->{fastpath}->{ "${eval}." . $self->{TABLE_NAME} } )
213 18140 100       57608 and return &$fp( $self->{COLUMN_NAME} );
214              
215             defined( $fp = $self->{fastpath}->{ "${eval}." . $self->{TMPVAL} } )
216 1413 100       4033 and return &$fp( $self->{TMPVAL} );
217              
218 394 100       2651 if ( defined( _INSTANCE( $eval, 'SQL::Eval' ) ) )
    50          
219             {
220             $self->{fastpath}->{ "${eval}." . $self->{TABLE_NAME} } =
221 224         696 $eval->_gen_access_fastpath( $self->{TABLE_NAME} );
222 224         470 return &{ $self->{fastpath}->{ "${eval}." . $self->{TABLE_NAME} } }( $self->{COLUMN_NAME} );
  224         785  
223             }
224             elsif ( defined( _INSTANCE( $eval, 'SQL::Eval::Table' ) ) )
225             {
226             $self->{fastpath}->{ "${eval}." . $self->{TMPVAL} } =
227 170         486 $eval->_gen_access_fastpath( $self->{TMPVAL} );
228 170         331 return &{ $self->{fastpath}->{ "${eval}." . $self->{TMPVAL} } }( $self->{TMPVAL} );
  170         564  
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-2020 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;